Nashboard/static/js/nashboard.js

58 lines
1.3 KiB
JavaScript

const baseUrl = "/api/";
function getSession() {
return localStorage.getItem("SessionID");
}
function request(url, method = "GET", data) {
let headers = [{ "Content-Type": "application/json" }];
const token = getSession();
if (token) headers.push({ SessionID: token });
let body = null;
if (method != "GET") {
body = JSON.stringify(data);
}
const r = new Request(baseUrl + url, {
method,
body,
headers: {
"Content-Type": "application/json",
SessionID: token,
},
});
return r;
}
function get(request) {}
async function login(username, password) {
const r = request("auth/login", "POST", { username, password });
try {
const result = await fetch(r);
if (result.status == 200) {
const token = await result.json();
localStorage.setItem("SessionID", token);
return true;
}
console.error({ status: result.status, body: await result.json() });
return false;
} catch (err) {
console.error(err);
return false;
}
}
async function getFolder(id) {
let url = "f";
if (id) url = `f/{id}`;
const r = request(url);
try {
const result = await fetch(r);
const folder = await result.json();
return { success: true, folder };
} catch (err) {
console.error(err);
return { success: false, err };
}
}