/** * JSON-safe fetch for HomePilot Settings API routes. * * Fetches a HomePilot API route and returns its parsed JSON, failing with an * actionable message instead of a raw ``Unexpected token '<', " { const res = await fetch(url); const ctype = res.headers.get("content-type") || ""; if (!ctype.includes("application/json")) { let path = url; try { const origin = typeof window !== "undefined" ? window.location.origin : "http://localhost"; path = new URL(url, origin).pathname; } catch { /* keep the raw url */ } throw new Error( `The HomePilot providers API is unavailable on this deployment ` + `(HTTP ${res.status}). The request for ${path} returned ` + `${ctype || "an unknown content type"} instead of JSON — check that ` + `this route is forwarded to the backend and not the SPA fallback.`, ); } if (!res.ok) { // A proper JSON error body — surface its message rather than a bare status. let detail = `HTTP ${res.status}`; try { const body = await res.json(); detail = body?.message || body?.detail || detail; } catch { /* fall back to the HTTP status */ } throw new Error(detail); } return res.json(); }