/
Danik-Off
/
ProjectV
Обзор
Документация
Войти
/
Danik-Off
/
ProjectV
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
frontend/src/utils/apiClient.ts
47 строк
1 KB
Ovchinnikov Danila
feat: новый voicecontrol, удаление сервера и еще что то
07 июл 2025, 03:28
07 июл 2025, 03:28
d3bb7c9
Код
Авторство
О чём код?
import { API_URL } from '../configs/apiConfig'; import { authStore } from '../store/authStore'; import { getCookie } from './cookie'; export const apiClient = async ( endpoint: string, options: RequestInit = {}, body?: any ) => { const token = getCookie('token'); // Установка заголовков const headers = { 'Content-Type': 'application/json', ...(token && { Authorization: `Bearer ${token}` }), ...options.headers, }; // Создание объекта запроса const requestOptions: RequestInit = { ...options, headers, }; // Если тело запроса передано, сериализуем его в JSON if (body) { requestOptions.body = JSON.stringify(body); } const response = await fetch(`${API_URL}${endpoint}`, requestOptions); if (!response.ok) { const errorMessage = await response.text(); if (errorMessage === '{"error":"Недействительный токен."}') { authStore.logout(); } throw new Error(errorMessage); } // Для ответов с кодом 204 (No Content) не пытаемся парсить JSON if (response.status === 204) { return null; } return response.json(); };