/
effective_wiki
/
web-client
Обзор
Документация
Войти
/
effective_wiki
/
web-client
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
src/api/utils.ts
50 строк
1 KB
Max-Sam231
fix: workspace role after name change
20 дек 2025, 13:28
20 дек 2025, 13:28
4fc02c4
Код
Авторство
О чём код?
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL; const headerConst = { "Content-Type": "application/json", } as const; export async function apiRequest<T>(endpoint: string, options: RequestInit = {}): Promise<T> { const url = `${API_BASE_URL}${endpoint}`; const response = await fetch(url, { headers: headerConst, ...options, }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } if (response.status === 204) { return undefined as T; } const text = await response.text(); if (!text) { return undefined as T; } return JSON.parse(text) as T; } export async function apiGet<T>(endpoint: string): Promise<T> { return apiRequest<T>(endpoint, { method: "GET" }); } export async function apiPost<T>(endpoint: string, body?: unknown): Promise<T> { return apiRequest<T>(endpoint, { method: "POST", body: body ? JSON.stringify(body) : undefined, }); } export async function apiPut<T>(endpoint: string, body?: unknown): Promise<T> { return apiRequest<T>(endpoint, { method: "PUT", body: body ? JSON.stringify(body) : undefined, }); } export async function apiDelete<T>(endpoint: string): Promise<T> { return apiRequest<T>(endpoint, { method: "DELETE" }); }