Updated api fetch to return an object.
This commit is contained in:
12
lib/web.js
12
lib/web.js
@@ -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)}`);
|
||||
|
||||
Reference in New Issue
Block a user