mirror of
https://git.pleroma.social/pleroma/relay.git
synced 2024-11-22 14:38:00 +00:00
move request method out of client class
This commit is contained in:
parent
0f3b72830b
commit
b068f4f91e
|
@ -33,48 +33,43 @@ function append_table_row(table, row_name, row) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class Client {
|
async function request(method, path, body = null) {
|
||||||
async request(method, path, body = null) {
|
var headers = {
|
||||||
var headers = {
|
"Accept": "application/json"
|
||||||
"Accept": "application/json"
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (body !== null) {
|
if (body !== null) {
|
||||||
headers["Content-Type"] = "application/json"
|
headers["Content-Type"] = "application/json"
|
||||||
body = JSON.stringify(body)
|
body = JSON.stringify(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch("/api/" + path, {
|
const response = await fetch("/api/" + path, {
|
||||||
method: method,
|
method: method,
|
||||||
mode: "cors",
|
mode: "cors",
|
||||||
cache: "no-store",
|
cache: "no-store",
|
||||||
redirect: "follow",
|
redirect: "follow",
|
||||||
body: body,
|
body: body,
|
||||||
headers: headers
|
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();
|
} else {
|
||||||
|
if (Object.hasOwn(message, "created")) {
|
||||||
if (Object.hasOwn(message, "error")) {
|
message.created = new Date(message.created);
|
||||||
throw new Error(message.error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ async function handle_config_change(event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client.request("POST", "v1/config", params);
|
await request("POST", "v1/config", params);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
|
@ -33,7 +33,7 @@ async function ban() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var ban = await client.request("POST", "v1/domain_ban", values);
|
var ban = await request("POST", "v1/domain_ban", values);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(err);
|
alert(err);
|
||||||
|
@ -69,7 +69,7 @@ async function update_ban(domain) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client.request("PATCH", "v1/domain_ban", values)
|
await request("PATCH", "v1/domain_ban", values)
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
@ -82,7 +82,7 @@ async function update_ban(domain) {
|
||||||
|
|
||||||
async function unban(domain) {
|
async function unban(domain) {
|
||||||
try {
|
try {
|
||||||
await client.request("DELETE", "v1/domain_ban", {"domain": domain});
|
await request("DELETE", "v1/domain_ban", {"domain": domain});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
|
@ -19,7 +19,7 @@ async function add_instance() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var instance = await client.request("POST", "v1/instance", values);
|
var instance = await request("POST", "v1/instance", values);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(err);
|
alert(err);
|
||||||
|
@ -44,7 +44,7 @@ async function add_instance() {
|
||||||
|
|
||||||
async function del_instance(domain) {
|
async function del_instance(domain) {
|
||||||
try {
|
try {
|
||||||
await client.request("DELETE", "v1/instance", {"domain": domain});
|
await request("DELETE", "v1/instance", {"domain": domain});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
@ -62,7 +62,7 @@ async function req_response(domain, accept) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client.request("POST", "v1/request", params);
|
await request("POST", "v1/request", params);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
@ -79,7 +79,7 @@ async function req_response(domain, accept) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
instances = await client.request("GET", `v1/instance`, null);
|
instances = await request("GET", `v1/instance`, null);
|
||||||
instances.forEach((instance) => {
|
instances.forEach((instance) => {
|
||||||
if (instance.domain === domain) {
|
if (instance.domain === domain) {
|
||||||
append_table_row(document.getElementById("instances"), instance.domain, {
|
append_table_row(document.getElementById("instances"), instance.domain, {
|
||||||
|
|
|
@ -35,7 +35,7 @@ async function ban() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var ban = await client.request("POST", "v1/software_ban", values);
|
var ban = await request("POST", "v1/software_ban", values);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(err);
|
alert(err);
|
||||||
|
@ -71,7 +71,7 @@ async function update_ban(name) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client.request("PATCH", "v1/software_ban", values)
|
await request("PATCH", "v1/software_ban", values)
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
@ -84,7 +84,7 @@ async function update_ban(name) {
|
||||||
|
|
||||||
async function unban(name) {
|
async function unban(name) {
|
||||||
try {
|
try {
|
||||||
await client.request("DELETE", "v1/software_ban", {"name": name});
|
await request("DELETE", "v1/software_ban", {"name": name});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
|
@ -24,7 +24,7 @@ async function add_user() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var user = await client.request("POST", "v1/user", values);
|
var user = await request("POST", "v1/user", values);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(err);
|
alert(err);
|
||||||
|
@ -49,7 +49,7 @@ async function add_user() {
|
||||||
|
|
||||||
async function del_user(username) {
|
async function del_user(username) {
|
||||||
try {
|
try {
|
||||||
await client.request("DELETE", "v1/user", {"username": username});
|
await request("DELETE", "v1/user", {"username": username});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
|
@ -8,16 +8,16 @@ async function add_whitelist() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var item = await client.request("POST", "v1/whitelist", {"domain": domain});
|
var item = await request("POST", "v1/whitelist", {"domain": domain});
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(err);
|
alert(err);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
append_table_row(document.getElementById("instances"), item.domain, {
|
append_table_row(document.getElementById("whitelist"), item.domain, {
|
||||||
domain: 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">✖</a>`
|
remove: `<a href="#" onclick="del_whitelist('${item.domain}')" title="Remove whitelisted domain">✖</a>`
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ async function add_whitelist() {
|
||||||
|
|
||||||
async function del_whitelist(domain) {
|
async function del_whitelist(domain) {
|
||||||
try {
|
try {
|
||||||
await client.request("DELETE", "v1/whitelist", {"domain": domain});
|
await request("DELETE", "v1/whitelist", {"domain": domain});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(error);
|
alert(error);
|
||||||
|
|
Loading…
Reference in a new issue