/** * PhilVerify API client — proxied through Vite to http://localhost:8000 * Typed via src/types.ts which mirrors api/schemas.py */ import type { VerificationResponse, HistoryParams, HistoryResponse, TrendsResponse, HealthResponse, ApiError as ApiErrorType, } from './types' import { ApiError } from './types' const BASE = '/api' // ── Internal fetch helpers ───────────────────────────────────────────────────── async function post(path: string, body: unknown): Promise { const res = await fetch(`${BASE}${path}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) if (!res.ok) { const err = await res.json().catch(() => ({})) as { detail?: string } throw new ApiError(err.detail ?? `HTTP ${res.status}`, true) } return res.json() as Promise } async function postForm(path: string, formData: FormData): Promise { const res = await fetch(`${BASE}${path}`, { method: 'POST', body: formData }) if (!res.ok) { const err = await res.json().catch(() => ({})) as { detail?: string } throw new ApiError(err.detail ?? `HTTP ${res.status}`, true) } return res.json() as Promise } async function get(path: string, params: Record = {}): Promise { const defined = Object.fromEntries( Object.entries(params).filter(([, v]) => v !== undefined), ) as Record const qs = new URLSearchParams(defined).toString() const res = await fetch(`${BASE}${path}${qs ? '?' + qs : ''}`) if (!res.ok) throw new ApiError(`HTTP ${res.status}`) return res.json() as Promise } // ── Public API surface ───────────────────────────────────────────────────────── export const api = { verifyText: (text: string): Promise => post('/verify/text', { text }), verifyUrl: (url: string): Promise => post('/verify/url', { url }), verifyImage: (file: File): Promise => { const f = new FormData() f.append('file', file) return postForm('/verify/image', f) }, verifyVideo: (file: File): Promise => { const f = new FormData() f.append('file', file) return postForm('/verify/video', f) }, history: (params?: HistoryParams): Promise => get('/history', params as Record), trends: (): Promise => get('/trends'), health: (): Promise => get('/health'), } as const // Re-export error class for consumers export { ApiError } from './types' export type { ApiErrorType }