Files
formbricks-formbricks/apps/web/components/pipelines/webhook/handler.ts
Matti Nannt 5c378bc8ce Feature/monorepo #95 (#105)
Move repository into a monorepo with turborepo and pnpm.
This is a big change in the way the code is organized, used and deployed.
2022-10-13 09:46:43 +02:00

23 lines
833 B
TypeScript

import crypto from "crypto";
import { ApiEvent } from "../../../lib/types";
export async function handleWebhook(pipeline, event: ApiEvent) {
if (pipeline.data.hasOwnProperty("endpointUrl") && pipeline.data.hasOwnProperty("secret")) {
if (event.type === "pageSubmission" && pipeline.events.includes("PAGE_SUBMISSION")) {
const webhookData = pipeline.data;
const body = { time: Math.floor(Date.now() / 1000), event };
fetch(webhookData.endpointUrl.toString(), {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Hub-Signature-256": `sha256=${crypto
.createHmac("sha256", webhookData.secret.toString())
.update(JSON.stringify(body))
.digest("base64")}`,
},
body: JSON.stringify(body),
});
}
}
}