import { Webhook } from "standardwebhooks";
export async function POST(request: Request) {
const rawBody = await request.text();
const verifier = new Webhook(process.env.BATCH_RELAY_WEBHOOK_SECRET!);
let event: Record<string, unknown>;
try {
event = verifier.verify(rawBody, {
"webhook-id": request.headers.get("webhook-id") ?? "",
"webhook-timestamp": request.headers.get("webhook-timestamp") ?? "",
"webhook-signature": request.headers.get("webhook-signature") ?? ""
}) as Record<string, unknown>;
} catch {
return new Response("invalid signature", { status: 400 });
}
// Validate event.specversion and event.type before handling the payload.
// Persist the CloudEvent id transactionally before returning success.
return new Response(null, { status: 204 });
}