lordvisorad's picture
Upload dashboard/src/lib/api.ts with huggingface_hub
a4233ca verified
/**
* API client for the FairRelay Brain backend.
* All dashboard data comes from real backend endpoints.
*/
const API_BASE = import.meta.env.VITE_API_URL || 'http://localhost:8000';
const API_PREFIX = '/api/v1';
async function fetchJson(path: string, options: RequestInit = {}) {
const token = localStorage.getItem('fairrelay_token') || '';
const res = await fetch(`${API_BASE}${path}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
...options.headers,
},
});
if (!res.ok) throw new Error(`API ${res.status}: ${res.statusText}`);
return res.json();
}
// ═══ Dashboard KPIs ═══
export const getHealth = () => fetchJson('/health');
export const getApiHealth = () => fetchJson(`${API_PREFIX}/health`);
// ═══ Consolidation ═══
export const runConsolidation = (payload: any) =>
fetchJson(`${API_PREFIX}/consolidate`, { method: 'POST', body: JSON.stringify(payload) });
export const runSimulation = (payload: any) =>
fetchJson(`${API_PREFIX}/consolidate/simulate`, { method: 'POST', body: JSON.stringify(payload) });
// ═══ Allocation ═══
export const runAllocation = (payload: any) =>
fetchJson(`${API_PREFIX}/allocate`, { method: 'POST', body: JSON.stringify(payload) });
// ═══ Drivers ═══
export const getDrivers = () => fetchJson(`${API_PREFIX}/drivers`);
// ═══ Routes ═══
export const optimizeRoute = (payload: any) =>
fetchJson(`${API_PREFIX}/routes/optimize`, { method: 'POST', body: JSON.stringify(payload) });
// ═══ WebSocket ═══
export function connectWS(channel: 'tracking' | 'shipments' | 'dashboard'): WebSocket {
const wsBase = API_BASE.replace('http', 'ws');
return new WebSocket(`${wsBase}/ws/${channel}`);
}
// ═══ Ops Backend (Node) ═══
const OPS_BASE = import.meta.env.VITE_OPS_URL || 'http://localhost:3000';
export const getOpsStats = () => fetch(`${OPS_BASE}/api/dashboard/stats`).then(r => r.json());
export const getOpsActivity = () => fetch(`${OPS_BASE}/api/dashboard/activity`).then(r => r.json());
export const getOpsTracking = () => fetch(`${OPS_BASE}/api/dashboard/live-tracking-gps`).then(r => r.json());
export const getOpsShipments = (params?: string) => fetch(`${OPS_BASE}/api/shipments${params ? '?' + params : ''}`).then(r => r.json());