Files
takeyourmeds/public/lib/api.js
T
2026-07-08 08:25:34 +02:00

34 lines
1.3 KiB
JavaScript

import { getToken, clearToken } from "./auth.js";
async function request(path, options = {}) {
const headers = { "Content-Type": "application/json", ...(options.headers || {}) };
const token = getToken();
if (token) headers.Authorization = `Bearer ${token}`;
const resp = await fetch(path, { ...options, headers });
if (resp.status === 401) {
clearToken();
window.location.reload();
throw new Error("Unauthorized");
}
if (!resp.ok) {
const err = await resp.json().catch(() => ({}));
throw new Error(err.detail || resp.statusText);
}
return resp.json();
}
export const api = {
login: (pin) => request("/api/auth/pin", { method: "POST", body: JSON.stringify({ pin }) }),
config: () => request("/api/config"),
today: () => request("/api/today"),
log: (data) => request("/api/log", { method: "POST", body: JSON.stringify(data) }),
history: (days = 90) => request(`/api/history?days=${days}`),
stats: () => request("/api/stats"),
roast: () => request("/api/roast"),
snooze: (slot_id, minutes) => request("/api/snooze", { method: "POST", body: JSON.stringify({ slot_id, minutes }) }),
vapidKey: () => request("/api/vapid-public-key"),
pushSubscribe: (subscription) =>
request("/api/push/subscribe", { method: "POST", body: JSON.stringify({ subscription }) }),
};