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.

fix webhooks

Steven Tey6df5fad4c51f
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 9c2bdfeaa5..763aa25db4 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -11,8 +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 { linkEventSchema, updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { deepEqual, UTMTags } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -136,7 +135,7 @@ export const PATCH = withWorkspace(
sendWorkspaceWebhook({
trigger: "link.updated",
workspace,
- data: transformLinkEventData(response),
+ data: linkEventSchema.parse(response),
}),
);
@@ -179,7 +178,7 @@ export const DELETE = withWorkspace(
sendWorkspaceWebhook({
trigger: "link.deleted",
workspace,
- data: transformLinkEventData(transformLink(link)),
+ data: linkEventSchema.parse(transformLink(link)),
}),
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 54f0495802..cec3c585cd 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -6,10 +6,10 @@ 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,
+ linkEventSchema,
} from "@/lib/zod/schemas/links";
import { LOCALHOST_IP, getSearchParamsWithArray } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
@@ -105,7 +105,7 @@ export const POST = withWorkspace(
sendWorkspaceWebhook({
trigger: "link.created",
workspace,
- data: transformLinkEventData(response),
+ data: linkEventSchema.parse(response),
}),
);
}
diff --git a/apps/web/lib/webhook/schemas.ts b/apps/web/lib/webhook/schemas.ts
index 768a700fd0..34c45f3aa3 100644
--- a/apps/web/lib/webhook/schemas.ts
+++ b/apps/web/lib/webhook/schemas.ts
@@ -15,7 +15,13 @@ const saleSchema = z.object({
invoiceId: z.string().nullable(),
});
-export const leadEventSchema = z.object({
+export const clickWebhookEventSchema = z.object({
+ timestamp: z.coerce.string(),
+ click: clickEventSchema,
+ link: linkEventSchema,
+});
+
+export const leadWebhookEventSchema = z.object({
eventId: z.string(),
eventName: z.string(),
timestamp: z.date(),
@@ -24,7 +30,7 @@ export const leadEventSchema = z.object({
link: linkEventSchema,
});
-export const saleEventSchema = z.object({
+export const saleWebhookEventSchema = z.object({
eventId: z.string(),
eventName: z.string(),
timestamp: z.date(),
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index 7ce8b36975..d043e8aa3f 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -1,13 +1,16 @@
-import { webhookPayloadSchema } from "@/lib/webhook/schemas";
+import {
+ clickWebhookEventSchema,
+ webhookPayloadSchema,
+} from "@/lib/webhook/schemas";
import { nanoid, toCamelCase } from "@dub/utils";
-import type { Link, Webhook } from "@prisma/client";
+import type { Webhook } from "@prisma/client";
import { LinkWithTags, transformLink } from "../api/links/utils/transform-link";
import { WebhookTrigger } from "../types";
import z from "../zod";
import { clickEventSchema, clickEventSchemaTB } from "../zod/schemas/clicks";
import { linkEventSchema } from "../zod/schemas/links";
import { WEBHOOK_EVENT_ID_PREFIX } from "./constants";
-import { leadEventSchema, saleEventSchema } from "./schemas";
+import { leadWebhookEventSchema, saleWebhookEventSchema } from "./schemas";
interface TransformWebhookProps
extends Pick<Webhook, "id" | "name" | "url" | "secret" | "triggers"> {
@@ -26,17 +29,6 @@ export const transformWebhook = (webhook: TransformWebhookProps) => {
};
};
-// Note utm_* properties are not converted to camelCase
-export const transformLinkEventData = (data: Link) => {
- return linkEventSchema.parse({
- ...data,
- expiresAt: data.expiresAt?.toISOString() || null,
- lastClicked: data.lastClicked?.toISOString() || null,
- createdAt: data.createdAt.toISOString(),
- updatedAt: data.updatedAt.toISOString(),
- });
-};
-
export const transformClickEventData = (
data: z.infer<typeof clickEventSchemaTB> & {
link: any;
@@ -46,16 +38,13 @@ export const transformClickEventData = (
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return clickEventSchema
- .omit({ id: true })
- .merge(
- z.object({
- timestamp: z.string(),
- clickId: z.string(),
- link: linkEventSchema,
- }),
- )
- .parse(click);
+ return clickWebhookEventSchema.parse({
+ ...click,
+ click: clickEventSchema.parse({
+ ...click,
+ id: data.click_id,
+ }),
+ });
};
export const transformLeadEventData = (data: any) => {
@@ -63,7 +52,7 @@ export const transformLeadEventData = (data: any) => {
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return leadEventSchema.parse({
+ return leadWebhookEventSchema.parse({
...lead,
customer: {
id: lead.customerId,
@@ -79,9 +68,8 @@ export const transformLeadEventData = (data: any) => {
qr: lead.qr === 1,
bot: lead.bot === 1,
},
- // transformLinkEventData -> normalize date to string
// transformLink -> add shortLink, qrCode, workspaceId, etc.
- link: transformLinkEventData(transformLink(lead.link as LinkWithTags)),
+ link: linkEventSchema.parse(transformLink(lead.link as LinkWithTags)),
});
};
@@ -90,7 +78,7 @@ export const transformSaleEventData = (data: any) => {
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return saleEventSchema.parse({
+ return saleWebhookEventSchema.parse({
...sale,
customer: {
id: sale.customerId,
@@ -110,9 +98,8 @@ export const transformSaleEventData = (data: any) => {
qr: sale.qr === 1,
bot: sale.bot === 1,
},
- // transformLinkEventData -> normalize date to string
// transformLink -> add shortLink, qrCode, workspaceId, etc.
- link: transformLinkEventData(transformLink(sale.link as LinkWithTags)),
+ link: linkEventSchema.parse(transformLink(sale.link as LinkWithTags)),
});
};
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
index 5e4ebef3f5..b804983969 100644
--- a/apps/web/lib/webhook/types.ts
+++ b/apps/web/lib/webhook/types.ts
@@ -1,15 +1,15 @@
import z from "../zod";
import { clickEventSchema } from "../zod/schemas/clicks";
import { linkEventSchema } from "../zod/schemas/links";
-import { leadEventSchema, saleEventSchema } from "./schemas";
+import { leadWebhookEventSchema, saleWebhookEventSchema } from "./schemas";
export type LinkEventDataProps = z.infer<typeof linkEventSchema>;
export type ClickEventDataProps = z.infer<typeof clickEventSchema>;
-export type LeadEventDataProps = z.infer<typeof leadEventSchema>;
+export type LeadEventDataProps = z.infer<typeof leadWebhookEventSchema>;
-export type SaleEventDataProps = z.infer<typeof saleEventSchema>;
+export type SaleEventDataProps = z.infer<typeof saleWebhookEventSchema>;
export type EventDataProps =
| LinkEventDataProps