filebrowser/frontend/src/api/utils.js

43 lines
856 B
JavaScript
Raw Normal View History

2021-03-21 11:51:58 +00:00
import store from "@/store";
import { renew } from "@/utils/auth";
import { baseURL } from "@/utils/constants";
2021-03-21 11:51:58 +00:00
export async function fetchURL(url, opts) {
opts = opts || {};
opts.headers = opts.headers || {};
2021-03-21 11:51:58 +00:00
let { headers, ...rest } = opts;
const res = await fetch(`${baseURL}${url}`, {
headers: {
2021-03-21 11:51:58 +00:00
"X-Auth": store.state.jwt,
...headers,
},
2021-03-21 11:51:58 +00:00
...rest,
});
2021-03-21 11:51:58 +00:00
if (res.headers.get("X-Renew-Token") === "true") {
await renew(store.state.jwt);
}
2021-03-21 11:51:58 +00:00
return res;
}
2021-03-21 11:51:58 +00:00
export async function fetchJSON(url, opts) {
const res = await fetchURL(url, opts);
if (res.status === 200) {
2021-03-21 11:51:58 +00:00
return res.json();
} else {
2021-03-21 11:51:58 +00:00
throw new Error(res.status);
}
}
2021-03-21 11:51:58 +00:00
export function removePrefix(url) {
url = url.split("/").splice(2).join("/");
2021-03-21 11:51:58 +00:00
if (url === "") url = "/";
if (url[0] !== "/") url = "/" + url;
return url;
}