SPB Git

spb/countryatlas

Public
1.1 KB · 30 lines typescript
1'use client';2import type { SearchResponse, SeriesResponse } from './types';34/**5 * Browser-side fetch helpers: same-origin `/api/v1/*` (rewritten by next.config.ts to the FastAPI service).6 * Kept deliberately small — server components should do the heavy lifting.7 */8export class ClientApiError extends Error {9  constructor(10    readonly status: number,11    message: string,12  ) {13    super(message);14    this.name = 'ClientApiError';15  }16}1718async function get<T>(path: string, signal?: AbortSignal): Promise<T> {19  const res = await fetch(`/api/v1${path}`, { headers: { accept: 'application/json' }, signal });20  if (!res.ok) throw new ClientApiError(res.status, `API ${res.status}`);21  return (await res.json()) as T;22}2324export const clientApi = {25  search: (q: string, limit = 12, signal?: AbortSignal) =>26    get<SearchResponse>(`/search?q=${encodeURIComponent(q)}&limit=${limit}`, signal),27  countrySeries: (id: string, indicator: string, signal?: AbortSignal) =>28    get<SeriesResponse>(`/countries/${encodeURIComponent(id)}/series/${encodeURIComponent(indicator)}`, signal),29};30
Files · b828cc86d5b2223081f42e670e6d1f2379c43230
Menu

Theme