feat(webhooks/respond-with): accept custom headers

This commit is contained in:
Ali BARIN
2024-03-19 19:21:20 +00:00
parent 9a63b213b0
commit 473d287c6d
3 changed files with 52 additions and 12 deletions

View File

@@ -14,24 +14,55 @@ export default defineAction({
value: '200',
},
{
label: 'JSON body',
key: 'stringifiedJsonBody',
label: 'Headers',
key: 'headers',
type: 'dynamic',
required: false,
description: 'Add or remove headers as needed',
fields: [
{
label: 'Key',
key: 'key',
type: 'string',
required: true,
description: 'The content of the JSON body. It must be a valid JSON.',
description: 'Header key',
variables: true,
},
{
label: 'Value',
key: 'value',
type: 'string',
required: true,
description: 'Header value',
variables: true,
},
],
},
{
label: 'Body',
key: 'body',
type: 'string',
required: true,
description: 'The content of the response body.',
variables: true,
},
],
async run($) {
const parsedStatusCode = parseInt($.step.parameters.statusCode, 10);
const stringifiedJsonBody = $.step.parameters.stringifiedJsonBody;
const parsedJsonBody = JSON.parse(stringifiedJsonBody);
const statusCode = parseInt($.step.parameters.statusCode, 10);
const body = $.step.parameters.body;
const headers = $.step.parameters.headers.reduce((result, entry) => {
return {
...result,
[entry.key]: entry.value,
};
}, {});
$.setActionItem({
raw: {
body: parsedJsonBody,
statusCode: parsedStatusCode,
headers,
body,
statusCode,
},
});
},

View File

@@ -26,6 +26,4 @@ export default async (request, response) => {
}
await handlerSync(flowId, request, response);
response.sendStatus(204);
};

View File

@@ -75,9 +75,20 @@ export default async (flowId, request, response) => {
});
if (actionStep.key === 'respondWith' && !response.headersSent) {
const { headers, statusCode, body } = actionExecutionStep.dataOut;
// we set the custom response headers
if (headers) {
for (const [key, value] of Object.entries(headers)) {
if (key) {
response.set(key, value);
}
}
}
// we send the response only if it's not sent yet. This allows us to early respond from the flow.
response.status(actionExecutionStep.dataOut.statusCode);
response.send(actionExecutionStep.dataOut.body);
response.status(statusCode);
response.send(body);
}
}
}