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 webhook data

Kiran K857c6ca8d992
diff --git a/apps/web/lib/webhook/publish-edge.ts b/apps/web/lib/webhook/publish-edge.ts
index 89be771d78..23e8e467b8 100644
--- a/apps/web/lib/webhook/publish-edge.ts
+++ b/apps/web/lib/webhook/publish-edge.ts
@@ -31,10 +31,6 @@ export const sendLinkWebhookOnEdge = async (
},
});
- if (linkWebhooks.length === 0) {
- return;
- }
-
return sendWebhooks(trigger, {
webhooks: linkWebhooks.map(({ webhook }) => webhook),
data,
diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.ts
index 0007d1ac3e..92c9bd8cf2 100644
--- a/apps/web/lib/webhook/publish.ts
+++ b/apps/web/lib/webhook/publish.ts
@@ -63,10 +63,6 @@ export const sendLinkWebhook = async (
},
});
- if (linkWebhooks.length === 0) {
- return;
- }
-
return sendWebhooks(trigger, {
webhooks: linkWebhooks.map(({ webhook }) => webhook),
data,
diff --git a/apps/web/lib/webhook/schemas.ts b/apps/web/lib/webhook/schemas.ts
index f383312da9..a1a420713c 100644
--- a/apps/web/lib/webhook/schemas.ts
+++ b/apps/web/lib/webhook/schemas.ts
@@ -1,6 +1,13 @@
import z from "@/lib/zod";
+import { LinkSchema } from "../zod/schemas/links";
-export const clickSchema = z.object({
+export const linkEventSchema = LinkSchema.extend({
+ expiresAt: z.date().nullable(),
+ createdAt: z.date(),
+ updatedAt: z.date(),
+});
+
+export const clickEventSchema = z.object({
id: z.string(),
aliasLinkId: z.string(),
url: z.string(),
@@ -31,26 +38,26 @@ export const clickSchema = z.object({
link: z.object({ id: z.string() }),
});
-export const leadSchema = z.object({
+export const leadEventSchema = z.object({
eventName: z.string(),
customerId: z.string(),
- customerName: z.string(),
+ customerName: z.string().nullable(),
customerEmail: z.string().nullable(),
customerAvatar: z.string().nullable(),
- click: clickSchema.partial(),
+ click: clickEventSchema.partial(),
link: z.object({ id: z.string() }),
});
-export const saleSchema = z.object({
+export const saleEventSchema = z.object({
eventName: z.string(),
customerId: z.string(),
- customerName: z.string(),
+ customerName: z.string().nullable(),
customerEmail: z.string().nullable(),
customerAvatar: z.string().nullable(),
amount: z.number(),
paymentProcessor: z.string(),
invoiceId: z.string().nullable(),
currency: z.string(),
- click: clickSchema.partial(),
+ click: clickEventSchema.partial(),
link: z.object({ id: z.string() }),
});
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index ad28b4193c..6a2cb851d6 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -4,7 +4,7 @@ import { WebhookTrigger } from "../types";
import { LinkSchema } from "../zod/schemas/links";
import { webhookPayloadSchema } from "../zod/schemas/webhooks";
import { WEBHOOK_EVENT_ID_PREFIX } from "./constants";
-import { clickSchema, leadSchema, saleSchema } from "./schemas";
+import { clickEventSchema, leadEventSchema, saleEventSchema } from "./schemas";
interface TransformWebhookProps
extends Pick<Webhook, "id" | "name" | "url" | "secret" | "triggers"> {
@@ -38,7 +38,7 @@ export const transformClick = (data: any) => {
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return clickSchema.parse({
+ return clickEventSchema.parse({
...click,
id: click.clickId,
link: {
@@ -53,7 +53,7 @@ export const transformLead = (data: any) => {
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return leadSchema.parse({
+ return leadEventSchema.parse({
...lead,
click: {
...lead,
@@ -72,7 +72,7 @@ export const transformSale = (data: any) => {
Object.entries(data).map(([key, value]) => [toCamelCase(key), value]),
);
- return saleSchema.parse({
+ return saleEventSchema.parse({
...sale,
click: {
...sale,
diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.ts
new file mode 100644
index 0000000000..86351f0456
--- /dev/null
+++ b/apps/web/lib/webhook/types.ts
@@ -0,0 +1,15 @@
+import z from "../zod";
+import {
+ clickEventSchema,
+ leadEventSchema,
+ linkEventSchema,
+ saleEventSchema,
+} from "./schemas";
+
+export type LinkEventData = z.infer<typeof linkEventSchema>;
+
+export type ClickEventData = z.infer<typeof clickEventSchema>;
+
+export type LeadEventData = z.infer<typeof leadEventSchema>;
+
+export type SaleEventData = z.infer<typeof saleEventSchema>;