/
perminevma
/
frontend
Обзор
Документация
Войти
/
perminevma
/
frontend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/api.js
102 строки
3 KB
ivgrd
1
26 авг 2025, 11:44
26 авг 2025, 11:44
d65cc04
Код
Авторство
О чём код?
// frontend/src/api.js import axios from "axios"; /** * Этот файл должен лежать строго в src/api.js */ const axiosInstance = axios.create({ timeout: 60000, }); const API_AUTH = "/api/auth"; const API_INGEST = "/api/ingest"; // Токен -> Authorization const authHeader = () => { const token = localStorage.getItem("token"); return token ? { Authorization: `Bearer ${token}` } : {}; }; // ====================== AUTH ====================== // Логин (OAuth2 Password) export const loginUser = (username, password) => axiosInstance.post( `${API_AUTH}/login`, new URLSearchParams({ username, password }) ); // Текущий пользователь export const getMe = () => axiosInstance.get(`${API_AUTH}/me`, { headers: { ...authHeader() }, }); /** * Создать пользователя (только админ). * Совместимо со старым вызовом: * createUser({ username, password, is_admin }) * и с новым: * createUser({ username, password, role }) // admin | diagnostic_engineer | viewer * * Если передан role='admin', is_admin будет проставлен в true. */ export const createUser = ({ username, password, is_admin, role }) => { const body = { username, password, }; if (role) { body.role = role; body.is_admin = role === "admin"; } else { body.is_admin = !!is_admin; } return axiosInstance.post(`${API_AUTH}/users`, body, { headers: { "Content-Type": "application/json", ...authHeader() }, }); }; // Список пользователей (только админ) export const listUsers = () => axiosInstance.get(`${API_AUTH}/users`, { headers: { ...authHeader() }, }); // Удалить пользователя (только админ) export const deleteUser = (id) => axiosInstance.delete(`${API_AUTH}/users/${id}`, { headers: { ...authHeader() }, }); // ====================== INGEST ====================== // Список файлов export const listFiles = () => axiosInstance.get(`${API_INGEST}/files`, { headers: { ...authHeader() }, }); // Детали файла export const getFileDetail = (id) => axiosInstance.get(`${API_INGEST}/files/${id}`, { headers: { ...authHeader() }, }); // Загрузка файла export const uploadFile = (file) => { const form = new FormData(); form.append("file", file); return axiosInstance.post(`${API_INGEST}/upload`, form, { headers: { "Content-Type": "multipart/form-data", ...authHeader() }, }); }; // Поставить файл в очередь export const enqueueFile = (id) => axiosInstance.post(`${API_INGEST}/files/${id}/enqueue`, null, { headers: { ...authHeader() }, });