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