/
dmitriysturov
/
workspaces_platform
Обзор
Документация
Войти
/
dmitriysturov
/
workspaces_platform
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/src/api/client.ts
49 строк
1 KB
dmitriysturov
vkr
04 июн 2026, 22:24
04 июн 2026, 22:24
971dfdc
Код
Авторство
О чём код?
import type { ApiErrorResponse } from '../types' const API_BASE_URL = (import.meta.env.VITE_API_BASE_URL as string | undefined) ?? 'http://localhost:8080' export class ApiError extends Error { status: number validationErrors?: Record<string, string> constructor(message: string, status: number, validationErrors?: Record<string, string>) { super(message) this.name = 'ApiError' this.status = status this.validationErrors = validationErrors } } export async function apiRequest<T>(path: string, init?: RequestInit): Promise<T> { const isFormData = init?.body instanceof FormData const response = await fetch(`${API_BASE_URL}${path}`, { ...init, headers: { ...(isFormData ? {} : { 'Content-Type': 'application/json' }), ...(init?.headers ?? {}), }, }) if (!response.ok) { let message = `Request failed with status ${response.status}` let validationErrors: Record<string, string> | undefined try { const errorBody = (await response.json()) as ApiErrorResponse message = errorBody.message ?? message validationErrors = errorBody.validationErrors } catch { // ignore parse errors and keep default message } throw new ApiError(message, response.status, validationErrors) } if (response.status === 204) { return undefined as T } return (await response.json()) as T } export { API_BASE_URL }