Spaces:
Running
Running
Download apps/web/lib/api.ts from 2026worldcup/predictor: direct link, hf CLI and curl.
- Browser
- Download file 7.6 kB
-
https://huggingface.co/spaces/2026worldcup/predictor/resolve/main/apps/web/lib/api.ts
- Command line
-
hf download hf://spaces/2026worldcup/predictor/apps/web/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/2026worldcup/predictor/resolve/main/apps/web/lib/api.ts
7.6 kB
| // Typed client for the FastAPI backend. | |
| // Empty => same-origin "/api/*" (proxied by Next.js rewrites to the backend), | |
| // so one public URL/tunnel serves both frontend and API. | |
| export const API = process.env.NEXT_PUBLIC_API_URL || ""; | |
| export async function api<T>(path: string, init?: RequestInit): Promise<T> { | |
| const res = await fetch(`${API}${path}`, { cache: "no-store", ...init }); | |
| if (!res.ok) { | |
| const txt = await res.text().catch(() => ""); | |
| throw new Error(`${res.status} ${txt}`); | |
| } | |
| return res.json() as Promise<T>; | |
| } | |
| export type Team = { | |
| name: string; | |
| group_label: string; | |
| confederation: string | null; | |
| elo: number; | |
| fifa_rank: number | null; | |
| attack: number | null; | |
| defence: number | null; | |
| p_group_win: number | null; | |
| p_advance: number | null; | |
| p_r16: number | null; | |
| p_qf: number | null; | |
| p_sf: number | null; | |
| p_final: number | null; | |
| p_win: number | null; | |
| }; | |
| export type Fixture = { | |
| date: string; | |
| kickoff?: string | null; | |
| home_team: string; | |
| away_team: string; | |
| city: string | null; | |
| country: string | null; | |
| neutral: number; | |
| group_label?: string | null; | |
| p_home: number | null; | |
| p_draw: number | null; | |
| p_away: number | null; | |
| exp_home: number | null; | |
| exp_away: number | null; | |
| over25?: number | null; | |
| btts?: number | null; | |
| top_scores: [number, number, number][] | null; | |
| }; | |
| export type Prediction = { | |
| home_team: string; | |
| away_team: string; | |
| neutral: boolean; | |
| p_home: number; | |
| p_draw: number; | |
| p_away: number; | |
| exp_home: number; | |
| exp_away: number; | |
| top_scores: [number, number, number][]; | |
| grid: number[][]; | |
| over25: number; | |
| btts: number; | |
| }; | |
| export type Meta = { | |
| id: number; | |
| model_name: string; | |
| xi: number; | |
| trained_at: string; | |
| n_matches: number; | |
| rps: number; | |
| brier: number; | |
| logloss: number; | |
| n_sims: number; | |
| notes: { T?: number; rps_calibrated?: number }; | |
| }; | |
| export type SimRow = Team & { team: string }; | |
| export const FLAG: Record<string, string> = { | |
| Argentina: "🇦🇷", Spain: "🇪🇸", Brazil: "🇧🇷", France: "🇫🇷", England: "🏴", | |
| Portugal: "🇵🇹", Germany: "🇩🇪", Netherlands: "🇳🇱", Belgium: "🇧🇪", Croatia: "🇭🇷", | |
| Morocco: "🇲🇦", Uruguay: "🇺🇾", Colombia: "🇨🇴", Japan: "🇯🇵", "South Korea": "🇰🇷", | |
| Mexico: "🇲🇽", "United States": "🇺🇸", Canada: "🇨🇦", Switzerland: "🇨🇭", | |
| Austria: "🇦🇹", Turkey: "🇹🇷", Norway: "🇳🇴", Sweden: "🇸🇪", Denmark: "🇩🇰", | |
| Senegal: "🇸🇳", "Ivory Coast": "🇨🇮", Ghana: "🇬🇭", Egypt: "🇪🇬", Algeria: "🇩🇿", | |
| Tunisia: "🇹🇳", "Cape Verde": "🇨🇻", "South Africa": "🇿🇦", "DR Congo": "🇨🇩", | |
| Iran: "🇮🇷", "Saudi Arabia": "🇸🇦", Qatar: "🇶🇦", Australia: "🇦🇺", Uzbekistan: "🇺🇿", | |
| Jordan: "🇯🇴", Iraq: "🇮🇶", Paraguay: "🇵🇾", Ecuador: "🇪🇨", Scotland: "🏴", | |
| "Czech Republic": "🇨🇿", "Bosnia and Herzegovina": "🇧🇦", Panama: "🇵🇦", | |
| Haiti: "🇭🇹", "Curaçao": "🇨🇼", "New Zealand": "🇳🇿", Wales: "🏴", | |
| }; | |
| export const flag = (t: string) => FLAG[t] || "⚽"; | |
| // ISO 3166-1 alpha-2 (flagcdn) — England/Scotland use sub-region codes. | |
| export const ISO: Record<string, string> = { | |
| Argentina: "ar", Spain: "es", Brazil: "br", France: "fr", England: "gb-eng", | |
| Portugal: "pt", Germany: "de", Netherlands: "nl", Belgium: "be", Croatia: "hr", | |
| Morocco: "ma", Uruguay: "uy", Colombia: "co", Japan: "jp", "South Korea": "kr", | |
| Mexico: "mx", "United States": "us", Canada: "ca", Switzerland: "ch", | |
| Austria: "at", Turkey: "tr", Norway: "no", Sweden: "se", Senegal: "sn", | |
| "Ivory Coast": "ci", Ghana: "gh", Egypt: "eg", Algeria: "dz", Tunisia: "tn", | |
| "Cape Verde": "cv", "South Africa": "za", "DR Congo": "cd", Iran: "ir", | |
| "Saudi Arabia": "sa", Qatar: "qa", Australia: "au", Uzbekistan: "uz", | |
| Jordan: "jo", Iraq: "iq", Paraguay: "py", Ecuador: "ec", Scotland: "gb-sct", | |
| "Czech Republic": "cz", "Bosnia and Herzegovina": "ba", Panama: "pa", | |
| Haiti: "ht", "Curaçao": "cw", "New Zealand": "nz", Wales: "gb-wls", | |
| }; | |
| export const flagUrl = (t: string, w: 20 | 40 | 80 | 160 = 40) => | |
| ISO[t] ? `https://flagcdn.com/w${w}/${ISO[t]}.png` : null; | |
| export const CONFED_COLOR: Record<string, string> = { | |
| UEFA: "#3b82f6", | |
| CONMEBOL: "#22c55e", | |
| CAF: "#f59e0b", | |
| AFC: "#ef4444", | |
| CONCACAF: "#a855f7", | |
| OFC: "#06b6d4", | |
| }; | |
| export const pct = (x: number | null | undefined, d = 1) => | |
| x == null ? "—" : `${(x * 100).toFixed(d)}%`; | |
| // Chinese team names | |
| export const CN: Record<string, string> = { | |
| Argentina: "阿根廷", Spain: "西班牙", Brazil: "巴西", France: "法国", England: "英格兰", | |
| Portugal: "葡萄牙", Germany: "德国", Netherlands: "荷兰", Belgium: "比利时", Croatia: "克罗地亚", | |
| Morocco: "摩洛哥", Uruguay: "乌拉圭", Colombia: "哥伦比亚", Japan: "日本", "South Korea": "韩国", | |
| Mexico: "墨西哥", "United States": "美国", Canada: "加拿大", Switzerland: "瑞士", | |
| Austria: "奥地利", Turkey: "土耳其", Norway: "挪威", Sweden: "瑞典", Senegal: "塞内加尔", | |
| "Ivory Coast": "科特迪瓦", Ghana: "加纳", Egypt: "埃及", Algeria: "阿尔及利亚", | |
| Tunisia: "突尼斯", "Cape Verde": "佛得角", "South Africa": "南非", "DR Congo": "刚果(金)", | |
| Iran: "伊朗", "Saudi Arabia": "沙特阿拉伯", Qatar: "卡塔尔", Australia: "澳大利亚", | |
| Uzbekistan: "乌兹别克斯坦", Jordan: "约旦", Iraq: "伊拉克", Paraguay: "巴拉圭", | |
| Ecuador: "厄瓜多尔", Scotland: "苏格兰", "Czech Republic": "捷克", Panama: "巴拿马", | |
| "Bosnia and Herzegovina": "波黑", Haiti: "海地", "Curaçao": "库拉索", | |
| "New Zealand": "新西兰", Wales: "威尔士", Denmark: "丹麦", | |
| }; | |
| /** Get Chinese team name, fallback to English. */ | |
| export const cn = (t: string) => CN[t] || t; | |
| /** Chinese confederation names */ | |
| export const CONFED_CN: Record<string, string> = { | |
| UEFA: "欧足联", CONMEBOL: "南美足联", CAF: "非洲足联", | |
| AFC: "亚足联", CONCACAF: "中北美足联", OFC: "大洋洲足联", | |
| }; | |
| // ---- Beijing time (UTC+8) helpers ---- | |
| function bjParts(iso: string) { | |
| const b = new Date(new Date(iso).getTime() + 8 * 3600 * 1000); | |
| const p = (n: number) => String(n).padStart(2, "0"); | |
| return { | |
| date: `${b.getUTCFullYear()}-${p(b.getUTCMonth() + 1)}-${p(b.getUTCDate())}`, | |
| md: `${p(b.getUTCMonth() + 1)}-${p(b.getUTCDate())}`, | |
| hm: `${p(b.getUTCHours())}:${p(b.getUTCMinutes())}`, | |
| }; | |
| } | |
| /** "06-12 03:00" Beijing time. */ | |
| export const bjTime = (iso?: string | null) => (iso ? `${bjParts(iso).md} ${bjParts(iso).hm}` : ""); | |
| /** "06-12" Beijing month-day. */ | |
| export const bjMD = (iso?: string | null) => (iso ? bjParts(iso).md : ""); | |
| /** "03:00" Beijing time. */ | |
| export const bjHM = (iso?: string | null) => (iso ? bjParts(iso).hm : ""); | |
| /** "2026-06-12" Beijing date. */ | |
| export const bjDate = (iso?: string | null) => (iso ? bjParts(iso).date : ""); | |
| /** Today's date in Beijing, "YYYY-MM-DD". */ | |
| export const bjToday = () => { | |
| const b = new Date(Date.now() + 8 * 3600 * 1000); | |
| const p = (n: number) => String(n).padStart(2, "0"); | |
| return `${b.getUTCFullYear()}-${p(b.getUTCMonth() + 1)}-${p(b.getUTCDate())}`; | |
| }; | |
| /** Whole days from Beijing-today to a YYYY-MM-DD date. */ | |
| export const daysUntil = (ymd: string) => { | |
| const [y, m, d] = ymd.split("-").map(Number); | |
| const t = bjToday().split("-").map(Number); | |
| return Math.round((Date.UTC(y, m - 1, d) - Date.UTC(t[0], t[1] - 1, t[2])) / 86400000); | |
| }; | |