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.

wip display the webhook logs

Kiran Kbbd1117e7dc4
diff --git a/apps/web/app/api/webhooks/[webhookId]/events/route.ts b/apps/web/app/api/webhooks/[webhookId]/events/route.ts
index 95e398dcb8..2186e70dd4 100644
--- a/apps/web/app/api/webhooks/[webhookId]/events/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/events/route.ts
@@ -3,7 +3,7 @@ import { prisma } from "@/lib/prisma";
import { getWebhookEvents } from "@/lib/tinybird/get-webhook-events";
import { NextResponse } from "next/server";
-// GET /api/webhooks/[webhookId]/events - get events occurred for a webhook
+// GET /api/webhooks/[webhookId]/events - get logs for a webhook
export const GET = withWorkspace(
async ({ workspace, params }) => {
const { webhookId } = params;
@@ -15,13 +15,18 @@ export const GET = withWorkspace(
},
});
+ // TODO:
+ // 1. Filter events by url, workspace, event type, http status
+ // 2. Paginate events
+ // 3. Sort events by createdAt
+
const events = await getWebhookEvents({
webhookId,
});
- console.log(events);
+ // console.log(events);
- return NextResponse.json(events);
+ return NextResponse.json(events.data);
},
{
requiredPermissions: ["webhooks.read"],
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx
index 0fd508dc4b..1c2495b677 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/webhooks/[webhookId]/page-client.tsx
@@ -2,6 +2,8 @@
import { clientAccessCheck } from "@/lib/api/tokens/permissions";
import useWorkspace from "@/lib/swr/use-workspace";
+import z from "@/lib/zod";
+import { webhookEventSchemaTB } from "@/lib/zod/schemas/webhooks";
import WebhookHeader from "@/ui/webhooks/webhook-header";
import { MaxWidthWrapper } from "@dub/ui";
import { fetcher } from "@dub/utils";
@@ -24,7 +26,11 @@ export default function WebhookLogsPageClient({
redirect(`/${slug}/settings`);
}
- const { data, isLoading, error } = useSWR<any[]>(
+ const {
+ data: events,
+ isLoading,
+ error,
+ } = useSWR<z.infer<typeof webhookEventSchemaTB>[]>(
`/api/webhooks/${webhookId}/events?workspaceId=${workspaceId}`,
fetcher,
{
@@ -32,11 +38,35 @@ export default function WebhookLogsPageClient({
},
);
+ console.log(events);
+
return (
<>
<WebhookHeader webhookId={webhookId} page="events" />
<MaxWidthWrapper className="max-w-screen-lg space-y-6">
- <></>
+ {/* display events table */}
+ <div>
+ <table>
+ <thead>
+ <tr>
+ <th>Status</th>
+ <th>Event Type</th>
+ <th>HTTP Status</th>
+ <th>Created At</th>
+ </tr>
+ </thead>
+ <tbody>
+ {events?.map((event) => (
+ <tr key={event.event_id}>
+ <td>{event.http_status}</td>
+ <td>{event.event}</td>
+ <td>{event.http_status}</td>
+ <td>{event.timestamp}</td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
</MaxWidthWrapper>
</>
);
diff --git a/apps/web/lib/tinybird/record-webhook-event.ts b/apps/web/lib/tinybird/record-webhook-event.ts
index 10790fc06a..ee81243c5c 100644
--- a/apps/web/lib/tinybird/record-webhook-event.ts
+++ b/apps/web/lib/tinybird/record-webhook-event.ts
@@ -3,5 +3,7 @@ import { tb } from "./client";
export const recordWebhookEvent = tb.buildIngestEndpoint({
datasource: "dub_webhook_events",
- event: webhookEventSchemaTB,
+ event: webhookEventSchemaTB.omit({
+ timestamp: true,
+ }),
});
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 2d6c9fc239..38e8cf95a1 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -260,5 +260,3 @@ export type WebhookTrigger = (typeof WEBHOOK_TRIGGERS)[number];
export type WebhookProps = z.infer<typeof webhookSchema>;
export type NewWebhook = z.infer<typeof createWebhookSchema>;
-
-export type WebhookPayloadSchema = z.infer<typeof webhookPayloadSchema>;
diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.ts
index dff2bf6dd7..b839278f39 100644
--- a/apps/web/lib/webhook/publish.ts
+++ b/apps/web/lib/webhook/publish.ts
@@ -1,10 +1,6 @@
-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, WorkspaceProps } from "../types";
-import { webhookPayloadSchema } from "../zod/schemas/webhooks";
-import { createWebhookSignature } from "./signature";
+import { sendWebhookEventToQStash } from "./qstash";
export const dispatchWebhookEvents = async ({
workspace,
@@ -47,39 +43,3 @@ export const dispatchWebhookEvents = async ({
),
);
};
-
-const sendWebhookEventToQStash = async ({
- webhook,
- event,
- data,
-}: {
- webhook: Pick<Webhook, "id" | "url" | "secret">;
- event: WebhookTrigger;
- data: any;
-}) => {
- const payload = webhookPayloadSchema.parse({
- event,
- data,
- webhookId: webhook.id,
- createdAt: new Date().toISOString(),
- });
-
- const callbackUrl = `${APP_DOMAIN_WITH_NGROK}/api/webhooks/callback`;
- const signature = await createWebhookSignature(webhook.secret, payload);
-
- const response = await qstash.publishJSON({
- url: webhook.url,
- body: payload,
- headers: {
- "Dub-Signature": signature,
- },
- callback: callbackUrl,
- failureCallback: callbackUrl,
- });
-
- if (response.messageId) {
- console.log(
- `Webhook event published to QStash with message ID: ${response.messageId}`,
- );
- }
-};
diff --git a/apps/web/lib/webhook/qstash.ts b/apps/web/lib/webhook/qstash.ts
new file mode 100644
index 0000000000..1349c6b304
--- /dev/null
+++ b/apps/web/lib/webhook/qstash.ts
@@ -0,0 +1,42 @@
+import { qstash } from "@/lib/cron";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+import { Webhook } from "@prisma/client";
+import { WebhookTrigger } from "../types";
+import { webhookPayloadSchema } from "../zod/schemas/webhooks";
+import { createWebhookSignature } from "./signature";
+
+export const sendWebhookEventToQStash = async ({
+ webhook,
+ event,
+ data,
+}: {
+ webhook: Pick<Webhook, "id" | "url" | "secret">;
+ event: WebhookTrigger;
+ data: any;
+}) => {
+ const payload = webhookPayloadSchema.parse({
+ event,
+ data,
+ webhookId: webhook.id,
+ createdAt: new Date().toISOString(),
+ });
+
+ const callbackUrl = `${APP_DOMAIN_WITH_NGROK}/api/webhooks/callback`;
+ const signature = await createWebhookSignature(webhook.secret, payload);
+
+ const response = await qstash.publishJSON({
+ url: webhook.url,
+ body: payload,
+ headers: {
+ "Dub-Signature": signature,
+ },
+ callback: callbackUrl,
+ failureCallback: callbackUrl,
+ });
+
+ if (response.messageId) {
+ console.log(
+ `Webhook event published to QStash with message ID: ${response.messageId}`,
+ );
+ }
+};
diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.ts
index dd40057706..c9fcd4c5fa 100644
--- a/apps/web/lib/zod/schemas/webhooks.ts
+++ b/apps/web/lib/zod/schemas/webhooks.ts
@@ -25,17 +25,6 @@ export const createWebhookSchema = z.object({
export const updateWebhookSchema = createWebhookSchema.partial();
-export const webhookEventSchemaTB = z.object({
- event_id: z.string(),
- webhook_id: z.string(),
- message_id: z.string(), // QStash message ID
- url: z.string(),
- event: z.enum(WEBHOOK_TRIGGERS),
- http_status: z.number(),
- request_body: z.string(),
- response_body: z.string(),
-});
-
// Schema of the payload sent to the webhook endpoint by Dub
export const webhookPayloadSchema = z.object({
event: z.enum(WEBHOOK_TRIGGERS),
@@ -50,6 +39,19 @@ export const webhookCallbackSchema = z.object({
url: z.string(),
createdAt: z.number(),
sourceMessageId: z.string(),
- body: z.string().optional().default(""), // Response from the users webhook URL
+ body: z.string().optional().default(""), // Response from the original webhook URL
sourceBody: z.string(), // Original request payload from Dub
});
+
+// Webhook event schema for the webhook logs
+export const webhookEventSchemaTB = z.object({
+ timestamp: z.string(),
+ event_id: z.string(),
+ webhook_id: z.string(),
+ message_id: z.string(), // QStash message ID
+ url: z.string(),
+ event: z.enum(WEBHOOK_TRIGGERS),
+ http_status: z.number(),
+ request_body: z.string(),
+ response_body: z.string(),
+});
diff --git a/packages/tinybird/datasources/dub_webhook_events.datasource b/packages/tinybird/datasources/dub_webhook_events.datasource
index 88f5fc5731..803bdac691 100644
--- a/packages/tinybird/datasources/dub_webhook_events.datasource
+++ b/packages/tinybird/datasources/dub_webhook_events.datasource
@@ -11,4 +11,4 @@ SCHEMA >
ENGINE "MergeTree"
ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"
-ENGINE_SORTING_KEY "timestamp, webhook_id, event_id"
+ENGINE_SORTING_KEY "timestamp, webhook_id, event_id"
\ No newline at end of file