lordvisorad commited on
Commit
a4233ca
Β·
verified Β·
1 Parent(s): 023fad1

Upload dashboard/src/lib/api.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. dashboard/src/lib/api.ts +56 -0
dashboard/src/lib/api.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * API client for the FairRelay Brain backend.
3
+ * All dashboard data comes from real backend endpoints.
4
+ */
5
+
6
+ const API_BASE = import.meta.env.VITE_API_URL || 'http://localhost:8000';
7
+ const API_PREFIX = '/api/v1';
8
+
9
+ async function fetchJson(path: string, options: RequestInit = {}) {
10
+ const token = localStorage.getItem('fairrelay_token') || '';
11
+ const res = await fetch(`${API_BASE}${path}`, {
12
+ ...options,
13
+ headers: {
14
+ 'Content-Type': 'application/json',
15
+ ...(token ? { Authorization: `Bearer ${token}` } : {}),
16
+ ...options.headers,
17
+ },
18
+ });
19
+ if (!res.ok) throw new Error(`API ${res.status}: ${res.statusText}`);
20
+ return res.json();
21
+ }
22
+
23
+ // ═══ Dashboard KPIs ═══
24
+ export const getHealth = () => fetchJson('/health');
25
+ export const getApiHealth = () => fetchJson(`${API_PREFIX}/health`);
26
+
27
+ // ═══ Consolidation ═══
28
+ export const runConsolidation = (payload: any) =>
29
+ fetchJson(`${API_PREFIX}/consolidate`, { method: 'POST', body: JSON.stringify(payload) });
30
+ export const runSimulation = (payload: any) =>
31
+ fetchJson(`${API_PREFIX}/consolidate/simulate`, { method: 'POST', body: JSON.stringify(payload) });
32
+
33
+ // ═══ Allocation ═══
34
+ export const runAllocation = (payload: any) =>
35
+ fetchJson(`${API_PREFIX}/allocate`, { method: 'POST', body: JSON.stringify(payload) });
36
+
37
+ // ═══ Drivers ═══
38
+ export const getDrivers = () => fetchJson(`${API_PREFIX}/drivers`);
39
+
40
+ // ═══ Routes ═══
41
+ export const optimizeRoute = (payload: any) =>
42
+ fetchJson(`${API_PREFIX}/routes/optimize`, { method: 'POST', body: JSON.stringify(payload) });
43
+
44
+ // ═══ WebSocket ═══
45
+ export function connectWS(channel: 'tracking' | 'shipments' | 'dashboard'): WebSocket {
46
+ const wsBase = API_BASE.replace('http', 'ws');
47
+ return new WebSocket(`${wsBase}/ws/${channel}`);
48
+ }
49
+
50
+ // ═══ Ops Backend (Node) ═══
51
+ const OPS_BASE = import.meta.env.VITE_OPS_URL || 'http://localhost:3000';
52
+
53
+ export const getOpsStats = () => fetch(`${OPS_BASE}/api/dashboard/stats`).then(r => r.json());
54
+ export const getOpsActivity = () => fetch(`${OPS_BASE}/api/dashboard/activity`).then(r => r.json());
55
+ export const getOpsTracking = () => fetch(`${OPS_BASE}/api/dashboard/live-tracking-gps`).then(r => r.json());
56
+ export const getOpsShipments = (params?: string) => fetch(`${OPS_BASE}/api/shipments${params ? '?' + params : ''}`).then(r => r.json());