// Where the SPA reaches the Fastify backend. // // In a browser (dev via the Vite proxy, or prod where Fastify serves the built // SPA) this is EMPTY — requests stay relative (`/api/...`) and same-origin, so // nothing changes. The Tauri desktop shell (apps/desktop) serves the bundled // SPA from `tauri://localhost`, which has no backend and no proxy; there we set // VITE_API_BASE to the appliance's Fastify origin (e.g. http://127.0.0.1:3000) // at build time so /api and the live WS feed resolve to the real server. // // Keep this the SINGLE source for the backend origin — api.ts and the live-feed // WebSocket both read it, so the web app and the desktop shell stay identical // except for this one build-time value. /** Backend HTTP origin, no trailing slash. Empty string = same-origin/relative. */ export const API_BASE: string = (import.meta.env.VITE_API_BASE ?? "").replace(/\/$/, ""); /** Resolve an API path to a full URL (or a relative path when API_BASE is empty). */ export function apiUrl(path: string): string { return API_BASE ? `${API_BASE}${path}` : path; } /** Build the ws:// or wss:// URL for the backend's live feed. Uses API_BASE when * set (Tauri), else the page origin (browser). */ export function wsUrl(path: string): string { if (API_BASE) { return `${API_BASE.replace(/^http/, "ws")}${path}`; } const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; return `${proto}//${window.location.host}${path}`; }