``` import { serve } from "https://deno.land/std@0.168.0/http/server.ts"; const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type" }; serve(async (req)=>{ const started = Date.now(); console.log(`[${new Date().toISOString()}] ➡️ Incoming ${req.method} ${req.url}`); /* ----- Handle CORS pre-flight ----- */ if (req.method === "OPTIONS") { console.log("↩️ CORS pre-flight handled"); return new Response(null, { headers: corsHeaders }); } try { /* ----- Parse request body ----- */ let payload; try { payload = await req.json(); // Trim large bodies in logs to avoid noise. console.log("✅ JSON payload parsed", JSON.stringify(payload).slice(0, 500)); } catch (jsonErr) { console.error("❌ Failed to parse JSON payload", jsonErr); throw new Error("Invalid JSON body"); } /* ----- Set up timeout/abort handling ----- */ const controller = new AbortController(); const timeoutId = setTimeout(()=>controller.abort(), 30_000); // 30 s controller.signal.addEventListener("abort", ()=>{ console.error("⏱️ Fetch aborted after 30 s (timeout reached)"); }); /* ----- Call external API ----- */ console.log("🌐 Forwarding request to analysis API …"); const response = await fetch("API-URL", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), signal: controller.signal }).catch((err)=>{ // network-level failure (before we have a Response object) console.error("❌ Network error while calling analysis API", err); throw err; }); clearTimeout(timeoutId); console.log(`↩️ Analysis API responded: ${response.status} ${response.statusText}`); if (!response.ok) { const text = await response.text().catch(()=>"(body read error)"); console.error(`❌ API responded with error body: ${text.substring(0, 500)}`); throw new Error(`API call failed: ${response.status} ${response.statusText}`); } /* ----- Success: relay data back to caller ----- */ const analysisData = await response.json(); console.log("✅ Analysis data received", JSON.stringify(analysisData).slice(0, 500)); const elapsed = Date.now() - started; console.log(`🎉 Edge function completed in ${elapsed} ms`); return new Response(JSON.stringify(analysisData), { headers: { ...corsHeaders, "Content-Type": "application/json" } }); } catch (error) { console.error("🚨 Edge function error", error); return new Response(JSON.stringify({ error: error.message ?? "Failed to analyze base" }), { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }); } }); ```