← Назад
const API_BASE = import.meta.env.VITE_API_BASE || 'https://dashboard.szhub.space/api'; export const getToken = () => localStorage.getItem('dashboard_token') || ''; const authHeaders = (): Record<string, string> => { const token = getToken(); return token ? { 'Authorization': `Bearer ${token}` } : {}; }; // Drop-in fetch replacement that adds auth header — use everywhere instead of fetch() export const authFetch = (url: string, options: RequestInit = {}) => fetch(url, { ...options, headers: { ...authHeaders(), ...(options.headers as Record<string, string> || {}) }, }); const apiFetch = async (url: string, options: RequestInit = {}) => { const res = await fetch(url, { ...options, headers: { ...authHeaders(), ...(options.headers as Record<string, string> || {}) }, }); if (res.status === 401) { localStorage.removeItem('dashboard_token'); window.location.reload(); throw new Error('Unauthorized'); } if (!res.ok) throw new Error(`Request failed: ${res.status}`); return res.json(); }; export async function fetchProjects() { return apiFetch(`${API_BASE}/projects`); } export async function fetchProject(id: string) { return apiFetch(`${API_BASE}/projects/${id}`); } export async function fetchSystem() { return apiFetch(`${API_BASE}/system`); } export async function fetchTasks(projectId: string) { return apiFetch(`${API_BASE}/projects/${projectId}/kanban-tasks`); } export async function restartService(service: string) { return apiFetch(`${API_BASE}/actions/restart`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ service }), }); }