Files
toolbox/api/static/js/api.js
T
2025-12-07 20:55:17 +03:00

21 lines
503 B
JavaScript

// api.js
export async function apiRequest(url, payload = {}, method = 'POST') {
const finalUrl = new URL(url, window.location.origin).toString();
const res = await fetch(finalUrl, {
method,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(payload),
credentials: 'same-origin'
});
if (!res.ok) {
const text = await res.text();
throw new Error(`HTTP ${res.status}: ${text}`);
}
return res.json();
}