TreasureTrails/frontend/src/services/api.ts

72 lines
3 KiB
TypeScript

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<AuthResponse>('/auth/register', data),
login: (data: { email: string; password: string }) =>
api.post<AuthResponse>('/auth/login', data),
me: () => api.get<User>('/auth/me'),
};
export const gameService = {
list: (params?: { search?: string; status?: string }) =>
api.get<Game[]>('/games', { params }),
myGames: () => api.get<Game[]>('/games/my-games'),
get: (id: string) => api.get<Game>(`/games/${id}`),
getByInvite: (code: string) => api.get<Game>(`/games/invite/${code}`),
create: (data: Partial<Game>) => api.post<Game>('/games', data),
update: (id: string, data: Partial<Game>) => api.put<Game>(`/games/${id}`, data),
delete: (id: string) => api.delete(`/games/${id}`),
publish: (id: string) => api.post<Game>(`/games/${id}/publish`),
end: (id: string) => api.post<Game>(`/games/${id}/end`),
archive: (id: string) => api.post<Game>(`/games/${id}/archive`),
unarchive: (id: string) => api.post<Game>(`/games/${id}/unarchive`),
getInvite: (id: string) => api.get<{ inviteCode: string }>(`/games/${id}/invite`),
};
export const legService = {
getByGame: (gameId: string) => api.get<Leg[]>(`/legs/game/${gameId}`),
create: (gameId: string, data: Partial<Leg>) => api.post<Leg>(`/legs/game/${gameId}`, data),
update: (legId: string, data: Partial<Leg>) => api.put<Leg>(`/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<Team[]>(`/teams/game/${gameId}`),
create: (gameId: string, data: { name: string }) => api.post<Team>(`/teams/game/${gameId}`, data),
get: (teamId: string) => api.get<Team>(`/teams/${teamId}`),
join: (teamId: string) => api.post(`/teams/${teamId}/join`),
leave: (teamId: string) => api.post(`/teams/${teamId}/leave`),
advance: (teamId: string) => api.post<Team>(`/teams/${teamId}/advance`),
deduct: (teamId: string, seconds: number) => api.post<Team>(`/teams/${teamId}/deduct`, { seconds }),
disqualify: (teamId: string) => api.post<Team>(`/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;