mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-28 07:50:32 +05:30
#389: fail early in case, when before
request middleware was rejected
This commit is contained in:
parent
e91bb09c33
commit
741639bd2b
@ -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.resolve(resp)
|
||||||
: Promise.reject(resp);
|
: Promise.reject(resp);
|
||||||
const toJSON = (resp = {}) => {
|
const toJSON = (resp = {}) => {
|
||||||
@ -118,7 +118,7 @@ const handleResponseSuccess = (resp) => resp.success || typeof resp.success ===
|
|||||||
? Promise.resolve(resp)
|
? Promise.resolve(resp)
|
||||||
: Promise.reject(resp);
|
: Promise.reject(resp);
|
||||||
|
|
||||||
function doFetch(url, options = {}) {
|
async function doFetch(url, options = {}) {
|
||||||
// NOTE: we are wrapping fetch, because it is returning
|
// NOTE: we are wrapping fetch, because it is returning
|
||||||
// Promise instance that can not be pollyfilled with Promise.prototype.finally
|
// Promise instance that can not be pollyfilled with Promise.prototype.finally
|
||||||
|
|
||||||
@ -126,12 +126,14 @@ function doFetch(url, options = {}) {
|
|||||||
options.headers.Accept = 'application/json';
|
options.headers.Accept = 'application/json';
|
||||||
|
|
||||||
return middlewareLayer.run('before', {url, options})
|
return middlewareLayer.run('before', {url, options})
|
||||||
.then(({url, options}) => fetch(url, options))
|
.then(({url, options}) =>
|
||||||
.then(checkStatus)
|
fetch(url, options)
|
||||||
.then(toJSON, rejectWithJSON)
|
.then(checkStatus)
|
||||||
.then(handleResponseSuccess)
|
.then(toJSON, rejectWithJSON)
|
||||||
.then((resp) => middlewareLayer.run('then', resp, {url, options}))
|
.then(handleResponseSuccess)
|
||||||
.catch((resp) => middlewareLayer.run('catch', resp, {url, options}, () => doFetch(url, options)));
|
.then((resp) => middlewareLayer.run('then', resp, {url, options}))
|
||||||
|
.catch((resp) => middlewareLayer.run('catch', resp, {url, options}, () => doFetch(url, options)))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user