import axios from 'axios'; import type { Game, Leg, Team, User, AuthResponse } from '../types'; const api = axios.create({ baseURL: 'http://localhost:3001/api', }); api.interceptors.request.use((config) => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }); export const authService = { register: (data: { email: string; password: string; name: string }) => api.post('/auth/register', data), login: (data: { email: string; password: string }) => api.post('/auth/login', data), me: () => api.get('/auth/me'), }; export const gameService = { list: (params?: { search?: string; status?: string }) => api.get('/games', { params }), myGames: () => api.get('/games/my-games'), get: (id: string) => api.get(`/games/${id}`), getByInvite: (code: string) => api.get(`/games/invite/${code}`), create: (data: Partial) => api.post('/games', data), update: (id: string, data: Partial) => api.put(`/games/${id}`, data), delete: (id: string) => api.delete(`/games/${id}`), publish: (id: string) => api.post(`/${id}/publish`), end: (id: string) => api.post(`/${id}/end`), getInvite: (id: string) => api.get<{ inviteCode: string }>(`/games/${id}/invite`), }; export const legService = { getByGame: (gameId: string) => api.get(`/legs/game/${gameId}`), create: (gameId: string, data: Partial) => api.post(`/legs/game/${gameId}`, data), update: (legId: string, data: Partial) => api.put(`/legs/${legId}`, data), delete: (legId: string) => api.delete(`/legs/${legId}`), submitPhoto: (legId: string, data: { teamId: string; photoUrl: string }) => api.post(`/legs/${legId}/photo`, data), }; export const teamService = { getByGame: (gameId: string) => api.get(`/teams/game/${gameId}`), create: (gameId: string, data: { name: string }) => api.post(`/teams/game/${gameId}`, data), get: (teamId: string) => api.get(`/teams/${teamId}`), join: (teamId: string) => api.post(`/teams/${teamId}/join`), leave: (teamId: string) => api.post(`/teams/${teamId}/leave`), advance: (teamId: string) => api.post(`/teams/${teamId}/advance`), deduct: (teamId: string, seconds: number) => api.post(`/teams/${teamId}/deduct`, { seconds }), disqualify: (teamId: string) => api.post(`/teams/${teamId}/disqualify`), updateLocation: (teamId: string, lat: number, lng: number) => api.post(`/teams/${teamId}/location`, { lat, lng }), }; export const uploadService = { upload: (file: File) => { const formData = new FormData(); formData.append('photo', file); return api.post<{ url: string }>('/upload/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }); }, }; export default api;