Skip to content

Commit

Permalink
fix: revert async/await to Promise for removing regenerator-runtime deps
Browse files Browse the repository at this point in the history
closes #54
  • Loading branch information
nodkz committed Nov 14, 2017
1 parent 71b39a1 commit 5a1970f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
29 changes: 17 additions & 12 deletions src/fetchWithMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
MiddlewareNextFn,
} from './definition';

async function runFetch(req: RRNLRequestObject): Promise<RRNLResponseObject> {
function runFetch(req: RRNLRequestObject): Promise<RRNLResponseObject> {
let { url, ...opts } = req;

if (!url) {
Expand All @@ -20,17 +20,22 @@ async function runFetch(req: RRNLRequestObject): Promise<RRNLResponseObject> {
}
}

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(
Expand Down
56 changes: 29 additions & 27 deletions src/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
};
}

0 comments on commit 5a1970f

Please sign in to comment.