From 1482422f2f5b236cdcdff4429ae06bb55dca4083 Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Sun, 17 May 2026 12:46:13 -0700 Subject: Add stack start and stop scripts --- frontend/lib/api.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 frontend/lib/api.ts (limited to 'frontend/lib/api.ts') diff --git a/frontend/lib/api.ts b/frontend/lib/api.ts new file mode 100644 index 0000000..b2f0dea --- /dev/null +++ b/frontend/lib/api.ts @@ -0,0 +1,52 @@ +import type { HistoryPoint, MarketIndex, SearchResult, TickerOverview, WatchlistResponse } from "@/types/api"; + +const API_BASE = process.env.NEXT_PUBLIC_API_BASE_URL || "http://localhost:8000"; + +export class ApiError extends Error { + status: number; + + constructor(message: string, status: number) { + super(message); + this.name = "ApiError"; + this.status = status; + } +} + +async function request(path: string, init?: RequestInit): Promise { + const res = await fetch(`${API_BASE}${path}`, { + ...init, + headers: { + "Content-Type": "application/json", + ...(init?.headers || {}) + } + }); + if (!res.ok) { + const body = await res.json().catch(() => ({})); + throw new ApiError(body.detail || `Request failed: ${res.status}`, res.status); + } + return res.json() as Promise; +} + +export const api = { + search(query: string) { + return request(`/api/search?q=${encodeURIComponent(query)}`); + }, + marketIndices() { + return request("/api/market/indices"); + }, + overview(symbol: string) { + return request(`/api/tickers/${encodeURIComponent(symbol)}/overview`); + }, + history(symbol: string, period: string) { + return request(`/api/tickers/${encodeURIComponent(symbol)}/history?period=${encodeURIComponent(period)}`); + }, + watchlist() { + return request("/api/watchlist"); + }, + addWatchlist(symbol: string) { + return request(`/api/watchlist/${encodeURIComponent(symbol)}`, { method: "POST" }); + }, + removeWatchlist(symbol: string) { + return request(`/api/watchlist/${encodeURIComponent(symbol)}`, { method: "DELETE" }); + } +}; -- cgit v1.3-2-g0d8e