Download frontend/src/api/client.ts from xunwei/fitAI: direct link, hf CLI and curl.
- Browser
- Download file 3.37 kB
-
https://huggingface.co/spaces/xunwei/fitAI/resolve/main/frontend/src/api/client.ts
- Command line
-
hf download hf://spaces/xunwei/fitAI/frontend/src/api/client.ts
-
curl -L -o client.ts https://huggingface.co/spaces/xunwei/fitAI/resolve/main/frontend/src/api/client.ts
3.37 kB
| const BASE = '/api' | |
| function getToken(): string | null { | |
| return localStorage.getItem('token') | |
| } | |
| async function request<T>(path: string, options: RequestInit = {}): Promise<T> { | |
| const token = getToken() | |
| const headers: Record<string, string> = { | |
| 'Content-Type': 'application/json', | |
| ...(options.headers as Record<string, string>), | |
| } | |
| if (token) headers['Authorization'] = `Bearer ${token}` | |
| const res = await fetch(`${BASE}${path}`, { ...options, headers }) | |
| if (!res.ok) { | |
| const err = await res.json().catch(() => ({ detail: res.statusText })) | |
| throw new Error(err.detail || 'Request failed') | |
| } | |
| return res.json() | |
| } | |
| // ββ Auth ββββββββββββββββββββββββββββββββββββββββββββββ | |
| export interface Token { access_token: string; token_type: string } | |
| export interface UserOut { id: string; email: string; coach_persona: string } | |
| export const auth = { | |
| register: (email: string, password: string, coach_persona = 'encouraging') => | |
| request<Token>('/auth/register', { method: 'POST', body: JSON.stringify({ email, password, coach_persona }) }), | |
| login: (email: string, password: string) => | |
| request<Token>('/auth/login', { method: 'POST', body: JSON.stringify({ email, password }) }), | |
| me: () => request<UserOut>('/auth/me'), | |
| updatePersona: (coach_persona: string) => | |
| request<UserOut>('/auth/me/persona', { method: 'PATCH', body: JSON.stringify({ coach_persona }) }), | |
| } | |
| // ββ Workouts ββββββββββββββββββββββββββββββββββββββββββ | |
| export interface Exercise { id: number; workout_id: number; name: string; sets?: number; reps?: number; weight_kg?: number; distance_km?: number } | |
| export interface Workout { id: number; user_id: string; date: string; type: string; duration_minutes: number; notes?: string; exercises: Exercise[] } | |
| export interface WorkoutCreate { type: string; duration_minutes: number; notes?: string; exercises?: Omit<Exercise, 'id' | 'workout_id'>[] } | |
| export const workouts = { | |
| list: () => request<Workout[]>('/workouts'), | |
| create: (data: WorkoutCreate) => request<Workout>('/workouts', { method: 'POST', body: JSON.stringify(data) }), | |
| delete: (id: number) => request<{ ok: boolean }>(`/workouts/${id}`, { method: 'DELETE' }), | |
| } | |
| // ββ Health Metrics ββββββββββββββββββββββββββββββββββββ | |
| export interface HealthMetric { id: number; user_id: string; date: string; weight_kg?: number; sleep_hours?: number; energy_level?: number } | |
| export interface HealthMetricCreate { weight_kg?: number; sleep_hours?: number; energy_level?: number } | |
| export const health = { | |
| list: () => request<HealthMetric[]>('/health-metrics'), | |
| create: (data: HealthMetricCreate) => request<HealthMetric>('/health-metrics', { method: 'POST', body: JSON.stringify(data) }), | |
| delete: (id: number) => request<{ ok: boolean }>(`/health-metrics/${id}`, { method: 'DELETE' }), | |
| } | |
| // ββ Chat ββββββββββββββββββββββββββββββββββββββββββββββ | |
| export const chat = { | |
| send: (message: string) => | |
| request<{ reply: string }>('/chat', { method: 'POST', body: JSON.stringify({ message }) }), | |
| } | |