api internalFetch refactor
parent
542a332f40
commit
6f37ca843b
|
@ -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;
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue