/
aaronair
/
Test_task_rt_solution
Обзор
Документация
Войти
/
aaronair
/
Test_task_rt_solution
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/features/documents/api.ts
60 строк
2 KB
Ilikedrinkpivo
first_commit
28 май 2026, 14:51
28 май 2026, 14:51
b4498fc
Код
Авторство
О чём код?
import { apiFetch } from "@/lib/api"; import type { InitiativeDocument } from "@/features/initiatives/types"; export interface DocumentWithUrl extends InitiativeDocument { id?: string; downloadUrl?: string; } export async function listDocuments(initiativeId: string): Promise<DocumentWithUrl[]> { const res = await apiFetch<{ data: DocumentWithUrl[] }>( `/initiatives/${encodeURIComponent(initiativeId)}/documents`, ); return res.data; } export async function uploadDocument( initiativeId: string, file: File, ): Promise<DocumentWithUrl> { const ext = file.name.split(".").pop()?.toLowerCase() ?? ""; const { upload_url, s3_key } = await apiFetch<{ upload_url: string; s3_key: string; }>(`/initiatives/${encodeURIComponent(initiativeId)}/documents/upload-url`, { method: "POST", body: JSON.stringify({ name: file.name, type: file.type || `application/${ext}`, size_kb: Math.ceil(file.size / 1024), }), }); const putRes = await fetch(upload_url, { method: "PUT", body: file, headers: { "Content-Type": file.type || "application/octet-stream" }, }); if (!putRes.ok) { throw new Error(`S3 upload failed: ${putRes.status}`); } return apiFetch(`/initiatives/${encodeURIComponent(initiativeId)}/documents`, { method: "POST", body: JSON.stringify({ s3_key, name: file.name, type: ext, size_kb: Math.ceil(file.size / 1024), }), }); } export async function deleteDocument(initiativeId: string, docId: string) { return apiFetch( `/initiatives/${encodeURIComponent(initiativeId)}/documents/${docId}`, { method: "DELETE" }, ); }