import { match } from "ts-pattern";
import { ApiResponse, searchParams } from "../../request-response";
import {
  sitePublishedWebhookPayloadSchema,
  WebhookEventType
} from "./types";
import { handleSitePublished } from "./site-published";
async function webhookHandler(req, { apiKey, events, revalidate }) {
  const secret = searchParams(req).get("secret");
  if (secret !== apiKey) {
    return ApiResponse.json({ message: "Unauthorized" }, { status: 401 });
  }
  let payload;
  try {
    const body = await req.json();
    payload = sitePublishedWebhookPayloadSchema.parse(body);
  } catch (error) {
    console.error(error);
    return ApiResponse.json({ message: "Invalid request body" }, { status: 400 });
  }
  const result = await match(payload.type).with(
    WebhookEventType.SITE_PUBLISHED,
    () => handleSitePublished(payload, { onPublish: events?.onPublish, revalidate })
  ).exhaustive();
  return ApiResponse.json(result.body, { status: result.status });
}
export {
  webhookHandler
};
//# sourceMappingURL=index.js.map