Allow to set HTTP response code on Webhook-Node

This commit is contained in:
Jan Oberhauser 2019-08-28 17:03:35 +02:00
parent 1ff038b563
commit 1ff456d77c
5 changed files with 38 additions and 11 deletions

View file

@ -304,6 +304,7 @@ export interface IPushDataTestWebhook {
export interface IResponseCallbackData {
data?: IDataObject | IDataObject[];
noWebhookResponse?: boolean;
responseCode?: number;
}

View file

@ -53,9 +53,13 @@ export function basicAuthAuthorizationError(resp: Response, realm: string, messa
}
export function sendSuccessResponse(res: Response, data: any, raw?: boolean) { // tslint:disable-line:no-any
export function sendSuccessResponse(res: Response, data: any, raw?: boolean, responseCode?: number) { // tslint:disable-line:no-any
res.setHeader('Content-Type', 'application/json');
if (responseCode !== undefined) {
res.status(responseCode);
}
if (raw === true) {
res.send(JSON.stringify(data));
return;

View file

@ -1019,7 +1019,7 @@ class App {
return;
}
ResponseHelper.sendSuccessResponse(res, response.data, true);
ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode);
});
@ -1043,7 +1043,7 @@ class App {
return;
}
ResponseHelper.sendSuccessResponse(res, response.data, true);
ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode);
});
@ -1067,7 +1067,7 @@ class App {
return;
}
ResponseHelper.sendSuccessResponse(res, response.data, true);
ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode);
});
@ -1091,7 +1091,7 @@ class App {
return;
}
ResponseHelper.sendSuccessResponse(res, response.data, true);
ResponseHelper.sendSuccessResponse(res, response.data, true, response.responseCode);
});

View file

@ -115,6 +115,7 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
// Get the responseMode
const reponseMode = webhookData.workflow.getSimpleParameterValue(workflowStartNode, webhookData.webhookDescription['reponseMode'], 'onReceived');
const responseCode = webhookData.workflow.getSimpleParameterValue(workflowStartNode, webhookData.webhookDescription['responseCode'], 200);
if (!['onReceived', 'lastNode'].includes(reponseMode as string)) {
// If the mode is not known we error. Is probably best like that instead of using
@ -152,7 +153,8 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
if (webhookResultData.webhookResponse !== undefined) {
// Data to respond with is given
responseCallback(null, {
data: webhookResultData.webhookResponse
data: webhookResultData.webhookResponse,
responseCode,
});
} else {
// Send default response
@ -160,6 +162,7 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
data: {
message: 'Webhook call got received.',
},
responseCode,
});
}
return;
@ -173,12 +176,16 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
// Data to respond with is given
responseCallback(null, {
data: webhookResultData.webhookResponse,
responseCode,
});
} else {
console.log('k1: ' + responseCode);
responseCallback(null, {
data: {
message: 'Workflow got started.',
}
},
responseCode,
});
}
@ -229,7 +236,8 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
responseCallback(null, {
data: {
message: 'Workflow did execute sucessfully but no data got returned.',
}
},
responseCode,
});
didSendResponse = true;
}
@ -242,7 +250,8 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
responseCallback(null, {
data: {
message: 'Workflow did execute sucessfully but the last node did not return any data.',
}
},
responseCode,
});
}
didSendResponse = true;
@ -302,6 +311,7 @@ export function getWorkflowWebhooks(workflow: Workflow, additionalData: IWorkflo
responseCallback(null, {
data,
responseCode,
});
}
didSendResponse = true;

View file

@ -71,6 +71,7 @@ export class Webhook implements INodeType {
{
name: 'default',
httpMethod: '={{$parameter["httpMethod"]}}',
responseCode: '={{$parameter["responseCode"]}}',
reponseMode: '={{$parameter["reponseMode"]}}',
reponseData: '={{$parameter["reponseData"]}}',
responseBinaryPropertyName: '={{$parameter["responseBinaryPropertyName"]}}',
@ -122,10 +123,21 @@ export class Webhook implements INodeType {
name: 'path',
type: 'string',
default: '',
placeholder: '',
placeholder: 'webhook',
required: true,
description: 'The path to listen to',
},
{
displayName: 'Response Code',
name: 'responseCode',
type: 'number',
typeOptions: {
minValue: 100,
maxValue: 599,
},
default: 200,
description: 'The HTTP Response code to return',
},
{
displayName: 'Reponse Mode',
name: 'reponseMode',
@ -134,7 +146,7 @@ export class Webhook implements INodeType {
{
name: 'On Received',
value: 'onReceived',
description: 'Returns directly with Reponse Code 200',
description: 'Returns directly with defined Response Code',
},
{
name: 'Last Node',