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.

update and delete webhook

Kiran K70cd69b40970
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index e69de29bb2..da30b155db 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -0,0 +1,73 @@
+import { parseRequestBody } from "@/lib/api/utils";
+import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { updateWebhookSchema } from "@/lib/zod/schemas/webhooks";
+import { NextResponse } from "next/server";
+
+// GET /api/webhooks/[webhookId] - get info about a specific webhook
+export const GET = withWorkspace(
+ async ({ workspace, params }) => {
+ const { webhookId } = params;
+
+ const webhook = await prisma.webhook.findUniqueOrThrow({
+ where: {
+ id: webhookId,
+ projectId: workspace.id,
+ },
+ });
+
+ return NextResponse.json(webhook);
+ },
+ {
+ requiredPermissions: ["webhooks.read"],
+ },
+);
+
+// PATCH /api/webhooks/[webhookId] - update a specific webhook
+export const PATCH = withWorkspace(
+ async ({ workspace, params, req }) => {
+ const { webhookId } = params;
+
+ const { name, url, triggers } = updateWebhookSchema.parse(
+ await parseRequestBody(req),
+ );
+
+ const webhook = await prisma.webhook.update({
+ where: {
+ id: webhookId,
+ projectId: workspace.id,
+ },
+ data: {
+ ...(name && { name }),
+ ...(url && { url }),
+ ...(triggers && { triggers }),
+ },
+ });
+
+ return NextResponse.json(webhook);
+ },
+ {
+ requiredPermissions: ["webhooks.write"],
+ },
+);
+
+// DELETE /api/webhooks/[webhookId] - delete a specific webhook
+export const DELETE = withWorkspace(
+ async ({ workspace, params }) => {
+ const { webhookId } = params;
+
+ await prisma.webhook.delete({
+ where: {
+ id: webhookId,
+ projectId: workspace.id,
+ },
+ });
+
+ return NextResponse.json({
+ id: webhookId,
+ });
+ },
+ {
+ requiredPermissions: ["webhooks.write"],
+ },
+);
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index 8b8f39813a..9c722a3385 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -16,7 +16,7 @@ export const GET = withWorkspace(
return NextResponse.json(webhooks);
},
{
- requiredPermissions: ["oauth_apps.read"],
+ requiredPermissions: ["webhooks.read"],
featureFlag: "integrations",
},
);
@@ -24,7 +24,7 @@ export const GET = withWorkspace(
// POST /api/webhooks/ - create a new webhook
export const POST = withWorkspace(
async ({ req, workspace }) => {
- const { name, url, secret, source, triggers } = createWebhookSchema.parse(
+ const { name, url, secret, triggers } = createWebhookSchema.parse(
await parseRequestBody(req),
);
@@ -33,16 +33,16 @@ export const POST = withWorkspace(
name,
url,
secret,
- source,
triggers,
projectId: workspace.id,
+ source: "user",
},
});
return NextResponse.json(webhook, { status: 201 });
},
{
- requiredPermissions: ["oauth_apps.write"],
+ requiredPermissions: ["webhooks.write"],
featureFlag: "integrations",
},
);
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
index 13101b080d..6de959b2a8 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/layout.tsx
@@ -12,6 +12,7 @@ import {
Tag,
Users6,
} from "@dub/ui/src/icons";
+// import { Webhook } from "lucide-react";
import { ReactNode } from "react";
export default async function WorkspaceSettingsLayout({
@@ -79,6 +80,11 @@ export default async function WorkspaceSettingsLayout({
icon: Key,
segment: "tokens",
},
+ {
+ name: "Webhooks",
+ icon: Key,
+ segment: "webhooks",
+ },
...(flags.integrations
? [{ name: "OAuth Apps", icon: CubeSettings, segment: "oauth-apps" }]
: []),
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page.tsx
new file mode 100644
index 0000000000..96760a40e2
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/page.tsx
@@ -0,0 +1,3 @@
+export default function WebhooksPage() {
+ return <div>Webhooks</div>;
+}
diff --git a/apps/web/lib/api/rbac/permissions.ts b/apps/web/lib/api/rbac/permissions.ts
index 9c7c937243..b690519b0e 100644
--- a/apps/web/lib/api/rbac/permissions.ts
+++ b/apps/web/lib/api/rbac/permissions.ts
@@ -17,6 +17,8 @@ export const PERMISSION_ACTIONS = [
"oauth_apps.write",
"integrations.read",
"integrations.write",
+ "webhooks.read",
+ "webhooks.write",
] as const;
export type PermissionAction = (typeof PERMISSION_ACTIONS)[number];
@@ -106,6 +108,16 @@ export const ROLE_PERMISSIONS: {
description: "disconnect authorized OAuth apps",
roles: ["owner", "member"],
},
+ {
+ action: "webhooks.read",
+ description: "read webhooks",
+ roles: ["owner", "member"],
+ },
+ {
+ action: "webhooks.write",
+ description: "create, update, or delete webhooks",
+ roles: ["owner"],
+ },
];
// Get permissions for a role
diff --git a/apps/web/lib/webhook/constants.ts b/apps/web/lib/webhook/constants.ts
new file mode 100644
index 0000000000..a9f7236c1f
--- /dev/null
+++ b/apps/web/lib/webhook/constants.ts
@@ -0,0 +1,5 @@
+// Webhook sources
+export const webhookSource = ["zapier", "make", "user"] as const;
+
+// Webhook triggers
+export const webhookTrigger = ["link.created", "link.clicked"] as const;
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
index 3f5cad5a94..d6342f89bf 100644
--- a/apps/web/lib/zod/schemas/webhooks.ts
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -1,11 +1,6 @@
+import { webhookTrigger } from "@/lib/webhook/constants";
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(),
@@ -13,7 +8,6 @@ export const webhookSchema = z.object({
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(),
@@ -23,6 +17,7 @@ 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)),
});
+
+export const updateWebhookSchema = createWebhookSchema.partial();