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); }