api internalFetch refactor

master
Tudor Stanciu 2020-05-06 18:59:27 +03:00
parent 542a332f40
commit 6f37ca843b
1 changed files with 10 additions and 14 deletions

View File

@ -5,20 +5,16 @@ function getHeaders() {
return headers;
}
function internalFetch(url, options) {
return fetch(url, options).then((res) =>
res.text().then((text) => {
let t = text ? JSON.parse(text) : res.statusText;
if (!res.ok) {
if (res.status === 404) throw new Error(t || "Not found");
if (res.status >= 400 && res.status < 600 && t) throw t;
throw new Error(t || "Unknown error");
}
return t;
})
);
async function internalFetch(url, options) {
const res = await fetch(url, options);
const text = await res.text();
const t = text ? JSON.parse(text) : res.statusText;
if (!res.ok) {
if (res.status === 404) throw new Error(t || "Not found");
if (res.status >= 400 && res.status < 600 && t) throw t;
throw new Error(t || "Unknown error");
}
return t;
}
export function post(url, body) {