/
theosov
/
Radiola
Обзор
Документация
Войти
/
theosov
/
Radiola
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
web/src/api/client.ts
42 строки
1 KB
Theosov
P0
25 июл 2026, 01:31
25 июл 2026, 01:31
1f4159e
Код
Авторство
О чём код?
import { retrieveRawInitData } from "@telegram-apps/sdk"; const API_BASE = import.meta.env.VITE_API_BASE_URL || ""; function getInitData(): string { try { return retrieveRawInitData() || ""; } catch { return ""; } } export interface ApiError { code: string; message: string; } export class RadiolaError extends Error { constructor(public readonly api: ApiError) { super(api.message); } } export async function apiFetch<T>(path: string, init: RequestInit = {}): Promise<T> { const initData = getInitData(); const headers: Record<string, string> = { "Content-Type": "application/json", ...(initData ? { "X-Telegram-Init-Data": initData } : {}), ...(init.headers as Record<string, string>), }; const res = await fetch(`${API_BASE}${path}`, { ...init, headers }); const body = await res.json().catch(() => ({})); if (!res.ok) { const err = (body as { error?: ApiError }).error || { code: "upstream", message: res.statusText }; throw new RadiolaError(err); } return body as T; } export type HealthResponse = { status: string }; export type MeResponse = { user_id: number };