-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Extract hook context out of NodeExecutionFunctions (n…
…o-changelog)
- Loading branch information
Showing
6 changed files
with
265 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
packages/core/src/node-execution-context/__tests__/hook-context.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { mock } from 'jest-mock-extended'; | ||
import type { | ||
Expression, | ||
ICredentialDataDecryptedObject, | ||
ICredentialsHelper, | ||
INode, | ||
INodeType, | ||
INodeTypes, | ||
IWebhookDescription, | ||
IWebhookData, | ||
IWorkflowExecuteAdditionalData, | ||
Workflow, | ||
WorkflowActivateMode, | ||
WorkflowExecuteMode, | ||
} from 'n8n-workflow'; | ||
import { ApplicationError } from 'n8n-workflow'; | ||
|
||
import { HookContext } from '../hook-context'; | ||
|
||
describe('HookContext', () => { | ||
const testCredentialType = 'testCredential'; | ||
const webhookDescription: IWebhookDescription = { | ||
name: 'default', | ||
httpMethod: 'GET', | ||
responseMode: 'onReceived', | ||
path: 'testPath', | ||
}; | ||
const nodeType = mock<INodeType>({ | ||
description: { | ||
credentials: [ | ||
{ | ||
name: testCredentialType, | ||
required: true, | ||
}, | ||
], | ||
properties: [ | ||
{ | ||
name: 'testParameter', | ||
required: true, | ||
}, | ||
], | ||
}, | ||
}); | ||
nodeType.description.webhooks = [webhookDescription]; | ||
const nodeTypes = mock<INodeTypes>(); | ||
const expression = mock<Expression>(); | ||
const workflow = mock<Workflow>({ expression, nodeTypes }); | ||
const node = mock<INode>({ | ||
credentials: { | ||
[testCredentialType]: { | ||
id: 'testCredentialId', | ||
}, | ||
}, | ||
}); | ||
node.parameters = { | ||
testParameter: 'testValue', | ||
}; | ||
const credentialsHelper = mock<ICredentialsHelper>(); | ||
const additionalData = mock<IWorkflowExecuteAdditionalData>({ credentialsHelper }); | ||
const mode: WorkflowExecuteMode = 'manual'; | ||
const activation: WorkflowActivateMode = 'init'; | ||
const webhookData = mock<IWebhookData>({ | ||
webhookDescription: { | ||
name: 'default', | ||
isFullPath: true, | ||
}, | ||
}); | ||
|
||
const hookContext = new HookContext( | ||
workflow, | ||
node, | ||
additionalData, | ||
mode, | ||
activation, | ||
webhookData, | ||
); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
nodeTypes.getByNameAndVersion.mockReturnValue(nodeType); | ||
expression.getParameterValue.mockImplementation((value) => value); | ||
expression.getSimpleParameterValue.mockImplementation((_, value) => value); | ||
}); | ||
|
||
describe('getActivationMode', () => { | ||
it('should return the activation property', () => { | ||
const result = hookContext.getActivationMode(); | ||
expect(result).toBe(activation); | ||
}); | ||
}); | ||
|
||
describe('getCredentials', () => { | ||
it('should get decrypted credentials', async () => { | ||
nodeTypes.getByNameAndVersion.mockReturnValue(nodeType); | ||
credentialsHelper.getDecrypted.mockResolvedValue({ secret: 'token' }); | ||
|
||
const credentials = | ||
await hookContext.getCredentials<ICredentialDataDecryptedObject>(testCredentialType); | ||
|
||
expect(credentials).toEqual({ secret: 'token' }); | ||
}); | ||
}); | ||
|
||
describe('getNodeParameter', () => { | ||
it('should return parameter value when it exists', () => { | ||
const parameter = hookContext.getNodeParameter('testParameter'); | ||
|
||
expect(parameter).toBe('testValue'); | ||
}); | ||
}); | ||
|
||
describe('getNodeWebhookUrl', () => { | ||
it('should return node webhook url', () => { | ||
const url = hookContext.getNodeWebhookUrl('default'); | ||
|
||
expect(url).toContain('testPath'); | ||
}); | ||
}); | ||
|
||
describe('getWebhookName', () => { | ||
it('should return webhook name', () => { | ||
const name = hookContext.getWebhookName(); | ||
|
||
expect(name).toBe('default'); | ||
}); | ||
|
||
it('should throw an error if webhookData is undefined', () => { | ||
const hookContextWithoutWebhookData = new HookContext( | ||
workflow, | ||
node, | ||
additionalData, | ||
mode, | ||
activation, | ||
); | ||
|
||
expect(() => hookContextWithoutWebhookData.getWebhookName()).toThrow(ApplicationError); | ||
}); | ||
}); | ||
|
||
describe('getWebhookDescription', () => { | ||
it('should return webhook description', () => { | ||
const description = hookContext.getWebhookDescription('default'); | ||
|
||
expect(description).toEqual<IWebhookDescription>(webhookDescription); | ||
}); | ||
}); | ||
}); |
103 changes: 103 additions & 0 deletions
103
packages/core/src/node-execution-context/hook-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import type { | ||
ICredentialDataDecryptedObject, | ||
IGetNodeParameterOptions, | ||
INode, | ||
INodeExecutionData, | ||
IHookFunctions, | ||
IRunExecutionData, | ||
IWorkflowExecuteAdditionalData, | ||
NodeParameterValueType, | ||
Workflow, | ||
WorkflowActivateMode, | ||
WorkflowExecuteMode, | ||
IWebhookData, | ||
WebhookType, | ||
} from 'n8n-workflow'; | ||
import { ApplicationError } from 'n8n-workflow'; | ||
|
||
// eslint-disable-next-line import/no-cycle | ||
import { | ||
getAdditionalKeys, | ||
getCredentials, | ||
getNodeParameter, | ||
getNodeWebhookUrl, | ||
getWebhookDescription, | ||
} from '@/NodeExecuteFunctions'; | ||
|
||
import { RequestHelpers } from './helpers/request-helpers'; | ||
import { NodeExecutionContext } from './node-execution-context'; | ||
|
||
export class HookContext extends NodeExecutionContext implements IHookFunctions { | ||
readonly helpers: IHookFunctions['helpers']; | ||
|
||
constructor( | ||
workflow: Workflow, | ||
node: INode, | ||
additionalData: IWorkflowExecuteAdditionalData, | ||
mode: WorkflowExecuteMode, | ||
private readonly activation: WorkflowActivateMode, | ||
private readonly webhookData?: IWebhookData, | ||
) { | ||
super(workflow, node, additionalData, mode); | ||
|
||
this.helpers = new RequestHelpers(this, workflow, node, additionalData); | ||
} | ||
|
||
getActivationMode() { | ||
return this.activation; | ||
} | ||
|
||
async getCredentials<T extends object = ICredentialDataDecryptedObject>(type: string) { | ||
return await getCredentials<T>(this.workflow, this.node, type, this.additionalData, this.mode); | ||
} | ||
|
||
getNodeParameter( | ||
parameterName: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
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: WebhookType): 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: WebhookType) { | ||
return getWebhookDescription(name, this.workflow, this.node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.