'use client'; import type { DNAResponse, SearchResponse, SeriesResponse, SimilarResponse } from './types'; /** * Browser-side fetch helpers: same-origin `/api/v1/*` (rewritten by next.config.ts to the FastAPI service). * Kept deliberately small — server components should do the heavy lifting. */ export class ClientApiError extends Error { constructor( readonly status: number, message: string, ) { super(message); this.name = 'ClientApiError'; } } async function get(path: string, signal?: AbortSignal): Promise { const res = await fetch(`/api/v1${path}`, { headers: { accept: 'application/json' }, signal }); if (!res.ok) throw new ClientApiError(res.status, `API ${res.status}`); return (await res.json()) as T; } export const clientApi = { search: (q: string, limit = 12, signal?: AbortSignal) => get(`/search?q=${encodeURIComponent(q)}&limit=${limit}`, signal), countrySeries: (id: string, indicator: string, signal?: AbortSignal) => get(`/countries/${encodeURIComponent(id)}/series/${encodeURIComponent(indicator)}`, signal), countrySimilar: (id: string, mode: string, signal?: AbortSignal) => get(`/countries/${encodeURIComponent(id)}/similar?mode=${encodeURIComponent(mode)}`, signal), countryDna: (id: string, reference: string, signal?: AbortSignal) => get(`/countries/${encodeURIComponent(id)}/dna?reference=${encodeURIComponent(reference)}`, signal), };