Imported the RequestUtil.js

This commit is contained in:
2022-12-27 15:18:11 +01:00
parent eedf9241f7
commit f7ece96e97

View File

@ -0,0 +1,32 @@
const API_ROOT = "/api";
// Get the default headers of the request
const getHeaders = () => ({'content-type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}`});
// Run a plain request with all default values
export const request = async (path, method = "GET", body = {}, headers = {}) => {
return await fetch(API_ROOT + path, {
headers: {...getHeaders(), ...headers}, method,
body: method !== "GET" ? JSON.stringify(body) : undefined
});
}
// Creates a new token / session
export const createSession = async (username, password) => {
return (await request("/auth/login", "POST", {username, password})).headers.get("Authorization");
}
// Run a GET request and get the json of the response
export const jsonRequest = async (path, headers = {}) => {
return (await request(path, "GET", null, headers)).json();
}
// Run a POST request and post some values
export const postRequest = async (path, body, headers = {}) => {
return await request(path, "POST", body, headers);
}
// Run a DELETE request and delete a resource
export const deleteRequest = async (path, body, headers = {}) => {
return await request(path, "DELETE", body, headers);
}