Relax contact route matching

This commit is contained in:
Calum Muir 2025-12-22 13:58:24 +00:00
parent cbbd6debbd
commit 72a1dc6541

View file

@ -163,19 +163,14 @@ const handleContact = async (request: Request, env: Env) => {
export default { export default {
async fetch(request: Request, env: Env): Promise<Response> { async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url); const url = new URL(request.url);
if ( const contactPath = url.pathname;
url.pathname === "/api/contact" || const isContact =
url.pathname === "/api/contact/" contactPath === "/api/contact" ||
) { contactPath === "/api/contact/" ||
if (request.method !== "POST") { contactPath.endsWith("/api/contact") ||
return jsonResponse( contactPath.endsWith("/api/contact/");
{ ok: false, error: "Method not allowed." }, if (isContact) {
405, if (request.method === "GET" && url.searchParams.has("debug")) {
);
}
return handleContact(request, env);
}
if (url.pathname === "/api/contact-debug") {
return jsonResponse({ return jsonResponse({
ok: true, ok: true,
hasTurnstileSecret: Boolean(env.TURNSTILE_SECRET_KEY), hasTurnstileSecret: Boolean(env.TURNSTILE_SECRET_KEY),
@ -186,6 +181,14 @@ export default {
hasContactToEmail: Boolean(env.CONTACT_TO_EMAIL), hasContactToEmail: Boolean(env.CONTACT_TO_EMAIL),
}); });
} }
if (request.method !== "POST") {
return jsonResponse(
{ ok: false, error: "Method not allowed." },
405,
);
}
return handleContact(request, env);
}
if (env.ASSETS?.fetch) { if (env.ASSETS?.fetch) {
return env.ASSETS.fetch(request); return env.ASSETS.fetch(request);
} }