kdjskdjsk
This commit is contained in:
@@ -196,24 +196,33 @@ function dragonrollEndpoint(path) {
|
||||
|
||||
const apiBase = new URL(endpoint);
|
||||
const basePath = apiBase.pathname.replace(/\/+$/, "");
|
||||
const isLocalBackend = ["localhost", "127.0.0.1", "[::1]"].includes(apiBase.hostname)
|
||||
&& apiBase.port === "5000";
|
||||
const hasApiPath = basePath.endsWith("/api");
|
||||
apiBase.pathname = `${hasApiPath || isLocalBackend ? basePath : `${basePath}/api`}/`;
|
||||
apiBase.pathname = `${hasApiPath ? basePath : `${basePath}/api`}/`;
|
||||
apiBase.search = "";
|
||||
apiBase.hash = "";
|
||||
return new URL(path.replace(/^\/+/, ""), apiBase);
|
||||
}
|
||||
|
||||
// A Dragonroll that is not there, or a URL that points at the web app instead of
|
||||
// the API, answers with HTML. Say that plainly instead of a JSON parse error.
|
||||
async function readDragonrollJson(response, url) {
|
||||
try {
|
||||
return await response.json();
|
||||
} catch {
|
||||
throw new Error(`Dragonroll answered HTTP ${response.status} at ${url} but not with JSON. Check the Dragonroll Endpoint setting.`);
|
||||
}
|
||||
}
|
||||
|
||||
async function postDragonroll(path, body, token = "") {
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
const response = await fetch(dragonrollEndpoint(path), {
|
||||
const url = dragonrollEndpoint(path);
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const result = await response.json();
|
||||
const result = await readDragonrollJson(response, url);
|
||||
if (!response.ok || result.status !== "ok") {
|
||||
throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`);
|
||||
}
|
||||
@@ -221,10 +230,11 @@ async function postDragonroll(path, body, token = "") {
|
||||
}
|
||||
|
||||
async function getDragonroll(path, token) {
|
||||
const response = await fetch(dragonrollEndpoint(path), {
|
||||
const url = dragonrollEndpoint(path);
|
||||
const response = await fetch(url, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
const result = await response.json();
|
||||
const result = await readDragonrollJson(response, url);
|
||||
if (!response.ok || result.status !== "ok") {
|
||||
throw new Error(result.msg || `Dragonroll returned HTTP ${response.status}.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user