export default { async fetch(request) { const cors = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type" }; if (request.method === "OPTIONS") { return new Response(null, { headers: cors }); } if (request.method !== "POST") { return json({ error: "Method not allowed" }, 405, cors); } try { const body = await request.json(); const GAS_URL = "https://script.google.com/macros/s/AKfycbzRf-spYfFIIoXk8Vyv0TfcaHFpzD0DZ2nRUZJfTmOV8YXWH1_IgkNM45StZXWPyCDu/exec"; const res = await fetch(GAS_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); const text = await res.text(); let data; try { data = JSON.parse(text); } catch (e) { return json({ error: "Invalid JSON from Apps Script", raw: text }, 500, cors); } return json(data, 200, cors); } catch (err) { return json({ error: err.message }, 500, cors); } } }; function json(data, status, headers) { return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json", ...headers } }); }