Merge pull request #1750 from automatisch/make-respond-with-flexible

feat(webhooks/respond-with): accept custom headers
This commit is contained in:
Ali BARIN
2024-03-21 12:03:51 +01:00
committed by GitHub
3 changed files with 52 additions and 12 deletions

View File

@@ -14,24 +14,55 @@ export default defineAction({
value: '200', value: '200',
}, },
{ {
label: 'JSON body', label: 'Headers',
key: 'stringifiedJsonBody', key: 'headers',
type: 'dynamic',
required: false,
description: 'Add or remove headers as needed',
fields: [
{
label: 'Key',
key: 'key',
type: 'string', type: 'string',
required: true, 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, variables: true,
}, },
], ],
async run($) { async run($) {
const parsedStatusCode = parseInt($.step.parameters.statusCode, 10); const statusCode = parseInt($.step.parameters.statusCode, 10);
const stringifiedJsonBody = $.step.parameters.stringifiedJsonBody; const body = $.step.parameters.body;
const parsedJsonBody = JSON.parse(stringifiedJsonBody); const headers = $.step.parameters.headers.reduce((result, entry) => {
return {
...result,
[entry.key]: entry.value,
};
}, {});
$.setActionItem({ $.setActionItem({
raw: { raw: {
body: parsedJsonBody, headers,
statusCode: parsedStatusCode, body,
statusCode,
}, },
}); });
}, },

View File

@@ -26,6 +26,4 @@ export default async (request, response) => {
} }
await handlerSync(flowId, 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) { 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. // 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.status(statusCode);
response.send(actionExecutionStep.dataOut.body); response.send(body);
} }
} }
} }