Files
mctf/dark-tariff/frontend/src/api/client.js
T
TakuyaYagam1 13f936bee9 chore: import MCTF services
- Added all seven issued service trees without omissions.
- Preserved the supplied runtime configuration, binaries, and assets.
2026-11-29 12:25:29 +03:00

35 lines
1.1 KiB
JavaScript
Executable File

function jsonPost(body) {
return { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) };
}
function responseError(result, fallback) {
return new Error(typeof result.detail === "string" ? result.detail : fallback);
}
export async function api(path, opts = {}) {
const response = await fetch(path, opts);
const result = await response.json();
if (!response.ok) throw responseError(result, "Проверьте поля запроса.");
return result;
}
export function post(path, body) {
return api(path, jsonPost(body));
}
export async function download(path, body, fallback) {
const response = await fetch(path, jsonPost(body));
if (!response.ok) {
const result = await response.json();
throw responseError(result, "Не удалось скачать отчёт.");
}
const url = URL.createObjectURL(await response.blob());
const link = document.createElement("a");
link.href = url;
link.download = response.headers.get("content-disposition")?.match(/filename="?([^";]+)"?/i)?.[1] || fallback;
document.body.append(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}