mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
extract out hook context
This commit is contained in:
parent
b1c5559832
commit
47764ca805
|
@ -166,7 +166,7 @@ import type { ExtendedValidationResult, IResponseError } from './Interfaces';
|
||||||
import { ScheduledTaskManager } from './ScheduledTaskManager';
|
import { ScheduledTaskManager } from './ScheduledTaskManager';
|
||||||
import { getSecretsProxy } from './Secrets';
|
import { getSecretsProxy } from './Secrets';
|
||||||
import { SSHClientsManager } from './SSHClientsManager';
|
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 { TriggerContext } from './node-execution-context/trigger-context';
|
||||||
import { ExecuteSingleContext } from './node-execution-context/execute-single-context';
|
import { ExecuteSingleContext } from './node-execution-context/execute-single-context';
|
||||||
import { WebhookContext } from './node-execution-context/webhook-context';
|
import { WebhookContext } from './node-execution-context/webhook-context';
|
||||||
|
@ -3670,9 +3670,7 @@ export function getLoadOptionsFunctions(
|
||||||
})(workflow, node, path);
|
})(workflow, node, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** @deprecated */
|
||||||
* Returns the execute functions regular nodes have access to in hook-function.
|
|
||||||
*/
|
|
||||||
export function getExecuteHookFunctions(
|
export function getExecuteHookFunctions(
|
||||||
workflow: Workflow,
|
workflow: Workflow,
|
||||||
node: INode,
|
node: INode,
|
||||||
|
@ -3681,59 +3679,7 @@ export function getExecuteHookFunctions(
|
||||||
activation: WorkflowActivateMode,
|
activation: WorkflowActivateMode,
|
||||||
webhookData?: IWebhookData,
|
webhookData?: IWebhookData,
|
||||||
): IHookFunctions {
|
): IHookFunctions {
|
||||||
return ((workflow: Workflow, node: INode) => {
|
return new HookContext(workflow, node, additionalData, mode, activation, webhookData);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated */
|
/** @deprecated */
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
export { ExecuteSingleContext } from './execute-single-context';
|
export { ExecuteSingleContext } from './execute-single-context';
|
||||||
|
export { HookContext } from './hook-context';
|
||||||
export { PollContext } from './poll-context';
|
export { PollContext } from './poll-context';
|
||||||
export { TriggerContext } from './trigger-context';
|
export { TriggerContext } from './trigger-context';
|
||||||
export { WebhookContext } from './webhook-context';
|
export { WebhookContext } from './webhook-context';
|
||||||
|
|
Loading…
Reference in a new issue