From 47764ca805fb3f093d579e4e8841c5a58f905076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 25 Oct 2024 23:04:27 +0200 Subject: [PATCH] extract out hook context --- packages/core/src/NodeExecuteFunctions.ts | 60 +-------- .../node-execution-context/hook-context.ts | 117 ++++++++++++++++++ .../core/src/node-execution-context/index.ts | 1 + 3 files changed, 121 insertions(+), 57 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 3de65ae2c9..46dfcdaa1c 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -166,7 +166,7 @@ import type { ExtendedValidationResult, IResponseError } from './Interfaces'; import { ScheduledTaskManager } from './ScheduledTaskManager'; import { getSecretsProxy } from './Secrets'; import { SSHClientsManager } from './SSHClientsManager'; -import { PollContext } from './node-execution-context'; +import { HookContext, PollContext } from './node-execution-context'; import { TriggerContext } from './node-execution-context/trigger-context'; import { ExecuteSingleContext } from './node-execution-context/execute-single-context'; import { WebhookContext } from './node-execution-context/webhook-context'; @@ -3670,9 +3670,7 @@ export function getLoadOptionsFunctions( })(workflow, node, path); } -/** - * Returns the execute functions regular nodes have access to in hook-function. - */ +/** @deprecated */ export function getExecuteHookFunctions( workflow: Workflow, node: INode, @@ -3681,59 +3679,7 @@ export function getExecuteHookFunctions( activation: WorkflowActivateMode, webhookData?: IWebhookData, ): IHookFunctions { - return ((workflow: Workflow, node: INode) => { - return { - ...getCommonWorkflowFunctions(workflow, node, additionalData), - getCredentials: async (type) => - await getCredentials(workflow, node, type, additionalData, mode), - getMode: () => mode, - getActivationMode: () => activation, - getNodeParameter: ( - parameterName: string, - fallbackValue?: any, - options?: IGetNodeParameterOptions, - ): NodeParameterValueType | object => { - const runExecutionData: IRunExecutionData | null = null; - const itemIndex = 0; - const runIndex = 0; - const connectionInputData: INodeExecutionData[] = []; - - return getNodeParameter( - workflow, - runExecutionData, - runIndex, - connectionInputData, - node, - parameterName, - itemIndex, - mode, - getAdditionalKeys(additionalData, mode, runExecutionData), - undefined, - fallbackValue, - options, - ); - }, - getNodeWebhookUrl: (name: string): string | undefined => { - return getNodeWebhookUrl( - name, - workflow, - node, - additionalData, - mode, - getAdditionalKeys(additionalData, mode, null), - webhookData?.isTest, - ); - }, - getWebhookName(): string { - if (webhookData === undefined) { - throw new ApplicationError('Only supported in webhook functions'); - } - return webhookData.webhookDescription.name; - }, - getWebhookDescription: (name) => getWebhookDescription(name, workflow, node), - helpers: getRequestHelperFunctions(workflow, node, additionalData), - }; - })(workflow, node); + return new HookContext(workflow, node, additionalData, mode, activation, webhookData); } /** @deprecated */ diff --git a/packages/core/src/node-execution-context/hook-context.ts b/packages/core/src/node-execution-context/hook-context.ts index e69de29bb2..21b572daa6 100644 --- a/packages/core/src/node-execution-context/hook-context.ts +++ b/packages/core/src/node-execution-context/hook-context.ts @@ -0,0 +1,117 @@ +import type { + ICredentialDataDecryptedObject, + IGetNodeParameterOptions, + INode, + INodeExecutionData, + IHookFunctions, + IRunExecutionData, + IWorkflowExecuteAdditionalData, + NodeParameterValueType, + Workflow, + WorkflowActivateMode, + WorkflowExecuteMode, + IWebhookData, +} from 'n8n-workflow'; +import { ApplicationError } from 'n8n-workflow'; + +import { + getAdditionalKeys, + getCredentials, + getNodeParameter, + getNodeWebhookUrl, + getWebhookDescription, +} from '@/NodeExecuteFunctions'; +import { BaseContext } from './base-contexts'; +import { RequestHelpers } from './helpers/request-helpers'; + +export class HookContext extends BaseContext implements IHookFunctions { + readonly helpers: IHookFunctions['helpers']; + + constructor( + workflow: Workflow, + node: INode, + additionalData: IWorkflowExecuteAdditionalData, + private readonly mode: WorkflowExecuteMode, + private readonly activation: WorkflowActivateMode, + private readonly webhookData?: IWebhookData, + ) { + super(workflow, node, additionalData); + + const requestHelpers = new RequestHelpers(this, workflow, node, additionalData); + + this.helpers = { + httpRequest: requestHelpers.httpRequest.bind(requestHelpers), + httpRequestWithAuthentication: + requestHelpers.httpRequestWithAuthentication.bind(requestHelpers), + requestWithAuthenticationPaginated: + requestHelpers.requestWithAuthenticationPaginated.bind(requestHelpers), + request: requestHelpers.request.bind(requestHelpers), + requestWithAuthentication: requestHelpers.requestWithAuthentication.bind(requestHelpers), + requestOAuth1: requestHelpers.requestOAuth1.bind(requestHelpers), + requestOAuth2: requestHelpers.requestOAuth2.bind(requestHelpers), + }; + } + + getMode() { + return this.mode; + } + + getActivationMode() { + return this.activation; + } + + // TODO: This is identical to PollContext + async getCredentials(type: string) { + return await getCredentials(this.workflow, this.node, type, this.additionalData, this.mode); + } + + // TODO: This is identical to PollContext + getNodeParameter( + parameterName: string, + fallbackValue?: any, + options?: IGetNodeParameterOptions, + ): NodeParameterValueType | object { + const runExecutionData: IRunExecutionData | null = null; + const itemIndex = 0; + const runIndex = 0; + const connectionInputData: INodeExecutionData[] = []; + + return getNodeParameter( + this.workflow, + runExecutionData, + runIndex, + connectionInputData, + this.node, + parameterName, + itemIndex, + this.mode, + getAdditionalKeys(this.additionalData, this.mode, runExecutionData), + undefined, + fallbackValue, + options, + ); + } + + getNodeWebhookUrl(name: string): string | undefined { + return getNodeWebhookUrl( + name, + this.workflow, + this.node, + this.additionalData, + this.mode, + getAdditionalKeys(this.additionalData, this.mode, null), + this.webhookData?.isTest, + ); + } + + getWebhookName(): string { + if (this.webhookData === undefined) { + throw new ApplicationError('Only supported in webhook functions'); + } + return this.webhookData.webhookDescription.name; + } + + getWebhookDescription(name: string) { + return getWebhookDescription(name, this.workflow, this.node); + } +} diff --git a/packages/core/src/node-execution-context/index.ts b/packages/core/src/node-execution-context/index.ts index 9878b62c87..7b59642121 100644 --- a/packages/core/src/node-execution-context/index.ts +++ b/packages/core/src/node-execution-context/index.ts @@ -1,4 +1,5 @@ export { ExecuteSingleContext } from './execute-single-context'; +export { HookContext } from './hook-context'; export { PollContext } from './poll-context'; export { TriggerContext } from './trigger-context'; export { WebhookContext } from './webhook-context';