feat(webhooks/respond-with): accept custom headers
This commit is contained in:
@@ -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: '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 JSON body. It must be a valid JSON.',
|
||||
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,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
@@ -26,6 +26,4 @@ export default async (request, response) => {
|
||||
}
|
||||
|
||||
await handlerSync(flowId, request, response);
|
||||
|
||||
response.sendStatus(204);
|
||||
};
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user