sto-k-odnomu/src/services/api.js
2026-01-11 01:17:30 +03:00

57 lines
2 KiB
JavaScript

import axios from 'axios';
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000';
const api = axios.create({
baseURL: API_URL,
withCredentials: true,
});
// Auth endpoints
export const authApi = {
createAnonymous: (name) => api.post('/auth/anonymous', { name }),
register: (email, password, name) => api.post('/auth/register', { email, password, name }),
login: (email, password) => api.post('/auth/login', { email, password }),
updateName: (token, name) =>
api.patch('/auth/user', { name }, {
headers: { Authorization: `Bearer ${token}` }
}),
};
// Rooms endpoints
export const roomsApi = {
create: (hostId, questionPackId, settings, hostName) =>
api.post('/rooms', { hostId, questionPackId, settings, hostName }),
getByCode: (code, password, userId) => {
const params = {};
if (password) params.password = password;
if (userId) params.userId = userId;
return api.get(`/rooms/${code}`, { params });
},
join: (roomId, userId, name, role) =>
api.post(`/rooms/${roomId}/join`, { userId, name, role }),
updateQuestionPack: (roomId, questionPackId) =>
api.patch(`/rooms/${roomId}/question-pack`, { questionPackId }),
updateRoomPack: (roomId, questions) =>
api.patch(`/rooms/${roomId}/room-pack`, { questions }),
getRoomPack: (roomId) =>
api.get(`/rooms/${roomId}/room-pack`),
};
// Questions endpoints
export const questionsApi = {
createPack: (data) => api.post('/questions/packs', data),
getPacks: (userId) => api.get('/questions/packs', { params: { userId } }),
getPack: (id) => api.get(`/questions/packs/${id}`),
updatePack: (id, data) => api.put(`/questions/packs/${id}`, data),
deletePack: (id) => api.delete(`/questions/packs/${id}`),
};
// Stats endpoints
export const statsApi = {
createHistory: (data) => api.post('/stats/game-history', data),
getHistory: (userId) => api.get(`/stats/game-history/${userId}`),
getUserStats: (userId) => api.get(`/stats/user/${userId}`),
};
export default api;