Created the RequestUtil.js

This commit is contained in:
Mathias Wagner 2023-07-30 17:18:45 +02:00
parent 1e1129881c
commit 9c076b6679
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -0,0 +1,19 @@
const URL = process.env.NODE_ENV === "production" ? "https://api.licenseapi.de" : "/api";
export const request = async (url, method, body, headers) => {
const response = await fetch(URL + url, {
method: method,
headers: {...headers, "Content-Type": "application/json"},
body: JSON.stringify(body)
});
if (response.status === 401) throw new Error("Unauthorized");
if (!response.ok) throw new Error(await response.text());
return await response.json();
}
export const sessionRequest = (url, method, token, body) => {
return request(url, method, body, {"Authorization": `Bearer ${token}`});
}