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.

add dispatchWebhookEvents

Kiran K233b564240de
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index acc69176f2..f727002068 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -10,8 +10,10 @@ 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 { updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { deepEqual } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/links/[linkId] – get a link
@@ -121,6 +123,14 @@ export const PATCH = withWorkspace(
updatedLink: processedLink,
});
+ waitUntil(
+ dispatchWebhookEvents({
+ event: "link.updated",
+ workspace,
+ data: response,
+ }),
+ );
+
return NextResponse.json(response, {
headers,
});
@@ -156,6 +166,16 @@ export const DELETE = withWorkspace(
await deleteLink(link.id);
+ waitUntil(
+ dispatchWebhookEvents({
+ event: "link.deleted",
+ workspace,
+ data: {
+ id: link.id,
+ },
+ }),
+ );
+
return NextResponse.json({ id: link.id }, { headers });
},
{
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 94d5c6f53e..668d3793e3 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -4,9 +4,8 @@ import { createLink, getLinksForWorkspace, processLink } from "@/lib/api/links";
import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
-import { prisma } from "@/lib/prisma";
import { ratelimit } from "@/lib/upstash";
-import { sendToWebhook } from "@/lib/webhook/publish";
+import { dispatchWebhookEvents } from "@/lib/webhook/publish";
import {
createLinkBodySchema,
getLinksQuerySchemaExtended,
@@ -101,33 +100,11 @@ export const POST = withWorkspace(
const response = await createLink(link);
waitUntil(
- (async () => {
- if (workspace.webhookEnabled) {
- const webhooks = await prisma.webhook.findMany({
- where: {
- projectId: workspace.id,
- triggers: {
- array_contains: ["link.created"],
- },
- },
- select: {
- id: true,
- url: true,
- secret: true,
- },
- });
-
- await Promise.all(
- webhooks.map((webhook) =>
- sendToWebhook({
- event: "link.created",
- data: response,
- webhook,
- }),
- ),
- );
- }
- })(),
+ dispatchWebhookEvents({
+ event: "link.created",
+ data: response,
+ workspace,
+ }),
);
return NextResponse.json(response, { headers });
diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.ts
index a382f98473..dff2bf6dd7 100644
--- a/apps/web/lib/webhook/publish.ts
+++ b/apps/web/lib/webhook/publish.ts
@@ -1,21 +1,62 @@
import { qstash } from "@/lib/cron";
+import { prisma } from "@/lib/prisma";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Webhook } from "@prisma/client";
-import { WebhookTrigger } from "../types";
+import { WebhookTrigger, WorkspaceProps } from "../types";
import { webhookPayloadSchema } from "../zod/schemas/webhooks";
import { createWebhookSignature } from "./signature";
-interface SendToWebhookArgs {
- webhook: Pick<Webhook, "id" | "url" | "secret">;
+export const dispatchWebhookEvents = async ({
+ workspace,
+ event,
+ data,
+}: {
+ workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;
event: WebhookTrigger;
data: any;
-}
+}) => {
+ if (!workspace.webhookEnabled) {
+ return;
+ }
+
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ projectId: workspace.id,
+ triggers: {
+ array_contains: [event],
+ },
+ },
+ select: {
+ id: true,
+ url: true,
+ secret: true,
+ },
+ });
-export const sendToWebhook = async ({
+ if (webhooks.length === 0) {
+ return;
+ }
+
+ await Promise.all(
+ webhooks.map((webhook) =>
+ sendWebhookEventToQStash({
+ webhook,
+ event,
+ data,
+ }),
+ ),
+ );
+};
+
+const sendWebhookEventToQStash = async ({
webhook,
event,
data,
-}: SendToWebhookArgs) => {
+}: {
+ webhook: Pick<Webhook, "id" | "url" | "secret">;
+ event: WebhookTrigger;
+ data: any;
+}) => {
const payload = webhookPayloadSchema.parse({
event,
data,