Umbau auf Alpine.js
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
const BASE = "/api";
|
||||
|
||||
async function request(path, options = {}) {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
...options
|
||||
});
|
||||
if (!res.ok) {
|
||||
let message = `Fehler ${res.status}`;
|
||||
try {
|
||||
const body = await res.json();
|
||||
if (body.error) message = body.error;
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
||||
if (res.status === 204) return null;
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export const api = {
|
||||
people: {
|
||||
list: () => request("/people"),
|
||||
create: (data) => request("/people", { method: "POST", body: JSON.stringify(data) }),
|
||||
update: (id, data) =>
|
||||
request(`/people/${id}`, { method: "PUT", body: JSON.stringify(data) }),
|
||||
remove: (id) => request(`/people/${id}`, { method: "DELETE" })
|
||||
},
|
||||
projects: {
|
||||
list: () => request("/projects"),
|
||||
create: (data) => request("/projects", { method: "POST", body: JSON.stringify(data) }),
|
||||
update: (id, data) =>
|
||||
request(`/projects/${id}`, { method: "PUT", body: JSON.stringify(data) }),
|
||||
remove: (id) => request(`/projects/${id}`, { method: "DELETE" })
|
||||
},
|
||||
tasks: {
|
||||
list: () => request("/tasks"),
|
||||
create: (data) => request("/tasks", { method: "POST", body: JSON.stringify(data) }),
|
||||
update: (id, data) =>
|
||||
request(`/tasks/${id}`, { method: "PUT", body: JSON.stringify(data) }),
|
||||
remove: (id) => request(`/tasks/${id}`, { method: "DELETE" })
|
||||
},
|
||||
absences: {
|
||||
list: () => request("/absences"),
|
||||
create: (data) => request("/absences", { method: "POST", body: JSON.stringify(data) }),
|
||||
remove: (id) => request(`/absences/${id}`, { method: "DELETE" })
|
||||
},
|
||||
capacity: {
|
||||
get: (from, to) => request(`/capacity?from=${from}&to=${to}`)
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user