/
stasnorman
/
cif_platform
Обзор
Документация
Войти
/
stasnorman
/
cif_platform
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
client/src/api.ts
63 строки
2 KB
Stas Makievsky
first_commit
30 окт 2025, 21:04
30 окт 2025, 21:04
53daf0f
Код
Авторство
О чём код?
const API = '/api'; async function jfetch(url: string, init: RequestInit = {}) { const res = await fetch(url, { ...init, headers: { 'Content-Type': 'application/json', ...(init.headers || {}) }, credentials: 'include' // куки всегда отправляются }); if (!res.ok) { const err: any = new Error(`HTTP ${res.status}`); err.status = res.status; throw err; } return res.json(); } // админ export function adminLogin(login: string, password: string) { return jfetch(`${API}/admin/login`, { method: 'POST', body: JSON.stringify({ login, password }) }); } export function adminLogout() { return jfetch(`${API}/admin/logout`, { method: 'POST' }); } export function createTest(payload: any) { return jfetch(`${API}/test`, { method: 'POST', body: JSON.stringify(payload) }).then(r => r.code as string); } export function getResults(code: string) { return jfetch(`${API}/results/${code}`); } export function setBonuses(code: string, fio: string, bonuses: { questionId: string, value: number }[]) { return jfetch(`${API}/bonus`, { method: 'POST', body: JSON.stringify({ code, fio, bonuses }) }); } // синоним для старого импорта export const awardBonus = setBonuses; // пользователь export function userLogin(code: string, fio: string) { return jfetch(`${API}/user/login`, { method: 'POST', body: JSON.stringify({ code, fio }) }); } export function userLogout() { return jfetch(`${API}/user/logout`, { method: 'POST' }); } export function getTest(code: string) { return jfetch(`${API}/test/${code}`); } export function submitAnswers(payload: { code: string; answers: any[]; notes: Record<string,string> }) { return jfetch(`${API}/submit`, { method: 'POST', body: JSON.stringify(payload) }); } export function listTests() { return jfetch('/api/tests'); // вернёт { tests: [...] } } export function getTestAdmin(code: string) { return jfetch(`/api/test-admin/${code}`); } export function updateTest(code: string, payload: any) { return jfetch(`/api/test/${code}`, { method: 'PUT', body: JSON.stringify(payload) }); }