fix: Add custom payload template support for form submission webhooks (#25719)
* fix: Add custom payload template support for form submission webhooks * delete * Update applyTemplate function signature Refactor applyTemplate function to accept specific data type.
This commit is contained in:
@@ -173,7 +173,11 @@ function getZapierPayload(data: WithUTCOffsetType<EventPayloadType & { createdAt
|
||||
return JSON.stringify(body);
|
||||
}
|
||||
|
||||
function applyTemplate(template: string, data: WebhookDataType, contentType: ContentType) {
|
||||
function applyTemplate(
|
||||
template: string,
|
||||
data: WebhookDataType | Record<string, unknown>,
|
||||
contentType: ContentType
|
||||
) {
|
||||
const compiled = compile(template)(data).replace(/"/g, '"');
|
||||
|
||||
if (contentType === "application/json") {
|
||||
@@ -268,15 +272,27 @@ export const sendGenericWebhookPayload = async ({
|
||||
data: Record<string, unknown>;
|
||||
rootData?: Record<string, unknown>;
|
||||
}) => {
|
||||
const body = JSON.stringify({
|
||||
const { payloadTemplate: template } = webhook;
|
||||
|
||||
const contentType =
|
||||
!template || jsonParse(template) ? "application/json" : "application/x-www-form-urlencoded";
|
||||
|
||||
const defaultPayload = {
|
||||
// Added rootData props first so that using the known(i.e. triggerEvent, createdAt, payload) properties in rootData doesn't override the known properties
|
||||
...rootData,
|
||||
triggerEvent: triggerEvent,
|
||||
createdAt: createdAt,
|
||||
payload: data,
|
||||
});
|
||||
};
|
||||
|
||||
return _sendPayload(secretKey, webhook, body, "application/json");
|
||||
let body: string;
|
||||
if (template) {
|
||||
body = applyTemplate(template, defaultPayload, contentType);
|
||||
} else {
|
||||
body = JSON.stringify(defaultPayload);
|
||||
}
|
||||
|
||||
return _sendPayload(secretKey, webhook, body, contentType);
|
||||
};
|
||||
|
||||
export const createWebhookSignature = (params: { secret?: string | null; body: string }) =>
|
||||
|
||||
Reference in New Issue
Block a user