- Replace stale TanStack Router scaffold with working react-router-dom app - Add all 9 page components + cyber theme CSS - Remove routeTree.gen.ts, add index.css, query.ts
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
const API_BASE = "/api";
|
|
|
|
async function request(path: string, opts: RequestInit = {}) {
|
|
const res = await fetch(API_BASE + path, {
|
|
...opts,
|
|
credentials: "include",
|
|
});
|
|
if (res.status === 401) {
|
|
window.location.href = "/login";
|
|
throw new Error("Unauthorized");
|
|
}
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(text || "HTTP " + res.status);
|
|
}
|
|
if (res.status === 204) return null;
|
|
return res.json();
|
|
}
|
|
|
|
export const api = {
|
|
login: (mailbox: string, pin: string) =>
|
|
request("/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ mailbox, pin }),
|
|
}),
|
|
logout: () => request("/logout", { method: "POST" }),
|
|
messages: (params?: Record<string, string>) => {
|
|
const qs = new URLSearchParams(params).toString();
|
|
return request("/messages" + (qs ? "?" + qs : ""));
|
|
},
|
|
toggleRead: (id: number) => request("/messages/" + id + "/read", { method: "POST" }),
|
|
deleteMessage: (id: number) => request("/messages/" + id + "/delete", { method: "POST" }),
|
|
settings: () => request("/settings"),
|
|
saveSettings: (formData: FormData) =>
|
|
request("/settings", { method: "POST", body: formData }),
|
|
contacts: (q?: string) =>
|
|
request("/contacts" + (q ? "?q=" + encodeURIComponent(q) : "")),
|
|
getContact: (id: number) => request("/contacts/" + id),
|
|
createContact: (data: { name: string; number?: string; email?: string | null }) =>
|
|
request("/contacts", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
}),
|
|
updateContact: (id: number, data: { name: string; number?: string; email?: string | null }) =>
|
|
request("/contacts/" + id, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
}),
|
|
deleteContact: (id: number) => request("/contacts/" + id, { method: "DELETE" }),
|
|
importVcf: () => request("/contacts/import_vcf", { method: "POST" }),
|
|
contactHistory: (num: string) =>
|
|
request("/contacts/history/" + encodeURIComponent(num)),
|
|
addContact: (data: { number?: string; name: string; email?: string | null }) =>
|
|
request("/add_contact", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
}),
|
|
getTranscript: (id: number) => request("/transcript/" + id),
|
|
};
|