get method for util
This commit is contained in:
parent
ae2a4ddbcc
commit
94db871b67
@ -31,15 +31,16 @@ export const fetchUser = async (force = false) => {
|
|||||||
|
|
||||||
const parseResponse = async (response) => {
|
const parseResponse = async (response) => {
|
||||||
const { headers: rawHeaders, status } = response;
|
const { headers: rawHeaders, status } = response;
|
||||||
|
// Fetch returns heders as an interable for some reason
|
||||||
const headers = [...rawHeaders].reduce((acc, [key, val]) => {
|
const headers = [...rawHeaders].reduce((acc, [key, val]) => {
|
||||||
acc[key.toLowerCase()] = val;
|
acc[key.toLowerCase()] = val;
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
const success = status >= 200 && status < 400;
|
const success = status >= 200 && status < 300;
|
||||||
const base = { success, status };
|
const base = { success, status };
|
||||||
if (headers['content-type']?.includes('application/json')) {
|
if (headers['content-type']?.includes('application/json')) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return {...base, ...data};
|
return {...base, data};
|
||||||
}
|
}
|
||||||
return { ...base, message: await response.text() };
|
return { ...base, message: await response.text() };
|
||||||
};
|
};
|
||||||
@ -53,4 +54,12 @@ export const post = async (url, body) => {
|
|||||||
body: JSON.stringify(body)
|
body: JSON.stringify(body)
|
||||||
});
|
});
|
||||||
return parseResponse(response);
|
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);
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user