export default { async fetch(request, env, ctx) { const url = new URL(request.url); const cors = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET,POST,OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", "Cache-Control": "no-store" }; const TARGET = env.APP_SCRIPT_URL || "https://script.google.com/macros/s/AKfycbzXWEQRYZQ3AjWpgxuewlXgeMjcCsa0gAXsO_IlU84FnVs4-kVmSYNlMQHzv7gNtV87kA/exec"; async function proxyJson(u, init = {}) { try { const upstream = await fetch(u, init); const status = upstream.status; const ctype = upstream.headers.get("content-type") || ""; const bodyText = await upstream.text(); try { const data = JSON.parse(bodyText); return new Response(JSON.stringify(data), { status, headers: { "content-type": "application/json; charset=utf-8", ...cors } }); } catch { const payload = { ok: false, proxy_note: "Upstream no devolvió JSON válido (¿permisos del Web App? ¿URL correcta?)", upstream_status: status, upstream_content_type: ctype, upstream_body_snippet: bodyText.slice(0, 800) }; return new Response(JSON.stringify(payload), { status: 502, headers: { "content-type": "application/json; charset=utf-8", ...cors } }); } } catch (err) { const payload = { ok: false, proxy_error: String(err), target: u }; return new Response(JSON.stringify(payload), { status: 502, headers: { "content-type": "application/json; charset=utf-8", ...cors } }); } } if (request.method === "OPTIONS") { return new Response("", { headers: cors }); } // ---------- Info / Health ---------- if (url.pathname === "/api/pedido" || url.pathname === "/api/pedido/") { if (request.method === "GET") { return new Response(JSON.stringify({ ok: true, target: TARGET }), { headers: { "content-type": "application/json; charset=utf-8", ...cors } }); } if (request.method === "POST") { const ct = request.headers.get("content-type") || "application/json"; const bodyText = await request.clone().text(); return proxyJson(TARGET, { method: "POST", headers: { "content-type": ct }, body: bodyText }); } return new Response("Method Not Allowed", { status: 405, headers: cors }); } // ---------- KDS LIST ---------- if (url.pathname === "/api/kds" || url.pathname === "/api/kds/") { return proxyJson(`${TARGET}?action=list`, { headers: { "cache-control": "no-store" } }); } // ---------- KDS PEEK / DIAG ---------- if (url.pathname === "/api/kds/peek" || url.pathname === "/api/kds/peek/") { return proxyJson(`${TARGET}?action=peek`, { headers: { "cache-control": "no-store" } }); } if (url.pathname === "/api/kds/diag" || url.pathname === "/api/kds/diag/") { return proxyJson(`${TARGET}?action=diag`, { headers: { "cache-control": "no-store" } }); } // ---------- KDS DONE / REOPEN ---------- if (url.pathname === "/api/kds/done" || url.pathname === "/api/kds/done/") { if (request.method !== "POST") return new Response("Method Not Allowed", { status: 405, headers: cors }); const bodyText = await request.clone().text(); return proxyJson(TARGET, { method: "POST", headers: { "content-type": "application/json" }, body: bodyText }); } // ---------- estáticos ---------- return env.ASSETS.fetch(request); } };