Updated api fetch to return an object.

This commit is contained in:
Mike Cao 2020-09-30 15:14:44 -07:00
parent e64b35f701
commit e5cd162b83
10 changed files with 28 additions and 28 deletions

View file

@ -1,6 +1,6 @@
import { getQueryString } from './url';
export const apiRequest = (method, url, body) =>
export const apiRequest = (method, url, body, headers) =>
fetch(url, {
method,
cache: 'no-cache',
@ -8,18 +8,16 @@ export const apiRequest = (method, url, body) =>
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...headers,
},
body,
}).then(res => {
console.log({ res });
if (res.ok) {
return res.json();
return res.json().then(data => ({ ok: res.ok, status: res.status, data }));
}
if (['post', 'put', 'delete'].includes(method)) {
return res.text();
}
return null;
return res.text().then(data => ({ ok: res.ok, status: res.status, res: res, data }));
});
export const get = (url, params) => apiRequest('get', `${url}${getQueryString(params)}`);