/
kochkatech
/
CorpGame
Обзор
Документация
Войти
/
kochkatech
/
CorpGame
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
master
frontend/src/api/client.ts
31 строка
1 KB
kochkareal
chore: добавлен .gitattributes, нормализованы переносы строк (LF везде, кроме .bat/.cmd)
06 авг 2026, 15:18
06 авг 2026, 15:18
c25ef1b
Код
Авторство
О чём код?
const BASE = '/api'; export class ApiError extends Error { constructor(message: string, public status: number) { super(message); } } async function request<T>(path: string, options: RequestInit = {}): Promise<T> { const res = await fetch(`${BASE}${path}`, { credentials: 'include', headers: { 'Content-Type': 'application/json', ...(options.headers || {}) }, ...options, }); const isJson = res.headers.get('content-type')?.includes('application/json'); const body = isJson ? await res.json().catch(() => null) : null; if (!res.ok) { const message = (body && (body.message as string)) || `Ошибка запроса (${res.status})`; throw new ApiError(Array.isArray(message) ? message.join(', ') : message, res.status); } return body as T; } export const api = { get: <T>(path: string) => request<T>(path), post: <T>(path: string, data?: unknown) => request<T>(path, { method: 'POST', body: data ? JSON.stringify(data) : undefined }), patch: <T>(path: string, data?: unknown) => request<T>(path, { method: 'PATCH', body: data ? JSON.stringify(data) : undefined }), delete: <T>(path: string) => request<T>(path, { method: 'DELETE' }), };