Created the RequestUtil.js
This commit is contained in:
parent
1e1129881c
commit
9c076b6679
19
src/common/utils/RequestUtil.js
Normal file
19
src/common/utils/RequestUtil.js
Normal 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}`});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user