From 6f37ca843bf7193ec4547e24dd5aa91f75bcd408 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 6 May 2020 18:59:27 +0300 Subject: [PATCH] api internalFetch refactor --- src/api/api.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/api/api.js b/src/api/api.js index b5d3697..3f67f4a 100644 --- a/src/api/api.js +++ b/src/api/api.js @@ -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) {