Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

transform the data for webhook

Kiran K048364d03e6b
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 3d50b97920..cf0646f099 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -11,6 +11,7 @@ import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { NewLinkProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
+import { transformLinkEventData } from "@/lib/webhook/transform";
import { updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { deepEqual } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
@@ -124,9 +125,11 @@ export const PATCH = withWorkspace(
});
waitUntil(
- sendWorkspaceWebhook("link.updated", {
+ sendWorkspaceWebhook({
+ trigger: "link.updated",
workspace,
- data: response,
+ // @ts-ignore
+ data: transformLinkEventData(response),
}),
);
@@ -166,11 +169,11 @@ export const DELETE = withWorkspace(
await deleteLink(link.id);
waitUntil(
- sendWorkspaceWebhook("link.deleted", {
+ sendWorkspaceWebhook({
+ trigger: "link.deleted",
workspace,
- data: {
- ...transformLink(link),
- },
+ // @ts-ignore
+ data: transformLinkEventData(transformLink(link)),
}),
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 89e82110cb..94668f20a7 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -6,6 +6,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { ratelimit } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
+import { transformLinkEventData } from "@/lib/webhook/transform";
import {
createLinkBodySchema,
getLinksQuerySchemaExtended,
@@ -99,12 +100,18 @@ export const POST = withWorkspace(
try {
const response = await createLink(link);
- waitUntil(
- sendWorkspaceWebhook("link.created", {
- data: response,
- workspace,
- }),
- );
+ if (response.projectId && response.userId) {
+ waitUntil(
+ (async () => {
+ await sendWorkspaceWebhook({
+ trigger: "link.created",
+ workspace,
+ // @ts-ignore
+ data: transformLinkEventData(response),
+ });
+ })(),
+ );
+ }
return NextResponse.json(response, { headers });
} catch (error) {
diff --git a/apps/web/lib/webhook/publish-edge.ts b/apps/web/lib/webhook/publish-edge.ts
index 88113ce9b4..c30bfbd6d7 100644
--- a/apps/web/lib/webhook/publish-edge.ts
+++ b/apps/web/lib/webhook/publish-edge.ts
@@ -1,16 +1,17 @@
import { prismaEdge } from "../prisma/edge";
import { WebhookTrigger } from "../types";
import { sendWebhooks } from "./qstash";
+import { LeadEventDataProps, SaleEventDataProps } from "./types";
-export const sendLinkWebhookOnEdge = async (
- trigger: WebhookTrigger,
- props: {
- linkId: string;
- data: any;
- },
-) => {
- const { data, linkId } = props;
-
+export const sendLinkWebhookOnEdge = async ({
+ trigger,
+ linkId,
+ data,
+}: {
+ trigger: WebhookTrigger;
+ linkId: string;
+ data: LeadEventDataProps | SaleEventDataProps;
+}) => {
const linkWebhooks = await prismaEdge.linkWebhook.findMany({
where: {
linkId,
diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.ts
index d0be4e63e0..916572624d 100644
--- a/apps/web/lib/webhook/publish.ts
+++ b/apps/web/lib/webhook/publish.ts
@@ -1,17 +1,23 @@
import { prisma } from "@/lib/prisma";
import { WebhookTrigger, WorkspaceProps } from "../types";
import { sendWebhooks } from "./qstash";
+import {
+ ClickEventDataProps,
+ LeadEventDataProps,
+ LinkEventDataProps,
+ SaleEventDataProps,
+} from "./types";
// Send workspace level webhook
-export const sendWorkspaceWebhook = async (
- trigger: WebhookTrigger,
- props: {
- workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
- data: any;
- },
-) => {
- const { workspace, data } = props;
-
+export const sendWorkspaceWebhook = async ({
+ trigger,
+ workspace,
+ data,
+}: {
+ trigger: WebhookTrigger;
+ workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
+ data: LinkEventDataProps;
+}) => {
if (!workspace.webhookEnabled) {
return;
}
@@ -34,15 +40,15 @@ export const sendWorkspaceWebhook = async (
};
// Send link level webhook
-export const sendLinkWebhook = async (
- trigger: WebhookTrigger,
- props: {
- linkId: string;
- data: any;
- },
-) => {
- const { data, linkId } = props;
-
+export const sendLinkWebhook = async ({
+ trigger,
+ linkId,
+ data,
+}: {
+ trigger: WebhookTrigger;
+ linkId: string;
+ data: ClickEventDataProps | LeadEventDataProps | SaleEventDataProps;
+}) => {
const linkWebhooks = await prisma.linkWebhook.findMany({
where: {
linkId,
diff --git a/apps/web/lib/webhook/qstash.ts b/apps/web/lib/webhook/qstash.ts
index 19df07f54a..a82becf443 100644
--- a/apps/web/lib/webhook/qstash.ts
+++ b/apps/web/lib/webhook/qstash.ts
@@ -6,6 +6,12 @@ import z from "../zod";
import { webhookPayloadSchema } from "../zod/schemas/webhooks";
import { createWebhookSignature } from "./signature";
import { prepareWebhookPayload } from "./transform";
+import {
+ ClickEventDataProps,
+ LeadEventDataProps,
+ LinkEventDataProps,
+ SaleEventDataProps,
+} from "./types";
// Send webhooks to multiple webhooks
export const sendWebhooks = async ({
@@ -14,7 +20,11 @@ export const sendWebhooks = async ({
trigger,
}: {
webhooks: Pick<Webhook, "id" | "url" | "secret">[];
- data: any;
+ data:
+ | LinkEventDataProps
+ | ClickEventDataProps
+ | LeadEventDataProps
+ | SaleEventDataProps;
trigger: WebhookTrigger;
}) => {
if (webhooks.length === 0) {
diff --git a/apps/web/lib/webhook/schemas.ts b/apps/web/lib/webhook/schemas.ts
index b75867866a..86ad76beaf 100644
--- a/apps/web/lib/webhook/schemas.ts
+++ b/apps/web/lib/webhook/schemas.ts
@@ -1,11 +1,14 @@
import z from "@/lib/zod";
import { LinkSchema } from "../zod/schemas/links";
-export const linkEventSchema = LinkSchema.extend({
- expiresAt: z.date().nullable(),
- createdAt: z.date(),
- updatedAt: z.date(),
-});
+export const linkEventSchema = LinkSchema
+
+
+// .extend({
+// expiresAt: z.string().nullable(),
+// createdAt: z.string(),
+// updatedAt: z.string(),
+// });
export const clickEventSchema = z.object({
id: z.string(),
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index cbe6feb813..2c6b5e00ba 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -1,5 +1,5 @@
import { nanoid, toCamelCase } from "@dub/utils";
-import type { Webhook } from "@prisma/client";
+import type { Link, Webhook } from "@prisma/client";
import { WebhookTrigger } from "../types";
import { LinkSchema } from "../zod/schemas/links";
import { webhookPayloadSchema } from "../zod/schemas/webhooks";
@@ -23,17 +23,17 @@ export const transformWebhook = (webhook: TransformWebhookProps) => {
};
};
-export const transformLink = (data: any) => {
- // Note utm_* properties are converted to camelCase
-
+// Note utm_* properties are not converted to camelCase
+export const transformLinkEventData = (data: Link) => {
return LinkSchema.parse({
...data,
createdAt: data.createdAt.toISOString(),
updatedAt: data.updatedAt.toISOString(),
+ expiresAt: data.expiresAt?.toISOString() || null,
});
};
-export const transformClick = (data: any) => {
+export const transformClickEventData = (data: any) => {
const click = Object.fromEntries(
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
@@ -48,7 +48,7 @@ export const transformClick = (data: any) => {
});
};
-export const transformLead = (data: any) => {
+export const transformLeadEventData = (data: any) => {
const lead = Object.fromEntries(
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
@@ -70,7 +70,7 @@ export const transformLead = (data: any) => {
});
};
-export const transformSale = (data: any) => {
+export const transformSaleEventData = (data: any) => {
const sale = Object.fromEntries(
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
@@ -91,25 +91,6 @@ export const transformSale = (data: any) => {
// Transform the payload to the format expected by the webhook
export const prepareWebhookPayload = (trigger: WebhookTrigger, data: any) => {
- switch (trigger) {
- case "link.created":
- case "link.updated":
- case "link.deleted":
- data = transformLink(data);
- break;
- case "link.clicked":
- data = transformClick(data);
- break;
- case "lead.created":
- data = transformLead(data);
- break;
- case "sale.created":
- data = transformSale(data);
- break;
- default:
- break;
- }
-
return webhookPayloadSchema.parse({
id: `${WEBHOOK_EVENT_ID_PREFIX}${nanoid(25)}`,
data: data,
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
index 86351f0456..e928b24e1f 100644
--- a/apps/web/lib/webhook/types.ts
+++ b/apps/web/lib/webhook/types.ts
@@ -6,10 +6,10 @@ import {
saleEventSchema,
} from "./schemas";
-export type LinkEventData = z.infer<typeof linkEventSchema>;
+export type LinkEventDataProps = z.infer<typeof linkEventSchema>;
-export type ClickEventData = z.infer<typeof clickEventSchema>;
+export type ClickEventDataProps = z.infer<typeof clickEventSchema>;
-export type LeadEventData = z.infer<typeof leadEventSchema>;
+export type LeadEventDataProps = z.infer<typeof leadEventSchema>;
-export type SaleEventData = z.infer<typeof saleEventSchema>;
+export type SaleEventDataProps = z.infer<typeof saleEventSchema>;