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.

create and list webhooks

Kiran K768cad1dfd05
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
new file mode 100644
index 0000000000..8b8f39813a
--- /dev/null
+++ b/apps/web/app/api/webhooks/route.ts
@@ -0,0 +1,48 @@
+import { parseRequestBody } from "@/lib/api/utils";
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";
+import { NextResponse } from "next/server";
+
+// GET /api/webhooks - get all webhooks for the given workspace
+export const GET = withWorkspace(
+ async ({ workspace }) => {
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ projectId: workspace.id,
+ },
+ });
+
+ return NextResponse.json(webhooks);
+ },
+ {
+ requiredPermissions: ["oauth_apps.read"],
+ featureFlag: "integrations",
+ },
+);
+
+// POST /api/webhooks/ - create a new webhook
+export const POST = withWorkspace(
+ async ({ req, workspace }) => {
+ const { name, url, secret, source, triggers } = createWebhookSchema.parse(
+ await parseRequestBody(req),
+ );
+
+ const webhook = await prisma.webhook.create({
+ data: {
+ name,
+ url,
+ secret,
+ source,
+ triggers,
+ projectId: workspace.id,
+ },
+ });
+
+ return NextResponse.json(webhook, { status: 201 });
+ },
+ {
+ requiredPermissions: ["oauth_apps.write"],
+ featureFlag: "integrations",
+ },
+);
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
new file mode 100644
index 0000000000..3f5cad5a94
--- /dev/null
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -0,0 +1,28 @@
+import { z } from "zod";
+
+// Webhook sources
+const webhookSource = ["zapier", "make", "user"] as const;
+
+// Webhook triggers
+const webhookTrigger = ["link.created", "link.clicked"] as const;
+
+export const webhookSchema = z.object({
+ id: z.string(),
+ linkId: z.string().optional(),
+ name: z.string(),
+ url: z.string(),
+ secret: z.string(),
+ active: z.boolean().default(true),
+ source: z.enum(webhookSource),
+ triggers: z.array(z.enum(webhookTrigger)),
+ createdAt: z.string(),
+ updatedAt: z.string(),
+});
+
+export const createWebhookSchema = z.object({
+ name: z.string().min(3).max(50),
+ url: z.string().url().max(190),
+ secret: z.string().length(40),
+ source: z.enum(webhookSource),
+ triggers: z.array(z.enum(webhookTrigger)),
+});
diff --git a/apps/web/prisma/schema/webhook.prisma b/apps/web/prisma/schema/webhook.prisma
index 15295cfdd5..a9f1a751ae 100644
--- a/apps/web/prisma/schema/webhook.prisma
+++ b/apps/web/prisma/schema/webhook.prisma
@@ -4,23 +4,18 @@ enum WebhookSource {
user
}
-enum WebhookEventTrigger {
- LINK_CLICKED
- LINK_CREATED
-}
-
model Webhook {
- id String @id @default(cuid())
- projectId String
- linkId String?
- name String
- url String
- secret String?
- active Boolean @default(true)
- source WebhookSource
- eventTriggers Json
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
+ id String @id @default(cuid())
+ projectId String
+ linkId String?
+ name String
+ url String
+ secret String
+ active Boolean @default(true)
+ source WebhookSource
+ triggers Json
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
Project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
Link Link? @relation(fields: [linkId], references: [id], onDelete: Cascade)