Nashboard/static/js/nashboard.js

108 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-12-14 02:27:39 +03:00
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}`;
2022-12-14 02:27:39 +03:00
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 };
}
}
async function createFolder(parent, name) {
if (name == "root") {
return { success: false, err: "can't create folder with that name" };
}
const r = request("f", "POST", {
ParentID: parent,
Name: name,
});
try {
const result = await fetch(r);
return { success: true };
} catch (err) {
console.error(err);
return { success: false, err };
}
}
async function createBookmark(parent, bookmark) {
const r = request("bookmark", "POST", bookmark);
try {
const result = await fetch(r);
return { success: true };
} catch (err) {
console.error(err);
return { success: false, err };
}
}
async function deleteFolder(id) {
const r = request(`f/${id}`, "DELETE");
try {
const result = await fetch(r);
return { success: true };
} catch (err) {
console.error(err);
return { success: false, err };
}
}
async function deleteBookmark(id) {
const r = request(`bookmark/${id}`, "DELETE");
try {
const result = await fetch(r);
return { success: true };
} catch (err) {
console.error(err);
return { success: false, err };
}
}