From 92848ce20d2d430c27efcd173008681792c4b8e8 Mon Sep 17 00:00:00 2001 From: Calum Muir Date: Mon, 22 Dec 2025 12:58:00 +0000 Subject: [PATCH] Handle contact form POST with onRequest --- functions/api/contact.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/functions/api/contact.ts b/functions/api/contact.ts index 90dc897..243cfc6 100644 --- a/functions/api/contact.ts +++ b/functions/api/contact.ts @@ -31,7 +31,7 @@ const parseConnectionString = (connectionString: string) => { }; }; -export const onRequestPost: PagesFunction = async ({ request, env }) => { +const handlePost: PagesFunction = async ({ request, env }) => { const formData = await request.formData(); const name = readFormField(formData.get("name")); const email = readFormField(formData.get("email")); @@ -147,3 +147,13 @@ export const onRequestPost: PagesFunction = async ({ request, env }) => { return jsonResponse({ ok: true }); }; + +export const onRequest: PagesFunction = async (context) => { + if (context.request.method !== "POST") { + return jsonResponse( + { ok: false, error: "Method not allowed." }, + 405, + ); + } + return handlePost(context); +};