The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
send test webhook and define schema
Kiran K3f636fc66b48
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.tsindex 9c722a3385..6ddd41f2c2 100644--- a/apps/web/app/api/webhooks/route.ts+++ b/apps/web/app/api/webhooks/route.ts@@ -2,6 +2,9 @@ import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";+import { waitUntil } from "@vercel/functions";+import { sendEmail } from "emails";+import WebhookAdded from "emails/webhook-added";import { NextResponse } from "next/server";// GET /api/webhooks - get all webhooks for the given workspace@@ -23,7 +26,7 @@ export const GET = withWorkspace(// POST /api/webhooks/ - create a new webhookexport const POST = withWorkspace(- async ({ req, workspace }) => {+ async ({ req, workspace, session }) => {const { name, url, secret, triggers } = createWebhookSchema.parse(await parseRequestBody(req),);@@ -39,6 +42,23 @@ export const POST = withWorkspace(},});+ waitUntil(+ sendEmail({+ email: session.user.email,+ subject: "New webhook added",+ react: WebhookAdded({+ email: session.user.email,+ workspace: {+ name: workspace.name,+ slug: workspace.slug,+ },+ webhook: {+ name,+ },+ }),+ }),+ );+return NextResponse.json(webhook, { status: 201 });},{diff --git a/apps/web/emails/webhook-added.tsx b/apps/web/emails/webhook-added.tsxnew file mode 100644index 0000000000..691103fbd6--- /dev/null+++ b/apps/web/emails/webhook-added.tsx@@ -0,0 +1,82 @@+import { DUB_WORDMARK } from "@dub/utils";+import {+ Body,+ Container,+ Head,+ Heading,+ Html,+ Img,+ Link,+ Preview,+ Section,+ Tailwind,+ Text,+} from "@react-email/components";+import Footer from "./components/footer";++export default function WebhookAdded({+ email = "[email protected]",+ workspace = {+ name: "Acme, Inc",+ slug: "acme",+ },+ webhook = {+ name: "My Webhook",+ },+}: {+ email: string;+ workspace: {+ name: string;+ slug: string;+ };+ webhook: {+ name: string;+ };+}) {+ return (+ <Html>+ <Head />+ <Preview>New webhook added</Preview>+ <Tailwind>+ <Body className="mx-auto my-auto bg-white font-sans">+ <Container className="mx-auto my-10 max-w-[500px] rounded border border-solid border-gray-200 px-10 py-5">+ <Section className="mt-8">+ <Img+ src={DUB_WORDMARK}+ height="40"+ alt="Dub.co"+ className="mx-auto my-0"+ />+ </Section>+ <Heading className="mx-0 my-7 p-0 text-center text-xl font-semibold text-black">+ New webhook added+ </Heading>+ <Text className="text-sm leading-6 text-black">+ Webhook with the name <strong>{webhook.name}</strong> has been+ added to your Dub.co workspace {workspace.name}.+ </Text>+ <Section className="mb-8 mt-4 text-center">+ <Link+ className="rounded-full bg-black px-6 py-3 text-center text-[12px] font-semibold text-white no-underline"+ href={`https://app.dub.co/${workspace.slug}/settings/webhooks`}+ >+ View Webhook+ </Link>+ </Section>+ <Text className="text-sm leading-6 text-black">+ If you did not create this webhook, you can{" "}+ <Link+ href={`https://app.dub.co/${workspace.slug}/settings/webhooks`}+ className="text-black underline"+ >+ <strong>delete this webhook</strong>+ </Link>{" "}+ from your account.+ </Text>+ <Footer email={email} />+ </Container>+ </Body>+ </Tailwind>+ </Html>+ );+}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex ef945a5315..739b0db48c 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -2,6 +2,7 @@ import z from "@/lib/zod";import { metaTagsSchema } from "@/lib/zod/schemas/metatags";import { DirectorySyncProviders } from "@boxyhq/saml-jackson";import { Link, Project } from "@prisma/client";+import { webhookTrigger } from "./webhook/constants";import { createLinkBodySchema } from "./zod/schemas/links";import { oAuthAppSchema } from "./zod/schemas/oauth";import { tokenSchema } from "./zod/schemas/token";@@ -227,3 +228,5 @@ export type InstalledIntegrationInfoProps = Pick<};} | null;};++export type WebhookTrigger = (typeof webhookTrigger)[number];diff --git a/apps/web/lib/webhook/samples.ts b/apps/web/lib/webhook/samples.tsnew file mode 100644index 0000000000..b892c5202d--- /dev/null+++ b/apps/web/lib/webhook/samples.ts@@ -0,0 +1,22 @@+import { WebhookTrigger } from "../types";++const linkCreatedEventSample = {+ id: "1",+ event: "link.created",+ createdAt: "2021-09-01T00:00:00Z",+};++const linkClickedEventSample = {+ id: "1",+ event: "link.clicked",+ createdAt: "2021-09-01T00:00:00Z",+};++export const getWebhookEventSample = <T extends WebhookTrigger>(event: T) => {+ switch (event) {+ case "link.created":+ return linkCreatedEventSample;+ case "link.clicked":+ return linkClickedEventSample;+ }+};diff --git a/apps/web/lib/webhook/send-test.ts b/apps/web/lib/webhook/send-test.tsnew file mode 100644index 0000000000..5c117b9320--- /dev/null+++ b/apps/web/lib/webhook/send-test.ts@@ -0,0 +1,29 @@+import { Webhook } from "@prisma/client";+import { WebhookTrigger } from "../types";+import { getWebhookEventSample } from "./samples";++const sendTestWebhook = async ({+ webhook,+ event,+}: {+ webhook: Pick<Webhook, "url" | "secret">;+ event: WebhookTrigger;+}) => {+ const payload = getWebhookEventSample(event);++ // Send webhook+ const response = await fetch(webhook.url, {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ "X-Webhook-Secret": webhook.secret,+ },+ body: JSON.stringify(payload),+ });++ if (!response.ok) {+ throw new Error(`Failed to send webhook: ${response.statusText}`);+ }++ return response.json();+};diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.tsindex d6342f89bf..0097e9e09a 100644--- a/apps/web/lib/zod/schemas/webhooks.ts+++ b/apps/web/lib/zod/schemas/webhooks.ts@@ -21,3 +21,12 @@ export const createWebhookSchema = z.object({});export const updateWebhookSchema = createWebhookSchema.partial();++export const webhookEventSchema = z.object({+ id: z.string(),+ event: z.string(),+ createdAt: z.string(),+ data: z.object({+ //+ }),+});