From ced9914185c3b660e37d7b19971ae48528de8115 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Thu, 1 Jun 2023 19:41:02 +0200 Subject: [PATCH] Created the RequestUtil.js --- client/src/common/utils/RequestUtil.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 client/src/common/utils/RequestUtil.js diff --git a/client/src/common/utils/RequestUtil.js b/client/src/common/utils/RequestUtil.js new file mode 100644 index 0000000..b4392f4 --- /dev/null +++ b/client/src/common/utils/RequestUtil.js @@ -0,0 +1,15 @@ +const URL = process.env.NODE_ENV === 'production' ? "https://tools-api.gnmyt.dev/" : "http://localhost:7182/"; + +// Run a plain request with all default values +export const request = (path, method = "GET", body = {}, headers = {}) => { + return fetch(URL + path, {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 = {}) => { + try { + return (await request(path, "GET", null, headers)).json(); + } catch (e) { + return null; + } +} \ No newline at end of file