import Vue from 'vue' ; import { parse } from 'flatted' ; import { Method } from 'axios' ; import { IActivationError , IExecutionsCurrentSummaryExtended , IExecutionDeleteFilter , IExecutionPushResponse , IExecutionResponse , IExecutionFlattedResponse , IExecutionsListResponse , IExecutionsStopData , IStartRunData , IWorkflowDb , IWorkflowShortResponse , IRestApi , IWorkflowDataUpdate , INodeTranslationHeaders , } from '@/Interface' ; import { IDataObject , INodeCredentials , INodeParameters , INodePropertyOptions , INodeTypeDescription , INodeTypeNameVersion , } from 'n8n-workflow' ; import { makeRestApiRequest } from '@/api/helpers' ; export param { IExecutionFlattedResponse } fullExecutionData returns { IExecutionResponse } function unflattenExecutionData ( fullExecutionData ) { const returnData = { ... 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 ( ) { const self = this ; return { async makeRestApiRequest ( method , endpoint , data ? ) { return makeRestApiRequest ( self . $store . getters . getRestApiContext , method , endpoint , data ) ; } , getActiveWorkflows : ( ) : Promise < string [ ] > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/active` ) ; } , getActivationError : ( id ) : Promise < IActivationError | undefined > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/active/error/${ id }` ) ; } , getCurrentExecutions : ( filter ) : Promise < IExecutionsCurrentSummaryExtended [ ] > => { let sendData = { } ; if ( filter ) { sendData = { filter , } ; } return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/executions-current` , sendData ) ; } , stopCurrentExecution : ( executionId ) : Promise < IExecutionsStopData > => { return self . restApi ( ) . makeRestApiRequest ( 'POST' , `/executions-current/${ executionId }/stop` ) ; } , getCredentialTranslation : ( credentialType ) : Promise < object > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , '/credential-translation' , { credentialType } ) ; } , getNodeTranslationHeaders : ( ) : Promise < INodeTranslationHeaders > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , '/node-translation-headers' ) ; } , getNodeTypes : ( onlyLatest = false ) : Promise < INodeTypeDescription [ ] > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/node-types` , { onlyLatest } ) ; } , getNodesInformation : ( nodeInfos ) : Promise < INodeTypeDescription [ ] > => { return self . restApi ( ) . makeRestApiRequest ( 'POST' , `/node-types` , { nodeInfos } ) ; } , getNodeParameterOptions : ( nodeTypeAndVersion , path , methodName , currentNodeParameters , credentials ? ) : Promise < INodePropertyOptions [ ] > => { const sendData = { nodeTypeAndVersion , path , methodName , credentials , currentNodeParameters , } ; return self . restApi ( ) . makeRestApiRequest ( 'GET' , '/node-parameter-options' , sendData ) ; } , removeTestWebhook : ( workflowId ) : Promise < boolean > => { return self . restApi ( ) . makeRestApiRequest ( 'DELETE' , `/test-webhook/${ workflowId }` ) ; } , runWorkflow : async ( startRunData ) : Promise < IExecutionPushResponse > => { return self . restApi ( ) . makeRestApiRequest ( 'POST' , `/workflows/run` , startRunData ) ; } , createNewWorkflow : ( sendData ) : Promise < IWorkflowDb > => { return self . restApi ( ) . makeRestApiRequest ( 'POST' , `/workflows` , sendData ) ; } , updateWorkflow : ( id , data ) : Promise < IWorkflowDb > => { return self . restApi ( ) . makeRestApiRequest ( 'PATCH' , `/workflows/${ id }` , data ) ; } , deleteWorkflow : ( name ) : Promise < void > => { return self . restApi ( ) . makeRestApiRequest ( 'DELETE' , `/workflows/${ name }` ) ; } , getWorkflow : ( id ) : Promise < IWorkflowDb > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/workflows/${ id }` ) ; } , getWorkflows : ( filter ? ) : Promise < IWorkflowShortResponse [ ] > => { let sendData ; if ( filter ) { sendData = { filter , } ; } return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/workflows` , sendData ) ; } , getWorkflowFromUrl : ( url ) : Promise < IWorkflowDb > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/workflows/from-url` , { url } ) ; } , getExecution : async ( id ) : Promise < IExecutionResponse > => { const response = await self . restApi ( ) . makeRestApiRequest ( 'GET' , `/executions/${ id }` ) ; return unflattenExecutionData ( response ) ; } , deleteExecutions : ( sendData ) : Promise < void > => { return self . restApi ( ) . makeRestApiRequest ( 'POST' , `/executions/delete` , sendData ) ; } , retryExecution : ( id , loadWorkflow ? ) : Promise < boolean > => { let sendData ; if ( loadWorkflow === true ) { sendData = { loadWorkflow : true , } ; } return self . restApi ( ) . makeRestApiRequest ( 'POST' , `/executions/${ id }/retry` , sendData ) ; } , getPastExecutions : ( filter , limit , lastId ? , firstId ? ) : Promise < IExecutionsListResponse > => { let sendData = { } ; if ( filter ) { sendData = { filter , firstId , lastId , limit , } ; } return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/executions` , sendData ) ; } , getTimezones : ( ) : Promise < IDataObject > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/options/timezones` ) ; } , getBinaryBufferString : ( dataPath ) : Promise < string > => { return self . restApi ( ) . makeRestApiRequest ( 'GET' , `/data/${ dataPath }` ) ; } , } ; } , } , } ) ;