feature: the beginnings of chat working

This commit is contained in:
Andy Burke 2025-07-01 15:37:35 -07:00
parent 85024c6e62
commit 649ff432bb
24 changed files with 1555 additions and 918 deletions

33
public/js/api.js Normal file
View file

@ -0,0 +1,33 @@
const api = {
fetch: async function (url, options = { method: "GET" }) {
const session_id = (document.cookie.match(
/^(?:.*;)?\s*session_id\s*=\s*([^;]+)(?:.*)?$/,
) || [, null])[1];
// TODO: this wasn't really intended to be persisted in a cookie
const session_secret = (document.cookie.match(
/^(?:.*;)?\s*session_secret\s*=\s*([^;]+)(?:.*)?$/,
) || [, null])[1];
const headers = {
Accept: "application/json",
"x-session_id": session_id,
"x-totp": await otp_totp(session_secret),
...(options.headers ?? {}),
};
const fetch_options = {
method: options.method,
headers,
};
if (options.json) {
headers["Content-Type"] = "application/json";
fetch_options.body = JSON.stringify(options.json);
}
const response = await fetch(`/api${url}`, fetch_options);
return response;
},
};