From 741639bd2b7831dc7bb2ef9e6e940fcf9842087d Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Mon, 12 Feb 2018 22:53:46 +0200 Subject: [PATCH] #389: fail early in case, when `before` request middleware was rejected --- src/services/request/request.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/services/request/request.js b/src/services/request/request.js index d71038e..54435c7 100644 --- a/src/services/request/request.js +++ b/src/services/request/request.js @@ -90,7 +90,7 @@ export default { } }; -const checkStatus = (resp) => resp.status >= 200 && resp.status < 300 +const checkStatus = (resp: Response) => resp.status >= 200 && resp.status < 300 ? Promise.resolve(resp) : Promise.reject(resp); const toJSON = (resp = {}) => { @@ -118,7 +118,7 @@ const handleResponseSuccess = (resp) => resp.success || typeof resp.success === ? Promise.resolve(resp) : Promise.reject(resp); -function doFetch(url, options = {}) { +async function doFetch(url, options = {}) { // NOTE: we are wrapping fetch, because it is returning // Promise instance that can not be pollyfilled with Promise.prototype.finally @@ -126,12 +126,14 @@ function doFetch(url, options = {}) { options.headers.Accept = 'application/json'; return middlewareLayer.run('before', {url, options}) - .then(({url, options}) => fetch(url, options)) - .then(checkStatus) - .then(toJSON, rejectWithJSON) - .then(handleResponseSuccess) - .then((resp) => middlewareLayer.run('then', resp, {url, options})) - .catch((resp) => middlewareLayer.run('catch', resp, {url, options}, () => doFetch(url, options))); + .then(({url, options}) => + fetch(url, options) + .then(checkStatus) + .then(toJSON, rejectWithJSON) + .then(handleResponseSuccess) + .then((resp) => middlewareLayer.run('then', resp, {url, options})) + .catch((resp) => middlewareLayer.run('catch', resp, {url, options}, () => doFetch(url, options))) + ); } /**