import type { Remote, Virtual, Artifact, OverviewStats, RemoteStatRow, FileStatRow, BandwidthStatRow, HealthStatus, ProbeResult } from './types'; const BASE = ''; async function fetchJSON(path: string, init?: RequestInit): Promise { const resp = await fetch(`${BASE}${path}`, { ...init, headers: { 'Content-Type': 'application/json', ...init?.headers }, }); if (!resp.ok) { const text = await resp.text(); throw new Error(`${resp.status}: ${text}`); } if (resp.status === 204) return undefined as T; return resp.json(); } export const api = { health: () => fetchJSON('/api/v2/health'), stats: () => fetchJSON('/api/v2/stats'), topRemotes: () => fetchJSON('/api/v2/stats/top-remotes'), topFilesByHits: () => fetchJSON('/api/v2/stats/top-files-by-hits'), topFilesByBandwidth: () => fetchJSON('/api/v2/stats/top-files-by-bandwidth'), listRemotes: () => fetchJSON('/api/v2/remotes'), getRemote: (name: string) => fetchJSON(`/api/v2/remotes/${name}`), listVirtuals: () => fetchJSON('/api/v2/virtuals'), getVirtual: (name: string) => fetchJSON(`/api/v2/virtuals/${name}`), listObjects: (remote: string, page = 1, perPage = 50) => fetchJSON(`/api/v2/remotes/${remote}/objects?page=${page}&per_page=${perPage}`), evictObject: (remote: string, path: string) => fetchJSON(`/api/v2/remotes/${remote}/objects/${path}`, { method: 'DELETE' }), flushRemoteCache: (remote: string) => fetchJSON(`/api/v2/remotes/${remote}/cache`, { method: 'DELETE' }), probe: (remote: string, path: string) => fetchJSON('/api/v2/probe', { method: 'POST', body: JSON.stringify({ remote, path }), }), };