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 transformWebhook

Kiran Kc0b9d2cdc0da
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index 686b45a514..ffe39dc5cc 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -2,6 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
+import { transformWebhook } from "@/lib/webhook/utils";
import { updateWebhookSchema } from "@/lib/zod/schemas/webhooks";
import { NextResponse } from "next/server";
@@ -25,16 +26,7 @@ export const GET = withWorkspace(
},
});
- const webhookWithLinks = {
- id: webhook.id,
- name: webhook.name,
- url: webhook.url,
- secret: webhook.secret,
- triggers: webhook.triggers,
- linkIds: webhook.linkWebhooks.map((linkWebhook) => linkWebhook.linkId),
- };
-
- return NextResponse.json(webhookWithLinks);
+ return NextResponse.json(transformWebhook(webhook));
},
{
requiredPermissions: ["webhooks.read"],
@@ -120,16 +112,7 @@ export const PATCH = withWorkspace(
},
});
- const webhookWithLinks = {
- id: webhook.id,
- name: webhook.name,
- url: webhook.url,
- secret: webhook.secret,
- triggers: webhook.triggers,
- linkIds: webhook.linkWebhooks.map((linkWebhook) => linkWebhook.linkId),
- };
-
- return NextResponse.json(webhookWithLinks);
+ return NextResponse.json(transformWebhook(webhook));
},
{
requiredPermissions: ["webhooks.write"],
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index c77728f091..68c0e1c85c 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -2,6 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
+import { transformWebhook } from "@/lib/webhook/utils";
import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";
import { waitUntil } from "@vercel/functions";
import { sendEmail } from "emails";
@@ -28,16 +29,7 @@ export const GET = withWorkspace(
},
});
- const webhookWithLinks = webhooks.map((webhook) => ({
- id: webhook.id,
- name: webhook.name,
- url: webhook.url,
- secret: webhook.secret,
- triggers: webhook.triggers,
- linkIds: webhook.linkWebhooks.map((linkWebhook) => linkWebhook.linkId),
- }));
-
- return NextResponse.json(webhookWithLinks);
+ return NextResponse.json(webhooks.map(transformWebhook));
},
{
requiredPermissions: ["webhooks.read"],
@@ -131,15 +123,6 @@ export const POST = withWorkspace(
});
}
- const webhookWithLinks = {
- id: webhook.id,
- name: webhook.name,
- url: webhook.url,
- secret: webhook.secret,
- triggers: webhook.triggers,
- linkIds: webhook.linkWebhooks.map((linkWebhook) => linkWebhook.linkId),
- };
-
waitUntil(
sendEmail({
email: session.user.email,
@@ -157,7 +140,7 @@ export const POST = withWorkspace(
}),
);
- return NextResponse.json(webhookWithLinks, { status: 201 });
+ return NextResponse.json(transformWebhook(webhook), { status: 201 });
},
{
requiredPermissions: ["webhooks.write"],
diff --git a/apps/web/lib/webhook/utils.ts b/apps/web/lib/webhook/utils.ts
new file mode 100644
index 0000000000..30751afc64
--- /dev/null
+++ b/apps/web/lib/webhook/utils.ts
@@ -0,0 +1,17 @@
+import { Webhook } from "@prisma/client";
+
+interface TransformWebhookProps
+ extends Pick<Webhook, "id" | "name" | "url" | "secret" | "triggers"> {
+ linkWebhooks: { linkId: string }[];
+}
+
+export const transformWebhook = (webhook: TransformWebhookProps) => {
+ return {
+ id: webhook.id,
+ name: webhook.name,
+ url: webhook.url,
+ secret: webhook.secret,
+ triggers: webhook.triggers,
+ linkIds: webhook.linkWebhooks.map((linkWebhook) => linkWebhook.linkId),
+ };
+};
diff --git a/apps/web/ui/webhooks/add-edit-webhook-form.tsx b/apps/web/ui/webhooks/add-edit-webhook-form.tsx
index 774b963e52..347455555d 100644
--- a/apps/web/ui/webhooks/add-edit-webhook-form.tsx
+++ b/apps/web/ui/webhooks/add-edit-webhook-form.tsx
@@ -217,8 +217,7 @@ export default function AddEditWebhookForm({
Link level events
</h2>
<span className="text-xs text-gray-500">
- These events are triggered at the link level. You can choose which
- links you want to listen for events on.
+ These events are triggered at the link level.
</span>
</label>
<div className="mt-3 flex flex-col gap-2">
@@ -256,7 +255,7 @@ export default function AddEditWebhookForm({
</h2>
</label>
<div className="mt-3 flex flex-col gap-4">
- <div className="max-h-[200px] min-h-[100px] overflow-y-auto border-b border-gray-200 pb-2">
+ <div className="max-h-[200px] min-h-[100px] overflow-y-auto pb-2">
{links?.map((link) => (
<div key={link.id} className="group flex h-8 gap-2 py-1">
<Checkbox