|
| 1 | +import { processResendDomainWebhookEvent } from "@/lib/managed-email-onboarding"; |
| 2 | +import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; |
| 3 | +import { yupBoolean, yupMixed, yupNumber, yupObject, yupString, yupTuple } from "@stackframe/stack-shared/dist/schema-fields"; |
| 4 | +import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; |
| 5 | +import { StackAssertionError, StatusError } from "@stackframe/stack-shared/dist/utils/errors"; |
| 6 | +import { Result } from "@stackframe/stack-shared/dist/utils/results"; |
| 7 | +import { Webhook } from "svix"; |
| 8 | + |
| 9 | +function decodeBody(bodyBuffer: ArrayBuffer) { |
| 10 | + return new TextDecoder().decode(bodyBuffer); |
| 11 | +} |
| 12 | + |
| 13 | +function ensureResendWebhookSignature(headers: Record<string, string[] | undefined>, bodyBuffer: ArrayBuffer) { |
| 14 | + const webhookSecret = getEnvVariable("STACK_RESEND_WEBHOOK_SECRET"); |
| 15 | + const svixId = headers["svix-id"]?.[0] ?? null; |
| 16 | + const svixTimestamp = headers["svix-timestamp"]?.[0] ?? null; |
| 17 | + const svixSignature = headers["svix-signature"]?.[0] ?? null; |
| 18 | + if (svixId == null || svixTimestamp == null || svixSignature == null) { |
| 19 | + throw new StatusError(400, "Missing Svix signature headers for Resend webhook"); |
| 20 | + } |
| 21 | + |
| 22 | + const verifier = new Webhook(webhookSecret); |
| 23 | + const result = Result.fromThrowing(() => verifier.verify(decodeBody(bodyBuffer), { |
| 24 | + "svix-id": svixId, |
| 25 | + "svix-timestamp": svixTimestamp, |
| 26 | + "svix-signature": svixSignature, |
| 27 | + })); |
| 28 | + if (result.status === "error") { |
| 29 | + throw new StatusError(400, "Invalid Resend webhook signature"); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type ResendDomainWebhookPayload = { |
| 34 | + type?: string, |
| 35 | + data?: { |
| 36 | + id?: string, |
| 37 | + status?: string, |
| 38 | + error?: string, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +export const POST = createSmartRouteHandler({ |
| 43 | + metadata: { |
| 44 | + hidden: true, |
| 45 | + }, |
| 46 | + request: yupObject({ |
| 47 | + headers: yupObject({ |
| 48 | + "svix-id": yupTuple([yupString().defined()]).defined(), |
| 49 | + "svix-timestamp": yupTuple([yupString().defined()]).defined(), |
| 50 | + "svix-signature": yupTuple([yupString().defined()]).defined(), |
| 51 | + }).defined(), |
| 52 | + body: yupMixed().optional(), |
| 53 | + method: yupString().oneOf(["POST"]).defined(), |
| 54 | + }), |
| 55 | + response: yupObject({ |
| 56 | + statusCode: yupNumber().oneOf([200]).defined(), |
| 57 | + bodyType: yupString().oneOf(["json"]).defined(), |
| 58 | + body: yupObject({ |
| 59 | + received: yupBoolean().defined(), |
| 60 | + }).defined(), |
| 61 | + }), |
| 62 | + handler: async (req, fullReq) => { |
| 63 | + ensureResendWebhookSignature(req.headers, fullReq.bodyBuffer); |
| 64 | + |
| 65 | + const payloadResult = Result.fromThrowing(() => JSON.parse(decodeBody(fullReq.bodyBuffer)) as ResendDomainWebhookPayload); |
| 66 | + if (payloadResult.status === "error") { |
| 67 | + throw new StatusError(400, "Invalid JSON payload in Resend webhook"); |
| 68 | + } |
| 69 | + if (payloadResult.data.type !== "domain.updated") { |
| 70 | + return { |
| 71 | + statusCode: 200, |
| 72 | + bodyType: "json", |
| 73 | + body: { received: true }, |
| 74 | + }; |
| 75 | + } |
| 76 | + const payload = payloadResult.data; |
| 77 | + |
| 78 | + const domainId = payload.data?.id; |
| 79 | + const providerStatusRaw = payload.data?.status; |
| 80 | + if (domainId == null || providerStatusRaw == null) { |
| 81 | + throw new StackAssertionError("Resend webhook payload missing required domain fields", { |
| 82 | + payload, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + await processResendDomainWebhookEvent({ |
| 87 | + domainId, |
| 88 | + providerStatusRaw, |
| 89 | + errorMessage: payload.data?.error, |
| 90 | + }); |
| 91 | + |
| 92 | + return { |
| 93 | + statusCode: 200, |
| 94 | + bodyType: "json", |
| 95 | + body: { |
| 96 | + received: true, |
| 97 | + }, |
| 98 | + }; |
| 99 | + }, |
| 100 | +}); |
0 commit comments