extract out hook context

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2024-10-25 23:04:27 +02:00
parent b1c5559832
commit 47764ca805
No known key found for this signature in database
3 changed files with 121 additions and 57 deletions

View file

@ -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 */

View file

@ -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<T extends object = ICredentialDataDecryptedObject>(type: string) {
return await getCredentials<T>(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);
}
}

View file

@ -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';