import type { Place, ProviderId, ProviderInfo, WeatherReport } from "./weather/types"; /** * Browser-side calls to our own API routes. * Each throws an Error carrying the server's message, ready to show in the UI. */ async function getJson(url: string, signal?: AbortSignal): Promise { const response = await fetch(url, { signal }); const body = await response.json().catch(() => ({})); if (!response.ok) { throw new Error(body.error ?? `Request failed (HTTP ${response.status})`); } return body as T; } export const fetchProviders = () => getJson("/api/providers"); export const searchPlaces = (query: string, signal?: AbortSignal) => getJson(`/api/geocode?q=${encodeURIComponent(query)}`, signal); export const namePlace = (lat: number, lon: number) => getJson(`/api/geocode?lat=${lat}&lon=${lon}`); export const fetchWeather = (provider: ProviderId, place: Place, signal?: AbortSignal) => getJson(`/api/weather?provider=${provider}&lat=${place.lat}&lon=${place.lon}`, signal);