import { apiUrl, getSelectedStoreId } from '../config'; import { normalizeAnomalies, normalizeFunnel, normalizeHeatmap, normalizeMetrics, } from './normalize'; import type { HealthResponse, StoreAnomalies, StoreFunnel, StoreHeatmap, StoreMetrics, } from '../types/api'; async function fetchJson(path: string, init?: RequestInit): Promise { const res = await fetch(apiUrl(path), { ...init, headers: { Accept: 'application/json', ...init?.headers, }, }); if (!res.ok) { const body = await res.text().catch(() => ''); throw new Error( `API ${res.status} ${res.statusText}${body ? `: ${body.slice(0, 200)}` : ''}`, ); } return res.json() as Promise; } export async function getStoreMetrics(storeId = getSelectedStoreId()): Promise { const raw = await fetchJson>( `/stores/${encodeURIComponent(storeId)}/metrics`, ); return normalizeMetrics(raw); } export async function getStoreFunnel(storeId = getSelectedStoreId()): Promise { const raw = await fetchJson>( `/stores/${encodeURIComponent(storeId)}/funnel`, ); return normalizeFunnel(raw); } export async function getStoreHeatmap(storeId = getSelectedStoreId()): Promise { const raw = await fetchJson>( `/stores/${encodeURIComponent(storeId)}/heatmap`, ); return normalizeHeatmap(raw); } export async function getStoreAnomalies(storeId = getSelectedStoreId()): Promise { const raw = await fetchJson>( `/stores/${encodeURIComponent(storeId)}/anomalies`, ); return normalizeAnomalies(raw); } export function getHealth(): Promise { return fetchJson('/health'); }