mirror of
https://github.com/nfonteyne/octane-website.git
synced 2026-09-03 23:24:48 +02:00
first commit
This commit is contained in:
commit
244cdbedd7
44 changed files with 3386 additions and 0 deletions
24
public/js/api.js
Normal file
24
public/js/api.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
async function apiFetch(path, options = {}) {
|
||||
const res = await fetch(path, {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
...options,
|
||||
});
|
||||
if (res.status === 401) {
|
||||
window.location.href = '/auth/login?returnTo=' + encodeURIComponent(window.location.pathname);
|
||||
return new Promise(() => {});
|
||||
}
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.message || body.error || `Request failed: ${res.status}`);
|
||||
}
|
||||
if (res.status === 204) return null;
|
||||
return res.json();
|
||||
}
|
||||
|
||||
const api = {
|
||||
get: (path) => apiFetch(path),
|
||||
post: (path, data) => apiFetch(path, { method: 'POST', body: JSON.stringify(data) }),
|
||||
patch: (path, data) => apiFetch(path, { method: 'PATCH', body: JSON.stringify(data) }),
|
||||
put: (path, data) => apiFetch(path, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
del: (path) => apiFetch(path, { method: 'DELETE' }),
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue