import { ApiClient } from "@photoreport/sdk"; import { desktopConfig, desktopRefreshStore, isDesktop } from "./platform.js"; /** * Where the API is: what the desktop app says; VITE_API_URL when the build sets it; otherwise next to this page, * app. -> api., so a web build works on any environment without rebuilding for it. */ export function apiUrlFor(configured: string | undefined, location: { protocol: string; hostname: string } | undefined, desktop = desktopConfig()): string { // The desktop app says where its API is (its interface may be a version downloaded from the web build). if (desktop?.api) return desktop.api.replace(/\/+$/, ""); if (configured) return configured.replace(/\/+$/, ""); if (location?.hostname.startsWith("app.")) return `${location.protocol}//api.${location.hostname.slice(4)}`; return "http://localhost:3000"; } export const API_URL = apiUrlFor(import.meta.env.VITE_API_URL as string | undefined, typeof window === "undefined" ? undefined : window.location); /** * The public sites next to this API: api. -> events. (events.next.photoreport.pt for next, * events.photoreport.pt in production), where the archive's and the events' pages are. */ export function eventsUrlFor(configured: string | undefined, api: string): string { if (configured) return configured.replace(/\/+$/, ""); const u = new URL(api); if (u.hostname.startsWith("api.")) return `${u.protocol}//events.${u.hostname.slice(4)}`; return "http://localhost:3100"; } export const EVENTS_URL = eventsUrlFor((import.meta.env.VITE_ARCHIVE_URL ?? import.meta.env.VITE_EVENTS_URL) as string | undefined, API_URL); export function createApi(onSessionLost: () => void): ApiClient { return new ApiClient({ baseUrl: API_URL, onSessionLost, ...(isDesktop() ? { refreshStore: desktopRefreshStore } : {}), }); }