From 4702402484a773c0eccf7415d0318e367fb996e1 Mon Sep 17 00:00:00 2001 From: Alexis Hovorka Date: Sat, 27 Jul 2024 19:43:38 -0600 Subject: [refactor] Try out some client-side UI libraries --- app/public/api.js | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 app/public/api.js (limited to 'app/public/api.js') diff --git a/app/public/api.js b/app/public/api.js new file mode 100644 index 0000000..6321909 --- /dev/null +++ b/app/public/api.js @@ -0,0 +1,91 @@ +export async function getNote(id) { + const res = await fetch(`${id}`); + const note = await res.json(); + //console.log(note); + return note; +} + +export async function getList() { + const res = await fetch("/list"); + const list = await res.json(); + //console.log(list); + + return Promise.all(list.map(id => getNote(id))); +} + +export async function saveNote(id, content) { + const res = await fetch("/"+id, { + method: "POST", + body: JSON.stringify({id, content}), + }); + + const note = await res.json(); + //console.log(note); +} + +export async function newNote() { + const res = await fetch("/new", { + method: "POST", + }); + + const note = await res.json(); + //console.log(note); + return note; +} + +export async function getUserData() { + const res = await fetch(`/user`); + const user = await res.json(); + //console.log(user); + return user; +} + +export async function getUserSessions() { + const res = await fetch(`/session-list`); + const sessions = await res.json(); + //console.log(sessions); + return sessions; +} + +export async function signIn({username, password, keepSession}) { + const res = await fetch("/sign-in", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({username, password, keepSession}) + }); + + if (res.ok) return res.json(); + return {code: res.status, error: res.statusText}; // TODO +} + +export async function changePassword({username, password, newPassword, keepSession}) { + const res = await fetch("/change-password", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({username, password, newPassword, keepSession}) + }); + + return res.json(); +} + +export async function changeUsername({newUsername, password}) { + const res = await fetch("/change-username", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({newUsername, password}) + }); + + return res.json(); +} + +export async function signOut() { + const res = await fetch("/sign-out", { + method: "POST", + }); +} + +export async function signOutEverywhere() { + const res = await fetch("/deauth-all", { + method: "POST", + }); +} -- cgit v1.2.3-70-g09d2