Spaces:
Sleeping
Sleeping
Download web/src/lib/api.ts from TBM99/quote-desk: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/spaces/TBM99/quote-desk/resolve/main/web/src/lib/api.ts
- Command line
-
hf download hf://spaces/TBM99/quote-desk/web/src/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/TBM99/quote-desk/resolve/main/web/src/lib/api.ts
3.13 kB
| // 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<T>(path: string, init?: RequestInit): Promise<T> { | |
| 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<InboxItem[]>('/api/inbox') | |
| export const getQuote = (id: string) => call<Quote>(`/api/quotes/${id}`) | |
| export const getRuns = () => call<Run[]>('/api/runs') | |
| export const getModel = () => call<ModelInfo>('/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<RepriceResult>('/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<Simulation>('/api/policy/simulate', { method: 'POST', body: JSON.stringify({ rule_id: ruleId, value }) }) | |
| export const getSources = () => call<Onboarding>('/api/sources') | |
| export const getLedger = () => call<LedgerScores>('/api/foundation-ledger') | |
| export const getIntakeEval = () => call<IntakeEval>('/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 }) }) | |