diff --git a/client/src/common/utils/RequestUtil.js b/client/src/common/utils/RequestUtil.js new file mode 100644 index 00000000..0632f1fd --- /dev/null +++ b/client/src/common/utils/RequestUtil.js @@ -0,0 +1,51 @@ +const API_ROOT = "/api"; + +// Get the default headers of the request +const getHeaders = () => { + let headers = localStorage.getItem("password") ? {password: localStorage.getItem("password")} : {}; + headers['content-type'] = 'application/json'; + + return headers; +} + +// 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 + }); +} + +// 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 PATCH request update a resource +export const patchRequest = async (path, body, headers = {}) => { + return await request(path, "PATCH", body, headers); +} + +// Run a DELETE request and delete a resource +export const deleteRequest = async (path, body, headers = {}) => { + return await request(path, "DELETE", body, headers); +} + +// Download a specific file from the response output +export const downloadRequest = async (path, body, headers = {}) => { + const file = await request(path, "GET", body, headers); + let element = document.createElement('a'); + let url = file.headers.get('Content-Disposition').split('filename=')[1]; + element.setAttribute("download", url.replaceAll("\"", "")); + + const blob = await file.blob(); + element.href = window.URL.createObjectURL(blob); + document.body.appendChild(element); + element.click(); + element.remove(); +} \ No newline at end of file