export const BASE = import.meta.env.DEV ? 'http://10.69.5.23' : '' export type DataRow = Record const cache: any = {}; async function get(path: string, params: Record = {}): Promise { const url = new URL(`${BASE}${path}`, window.location.origin) Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v)) if(cache[url.toString()]) return cache[url.toString()]; cache[url.toString()] = await fetch(url.toString()).then(resp => resp.json()).finally(() => { delete cache[url.toString()]; }); return cache[url.toString()] } export const api = { position: (fields?: string) => get<{latitude: number, longitude: number, altitude: number}>('/api/position', fields ? { fields } : {}), current: (fields?: string) => get('/api/current', fields ? { fields } : {}), hourly: (start?: string, end?: string, fields?: string) => get('/api/hourly', { ...(start ? { start } : {}), ...(end ? { end } : {}), ...(fields ? { fields } : {}) }), daily: (start?: string, end?: string, fields?: string) => get('/api/daily', { ...(start ? { start } : {}), ...(end ? { end } : {}), ...(fields ? { fields } : {}) }), }