arbitrage-engine/frontend/lib/api.ts

52 lines
1.1 KiB
TypeScript

const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:4330";
export interface RateData {
symbol: string;
markPrice: number;
indexPrice: number;
lastFundingRate: number;
nextFundingTime: number;
timestamp: number;
}
export interface RatesResponse {
BTC: RateData;
ETH: RateData;
}
export interface HistoryPoint {
fundingTime: number;
fundingRate: number;
timestamp: string;
}
export interface HistoryResponse {
BTC: HistoryPoint[];
ETH: HistoryPoint[];
}
export interface AssetStats {
mean7d: number;
annualized: number;
count: number;
}
export interface StatsResponse {
BTC: AssetStats;
ETH: AssetStats;
combo: { mean7d: number; annualized: number };
}
async function fetchAPI<T>(path: string): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, { cache: "no-store" });
if (!res.ok) throw new Error(`API error ${res.status}`);
return res.json();
}
export const api = {
rates: () => fetchAPI<RatesResponse>("/api/rates"),
history: () => fetchAPI<HistoryResponse>("/api/history"),
stats: () => fetchAPI<StatsResponse>("/api/stats"),
health: () => fetchAPI<{ status: string }>("/api/health"),
};