Files
rustpad/static/js/api.js
T
2026-07-20 15:59:15 +02:00

16 lines
738 B
JavaScript

export async function api(path, options = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 12000);
try {
const headers = new Headers(options.headers || {});
if (!(options.body instanceof FormData) && !headers.has("content-type")) headers.set("content-type", "application/json");
const response = await fetch(path, { ...options, headers, signal: controller.signal });
const data = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(data.error || `Error ${response.status}`);
return data;
} catch (error) {
if (error.name === "AbortError") throw new Error("Timed out");
throw error;
} finally { clearTimeout(timeout); }
}