The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
prepare webhook payload
Kiran K9b9fcd3bcb1c
diff --git a/apps/web/lib/webhook/prepare-payload.ts b/apps/web/lib/webhook/prepare-payload.tsindex 039efbbdc3..d2b2475f32 100644--- a/apps/web/lib/webhook/prepare-payload.ts+++ b/apps/web/lib/webhook/prepare-payload.ts@@ -5,9 +5,19 @@ import { WEBHOOK_EVENT_ID_PREFIX } from "./constants";// Transform the payload to the format expected by the webhookexport const prepareWebhookPayload = (trigger: WebhookTrigger, data: any) => {- // Unique identifier for the event within Dubconst eventId = `${WEBHOOK_EVENT_ID_PREFIX}${nanoid(16)}`;+ switch (trigger) {+ case "lead.created":+ data = transformLead(data);+ break;+ case "sale.created":+ data = transformSale(data);+ break;+ default:+ break;+ }+const payload = webhookPayloadSchema.parse({id: eventId,data: data,@@ -19,3 +29,38 @@ export const prepareWebhookPayload = (trigger: WebhookTrigger, data: any) => {return payload;};++export const transformLead = (lead: any) => {+ const camelCaseLead = Object.fromEntries(+ Object.entries(lead).map(([key, value]) => [toCamelCase(key), value]),+ );++ // TODO:+ // Define zod schema to parse the data++ return camelCaseLead;+};++export const transformSale = (sale: any) => {+ const camelCaseSale = Object.fromEntries(+ Object.entries(sale).map(([key, value]) => [toCamelCase(key), value]),+ );++ // TODO:+ // Define zod schema to parse the data++ return camelCaseSale;+};++// From snake_case to camelCase+export const toCamelCase = (str: string) => {+ // If already camelCase, return as is+ if (/^[a-z][a-zA-Z0-9]*$/.test(str)) {+ return str;+ }++ // Convert snake_case to camelCase+ return str+ .toLowerCase()+ .replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());+};diff --git a/apps/web/lib/webhook/utils.ts b/apps/web/lib/webhook/utils.tsindex cf724bc8de..6833e3f8d4 100644--- a/apps/web/lib/webhook/utils.ts+++ b/apps/web/lib/webhook/utils.ts@@ -58,4 +58,4 @@ export const updateLinksInRedis = async ({// });// await pipeline.exec();-};+};\ No newline at end of filediff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.tsindex be037410ff..2e1f91046c 100644--- a/apps/web/lib/zod/schemas/webhooks.ts+++ b/apps/web/lib/zod/schemas/webhooks.ts@@ -3,7 +3,6 @@ import {WEBHOOK_TRIGGERS,} from "@/lib/webhook/constants";import { z } from "zod";-import { LinkSchema } from "./links";export const webhookSchema = z.object({id: z.string(),@@ -24,20 +23,6 @@ export const createWebhookSchema = z.object({export const updateWebhookSchema = createWebhookSchema.partial();-// Schema of the payload sent to the webhook endpoint by Dub-export const webhookPayloadSchema = z.object({- id: z.string().describe("Unique identifier for the event."),- event: z- .enum(WEBHOOK_TRIGGERS)- .describe("The type of event that triggered the webhook."),- createdAt: z- .string()- .describe("The date and time when the event was created."),- data: z- .union([LinkSchema, z.any()])- .describe("The data associated with the event."),-});-// Schema of response sent to the webhook callback URL by QStashexport const webhookCallbackSchema = z.object({status: z.number(),@@ -60,3 +45,15 @@ export const webhookEventSchemaTB = z.object({response_body: z.string(),timestamp: z.string(),});++// Schema of the payload sent to the webhook endpoint by Dub+export const webhookPayloadSchema = z.object({+ id: z.string().describe("Unique identifier for the event."),+ event: z+ .enum(WEBHOOK_TRIGGERS)+ .describe("The type of event that triggered the webhook."),+ createdAt: z+ .string()+ .describe("The date and time when the event was created."),+ data: z.any().describe("The data associated with the event."),+});