The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
rename the methods
Kiran Kf83615943237
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.tsindex f727002068..00362a01d9 100644--- a/apps/web/app/api/links/[linkId]/route.ts+++ b/apps/web/app/api/links/[linkId]/route.ts@@ -10,7 +10,7 @@ import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";import { NewLinkProps } from "@/lib/types";-import { dispatchWebhookEvents } from "@/lib/webhook/publish";+import { dispatchWebhook } from "@/lib/webhook/publish";import { updateLinkBodySchema } from "@/lib/zod/schemas/links";import { deepEqual } from "@dub/utils";import { waitUntil } from "@vercel/functions";@@ -124,8 +124,7 @@ export const PATCH = withWorkspace(});waitUntil(- dispatchWebhookEvents({- event: "link.updated",+ dispatchWebhook("link.updated", {workspace,data: response,}),@@ -167,8 +166,7 @@ export const DELETE = withWorkspace(await deleteLink(link.id);waitUntil(- dispatchWebhookEvents({- event: "link.deleted",+ dispatchWebhook("link.deleted", {workspace,data: {id: link.id,diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.tsindex 668d3793e3..bfca4818c8 100644--- a/apps/web/app/api/links/route.ts+++ b/apps/web/app/api/links/route.ts@@ -5,7 +5,7 @@ import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { ratelimit } from "@/lib/upstash";-import { dispatchWebhookEvents } from "@/lib/webhook/publish";+import { dispatchWebhook } from "@/lib/webhook/publish";import {createLinkBodySchema,getLinksQuerySchemaExtended,@@ -100,8 +100,7 @@ export const POST = withWorkspace(const response = await createLink(link);waitUntil(- dispatchWebhookEvents({- event: "link.created",+ dispatchWebhook("link.created", {data: response,workspace,}),diff --git a/apps/web/app/api/stripe/connect/webhook/customer-created.ts b/apps/web/app/api/stripe/connect/webhook/customer-created.tsindex 84287b98d0..e525ccf04c 100644--- a/apps/web/app/api/stripe/connect/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/connect/webhook/customer-created.ts@@ -1,7 +1,9 @@import { prisma } from "@/lib/prisma";import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";+import { dispatchLinkWebhook } from "@/lib/webhook/publish-edge";import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import { nanoid } from "@dub/utils";+import { waitUntil } from "@vercel/functions";import type Stripe from "stripe";// Handle event "customer.created"diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex 7f0d989e89..444d6479d8 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -5,7 +5,7 @@ import { generateRandomName } from "@/lib/names";import { prismaEdge } from "@/lib/prisma/edge";import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";import { ratelimit } from "@/lib/upstash";-import { dispatchLinkWebhook } from "@/lib/webhook/publish-edge";+import { dispatchWebhook } from "@/lib/webhook/publish-edge";import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import {trackLeadRequestSchema,@@ -140,10 +140,9 @@ export const POST = withWorkspaceEdge(});waitUntil(- dispatchLinkWebhook({+ dispatchWebhook("lead.created", {workspace,linkId: clickData.link_id,- event: "lead.created",data: {...response,...clickData,diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.tsindex 8305f5dbb3..43b8d2498e 100644--- a/apps/web/app/api/track/sale/route.ts+++ b/apps/web/app/api/track/sale/route.ts@@ -3,7 +3,7 @@ import { parseRequestBody } from "@/lib/api/utils";import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";import { prismaEdge } from "@/lib/prisma/edge";import { getLeadEvent, recordSale } from "@/lib/tinybird";-import { dispatchLinkWebhook } from "@/lib/webhook/publish-edge";+import { dispatchWebhook } from "@/lib/webhook/publish-edge";import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import {trackSaleRequestSchema,@@ -111,10 +111,9 @@ export const POST = withWorkspaceEdge(});waitUntil(- dispatchLinkWebhook({+ dispatchWebhook("sale.created", {workspace,linkId: clickData.link_id,- event: "sale.created",data: {...response,...clickData,diff --git a/apps/web/lib/webhook/publish-edge.ts b/apps/web/lib/webhook/publish-edge.tsindex 3475e0d62c..cf22fa1a96 100644--- a/apps/web/lib/webhook/publish-edge.ts+++ b/apps/web/lib/webhook/publish-edge.ts@@ -2,17 +2,18 @@ import { prismaEdge } from "@/lib/prisma/edge";import { WebhookTrigger, WorkspaceProps } from "../types";import { sendWebhookEventToQStash } from "./qstash";-export const dispatchLinkWebhook = async ({- workspace,- linkId,- event,- data,-}: {+interface DispatchWebhookProps {workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;- linkId: string;- event: WebhookTrigger;data: any;-}) => {+ linkId?: string;+}++export const dispatchWebhook = async (+ trigger: WebhookTrigger,+ props: DispatchWebhookProps,+) => {+ const { workspace, linkId, data } = props;+if (!workspace.webhookEnabled) {return;}@@ -21,13 +22,15 @@ export const dispatchLinkWebhook = async ({where: {projectId: workspace.id,triggers: {- array_contains: [event],+ array_contains: [trigger],},- linkWebhooks: {- some: {- linkId,+ ...(linkId && {+ linkWebhooks: {+ some: {+ linkId,+ },},- },+ }),},select: {id: true,@@ -44,8 +47,8 @@ export const dispatchLinkWebhook = async ({webhooks.map((webhook) =>sendWebhookEventToQStash({webhook,- event,data,+ trigger,}),),);diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.tsindex b839278f39..3af0240cf0 100644--- a/apps/web/lib/webhook/publish.ts+++ b/apps/web/lib/webhook/publish.ts@@ -2,15 +2,18 @@ import { prisma } from "@/lib/prisma";import { WebhookTrigger, WorkspaceProps } from "../types";import { sendWebhookEventToQStash } from "./qstash";-export const dispatchWebhookEvents = async ({- workspace,- event,- data,-}: {+interface DispatchWebhookProps {workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;- event: WebhookTrigger;data: any;-}) => {+ linkId?: string;+}++export const dispatchWebhook = async (+ trigger: WebhookTrigger,+ props: DispatchWebhookProps,+) => {+ const { workspace, linkId, data } = props;+if (!workspace.webhookEnabled) {return;}@@ -19,8 +22,15 @@ export const dispatchWebhookEvents = async ({where: {projectId: workspace.id,triggers: {- array_contains: [event],+ array_contains: [trigger],},+ ...(linkId && {+ linkWebhooks: {+ some: {+ linkId,+ },+ },+ }),},select: {id: true,@@ -37,8 +47,8 @@ export const dispatchWebhookEvents = async ({webhooks.map((webhook) =>sendWebhookEventToQStash({webhook,- event,data,+ trigger,}),),);diff --git a/apps/web/lib/webhook/qstash.ts b/apps/web/lib/webhook/qstash.tsindex 1349c6b304..322111795e 100644--- a/apps/web/lib/webhook/qstash.ts+++ b/apps/web/lib/webhook/qstash.ts@@ -7,16 +7,16 @@ import { createWebhookSignature } from "./signature";export const sendWebhookEventToQStash = async ({webhook,- event,+ trigger,data,}: {webhook: Pick<Webhook, "id" | "url" | "secret">;- event: WebhookTrigger;+ trigger: WebhookTrigger;data: any;}) => {const payload = webhookPayloadSchema.parse({- event,data,+ event: trigger,webhookId: webhook.id,createdAt: new Date().toISOString(),});