move request method out of client class

This commit is contained in:
Izalia Mae 2024-03-15 06:55:52 -04:00
parent 0f3b72830b
commit b068f4f91e
7 changed files with 50 additions and 55 deletions

View file

@ -33,48 +33,43 @@ function append_table_row(table, row_name, row) {
}
class Client {
async request(method, path, body = null) {
var headers = {
"Accept": "application/json"
}
async function request(method, path, body = null) {
var headers = {
"Accept": "application/json"
}
if (body !== null) {
headers["Content-Type"] = "application/json"
body = JSON.stringify(body)
}
if (body !== null) {
headers["Content-Type"] = "application/json"
body = JSON.stringify(body)
}
const response = await fetch("/api/" + path, {
method: method,
mode: "cors",
cache: "no-store",
redirect: "follow",
body: body,
headers: headers
const response = await fetch("/api/" + path, {
method: method,
mode: "cors",
cache: "no-store",
redirect: "follow",
body: body,
headers: headers
});
const message = await response.json();
if (Object.hasOwn(message, "error")) {
throw new Error(message.error);
}
if (Array.isArray(message)) {
message.forEach((msg) => {
if (Object.hasOwn(msg, "created")) {
msg.created = new Date(msg.created);
}
});
const message = await response.json();
if (Object.hasOwn(message, "error")) {
throw new Error(message.error);
} else {
if (Object.hasOwn(message, "created")) {
message.created = new Date(message.created);
}
if (Array.isArray(message)) {
message.forEach((msg) => {
if (Object.hasOwn(msg, "created")) {
msg.created = new Date(msg.created);
}
});
} else {
if (Object.hasOwn(message, "created")) {
message.created = new Date(message.created);
}
}
return message;
}
return message;
}
client = new Client();

View file

@ -15,7 +15,7 @@ async function handle_config_change(event) {
}
try {
await client.request("POST", "v1/config", params);
await request("POST", "v1/config", params);
} catch (error) {
alert(error);

View file

@ -33,7 +33,7 @@ async function ban() {
}
try {
var ban = await client.request("POST", "v1/domain_ban", values);
var ban = await request("POST", "v1/domain_ban", values);
} catch (err) {
alert(err);
@ -69,7 +69,7 @@ async function update_ban(domain) {
}
try {
await client.request("PATCH", "v1/domain_ban", values)
await request("PATCH", "v1/domain_ban", values)
} catch (error) {
alert(error);
@ -82,7 +82,7 @@ async function update_ban(domain) {
async function unban(domain) {
try {
await client.request("DELETE", "v1/domain_ban", {"domain": domain});
await request("DELETE", "v1/domain_ban", {"domain": domain});
} catch (error) {
alert(error);

View file

@ -19,7 +19,7 @@ async function add_instance() {
}
try {
var instance = await client.request("POST", "v1/instance", values);
var instance = await request("POST", "v1/instance", values);
} catch (err) {
alert(err);
@ -44,7 +44,7 @@ async function add_instance() {
async function del_instance(domain) {
try {
await client.request("DELETE", "v1/instance", {"domain": domain});
await request("DELETE", "v1/instance", {"domain": domain});
} catch (error) {
alert(error);
@ -62,7 +62,7 @@ async function req_response(domain, accept) {
}
try {
await client.request("POST", "v1/request", params);
await request("POST", "v1/request", params);
} catch (error) {
alert(error);
@ -79,7 +79,7 @@ async function req_response(domain, accept) {
return;
}
instances = await client.request("GET", `v1/instance`, null);
instances = await request("GET", `v1/instance`, null);
instances.forEach((instance) => {
if (instance.domain === domain) {
append_table_row(document.getElementById("instances"), instance.domain, {

View file

@ -35,7 +35,7 @@ async function ban() {
}
try {
var ban = await client.request("POST", "v1/software_ban", values);
var ban = await request("POST", "v1/software_ban", values);
} catch (err) {
alert(err);
@ -71,7 +71,7 @@ async function update_ban(name) {
}
try {
await client.request("PATCH", "v1/software_ban", values)
await request("PATCH", "v1/software_ban", values)
} catch (error) {
alert(error);
@ -84,7 +84,7 @@ async function update_ban(name) {
async function unban(name) {
try {
await client.request("DELETE", "v1/software_ban", {"name": name});
await request("DELETE", "v1/software_ban", {"name": name});
} catch (error) {
alert(error);

View file

@ -24,7 +24,7 @@ async function add_user() {
}
try {
var user = await client.request("POST", "v1/user", values);
var user = await request("POST", "v1/user", values);
} catch (err) {
alert(err);
@ -49,7 +49,7 @@ async function add_user() {
async function del_user(username) {
try {
await client.request("DELETE", "v1/user", {"username": username});
await request("DELETE", "v1/user", {"username": username});
} catch (error) {
alert(error);

View file

@ -8,16 +8,16 @@ async function add_whitelist() {
}
try {
var item = await client.request("POST", "v1/whitelist", {"domain": domain});
var item = await request("POST", "v1/whitelist", {"domain": domain});
} catch (err) {
alert(err);
return
}
append_table_row(document.getElementById("instances"), item.domain, {
append_table_row(document.getElementById("whitelist"), item.domain, {
domain: item.domain,
date: get_date_string(ban.created),
date: get_date_string(item.created),
remove: `<a href="#" onclick="del_whitelist('${item.domain}')" title="Remove whitelisted domain">&#10006;</a>`
});
@ -28,7 +28,7 @@ async function add_whitelist() {
async function del_whitelist(domain) {
try {
await client.request("DELETE", "v1/whitelist", {"domain": domain});
await request("DELETE", "v1/whitelist", {"domain": domain});
} catch (error) {
alert(error);