From ea1b627ab0852fb048ace3fab5c00193a8a64643 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sat, 19 Feb 2022 12:37:41 +0100 Subject: [PATCH] :zap: Move options around and also allow no data for last node --- packages/cli/src/WebhookHelpers.ts | 48 ++++++------- .../nodes-base/nodes/Webhook/Webhook.node.ts | 67 +++++++++++++------ packages/workflow/src/Interfaces.ts | 2 +- 3 files changed, 67 insertions(+), 50 deletions(-) diff --git a/packages/cli/src/WebhookHelpers.ts b/packages/cli/src/WebhookHelpers.ts index a83ed169b9..8e5c27e2e3 100644 --- a/packages/cli/src/WebhookHelpers.ts +++ b/packages/cli/src/WebhookHelpers.ts @@ -204,14 +204,15 @@ export async function executeWebhook( 200, ) as number; - if ( - ![ - 'onReceived', - 'lastNode', - 'responseNode', - 'noBodyResponse' - ].includes(responseMode as string) - ) { + const responseData = workflow.expression.getSimpleParameterValue( + workflowStartNode, + webhookData.webhookDescription.responseData, + executionMode, + additionalKeys, + 'firstEntryJson', + ); + + if (!['onReceived', 'lastNode', 'responseNode'].includes(responseMode as string)) { // If the mode is not known we error. Is probably best like that instead of using // the default that people know as early as possible (probably already testing phase) // that something does not resolve properly. @@ -338,7 +339,12 @@ export async function executeWebhook( // directly if responseMode it set to "onReceived" and a respone should be sent if (responseMode === 'onReceived' && !didSendResponse) { // Return response directly and do not wait for the workflow to finish - if (webhookResultData.webhookResponse !== undefined) { + if (responseData === 'noData') { + // Return without data + responseCallback(null, { + responseCode, + }); + } else if (webhookResultData.webhookResponse !== undefined) { // Data to respond with is given responseCallback(null, { data: webhookResultData.webhookResponse, @@ -356,17 +362,6 @@ export async function executeWebhook( didSendResponse = true; } - // Some systems require that a webhook doesn't respond with any data - // if responseMode is set to noBodyResponse we will see this fire - if (responseMode === 'noBodyResponse' && !didSendResponse) { - // Return response without data - responseCallback(null, { - responseCode, - }); - - didSendResponse = true; - } - // Initialize the data of the webhook node const nodeExecutionStack: IExecuteData[] = []; nodeExecutionStack.push({ @@ -526,16 +521,8 @@ export async function executeWebhook( $executionId: executionId, }; - const responseData = workflow.expression.getSimpleParameterValue( - workflowStartNode, - webhookData.webhookDescription.responseData, - executionMode, - additionalKeys, - 'firstEntryJson', - ); - if (!didSendResponse) { - let data: IDataObject | IDataObject[]; + let data: IDataObject | IDataObject[] | undefined; if (responseData === 'firstEntryJson') { // Return the JSON data of the first entry @@ -639,6 +626,9 @@ export async function executeWebhook( noWebhookResponse: true, }); } + } else if (responseData === 'noData') { + // Return without data + data = undefined; } else { // Return the JSON data of all the entries data = []; diff --git a/packages/nodes-base/nodes/Webhook/Webhook.node.ts b/packages/nodes-base/nodes/Webhook/Webhook.node.ts index fb39cd78c3..352c3d2308 100644 --- a/packages/nodes-base/nodes/Webhook/Webhook.node.ts +++ b/packages/nodes-base/nodes/Webhook/Webhook.node.ts @@ -85,7 +85,7 @@ export class Webhook implements INodeType { isFullPath: true, responseCode: '={{$parameter["responseCode"]}}', responseMode: '={{$parameter["responseMode"]}}', - responseData: '={{$parameter["responseData"]}}', + responseData: '={{$parameter["responseData"] || ($parameter.options.noResponseBody ? "noData" : undefined) }}', responseBinaryPropertyName: '={{$parameter["responseBinaryPropertyName"]}}', responseContentType: '={{$parameter["options"]["responseContentType"]}}', responsePropertyName: '={{$parameter["options"]["responsePropertyName"]}}', @@ -165,11 +165,6 @@ export class Webhook implements INodeType { value: 'responseNode', description: 'Response defined in that node', }, - { - name: 'No Body Response', - value: 'noBodyResponse', - description: 'Returns data without a body', - }, ], default: 'onReceived', description: 'When and how to respond to the webhook.', @@ -232,6 +227,11 @@ export class Webhook implements INodeType { value: 'firstEntryBinary', description: 'Returns the binary data of the first entry of the last node. Always returns a binary file.', }, + { + name: 'No Response Body', + value: 'noData', + description: 'Returns without a body.', + }, ], default: 'firstEntryJson', description: 'What data should be returned. If it should return all items as an array or only the first item as object.', @@ -296,6 +296,42 @@ export class Webhook implements INodeType { default: false, description: 'Set to true to ignore requests from bots like link previewers and web crawlers', }, + { + displayName: 'No Response Body', + name: 'noResponseBody', + type: 'boolean', + default: false, + description: 'Do not send any body in the response', + displayOptions: { + hide: { + 'rawBody': [ + true, + ], + }, + show: { + '/responseMode': [ + 'onReceived', + ], + }, + }, + }, + { + displayName: 'Raw Body', + name: 'rawBody', + type: 'boolean', + displayOptions: { + hide: { + binaryData: [ + true, + ], + 'noResponseBody': [ + true, + ], + }, + }, + default: false, + description: 'Raw body (binary)', + }, { displayName: 'Response Data', name: 'responseData', @@ -306,6 +342,11 @@ export class Webhook implements INodeType { 'onReceived', ], }, + hide: { + 'noResponseBody': [ + true, + ], + }, }, default: '', placeholder: 'success', @@ -379,20 +420,6 @@ export class Webhook implements INodeType { default: 'data', description: 'Name of the property to return the data of instead of the whole JSON.', }, - { - displayName: 'Raw Body', - name: 'rawBody', - type: 'boolean', - displayOptions: { - hide: { - binaryData: [ - true, - ], - }, - }, - default: false, - description: 'Raw body (binary)', - }, ], }, ], diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 517db7eafc..5bd066c17f 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -1186,7 +1186,7 @@ export interface IWebhookResponseData { noWebhookResponse?: boolean; } -export type WebhookResponseData = 'allEntries' | 'firstEntryJson' | 'firstEntryBinary'; +export type WebhookResponseData = 'allEntries' | 'firstEntryJson' | 'firstEntryBinary' | 'noData'; export type WebhookResponseMode = 'onReceived' | 'lastNode'; export interface INodeTypes {