This commit is contained in:
Mateusz Gruszczyński
2026-07-20 15:59:15 +02:00
parent 771494671b
commit dfa0828c38
42 changed files with 964 additions and 755 deletions
+6 -10
View File
@@ -2,18 +2,14 @@ export async function api(path, options = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 12000);
try {
const response = await fetch(path, {
...options,
headers: { "content-type": "application/json", ...(options.headers || {}) },
signal: controller.signal,
});
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 || `Błąd ${response.status}`);
if (!response.ok) throw new Error(data.error || `Error ${response.status}`);
return data;
} catch (error) {
if (error.name === "AbortError") throw new Error("Przekroczono czas odpowiedzi serwera");
if (error.name === "AbortError") throw new Error("Timed out");
throw error;
} finally {
clearTimeout(timeout);
}
} finally { clearTimeout(timeout); }
}