feat: simplified api module handler logic, content-type detection/parsing

isekai-main
Julian Lam 2 years ago
parent e72fab5417
commit 2d016af82f

@ -63,20 +63,26 @@ async function xhr(options, cb) {
delete options.data; delete options.data;
} }
await fetch(url, { const res = await fetch(url, options);
...options, const { headers } = res;
}).then(async (res) => { const isJSON = headers.get('content-type').startsWith('application/json');
const response = await res.json();
if (Math.floor(res.status / 100) === 2) { let response;
cb(null, ( if (isJSON) {
response && response = await res.json();
response.hasOwnProperty('status') && } else {
response.hasOwnProperty('response') ? response.response : (response || {}) response = await res.text();
)); }
} else {
cb(new Error(response.status.message)); if (!res.ok) {
} return cb(new Error(isJSON ? response.status.message : response));
}).catch(cb); }
cb(null, (
isJSON && response.hasOwnProperty('status') && response.hasOwnProperty('response') ?
response.response :
response
));
} }
export function get(route, data, onSuccess) { export function get(route, data, onSuccess) {

Loading…
Cancel
Save