diff --git a/packages/core/src/Interfaces.ts b/packages/core/src/Interfaces.ts index 51dec31fbd..b985fbad42 100644 --- a/packages/core/src/Interfaces.ts +++ b/packages/core/src/Interfaces.ts @@ -1,293 +1,41 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ import { - IAdditionalCredentialOptions, - IAllExecuteFunctions, - IBinaryData, - ICredentialTestFunctions as ICredentialTestFunctionsBase, - IDataObject, + IPollResponse, + ITriggerResponse, + IWorkflowSettings as IWorkflowSettingsWorkflow, IExecuteFunctions as IExecuteFunctionsBase, IExecuteSingleFunctions as IExecuteSingleFunctionsBase, IHookFunctions as IHookFunctionsBase, - IHttpRequestOptions, ILoadOptionsFunctions as ILoadOptionsFunctionsBase, - INodeExecutionData, - IOAuth2Options, - IPairedItemData, IPollFunctions as IPollFunctionsBase, - IPollResponse, ITriggerFunctions as ITriggerFunctionsBase, - ITriggerResponse, IWebhookFunctions as IWebhookFunctionsBase, - IWorkflowSettings as IWorkflowSettingsWorkflow, - NodeExecutionWithMetadata, } from 'n8n-workflow'; -import type { OptionsWithUri, OptionsWithUrl } from 'request'; -import type { RequestPromiseAPI, RequestPromiseOptions } from 'request-promise-native'; +// TODO: remove these after removing `n8n-core` dependency from `nodes-bases` +export type IExecuteFunctions = IExecuteFunctionsBase; +export type IExecuteSingleFunctions = IExecuteSingleFunctionsBase; +export type IHookFunctions = IHookFunctionsBase; +export type ILoadOptionsFunctions = ILoadOptionsFunctionsBase; +export type IPollFunctions = IPollFunctionsBase; +export type ITriggerFunctions = ITriggerFunctionsBase; +export type IWebhookFunctions = IWebhookFunctionsBase; export interface IProcessMessage { + // eslint-disable-next-line @typescript-eslint/no-explicit-any data?: any; type: string; } -export interface IExecuteFunctions extends IExecuteFunctionsBase { - helpers: { - httpRequest(requestOptions: IHttpRequestOptions): Promise; - prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise; - getBinaryDataBuffer(itemIndex: number, propertyName: string): Promise; - setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise; - request: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise; - requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; - normalizeItems(items: INodeExecutionData | INodeExecutionData[]): INodeExecutionData[]; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - constructExecutionMetaData( - inputData: INodeExecutionData[], - options: { itemData: IPairedItemData | IPairedItemData[] }, - ): NodeExecutionWithMetadata[]; - }; -} - -export interface IExecuteSingleFunctions extends IExecuteSingleFunctionsBase { - helpers: { - getBinaryDataBuffer(propertyName: string, inputIndex?: number): Promise; - setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise; - httpRequest(requestOptions: IHttpRequestOptions): Promise; - prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise; - request: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise; - requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - }; -} - -export interface IPollFunctions extends IPollFunctionsBase { - helpers: { - httpRequest(requestOptions: IHttpRequestOptions): Promise; - prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise; - request: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise; - requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - }; -} - export interface IResponseError extends Error { statusCode?: number; } -export interface ITriggerFunctions extends ITriggerFunctionsBase { - helpers: { - httpRequest(requestOptions: IHttpRequestOptions): Promise; - prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise; - request: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise; - requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - }; -} - export interface IUserSettings { encryptionKey?: string; tunnelSubdomain?: string; instanceId?: string; } -export interface ILoadOptionsFunctions extends ILoadOptionsFunctionsBase { - helpers: { - httpRequest(requestOptions: IHttpRequestOptions): Promise; - request?: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2?: ( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ) => Promise; - requestOAuth1?( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - }; -} - -export interface ICredentialTestFunctions extends ICredentialTestFunctionsBase { - helpers: { - request: RequestPromiseAPI; - }; -} - -export interface IHookFunctions extends IHookFunctionsBase { - helpers: { - httpRequest(requestOptions: IHttpRequestOptions): Promise; - request: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise; - requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - }; -} - -export interface IWebhookFunctions extends IWebhookFunctionsBase { - helpers: { - httpRequest(requestOptions: IHttpRequestOptions): Promise; - prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise; - copyBinaryFile(filePath: string, fileName: string, mimeType?: string): Promise; - request: (uriOrObject: string | IDataObject | any, options?: IDataObject) => Promise; - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise; - requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | RequestPromiseOptions, - ): Promise; - returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - ): Promise; - }; -} - export interface IWorkflowSettings extends IWorkflowSettingsWorkflow { errorWorkflow?: string; timezone?: string; diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index e6cc59cd57..b8392ca39d 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -1,7 +1,5 @@ /* eslint-disable @typescript-eslint/no-unsafe-argument */ -/* eslint-disable no-lonely-if */ /* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable no-prototype-builtins */ /* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/prefer-nullish-coalescing */ /* eslint-disable @typescript-eslint/naming-convention */ @@ -14,7 +12,6 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable @typescript-eslint/no-shadow */ /* eslint-disable no-param-reassign */ -/* eslint-disable @typescript-eslint/prefer-optional-chain */ import { GenericValue, IAdditionalCredentialOptions, @@ -24,9 +21,7 @@ import { ICredentialDataDecryptedObject, ICredentialsExpressionResolveValues, IDataObject, - IExecuteFunctions, IExecuteResponsePromiseData, - IExecuteSingleFunctions, IExecuteWorkflowInfo, IHttpRequestOptions, IN8nHttpFullResponse, @@ -35,20 +30,15 @@ import { INodeCredentialDescription, INodeCredentialsDetails, INodeExecutionData, - INodeParameters, IOAuth2Options, - IPollFunctions, IRunExecutionData, ISourceData, ITaskDataConnections, - ITriggerFunctions, IWebhookData, IWebhookDescription, - IWebhookFunctions, IWorkflowDataProxyAdditionalKeys, IWorkflowDataProxyData, IWorkflowExecuteAdditionalData, - IWorkflowMetadata, NodeApiError, NodeHelpers, NodeOperationError, @@ -65,6 +55,17 @@ import { IPairedItemData, deepCopy, fileTypeFromMimeType, + ICredentialTestFunctions, + BinaryHelperFunctions, + RequestHelperFunctions, + FunctionsBase, + IExecuteFunctions, + IExecuteSingleFunctions, + IHookFunctions, + ILoadOptionsFunctions, + IPollFunctions, + ITriggerFunctions, + IWebhookFunctions, } from 'n8n-workflow'; import { Agent } from 'https'; @@ -91,13 +92,7 @@ import axios, { } from 'axios'; import url, { URL, URLSearchParams } from 'url'; import { BinaryDataManager } from './BinaryDataManager'; -import { - ICredentialTestFunctions, - IHookFunctions, - ILoadOptionsFunctions, - IResponseError, - IWorkflowSettings, -} from './Interfaces'; +import type { IResponseError, IWorkflowSettings } from './Interfaces'; import { extractValue } from './ExtractValue'; import { getClientCredentialsToken } from './OAuth2Helper'; import { PLACEHOLDER_EMPTY_EXECUTION_ID } from './Constants'; @@ -911,7 +906,7 @@ export async function copyBinaryFile( * Takes a buffer and converts it into the format n8n uses. It encodes the binary data as * base64 and adds metadata. */ -export async function prepareBinaryData( +async function prepareBinaryData( binaryData: Buffer, executionId: string, filePath?: string, @@ -1116,7 +1111,7 @@ export async function requestOAuth2( }); } - return this.helpers.request!(newRequestOptions).catch(async (error: IResponseError) => { + return this.helpers.request(newRequestOptions).catch(async (error: IResponseError) => { const statusCodeReturned = oAuth2Options?.tokenExpiredStatusCode === undefined ? 401 @@ -1188,7 +1183,7 @@ export async function requestOAuth2( }); } - return this.helpers.request!(newRequestOptions); + return this.helpers.request(newRequestOptions); } // Unknown error so simply throw it @@ -1257,7 +1252,7 @@ export async function requestOAuth1( return this.helpers.httpRequest(requestOptions as IHttpRequestOptions); } - return this.helpers.request!(requestOptions).catch(async (error: IResponseError) => { + return this.helpers.request(requestOptions).catch(async (error: IResponseError) => { // Unknown error so simply throw it throw error; }); @@ -1304,7 +1299,7 @@ export async function httpRequestWithAuthentication( } const data = await additionalData.credentialsHelper.preAuthentication( - { helpers: { httpRequest: this.helpers.httpRequest } }, + { helpers: this.helpers }, credentialsDecrypted, credentialsType, node, @@ -1337,7 +1332,7 @@ export async function httpRequestWithAuthentication( if (credentialsDecrypted !== undefined) { // try to refresh the credentials const data = await additionalData.credentialsHelper.preAuthentication( - { helpers: { httpRequest: this.helpers.httpRequest } }, + { helpers: this.helpers }, credentialsDecrypted, credentialsType, node, @@ -1499,7 +1494,7 @@ export async function requestWithAuthentication( } const data = await additionalData.credentialsHelper.preAuthentication( - { helpers: { httpRequest: this.helpers.httpRequest } }, + { helpers: this.helpers }, credentialsDecrypted, credentialsType, node, @@ -1526,7 +1521,7 @@ export async function requestWithAuthentication( if (credentialsDecrypted !== undefined) { // try to refresh the credentials const data = await additionalData.credentialsHelper.preAuthentication( - { helpers: { httpRequest: this.helpers.httpRequest } }, + { helpers: this.helpers }, credentialsDecrypted, credentialsType, node, @@ -1715,14 +1710,6 @@ export async function getCredentials( return decryptedDataObject; } -/** - * Returns a copy of the node - * - */ -export function getNode(node: INode): INode { - return deepCopy(node); -} - /** * Clean up parameter data to make sure that only valid data gets returned * INFO: Currently only converts Luxon Dates as we know for sure it will not be breaking @@ -1910,21 +1897,105 @@ export function getWebhookDescription( return undefined; } -/** - * Returns the workflow metadata - * - */ -export function getWorkflowMetadata(workflow: Workflow): IWorkflowMetadata { - return { +const getCommonWorkflowFunctions = ( + workflow: Workflow, + node: INode, + additionalData: IWorkflowExecuteAdditionalData, +): Omit => ({ + getNode: () => deepCopy(node), + getWorkflow: () => ({ id: workflow.id, name: workflow.name, active: workflow.active, - }; -} + }), + getWorkflowStaticData: (type) => workflow.getStaticData(type, node), + + getRestApiUrl: () => additionalData.restApiUrl, + getTimezone: () => getTimezone(workflow, additionalData), +}); + +const getRequestHelperFunctions = ( + workflow: Workflow, + node: INode, + additionalData: IWorkflowExecuteAdditionalData, +): RequestHelperFunctions => ({ + httpRequest, + + async httpRequestWithAuthentication( + this, + credentialsType, + requestOptions, + additionalCredentialOptions, + ): Promise { + return httpRequestWithAuthentication.call( + this, + credentialsType, + requestOptions, + workflow, + node, + additionalData, + additionalCredentialOptions, + ); + }, + + request: async (uriOrObject, options) => + proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options), + + async requestWithAuthentication( + this, + credentialsType, + requestOptions, + additionalCredentialOptions, + ): Promise { + return requestWithAuthentication.call( + this, + credentialsType, + requestOptions, + workflow, + node, + additionalData, + additionalCredentialOptions, + ); + }, + + async requestOAuth1( + this: IAllExecuteFunctions, + credentialsType: string, + requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, + ): Promise { + return requestOAuth1.call(this, credentialsType, requestOptions); + }, + + async requestOAuth2( + this: IAllExecuteFunctions, + credentialsType: string, + requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, + oAuth2Options?: IOAuth2Options, + ): Promise { + return requestOAuth2.call( + this, + credentialsType, + requestOptions, + node, + additionalData, + oAuth2Options, + ); + }, +}); + +const getBinaryHelperFunctions = ({ + executionId, +}: IWorkflowExecuteAdditionalData): BinaryHelperFunctions => ({ + prepareBinaryData: async (binaryData, filePath, mimeType) => + prepareBinaryData(binaryData, executionId!, filePath, mimeType), + setBinaryDataBuffer: async (data, binaryData) => + setBinaryDataBuffer(data, binaryData, executionId!), + copyBinaryFile: async (filePath, fileName, mimeType) => + copyBinaryFile(executionId!, filePath, fileName, mimeType), +}); /** * Returns the execute functions the poll nodes have access to. - * */ // TODO: Check if I can get rid of: additionalData, and so then maybe also at ActiveWorkflowRunner.add export function getExecutePollFunctions( @@ -1936,6 +2007,7 @@ export function getExecutePollFunctions( ): IPollFunctions { return ((workflow: Workflow, node: INode) => { return { + ...getCommonWorkflowFunctions(workflow, node, additionalData), __emit: (data: INodeExecutionData[][]): void => { throw new Error('Overwrite NodeExecuteFunctions.getExecutePollFunctions.__emit function!'); }, @@ -1944,18 +2016,9 @@ export function getExecutePollFunctions( 'Overwrite NodeExecuteFunctions.getExecutePollFunctions.__emitError function!', ); }, - async getCredentials(type: string): Promise { - return getCredentials(workflow, node, type, additionalData, mode); - }, - getMode: (): WorkflowExecuteMode => { - return mode; - }, - getActivationMode: (): WorkflowActivateMode => { - return activation; - }, - getNode: () => { - return getNode(node); - }, + getMode: () => mode, + getActivationMode: () => activation, + getCredentials: async (type) => getCredentials(workflow, node, type, additionalData, mode), getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -1982,93 +2045,9 @@ export function getExecutePollFunctions( options, ); }, - getRestApiUrl: (): string => { - return additionalData.restApiUrl; - }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, - getWorkflow: () => { - return getWorkflowMetadata(workflow); - }, - getWorkflowStaticData(type: string): IDataObject { - return workflow.getStaticData(type, node); - }, helpers: { - httpRequest, - async setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise { - return setBinaryDataBuffer.call(this, data, binaryData, additionalData.executionId!); - }, - async prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise { - return prepareBinaryData.call( - this, - binaryData, - additionalData.executionId!, - filePath, - mimeType, - ); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, + ...getRequestHelperFunctions(workflow, node, additionalData), + ...getBinaryHelperFunctions(additionalData), returnJsonArray, }, }; @@ -2077,7 +2056,6 @@ export function getExecutePollFunctions( /** * Returns the execute functions the trigger nodes have access to. - * */ // TODO: Check if I can get rid of: additionalData, and so then maybe also at ActiveWorkflowRunner.add export function getExecuteTriggerFunctions( @@ -2089,24 +2067,16 @@ export function getExecuteTriggerFunctions( ): ITriggerFunctions { return ((workflow: Workflow, node: INode) => { return { + ...getCommonWorkflowFunctions(workflow, node, additionalData), emit: (data: INodeExecutionData[][]): void => { throw new Error('Overwrite NodeExecuteFunctions.getExecuteTriggerFunctions.emit function!'); }, emitError: (error: Error): void => { throw new Error('Overwrite NodeExecuteFunctions.getExecuteTriggerFunctions.emit function!'); }, - async getCredentials(type: string): Promise { - return getCredentials(workflow, node, type, additionalData, mode); - }, - getNode: () => { - return getNode(node); - }, - getMode: (): WorkflowExecuteMode => { - return mode; - }, - getActivationMode: (): WorkflowActivateMode => { - return activation; - }, + getMode: () => mode, + getActivationMode: () => activation, + getCredentials: async (type) => getCredentials(workflow, node, type, additionalData, mode), getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -2133,93 +2103,9 @@ export function getExecuteTriggerFunctions( options, ); }, - getRestApiUrl: (): string => { - return additionalData.restApiUrl; - }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, - getWorkflow: () => { - return getWorkflowMetadata(workflow); - }, - getWorkflowStaticData(type: string): IDataObject { - return workflow.getStaticData(type, node); - }, helpers: { - httpRequest, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - async setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise { - return setBinaryDataBuffer.call(this, data, binaryData, additionalData.executionId!); - }, - async prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise { - return prepareBinaryData.call( - this, - binaryData, - additionalData.executionId!, - filePath, - mimeType, - ); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, + ...getRequestHelperFunctions(workflow, node, additionalData), + ...getBinaryHelperFunctions(additionalData), returnJsonArray, }, }; @@ -2228,7 +2114,6 @@ export function getExecuteTriggerFunctions( /** * Returns the execute functions regular nodes have access to. - * */ export function getExecuteFunctions( workflow: Workflow, @@ -2243,9 +2128,22 @@ export function getExecuteFunctions( ): IExecuteFunctions { return ((workflow, runExecutionData, connectionInputData, inputData, node) => { return { - continueOnFail: () => { - return continueOnFail(node); - }, + ...getCommonWorkflowFunctions(workflow, node, additionalData), + getMode: () => mode, + getCredentials: async (type, itemIndex) => + getCredentials( + workflow, + node, + type, + additionalData, + mode, + runExecutionData, + runIndex, + connectionInputData, + itemIndex, + ), + getExecuteData: () => executeData, + continueOnFail: () => continueOnFail(node), evaluateExpression: (expression: string, itemIndex: number) => { return workflow.expression.resolveSimpleParameterValue( `=${expression}`, @@ -2281,25 +2179,7 @@ export function getExecuteFunctions( getContext(type: string): IContextObject { return NodeHelpers.getContext(runExecutionData, type, node); }, - async getCredentials( - type: string, - itemIndex?: number, - ): Promise { - return getCredentials( - workflow, - node, - type, - additionalData, - mode, - runExecutionData, - runIndex, - connectionInputData, - itemIndex, - ); - }, - getExecutionId: (): string => { - return additionalData.executionId!; - }, + getExecutionId: () => additionalData.executionId!, getInputData: (inputIndex = 0, inputName = 'main') => { if (!inputData.hasOwnProperty(inputName)) { // Return empty array because else it would throw error when nothing is connected to input @@ -2347,24 +2227,6 @@ export function getExecuteFunctions( options, ); }, - getMode: (): WorkflowExecuteMode => { - return mode; - }, - getNode: () => { - return getNode(node); - }, - getRestApiUrl: (): string => { - return additionalData.restApiUrl; - }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, - getExecuteData: (): IExecuteData => { - return executeData; - }, - getWorkflow: () => { - return getWorkflowMetadata(workflow); - }, getWorkflowDataProxy: (itemIndex: number): IWorkflowDataProxyData => { const dataProxy = new WorkflowDataProxy( workflow, @@ -2381,9 +2243,6 @@ export function getExecuteFunctions( ); return dataProxy.getDataProxy(); }, - getWorkflowStaticData(type: string): IDataObject { - return workflow.getStaticData(type, node); - }, prepareOutputData: NodeHelpers.prepareOutputData, async putExecutionToWait(waitTill: Date): Promise { runExecutionData.waitTill = waitTill; @@ -2416,87 +2275,10 @@ export function getExecuteFunctions( await additionalData.hooks?.executeHookFunctions('sendResponse', [response]); }, helpers: { - httpRequest, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - async setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise { - return setBinaryDataBuffer.call(this, data, binaryData, additionalData.executionId!); - }, - async prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise { - return prepareBinaryData.call( - this, - binaryData, - additionalData.executionId!, - filePath, - mimeType, - ); - }, - async getBinaryDataBuffer( - itemIndex: number, - propertyName: string, - inputIndex = 0, - ): Promise { - return getBinaryDataBuffer.call(this, inputData, itemIndex, propertyName, inputIndex); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, + ...getRequestHelperFunctions(workflow, node, additionalData), + ...getBinaryHelperFunctions(additionalData), + getBinaryDataBuffer: async (itemIndex, propertyName, inputIndex = 0) => + getBinaryDataBuffer(inputData, itemIndex, propertyName, inputIndex), returnJsonArray, normalizeItems, constructExecutionMetaData, @@ -2507,7 +2289,6 @@ export function getExecuteFunctions( /** * Returns the execute functions regular nodes have access to when single-function is defined. - * */ export function getExecuteSingleFunctions( workflow: Workflow, @@ -2523,9 +2304,8 @@ export function getExecuteSingleFunctions( ): IExecuteSingleFunctions { return ((workflow, runExecutionData, connectionInputData, inputData, node, itemIndex) => { return { - continueOnFail: () => { - return continueOnFail(node); - }, + ...getCommonWorkflowFunctions(workflow, node, additionalData), + continueOnFail: () => continueOnFail(node), evaluateExpression: (expression: string, evaluateItemIndex: number | undefined) => { evaluateItemIndex = evaluateItemIndex === undefined ? itemIndex : evaluateItemIndex; return workflow.expression.resolveSimpleParameterValue( @@ -2545,8 +2325,8 @@ export function getExecuteSingleFunctions( getContext(type: string): IContextObject { return NodeHelpers.getContext(runExecutionData, type, node); }, - async getCredentials(type: string): Promise { - return getCredentials( + getCredentials: async (type) => + getCredentials( workflow, node, type, @@ -2556,8 +2336,7 @@ export function getExecuteSingleFunctions( runIndex, connectionInputData, itemIndex, - ); - }, + ), getInputData: (inputIndex = 0, inputName = 'main') => { if (!inputData.hasOwnProperty(inputName)) { // Return empty array because else it would throw error when nothing is connected to input @@ -2592,24 +2371,9 @@ export function getExecuteSingleFunctions( } return executeData.source[inputName][inputIndex] as ISourceData; }, - getItemIndex() { - return itemIndex; - }, - getMode: (): WorkflowExecuteMode => { - return mode; - }, - getNode: () => { - return getNode(node); - }, - getRestApiUrl: (): string => { - return additionalData.restApiUrl; - }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, - getExecuteData: (): IExecuteData => { - return executeData; - }, + getItemIndex: () => itemIndex, + getMode: () => mode, + getExecuteData: () => executeData, getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -2631,9 +2395,6 @@ export function getExecuteSingleFunctions( options, ); }, - getWorkflow: () => { - return getWorkflowMetadata(workflow); - }, getWorkflowDataProxy: (): IWorkflowDataProxyData => { const dataProxy = new WorkflowDataProxy( workflow, @@ -2650,87 +2411,12 @@ export function getExecuteSingleFunctions( ); return dataProxy.getDataProxy(); }, - getWorkflowStaticData(type: string): IDataObject { - return workflow.getStaticData(type, node); - }, helpers: { - async getBinaryDataBuffer(propertyName: string, inputIndex = 0): Promise { - return getBinaryDataBuffer.call(this, inputData, itemIndex, propertyName, inputIndex); - }, - httpRequest, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - async setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise { - return setBinaryDataBuffer.call(this, data, binaryData, additionalData.executionId!); - }, - async prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise { - return prepareBinaryData.call( - this, - binaryData, - additionalData.executionId!, - filePath, - mimeType, - ); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, + ...getRequestHelperFunctions(workflow, node, additionalData), + ...getBinaryHelperFunctions(additionalData), + + getBinaryDataBuffer: async (propertyName, inputIndex = 0) => + getBinaryDataBuffer(inputData, itemIndex, propertyName, inputIndex), }, }; })(workflow, runExecutionData, connectionInputData, inputData, node, itemIndex); @@ -2746,7 +2432,6 @@ export function getCredentialTestFunctions(): ICredentialTestFunctions { /** * Returns the execute functions regular nodes have access to in load-options-function. - * */ export function getLoadOptionsFunctions( workflow: Workflow, @@ -2755,10 +2440,10 @@ export function getLoadOptionsFunctions( additionalData: IWorkflowExecuteAdditionalData, ): ILoadOptionsFunctions { return ((workflow: Workflow, node: INode, path: string) => { - const that = { - async getCredentials(type: string): Promise { - return getCredentials(workflow, node, type, additionalData, 'internal'); - }, + return { + ...getCommonWorkflowFunctions(workflow, node, additionalData), + getCredentials: async (type) => + getCredentials(workflow, node, type, additionalData, 'internal'), getCurrentNodeParameter: ( parameterPath: string, options?: IGetNodeParameterOptions, @@ -2789,12 +2474,7 @@ export function getLoadOptionsFunctions( return returnData; }, - getCurrentNodeParameters: (): INodeParameters | undefined => { - return additionalData.currentNodeParameters; - }, - getNode: () => { - return getNode(node); - }, + getCurrentNodeParameters: () => additionalData.currentNodeParameters, getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -2822,80 +2502,13 @@ export function getLoadOptionsFunctions( options, ); }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, - getRestApiUrl: (): string => { - return additionalData.restApiUrl; - }, - helpers: { - httpRequest, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - }, + helpers: getRequestHelperFunctions(workflow, node, additionalData), }; - return that; })(workflow, node, path); } /** * Returns the execute functions regular nodes have access to in hook-function. - * */ export function getExecuteHookFunctions( workflow: Workflow, @@ -2907,19 +2520,11 @@ export function getExecuteHookFunctions( webhookData?: IWebhookData, ): IHookFunctions { return ((workflow: Workflow, node: INode) => { - const that = { - async getCredentials(type: string): Promise { - return getCredentials(workflow, node, type, additionalData, mode); - }, - getMode: (): WorkflowExecuteMode => { - return mode; - }, - getActivationMode: (): WorkflowActivateMode => { - return activation; - }, - getNode: () => { - return getNode(node); - }, + return { + ...getCommonWorkflowFunctions(workflow, node, additionalData), + getCredentials: async (type) => getCredentials(workflow, node, type, additionalData, mode), + getMode: () => mode, + getActivationMode: () => activation, getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -2958,92 +2563,20 @@ export function getExecuteHookFunctions( isTest, ); }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, getWebhookName(): string { if (webhookData === undefined) { throw new Error('Is only supported in webhook functions!'); } return webhookData.webhookDescription.name; }, - getWebhookDescription(name: string): IWebhookDescription | undefined { - return getWebhookDescription(name, workflow, node); - }, - getWorkflow: () => { - return getWorkflowMetadata(workflow); - }, - getWorkflowStaticData(type: string): IDataObject { - return workflow.getStaticData(type, node); - }, - helpers: { - httpRequest, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - }, + getWebhookDescription: (name) => getWebhookDescription(name, workflow, node), + helpers: getRequestHelperFunctions(workflow, node, additionalData), }; - return that; })(workflow, node); } /** * Returns the execute functions regular nodes have access to when webhook-function is defined. - * */ export function getExecuteWebhookFunctions( workflow: Workflow, @@ -3054,27 +2587,21 @@ export function getExecuteWebhookFunctions( ): IWebhookFunctions { return ((workflow: Workflow, node: INode) => { return { + ...getCommonWorkflowFunctions(workflow, node, additionalData), getBodyData(): IDataObject { if (additionalData.httpRequest === undefined) { throw new Error('Request is missing!'); } return additionalData.httpRequest.body; }, - async getCredentials(type: string): Promise { - return getCredentials(workflow, node, type, additionalData, mode); - }, + getCredentials: async (type) => getCredentials(workflow, node, type, additionalData, mode), getHeaderData(): IncomingHttpHeaders { if (additionalData.httpRequest === undefined) { throw new Error('Request is missing!'); } return additionalData.httpRequest.headers; }, - getMode: (): WorkflowExecuteMode => { - return mode; - }, - getNode: () => { - return getNode(node); - }, + getMode: () => mode, getNodeParameter: ( parameterName: string, fallbackValue?: any, @@ -3125,8 +2652,8 @@ export function getExecuteWebhookFunctions( } return additionalData.httpResponse; }, - getNodeWebhookUrl: (name: string): string | undefined => { - return getNodeWebhookUrl( + getNodeWebhookUrl: (name: string): string | undefined => + getNodeWebhookUrl( name, workflow, node, @@ -3134,109 +2661,12 @@ export function getExecuteWebhookFunctions( mode, additionalData.timezone, getAdditionalKeys(additionalData, mode), - ); - }, - getTimezone: (): string => { - return getTimezone(workflow, additionalData); - }, - getWorkflow: () => { - return getWorkflowMetadata(workflow); - }, - getWorkflowStaticData(type: string): IDataObject { - return workflow.getStaticData(type, node); - }, - getWebhookName(): string { - return webhookData.webhookDescription.name; - }, + ), + getWebhookName: () => webhookData.webhookDescription.name, prepareOutputData: NodeHelpers.prepareOutputData, helpers: { - httpRequest, - async requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return requestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, - async setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise { - return setBinaryDataBuffer.call(this, data, binaryData, additionalData.executionId!); - }, - async copyBinaryFile( - filePath: string, - fileName: string, - mimeType?: string, - ): Promise { - return copyBinaryFile.call( - this, - additionalData.executionId!, - filePath, - fileName, - mimeType, - ); - }, - async prepareBinaryData( - binaryData: Buffer, - filePath?: string, - mimeType?: string, - ): Promise { - return prepareBinaryData.call( - this, - binaryData, - additionalData.executionId!, - filePath, - mimeType, - ); - }, - request: async (uriOrObject: string | IDataObject, options?: IDataObject | undefined) => { - return proxyRequestToAxios(workflow, additionalData, node, uriOrObject, options); - }, - async requestOAuth2( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUri | requestPromise.RequestPromiseOptions, - oAuth2Options?: IOAuth2Options, - ): Promise { - return requestOAuth2.call( - this, - credentialsType, - requestOptions, - node, - additionalData, - oAuth2Options, - ); - }, - async requestOAuth1( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: OptionsWithUrl | requestPromise.RequestPromiseOptions, - ): Promise { - return requestOAuth1.call(this, credentialsType, requestOptions); - }, - async httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise { - return httpRequestWithAuthentication.call( - this, - credentialsType, - requestOptions, - workflow, - node, - additionalData, - additionalCredentialOptions, - ); - }, + ...getRequestHelperFunctions(workflow, node, additionalData), + ...getBinaryHelperFunctions(additionalData), returnJsonArray, }, }; diff --git a/packages/nodes-base/nodes/Aws/AwsSnsTrigger.node.ts b/packages/nodes-base/nodes/Aws/AwsSnsTrigger.node.ts index 4f67f1f2df..45b589487d 100644 --- a/packages/nodes-base/nodes/Aws/AwsSnsTrigger.node.ts +++ b/packages/nodes-base/nodes/Aws/AwsSnsTrigger.node.ts @@ -154,7 +154,6 @@ export class AwsSnsTrigger implements INodeType { }, }; - //@ts-expect-error because of webhook webhookMethods = { default: { async checkExists(this: IHookFunctions): Promise { diff --git a/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts b/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts index 4bbd62de84..f2889c9141 100644 --- a/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts +++ b/packages/nodes-base/nodes/Metabase/QuestionsDescription.ts @@ -82,7 +82,7 @@ export const questionsOperations: INodeProperties[] = [ items[i].binary!.data = await this.helpers.prepareBinaryData( response.body as Buffer, 'data', - response.headers['content-type'], + response.headers['content-type'] as string, ); } result.push(items[i]); diff --git a/packages/nodes-base/nodes/SendInBlue/GenericFunctions.ts b/packages/nodes-base/nodes/SendInBlue/GenericFunctions.ts index 98cfac34b7..e8a68f7798 100644 --- a/packages/nodes-base/nodes/SendInBlue/GenericFunctions.ts +++ b/packages/nodes-base/nodes/SendInBlue/GenericFunctions.ts @@ -78,9 +78,9 @@ export namespace SendInBlueNode { ); } - const bufferFromIncomingData = (await this.helpers.getBinaryDataBuffer( + const bufferFromIncomingData = await this.helpers.getBinaryDataBuffer( binaryPropertyAttachmentName, - )) as Buffer; + ); const { data: content, @@ -93,8 +93,8 @@ export namespace SendInBlueNode { const name = getFileName( itemIndex, mimeType, - fileExtension, - fileName || item.binary!.data.fileName, + fileExtension!, + fileName || item.binary!.data.fileName!, ); attachment.push({ content, name }); diff --git a/packages/nodes-base/nodes/Splunk/GenericFunctions.ts b/packages/nodes-base/nodes/Splunk/GenericFunctions.ts index 9e3b6c3856..4acf0a312e 100644 --- a/packages/nodes-base/nodes/Splunk/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Splunk/GenericFunctions.ts @@ -20,7 +20,7 @@ export async function splunkApiRequest( endpoint: string, body: IDataObject = {}, qs: IDataObject = {}, -) { +): Promise { const { authToken, baseUrl, allowUnauthorizedCerts } = (await this.getCredentials( 'splunkApi', )) as SplunkCredentials; diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 64c3c7be5b..f91ed4b737 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -4,6 +4,9 @@ import type * as express from 'express'; import type * as FormData from 'form-data'; import type { IncomingHttpHeaders } from 'http'; import type { URLSearchParams } from 'url'; +import type { OptionsWithUri, OptionsWithUrl } from 'request'; +import type { RequestPromiseOptions, RequestPromiseAPI } from 'request-promise-native'; + import type { IDeferredPromise } from './DeferredPromise'; import type { Workflow } from './Workflow'; import type { WorkflowHooks } from './WorkflowHooks'; @@ -609,87 +612,6 @@ namespace ExecuteFunctions { }; } -export type IExecuteFunctions = ExecuteFunctions.GetNodeParameterFn & { - continueOnFail(): boolean; - evaluateExpression(expression: string, itemIndex: number): NodeParameterValueType; - executeWorkflow( - workflowInfo: IExecuteWorkflowInfo, - inputData?: INodeExecutionData[], - ): Promise; - getContext(type: string): IContextObject; - getCredentials(type: string, itemIndex?: number): Promise; - getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[]; - getInputSourceData(inputIndex?: number, inputName?: string): ISourceData; - getMode(): WorkflowExecuteMode; - getNode(): INode; - getWorkflowDataProxy(itemIndex: number): IWorkflowDataProxyData; - getWorkflowStaticData(type: string): IDataObject; - getRestApiUrl(): string; - getTimezone(): string; - getExecuteData(): IExecuteData; - getWorkflow(): IWorkflowMetadata; - prepareOutputData( - outputData: INodeExecutionData[], - outputIndex?: number, - ): Promise; - putExecutionToWait(waitTill: Date): Promise; - sendMessageToUI(message: any): void; - sendResponse(response: IExecuteResponsePromiseData): void; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: (...args: any[]) => any; - }; -}; - -export interface IExecuteSingleFunctions { - continueOnFail(): boolean; - evaluateExpression(expression: string, itemIndex: number | undefined): NodeParameterValueType; - getContext(type: string): IContextObject; - getCredentials(type: string): Promise; - getInputData(inputIndex?: number, inputName?: string): INodeExecutionData; - getInputSourceData(inputIndex?: number, inputName?: string): ISourceData; - getItemIndex(): number; - getMode(): WorkflowExecuteMode; - getNode(): INode; - getNodeParameter( - parameterName: string, - fallbackValue?: any, - options?: IGetNodeParameterOptions, - ): NodeParameterValueType | object; - getRestApiUrl(): string; - getTimezone(): string; - getExecuteData(): IExecuteData; - getWorkflow(): IWorkflowMetadata; - getWorkflowDataProxy(): IWorkflowDataProxyData; - getWorkflowStaticData(type: string): IDataObject; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: (...args: any[]) => any; - }; -} - -export interface IExecutePaginationFunctions extends IExecuteSingleFunctions { - makeRoutingRequest( - this: IAllExecuteFunctions, - requestOptions: DeclarativeRestApiSettings.ResultOptions, - ): Promise; -} export interface IExecuteWorkflowInfo { code?: IWorkflowBase; id?: string; @@ -702,13 +624,126 @@ export type ICredentialTestFunction = ( export interface ICredentialTestFunctions { helpers: { - [key: string]: (...args: any[]) => any; + request: RequestPromiseAPI; }; } -export interface ILoadOptionsFunctions { - getCredentials(type: string): Promise; +export interface JsonHelperFunctions { + returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; +} + +export interface BinaryHelperFunctions { + prepareBinaryData(binaryData: Buffer, filePath?: string, mimeType?: string): Promise; + setBinaryDataBuffer(data: IBinaryData, binaryData: Buffer): Promise; + copyBinaryFile(filePath: string, fileName: string, mimeType?: string): Promise; +} + +export interface RequestHelperFunctions { + request(uriOrObject: string | IDataObject | any, options?: IDataObject): Promise; + requestWithAuthentication( + this: IAllExecuteFunctions, + credentialsType: string, + requestOptions: OptionsWithUri | RequestPromiseOptions, + additionalCredentialOptions?: IAdditionalCredentialOptions, + ): Promise; + + httpRequest(requestOptions: IHttpRequestOptions): Promise; + httpRequestWithAuthentication( + this: IAllExecuteFunctions, + credentialsType: string, + requestOptions: IHttpRequestOptions, + additionalCredentialOptions?: IAdditionalCredentialOptions, + ): Promise; + + requestOAuth1( + this: IAllExecuteFunctions, + credentialsType: string, + requestOptions: OptionsWithUrl | RequestPromiseOptions, + ): Promise; + requestOAuth2( + this: IAllExecuteFunctions, + credentialsType: string, + requestOptions: OptionsWithUri | RequestPromiseOptions, + oAuth2Options?: IOAuth2Options, + ): Promise; +} + +export interface FunctionsBase { + getCredentials(type: string, itemIndex?: number): Promise; getNode(): INode; + getWorkflow(): IWorkflowMetadata; + getWorkflowStaticData(type: string): IDataObject; + getTimezone(): string; + getRestApiUrl(): string; + + getMode?: () => WorkflowExecuteMode; + getActivationMode?: () => WorkflowActivateMode; +} + +type FunctionsBaseWithRequiredKeys = FunctionsBase & { + [K in Keys]: NonNullable; +}; + +type BaseExecutionFunctions = FunctionsBaseWithRequiredKeys<'getMode'> & { + continueOnFail(): boolean; + evaluateExpression(expression: string, itemIndex: number): NodeParameterValueType; + getContext(type: string): IContextObject; + getExecuteData(): IExecuteData; + getWorkflowDataProxy(itemIndex: number): IWorkflowDataProxyData; + getInputSourceData(inputIndex?: number, inputName?: string): ISourceData; +}; + +export type IExecuteFunctions = ExecuteFunctions.GetNodeParameterFn & + BaseExecutionFunctions & { + executeWorkflow( + workflowInfo: IExecuteWorkflowInfo, + inputData?: INodeExecutionData[], + ): Promise; + getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[]; + prepareOutputData( + outputData: INodeExecutionData[], + outputIndex?: number, + ): Promise; + putExecutionToWait(waitTill: Date): Promise; + sendMessageToUI(message: any): void; + sendResponse(response: IExecuteResponsePromiseData): void; + + helpers: RequestHelperFunctions & + BinaryHelperFunctions & + JsonHelperFunctions & { + normalizeItems(items: INodeExecutionData | INodeExecutionData[]): INodeExecutionData[]; + constructExecutionMetaData( + inputData: INodeExecutionData[], + options: { itemData: IPairedItemData | IPairedItemData[] }, + ): NodeExecutionWithMetadata[]; + + getBinaryDataBuffer(itemIndex: number, propertyName: string): Promise; + }; + }; + +export interface IExecuteSingleFunctions extends BaseExecutionFunctions { + getInputData(inputIndex?: number, inputName?: string): INodeExecutionData; + getItemIndex(): number; + getNodeParameter( + parameterName: string, + fallbackValue?: any, + options?: IGetNodeParameterOptions, + ): NodeParameterValueType | object; + + helpers: RequestHelperFunctions & + BinaryHelperFunctions & { + getBinaryDataBuffer(propertyName: string, inputIndex?: number): Promise; + }; +} + +export interface IExecutePaginationFunctions extends IExecuteSingleFunctions { + makeRoutingRequest( + this: IAllExecuteFunctions, + requestOptions: DeclarativeRestApiSettings.ResultOptions, + ): Promise; +} + +export interface ILoadOptionsFunctions extends FunctionsBase { getNodeParameter( parameterName: string, fallbackValue?: any, @@ -719,134 +754,57 @@ export interface ILoadOptionsFunctions { options?: IGetNodeParameterOptions, ): NodeParameterValueType | object | undefined; getCurrentNodeParameters(): INodeParameters | undefined; - getTimezone(): string; - getRestApiUrl(): string; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - // TODO: Remove from here. Add it only now to LoadOptions as many nodes do import - // from n8n-workflow instead of n8n-core - requestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: any, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: ((...args: any[]) => any) | undefined; - }; + helpers: RequestHelperFunctions; } -export interface IHookFunctions { - getCredentials(type: string): Promise; - getMode(): WorkflowExecuteMode; - getActivationMode(): WorkflowActivateMode; - getNode(): INode; - getNodeWebhookUrl: (name: string) => string | undefined; - getNodeParameter( - parameterName: string, - fallbackValue?: any, - options?: IGetNodeParameterOptions, - ): NodeParameterValueType | object; - getTimezone(): string; - getWebhookDescription(name: string): IWebhookDescription | undefined; - getWebhookName(): string; - getWorkflow(): IWorkflowMetadata; - getWorkflowStaticData(type: string): IDataObject; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: (...args: any[]) => any; - }; -} - -export interface IPollFunctions { +export interface IPollFunctions + extends FunctionsBaseWithRequiredKeys<'getMode' | 'getActivationMode'> { __emit( data: INodeExecutionData[][], responsePromise?: IDeferredPromise, donePromise?: IDeferredPromise, ): void; __emitError(error: Error, responsePromise?: IDeferredPromise): void; - getCredentials(type: string): Promise; - getMode(): WorkflowExecuteMode; - getActivationMode(): WorkflowActivateMode; - getNode(): INode; getNodeParameter( parameterName: string, fallbackValue?: any, options?: IGetNodeParameterOptions, ): NodeParameterValueType | object; - getRestApiUrl(): string; - getTimezone(): string; - getWorkflow(): IWorkflowMetadata; - getWorkflowStaticData(type: string): IDataObject; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: (...args: any[]) => any; - }; + helpers: RequestHelperFunctions & BinaryHelperFunctions & JsonHelperFunctions; } -export interface ITriggerFunctions { +export interface ITriggerFunctions + extends FunctionsBaseWithRequiredKeys<'getMode' | 'getActivationMode'> { emit( data: INodeExecutionData[][], responsePromise?: IDeferredPromise, donePromise?: IDeferredPromise, ): void; emitError(error: Error, responsePromise?: IDeferredPromise): void; - getCredentials(type: string): Promise; - getMode(): WorkflowExecuteMode; - getActivationMode(): WorkflowActivateMode; - getNode(): INode; getNodeParameter( parameterName: string, fallbackValue?: any, options?: IGetNodeParameterOptions, ): NodeParameterValueType | object; - getRestApiUrl(): string; - getTimezone(): string; - getWorkflow(): IWorkflowMetadata; - getWorkflowStaticData(type: string): IDataObject; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: (...args: any[]) => any; - }; + helpers: RequestHelperFunctions & BinaryHelperFunctions & JsonHelperFunctions; } -export interface IWebhookFunctions { +export interface IHookFunctions + extends FunctionsBaseWithRequiredKeys<'getMode' | 'getActivationMode'> { + getWebhookName(): string; + getWebhookDescription(name: string): IWebhookDescription | undefined; + getNodeWebhookUrl: (name: string) => string | undefined; + getNodeParameter( + parameterName: string, + fallbackValue?: any, + options?: IGetNodeParameterOptions, + ): NodeParameterValueType | object; + helpers: RequestHelperFunctions; +} + +export interface IWebhookFunctions extends FunctionsBaseWithRequiredKeys<'getMode'> { getBodyData(): IDataObject; - getCredentials(type: string): Promise; getHeaderData(): IncomingHttpHeaders; - getMode(): WorkflowExecuteMode; - getNode(): INode; getNodeParameter( parameterName: string, fallbackValue?: any, @@ -857,26 +815,12 @@ export interface IWebhookFunctions { getQueryData(): object; getRequestObject(): express.Request; getResponseObject(): express.Response; - getTimezone(): string; getWebhookName(): string; - getWorkflowStaticData(type: string): IDataObject; - getWorkflow(): IWorkflowMetadata; prepareOutputData( outputData: INodeExecutionData[], outputIndex?: number, ): Promise; - helpers: { - httpRequest( - requestOptions: IHttpRequestOptions, - ): Promise; - httpRequestWithAuthentication( - this: IAllExecuteFunctions, - credentialsType: string, - requestOptions: IHttpRequestOptions, - additionalCredentialOptions?: IAdditionalCredentialOptions, - ): Promise; - [key: string]: (...args: any[]) => any; - }; + helpers: RequestHelperFunctions & BinaryHelperFunctions & JsonHelperFunctions; } export interface INodeCredentialsDetails { diff --git a/packages/workflow/src/RoutingNode.ts b/packages/workflow/src/RoutingNode.ts index 4a119eb2dd..068fb148be 100644 --- a/packages/workflow/src/RoutingNode.ts +++ b/packages/workflow/src/RoutingNode.ts @@ -391,7 +391,7 @@ export class RoutingNode { return returnData; } if (action.type === 'binaryData') { - responseData.body = Buffer.from(responseData.body as string); + const body = (responseData.body = Buffer.from(responseData.body as string)); let { destinationProperty } = action.properties; destinationProperty = this.getParameterValue( @@ -403,7 +403,7 @@ export class RoutingNode { false, ) as string; - const binaryData = await executeSingleFunctions.helpers.prepareBinaryData(responseData.body); + const binaryData = await executeSingleFunctions.helpers.prepareBinaryData(body); return inputData.map((item) => { if (typeof item.json === 'string') {