/
Sturon
/
Diplom
Обзор
Документация
Войти
/
Sturon
/
Diplom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/src/api.ts
136 строк
4 KB
Sturon
Initial
03 июн 2026, 15:01
03 июн 2026, 15:01
17b44a2
Код
Авторство
О чём код?
import type { CameraRequest, CameraResponse, InspectionItemDetails, InspectionItemSummary, ProfileDetails, ProfileRequest, ProfileStats, ProfileSummary, ReferenceImageResponse, Roi, SystemStatus, } from './types'; const API_BASE = (import.meta.env.VITE_API_BASE as string | undefined)?.replace(/\/$/, '') ?? ''; class ApiError extends Error { status: number; constructor(message: string, status: number) { super(message); this.status = status; } } async function request<T>(path: string, init?: RequestInit): Promise<T> { const response = await fetch(`${API_BASE}${path}`, { headers: { Accept: 'application/json', ...(init?.body instanceof FormData ? {} : { 'Content-Type': 'application/json' }), ...init?.headers, }, ...init, }); if (!response.ok) { let message = `Request failed with status ${response.status}`; try { const data = await response.json(); message = data.error ?? message; } catch { message = response.statusText || message; } throw new ApiError(message, response.status); } if (response.status === 204) { return undefined as T; } const contentType = response.headers.get('content-type') ?? ''; const contentLength = response.headers.get('content-length'); if ((contentLength === '0' || !contentType.includes('application/json')) && response.status !== 201) { return undefined as T; } return response.json() as Promise<T>; } export function toAbsoluteMediaUrl(url?: string | null): string { if (!url) { return ''; } if (url.startsWith('http://') || url.startsWith('https://')) { return url; } return `${API_BASE}${url}`; } export const api = { getProfiles: () => request<ProfileSummary[]>('/api/profiles'), getProfile: (id: number) => request<ProfileDetails>(`/api/profiles/${id}`), createProfile: (payload: ProfileRequest) => request<ProfileDetails>('/api/profiles', { method: 'POST', body: JSON.stringify(payload), }), updateProfile: (id: number, payload: ProfileRequest) => request<ProfileDetails>(`/api/profiles/${id}`, { method: 'PUT', body: JSON.stringify(payload), }), deleteProfile: (id: number) => request<void>(`/api/profiles/${id}`, { method: 'DELETE' }), runProfile: (id: number) => request<ProfileDetails>(`/api/profiles/${id}/run`, { method: 'POST' }), stopProfile: (id: number) => request<ProfileDetails>(`/api/profiles/${id}/stop`, { method: 'POST' }), getProfileItems: (id: number) => request<InspectionItemSummary[]>(`/api/profiles/${id}/items`), getProfileStats: (id: number) => request<ProfileStats>(`/api/profiles/${id}/stats`), getCameras: () => request<CameraResponse[]>('/api/cameras'), createCamera: (payload: CameraRequest) => request<CameraResponse>('/api/cameras', { method: 'POST', body: JSON.stringify(payload), }), updateCamera: (id: number, payload: CameraRequest) => request<CameraResponse>(`/api/cameras/${id}`, { method: 'PUT', body: JSON.stringify(payload), }), deleteCamera: (id: number) => request<void>(`/api/cameras/${id}`, { method: 'DELETE' }), getReferenceImages: () => request<ReferenceImageResponse[]>('/api/reference-images'), getReferenceImage: (id: number) => request<ReferenceImageResponse>(`/api/reference-images/${id}`), uploadReferenceImage: (name: string, file: File) => { const formData = new FormData(); formData.append('file', file); if (name.trim()) { formData.append('name', name.trim()); } return request<ReferenceImageResponse>('/api/reference-images', { method: 'POST', body: formData, }); }, updateReferenceRoi: (id: number, rois: Roi[]) => request<ReferenceImageResponse>(`/api/reference-images/${id}/roi`, { method: 'PUT', body: JSON.stringify({ rois }), }), deleteReferenceImage: (id: number) => request<void>(`/api/reference-images/${id}`, { method: 'DELETE' }), getInspectionItem: (id: number) => request<InspectionItemDetails>(`/api/inspection-items/${id}`), getRejected: (profileId?: number) => request<InspectionItemSummary[]>(`/api/rejected${profileId ? `?profileId=${profileId}` : ''}`), getSystemStatus: () => request<SystemStatus>('/api/system-status'), }; export { ApiError };