/
PashigorevAY
/
TestFastAPI
Обзор
Документация
Войти
/
PashigorevAY
/
TestFastAPI
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/src/api.ts
122 строки
4 KB
Pashigorev
Initial commit
05 июл 2026, 21:43
05 июл 2026, 21:43
19770a6
Код
Авторство
О чём код?
import { PeopleData, StatusesData, Task, Catalog, PathItem, PathsData } from './types'; const API_BASE = 'http://localhost:8000/api'; export const api = { async getPeople(): Promise<PeopleData> { const response = await fetch(`${API_BASE}/people`); if (!response.ok) throw new Error('Failed to fetch people'); return response.json(); }, async savePeople(data: PeopleData): Promise<void> { const response = await fetch(`${API_BASE}/people`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Failed to save people'); }, async getStatuses(): Promise<StatusesData> { const response = await fetch(`${API_BASE}/statuses`); if (!response.ok) throw new Error('Failed to fetch statuses'); return response.json(); }, async saveStatuses(data: StatusesData): Promise<void> { const response = await fetch(`${API_BASE}/statuses`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); if (!response.ok) throw new Error('Failed to save statuses'); }, async getCatalogs(): Promise<{ catalogs: Catalog[] }> { const response = await fetch(`${API_BASE}/tasks/catalogs`); if (!response.ok) throw new Error('Failed to fetch catalogs'); return response.json(); }, async createCatalog(folderName: string, description: string): Promise<void> { const response = await fetch(`${API_BASE}/tasks/catalogs`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ folder_name: folderName, description }), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Failed to create catalog'); } }, async getTasks(catalogName: string): Promise<{ tasks: Task[] }> { const response = await fetch(`${API_BASE}/tasks/catalogs/${catalogName}/tasks`); if (!response.ok) throw new Error('Failed to fetch tasks'); return response.json(); }, async getTask(catalogName: string, taskId: string): Promise<Task> { const response = await fetch(`${API_BASE}/tasks/catalogs/${catalogName}/tasks/${taskId}`); if (!response.ok) throw new Error('Failed to fetch task'); return response.json(); }, async createTask(catalogName: string): Promise<Task> { const response = await fetch(`${API_BASE}/tasks/catalogs/${catalogName}/tasks`, { method: 'POST', }); if (!response.ok) throw new Error('Failed to create task'); return response.json(); }, async saveTask(catalogName: string, taskId: string, task: Task): Promise<void> { const response = await fetch(`${API_BASE}/tasks/catalogs/${catalogName}/tasks/${taskId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(task), }); if (!response.ok) throw new Error('Failed to save task'); }, async getPaths(): Promise<PathsData> { const response = await fetch(`${API_BASE}/paths`); if (!response.ok) throw new Error('Failed to fetch paths'); return response.json(); }, async createPath(name: string, tasksGroup: string): Promise<void> { const response = await fetch(`${API_BASE}/paths`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, tasks_group: tasksGroup }), }); if (!response.ok) { const error = await response.text(); throw new Error(error || 'Failed to create path'); } }, async savePath(pathName: string, pathItem: PathItem): Promise<void> { const response = await fetch(`${API_BASE}/paths/${encodeURIComponent(pathName)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(pathItem), }); if (!response.ok) throw new Error('Failed to save path'); }, async deletePath(pathName: string): Promise<void> { const response = await fetch(`${API_BASE}/paths/${encodeURIComponent(pathName)}`, { method: 'DELETE', }); if (!response.ok) throw new Error('Failed to delete path'); }, async getTasksGroups(): Promise<{ groups: string[] }> { const response = await fetch(`${API_BASE}/tasks-groups`); if (!response.ok) throw new Error('Failed to fetch tasks groups'); return response.json(); }, };