diff --git a/src/common/utils/RequestUtil.js b/src/common/utils/RequestUtil.js new file mode 100644 index 0000000..a83cab0 --- /dev/null +++ b/src/common/utils/RequestUtil.js @@ -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}`}); +} \ No newline at end of file