// Thin wrappers over the HTTP API. Errors carry the server's own reason. import type { InboxItem, IntakeEval, LedgerScores, ModelInfo, Onboarding, PolicyRule, Quote, RepriceResult, Run, Simulation } from './types' export class ApiError extends Error { status: number constructor(status: number, message: string) { super(message) this.status = status } } async function call(path: string, init?: RequestInit): Promise { const response = await fetch(path, { ...init, headers: { 'content-type': 'application/json', ...(init?.headers ?? {}) }, }) if (!response.ok) { let message = `HTTP ${response.status}` try { const body = await response.json() if (typeof body.detail === 'string') message = body.detail else if (Array.isArray(body.detail)) message = body.detail.map((d: { msg: string }) => d.msg).join('; ') } catch { // not JSON } throw new ApiError(response.status, message) } return (await response.json()) as T } export const getInfo = () => call<{ provider: string; policy_version: string; sample_sender: string }>('/api/info') export const getInbox = () => call('/api/inbox') export const getQuote = (id: string) => call(`/api/quotes/${id}`) export const getRuns = () => call('/api/runs') export const getModel = () => call('/api/model') export const postRfq = (sender: string, subject: string, body: string) => call<{ run_id: string }>('/api/rfq', { method: 'POST', body: JSON.stringify({ sender, subject, body }) }) export const decide = ( id: string, decision: 'approved' | 'rejected', role: 'estimator' | 'senior', expectedVersion: number, reason = '', ) => call<{ position: number }>(`/api/quotes/${id}/decide`, { method: 'POST', body: JSON.stringify({ decision, role, reason, expected_version: expectedVersion }), }) export const reprice = (copperPct: number) => call('/api/reprice', { method: 'POST', body: JSON.stringify({ copper_pct: copperPct }) }) export const getPolicy = () => call<{ version: string; rules: PolicyRule[] }>('/api/policy') export const simulatePolicy = (ruleId: string, value: number) => call('/api/policy/simulate', { method: 'POST', body: JSON.stringify({ rule_id: ruleId, value }) }) export const getSources = () => call('/api/sources') export const getLedger = () => call('/api/foundation-ledger') export const getIntakeEval = () => call('/api/intake-eval') export const getClock = () => call<{ business_date: string; followups: { drafted: number; sent: number; rejected: number } }>('/api/clock') export const advanceClock = (days: number) => call<{ business_date: string; actions: { kind: string; quote_id: string; reason: string }[] }>('/api/clock/advance', { method: 'POST', body: JSON.stringify({ days }), }) export const decideFollowup = (id: string, decision: 'approved' | 'rejected', role: 'estimator' | 'senior') => call<{ position: number }>('/api/quotes/' + id + '/followup', { method: 'POST', body: JSON.stringify({ decision, role }) })