From 94db871b67a52d5b00e0a980f6cee8afaf705b9d Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 21 Nov 2022 12:50:44 +0200 Subject: [PATCH] get method for util --- src/util/Util.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/util/Util.js b/src/util/Util.js index f868626..0dedbaf 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -31,15 +31,16 @@ export const fetchUser = async (force = false) => { const parseResponse = async (response) => { const { headers: rawHeaders, status } = response; + // Fetch returns heders as an interable for some reason const headers = [...rawHeaders].reduce((acc, [key, val]) => { acc[key.toLowerCase()] = val; return acc; }, {}); - const success = status >= 200 && status < 400; + const success = status >= 200 && status < 300; const base = { success, status }; if (headers['content-type']?.includes('application/json')) { const data = await response.json(); - return {...base, ...data}; + return {...base, data}; } return { ...base, message: await response.text() }; }; @@ -53,4 +54,12 @@ export const post = async (url, body) => { body: JSON.stringify(body) }); return parseResponse(response); +}; + +export const get = async (url, params) => { + if (params) url += '?' + Object.entries(params) + .map(([key, val]) => `${key}=${val}`) + .join('&'); + const response = await fetch(url); + return parseResponse(response); }; \ No newline at end of file