From 5a1970f1bf56272b6fc8b53e1e23fe66c9431c86 Mon Sep 17 00:00:00 2001 From: nodkz Date: Tue, 14 Nov 2017 09:57:51 +0600 Subject: [PATCH] fix: revert async/await to Promise for removing regenerator-runtime deps closes #54 --- src/fetchWithMiddleware.js | 29 ++++++++++++-------- src/middleware/auth.js | 56 ++++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/fetchWithMiddleware.js b/src/fetchWithMiddleware.js index 2b90637..67f1e01 100644 --- a/src/fetchWithMiddleware.js +++ b/src/fetchWithMiddleware.js @@ -9,7 +9,7 @@ import type { MiddlewareNextFn, } from './definition'; -async function runFetch(req: RRNLRequestObject): Promise { +function runFetch(req: RRNLRequestObject): Promise { let { url, ...opts } = req; if (!url) { @@ -20,17 +20,22 @@ async function runFetch(req: RRNLRequestObject): Promise { } } - const res = await fetch(url, opts); - - if (res.status < 200 || res.status >= 300) { - const text = await res.text(); - const err: any = new Error(text); - err.fetchResponse = res; - throw err; - } - - const payload = await res.json(); - return { ...res, payload }; + return fetch(url, opts) + .then(res => { + if (res.status < 200 || res.status >= 300) { + return res.text().then(text => { + const err: any = new Error(text); + err.fetchResponse = res; + throw err; + }); + } + return res; + }) + .then(res => { + return res.json().then(payload => { + return { ...res, payload }; + }); + }); } export default function fetchWithMiddleware( diff --git a/src/middleware/auth.js b/src/middleware/auth.js index 025283c..7b1407c 100644 --- a/src/middleware/auth.js +++ b/src/middleware/auth.js @@ -34,42 +34,44 @@ export default function authMiddleware(opts?: AuthMiddlewareOpts): Middleware { let tokenRefreshInProgress = null; - return next => async req => { - try { + return next => req => { + return new Promise((resolve, reject) => { // $FlowFixMe - const token = await (isFunction(tokenOrThunk) ? tokenOrThunk(req) : tokenOrThunk); + const token: string = isFunction(tokenOrThunk) ? tokenOrThunk(req) : tokenOrThunk; if (!token && tokenRefreshPromise && !allowEmptyToken) { - throw new WrongTokenError('Empty token'); + reject(new WrongTokenError('Empty token')); } + resolve(token); + }) + .then(token => { + if (token) { + req.headers[header] = `${prefix}${token}`; + } + return next(req); + }) + .catch(e => { + if (e && tokenRefreshPromise) { + if (e.message === 'Empty token' || (e.fetchResponse && e.fetchResponse.status === 401)) { + if (tokenRefreshPromise) { + if (!tokenRefreshInProgress) { + tokenRefreshInProgress = Promise.resolve( + tokenRefreshPromise(req, e.fetchResponse) + ).then(newToken => { + tokenRefreshInProgress = null; + return newToken; + }); + } - if (token) { - req.headers[header] = `${prefix}${token}`; - } - const res = await next(req); - return res; - } catch (e) { - if (e && tokenRefreshPromise) { - if (e.message === 'Empty token' || (e.fetchResponse && e.fetchResponse.status === 401)) { - if (tokenRefreshPromise) { - if (!tokenRefreshInProgress) { - tokenRefreshInProgress = Promise.resolve( - tokenRefreshPromise(req, e.fetchResponse) - ).then(newToken => { - tokenRefreshInProgress = null; - return newToken; + return tokenRefreshInProgress.then(newToken => { + req.headers[header] = `${prefix}${newToken}`; + return next(req); // re-run query with new token }); } - - return tokenRefreshInProgress.then(newToken => { - req.headers[header] = `${prefix}${newToken}`; - return next(req); // re-run query with new token - }); } } - } - throw e; - } + throw e; + }); }; }