2019-06-23 03:35:23 -07:00
|
|
|
import Vue from 'vue';
|
|
|
|
import { parse } from 'flatted';
|
|
|
|
|
2019-12-29 13:02:21 -08:00
|
|
|
import axios, { AxiosRequestConfig, Method } from 'axios';
|
2019-06-23 03:35:23 -07:00
|
|
|
import {
|
|
|
|
IActivationError,
|
|
|
|
ICredentialsDecryptedResponse,
|
|
|
|
ICredentialsResponse,
|
|
|
|
IExecutionsCurrentSummaryExtended,
|
|
|
|
IExecutionDeleteFilter,
|
|
|
|
IExecutionPushResponse,
|
|
|
|
IExecutionResponse,
|
|
|
|
IExecutionFlattedResponse,
|
|
|
|
IExecutionsListResponse,
|
|
|
|
IExecutionsStopData,
|
|
|
|
IN8nUISettings,
|
|
|
|
IStartRunData,
|
|
|
|
IWorkflowDb,
|
|
|
|
IWorkflowShortResponse,
|
|
|
|
IRestApi,
|
|
|
|
IWorkflowData,
|
|
|
|
IWorkflowDataUpdate,
|
|
|
|
} from '@/Interface';
|
|
|
|
import {
|
|
|
|
ICredentialsDecrypted,
|
|
|
|
ICredentialType,
|
|
|
|
IDataObject,
|
|
|
|
INodeCredentials,
|
2019-12-16 18:27:56 -08:00
|
|
|
INodeParameters,
|
2019-06-23 03:35:23 -07:00
|
|
|
INodePropertyOptions,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2021-05-29 11:31:21 -07:00
|
|
|
import { makeRestApiRequest } from '@/api/helpers';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unflattens the Execution data.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {IExecutionFlattedResponse} fullExecutionData The data to unflatten
|
|
|
|
* @returns {IExecutionResponse}
|
|
|
|
*/
|
|
|
|
function unflattenExecutionData (fullExecutionData: IExecutionFlattedResponse): IExecutionResponse {
|
|
|
|
// Unflatten the data
|
|
|
|
const returnData: IExecutionResponse = {
|
|
|
|
...fullExecutionData,
|
|
|
|
workflowData: fullExecutionData.workflowData as IWorkflowDb,
|
|
|
|
data: parse(fullExecutionData.data),
|
|
|
|
};
|
|
|
|
|
|
|
|
returnData.finished = returnData.finished ? returnData.finished : false;
|
|
|
|
|
|
|
|
if (fullExecutionData.id) {
|
|
|
|
returnData.id = fullExecutionData.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const restApi = Vue.extend({
|
|
|
|
methods: {
|
|
|
|
restApi (): IRestApi {
|
|
|
|
const self = this;
|
|
|
|
return {
|
2019-12-29 13:02:21 -08:00
|
|
|
async makeRestApiRequest (method: Method, endpoint: string, data?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
2021-05-29 11:31:21 -07:00
|
|
|
return makeRestApiRequest(self.$store.getters.getRestApiContext, method, endpoint, data);
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
getActiveWorkflows: (): Promise<string[]> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/active`);
|
|
|
|
},
|
|
|
|
getActivationError: (id: string): Promise<IActivationError | undefined> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/active/error/${id}`);
|
|
|
|
},
|
|
|
|
getCurrentExecutions: (filter: object): Promise<IExecutionsCurrentSummaryExtended[]> => {
|
|
|
|
let sendData = {};
|
|
|
|
if (filter) {
|
|
|
|
sendData = {
|
|
|
|
filter,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/executions-current`, sendData);
|
|
|
|
},
|
|
|
|
stopCurrentExecution: (executionId: string): Promise<IExecutionsStopData> => {
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/executions-current/${executionId}/stop`);
|
|
|
|
},
|
|
|
|
getSettings: (): Promise<IN8nUISettings> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/settings`);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all node-types
|
|
|
|
getNodeTypes: (): Promise<INodeTypeDescription[]> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/node-types`);
|
|
|
|
},
|
|
|
|
|
2020-10-22 08:24:35 -07:00
|
|
|
getNodesInformation: (nodeList: string[]): Promise<INodeTypeDescription[]> => {
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/node-types`, {nodeNames: nodeList});
|
|
|
|
},
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
// Returns all the parameter options from the server
|
2021-05-16 16:16:24 -07:00
|
|
|
getNodeParameterOptions: (nodeType: string, path: string, methodName: string, currentNodeParameters: INodeParameters, credentials?: INodeCredentials): Promise<INodePropertyOptions[]> => {
|
2019-06-23 03:35:23 -07:00
|
|
|
const sendData = {
|
|
|
|
nodeType,
|
2021-05-16 16:16:24 -07:00
|
|
|
path,
|
2019-06-23 03:35:23 -07:00
|
|
|
methodName,
|
|
|
|
credentials,
|
2019-12-16 18:27:56 -08:00
|
|
|
currentNodeParameters,
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
return self.restApi().makeRestApiRequest('GET', '/node-parameter-options', sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes a test webhook
|
|
|
|
removeTestWebhook: (workflowId: string): Promise<boolean> => {
|
|
|
|
return self.restApi().makeRestApiRequest('DELETE', `/test-webhook/${workflowId}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Execute a workflow
|
|
|
|
runWorkflow: async (startRunData: IStartRunData): Promise<IExecutionPushResponse> => {
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/workflows/run`, startRunData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Creates new credentials
|
2021-05-29 11:31:21 -07:00
|
|
|
createNewWorkflow: (sendData: IWorkflowDataUpdate): Promise<IWorkflowDb> => {
|
2019-06-23 03:35:23 -07:00
|
|
|
return self.restApi().makeRestApiRequest('POST', `/workflows`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Updates an existing workflow
|
|
|
|
updateWorkflow: (id: string, data: IWorkflowDataUpdate): Promise<IWorkflowDb> => {
|
|
|
|
return self.restApi().makeRestApiRequest('PATCH', `/workflows/${id}`, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Deletes a workflow
|
|
|
|
deleteWorkflow: (name: string): Promise<void> => {
|
|
|
|
return self.restApi().makeRestApiRequest('DELETE', `/workflows/${name}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the workflow with the given name
|
|
|
|
getWorkflow: (id: string): Promise<IWorkflowDb> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/workflows/${id}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all saved workflows
|
|
|
|
getWorkflows: (filter?: object): Promise<IWorkflowShortResponse[]> => {
|
|
|
|
let sendData;
|
|
|
|
if (filter) {
|
|
|
|
sendData = {
|
|
|
|
filter,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/workflows`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns a workflow from a given URL
|
|
|
|
getWorkflowFromUrl: (url: string): Promise<IWorkflowDb> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/workflows/from-url`, { url });
|
|
|
|
},
|
|
|
|
|
|
|
|
// Creates a new workflow
|
|
|
|
createNewCredentials: (sendData: ICredentialsDecrypted): Promise<ICredentialsResponse> => {
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/credentials`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Deletes a credentials
|
|
|
|
deleteCredentials: (id: string): Promise<void> => {
|
|
|
|
return self.restApi().makeRestApiRequest('DELETE', `/credentials/${id}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Updates existing credentials
|
|
|
|
updateCredentials: (id: string, data: ICredentialsDecrypted): Promise<ICredentialsResponse> => {
|
|
|
|
return self.restApi().makeRestApiRequest('PATCH', `/credentials/${id}`, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the credentials with the given id
|
|
|
|
getCredentials: (id: string, includeData?: boolean): Promise<ICredentialsDecryptedResponse | ICredentialsResponse | undefined> => {
|
|
|
|
let sendData;
|
|
|
|
if (includeData) {
|
|
|
|
sendData = {
|
|
|
|
includeData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/credentials/${id}`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all saved credentials
|
|
|
|
getAllCredentials: (filter?: object): Promise<ICredentialsResponse[]> => {
|
|
|
|
let sendData;
|
|
|
|
if (filter) {
|
|
|
|
sendData = {
|
|
|
|
filter,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/credentials`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all credential types
|
|
|
|
getCredentialTypes: (): Promise<ICredentialType[]> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/credential-types`);
|
|
|
|
},
|
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
// Get OAuth1 Authorization URL using the stored credentials
|
|
|
|
oAuth1CredentialAuthorize: (sendData: ICredentialsResponse): Promise<string> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/oauth1-credential/auth`, sendData);
|
|
|
|
},
|
|
|
|
|
2020-01-01 22:49:18 -08:00
|
|
|
// Get OAuth2 Authorization URL using the stored credentials
|
2020-01-07 16:29:11 -08:00
|
|
|
oAuth2CredentialAuthorize: (sendData: ICredentialsResponse): Promise<string> => {
|
2020-01-01 22:49:18 -08:00
|
|
|
return self.restApi().makeRestApiRequest('GET', `/oauth2-credential/auth`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Verify OAuth2 provider callback and kick off token generation
|
2020-01-07 16:29:11 -08:00
|
|
|
oAuth2Callback: (code: string, state: string): Promise<string> => {
|
2020-01-01 22:49:18 -08:00
|
|
|
const sendData = {
|
|
|
|
'code': code,
|
2020-01-07 16:29:11 -08:00
|
|
|
'state': state,
|
2020-01-01 22:49:18 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/oauth2-credential/callback`, sendData);
|
|
|
|
},
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
// Returns the execution with the given name
|
|
|
|
getExecution: async (id: string): Promise<IExecutionResponse> => {
|
|
|
|
const response = await self.restApi().makeRestApiRequest('GET', `/executions/${id}`);
|
|
|
|
return unflattenExecutionData(response);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Deletes executions
|
|
|
|
deleteExecutions: (sendData: IExecutionDeleteFilter): Promise<void> => {
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/executions/delete`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns the execution with the given name
|
2019-12-12 16:12:38 -08:00
|
|
|
retryExecution: (id: string, loadWorkflow?: boolean): Promise<boolean> => {
|
|
|
|
let sendData;
|
|
|
|
if (loadWorkflow === true) {
|
|
|
|
sendData = {
|
|
|
|
loadWorkflow: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return self.restApi().makeRestApiRequest('POST', `/executions/${id}/retry`, sendData);
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all saved executions
|
|
|
|
// TODO: For sure needs some kind of default filter like last day, with max 10 results, ...
|
2021-02-09 14:32:40 -08:00
|
|
|
getPastExecutions: (filter: object, limit: number, lastId?: string | number, firstId?: string | number): Promise<IExecutionsListResponse> => {
|
2019-06-23 03:35:23 -07:00
|
|
|
let sendData = {};
|
|
|
|
if (filter) {
|
|
|
|
sendData = {
|
|
|
|
filter,
|
2021-02-09 14:32:40 -08:00
|
|
|
firstId,
|
2019-12-12 11:57:11 -08:00
|
|
|
lastId,
|
2019-06-23 03:35:23 -07:00
|
|
|
limit,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/executions`, sendData);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns all the available timezones
|
|
|
|
getTimezones: (): Promise<IDataObject> => {
|
|
|
|
return self.restApi().makeRestApiRequest('GET', `/options/timezones`);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|