Spaces:
Paused
Paused
File size: 3,742 Bytes
c562e21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | // Пусто = тот же origin (фронт отдаётся самим API → один туннель для Telegram).
// Для dev в браузере (Vite на :5173) задай VITE_API_URL=http://localhost:3000 в apps/web/.env
const BASE_URL = import.meta.env.VITE_API_URL ?? '';
async function request(path, options = {}) {
const token = localStorage.getItem('token');
const headers = {
'Content-Type': 'application/json',
...(options.headers || {}),
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const config = {
...options,
headers,
};
try {
const response = await fetch(`${BASE_URL}${path}`, config);
let data = {};
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
data = await response.json();
}
if (!response.ok) {
if (data.error) {
throw data.error;
}
throw {
code: 'HTTP_ERROR',
message: data.message || `Request failed with status ${response.status}`,
};
}
return data;
} catch (error) {
console.error(`API Error on ${path}:`, error);
if (error.code && error.message) {
throw error;
}
throw {
code: 'NETWORK_ERROR',
message: error.message || 'Ошибка сети или бэкенд недоступен',
};
}
}
export const api = {
// Auth
authTelegram: (initData) => request('/auth/telegram', {
method: 'POST',
body: JSON.stringify({ initData }),
}),
authRegister: (email, password, name) => request('/auth/register', {
method: 'POST',
body: JSON.stringify({ email, password, name }),
}),
authLogin: (email, password) => request('/auth/login', {
method: 'POST',
body: JSON.stringify({ email, password }),
}),
authMe: () => request('/auth/me', {
method: 'GET',
}),
// Spaces
getSpaces: () => request('/spaces', {
method: 'GET',
}),
createSpace: (name, type) => request('/spaces', {
method: 'POST',
body: JSON.stringify({ name, type }),
}),
getSpace: (id) => request(`/spaces/${id}`, {
method: 'GET',
}),
joinSpace: (id) => request(`/spaces/${id}/join`, {
method: 'POST',
}),
getSpaceMembers: (id) => request(`/spaces/${id}/members`, {
method: 'GET',
}),
// Finance
getTransactions: (spaceId) => request(`/spaces/${spaceId}/transactions`, {
method: 'GET',
}),
createTransaction: (spaceId, { title, amount, paid_by, split }) => request(`/spaces/${spaceId}/transactions`, {
method: 'POST',
body: JSON.stringify({ title, amount, paid_by, split }),
}),
getBalance: (spaceId) => request(`/spaces/${spaceId}/balance`, {
method: 'GET',
}),
// Tasks
getTasks: (spaceId) => request(`/spaces/${spaceId}/tasks`, {
method: 'GET',
}),
createTask: (spaceId, { title, assignee_id, due_at }) => request(`/spaces/${spaceId}/tasks`, {
method: 'POST',
body: JSON.stringify({ title, assignee_id, due_at }),
}),
updateTask: (spaceId, taskId, { done, assignee_id, title }) => request(`/spaces/${spaceId}/tasks/${taskId}`, {
method: 'PATCH',
body: JSON.stringify({ done, assignee_id, title }),
}),
// Feed (posts)
getPosts: (spaceId) => request(`/spaces/${spaceId}/posts`, {
method: 'GET',
}),
createPost: (spaceId, { text }) => request(`/spaces/${spaceId}/posts`, {
method: 'POST',
body: JSON.stringify({ text }),
}),
// Calendar (events)
getEvents: (spaceId) => request(`/spaces/${spaceId}/events`, {
method: 'GET',
}),
createEvent: (spaceId, { title, start_at }) => request(`/spaces/${spaceId}/events`, {
method: 'POST',
body: JSON.stringify({ title, start_at }),
}),
};
|