Spaces:
Sleeping
Sleeping
File size: 1,535 Bytes
43280d3 | 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 | // src/lib/api.ts
import type { HistoricalMatch, UpcomingFixture, PredictionResponse } from "@/types"
const BASE = process.env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000"
async function apiCall<T>(url: string, options?: RequestInit): Promise<T> {
const res = await fetch(url, options)
if (!res.ok) {
const body = await res.json().catch(() => ({}))
const message = body?.detail ?? `API error: ${res.status}`
const err = new Error(message) as Error & { status: number }
err.status = res.status
throw err
}
return res.json()
}
export const api = {
health: () =>
apiCall<{ status: string }>(`${BASE}/health`),
getMatches: async (season?: string, limit = 50, offset = 0) => {
const raw = await apiCall<
HistoricalMatch[] | { count: number; data: HistoricalMatch[] }
>(
`${BASE}/api/v1/matches?limit=${limit}&offset=${offset}${season ? `&season=${season}` : ""}`
)
// Backend returns { count, data: [...] } — unwrap to plain array
return Array.isArray(raw) ? raw : raw.data ?? []
},
predict: (
home_team: string,
away_team: string,
home_odd?: number,
draw_odd?: number,
away_odd?: number
) =>
apiCall<PredictionResponse>(`${BASE}/api/v1/predict`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ home_team, away_team, home_odd, draw_odd, away_odd }),
}),
getUpcoming: (limit = 20) =>
apiCall<UpcomingFixture[]>(
`${BASE}/api/v1/predict/upcoming?limit=${limit}`
),
} |