File size: 1,930 Bytes
d31acaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type {
  AgentRunRow,
  BondDetail,
  BondInvestigationResponse,
  BondSummary,
  DashboardSummary,
  HistoryResponse,
  InvestigationRow,
  IssuerDetail,
  ManagerDashboard,
} from "@/types/api";

const BASE = "/api";

async function req<T>(path: string, init?: RequestInit): Promise<T> {
  const r = await fetch(`${BASE}${path}`, {
    headers: { "content-type": "application/json" },
    ...init,
  });
  if (!r.ok) throw new Error(`${r.status} ${r.statusText}${path}`);
  return r.json() as Promise<T>;
}

export const api = {
  health: () => req<{ status: string; version: string }>("/health"),
  dashboardSummary: () => req<DashboardSummary>("/dashboard/summary"),
  listBonds: (params: Record<string, string | number> = {}) => {
    const q = new URLSearchParams(
      Object.entries(params).map(([k, v]) => [k, String(v)]),
    ).toString();
    return req<BondSummary[]>(`/bonds${q ? `?${q}` : ""}`);
  },
  bondDetail: (id: string) => req<BondDetail>(`/bonds/${encodeURIComponent(id)}`),
  bondHistory: (id: string, windowDays = 60) =>
    req<HistoryResponse>(`/bonds/${encodeURIComponent(id)}/history?window_days=${windowDays}`),
  listInvestigations: (status?: string) =>
    req<InvestigationRow[]>(`/investigations${status ? `?status=${status}` : ""}`),
  caseAgentRuns: (caseId: number) =>
    req<AgentRunRow[]>(`/investigations/${caseId}/agent-runs`),
  issuerDetail: (id: string) => req<IssuerDetail>(`/issuers/${encodeURIComponent(id)}`),
  methodology: () => req<Record<string, unknown>>(`/methodology`),
  bondInvestigation: (id: string) =>
    req<BondInvestigationResponse>(`/bonds/${encodeURIComponent(id)}/investigation`),
  runInvestigation: (id: string) =>
    req<{ case_id: number; status: string; recommended_action: string | null }>(
      `/bonds/${encodeURIComponent(id)}/investigate`,
      { method: "POST" },
    ),
  managerKpis: () => req<ManagerDashboard>("/manager/kpis"),
};