IMPORTANT: Fresh fetch renamed to Refresh fetch, head over there for latest version
Fresh Fetch
Wrapper around fetch capable of graceful authentication token refreshing.
For situations when there is API which issues authentication tokens on login endpoint, API requires you to add the authentication token to all requests, those tokens must be refreshed every X minutes, and you just want to call fetch
and be abstracted away from the refreshing.
The following ES6 functions are required:
Install
Add to your app using package manager:
yarn add fresh-fetch
npm install fresh-fetch --save
Example
Imagine you have in your app a request to /api/data
which needs authentication/authorization token in Authorization header like this:
// retrieveToken reads the token from cookie, local storage, what have you...const token =
That is all fine and dandy, but what if you have to refresh the token, because it expires every 10 minutes? You will start doing something like this:
// retrieveToken reads the token from cookie, local storage, what have you...const token =
And now you want to have the original request repeated. And also if there is request called during the refreshing, you don't want to start refreshing second time, but you just want to wait for the first refresh to complete and use the new token.
Sigh. That's a lot you don't want to be writing in every app.
With fresh-fetch
you configure 3 parameters, shouldRefreshToken
, refreshToken
and fetch
, and the refreshing works exactly like described. See it in action:
// api.js // fetchJSON is bundled wrapper around fetch which simplifies working// with JSON API:// * Automatically adds Content-Type: application/json to request headers// * Parses response as JSON when Content-Type: application/json header is// present in response headers// * Converts non-success responses (HTTP status code >= 300) to errors // Provide your favorite token saving -- to cookies, local storage, ...const retrieveToken = { /* ... */ }const saveToken = { /* ... */ }const clearToken = { /* ... */ } // Decide whether this error returned from API means that we want// to try refreshing the token. error.response contains the fetch Response// object, error.body contains the parsed JSON response bodyconst shouldRefreshToken = errorresponsestatus === 401 && errorbodymessage === 'Token has expired' // Do the actual token refreshing and update the saved tokenconst refreshToken = { return } // Add authentication header to the request, input and init// are the constructor parameters of Request object in Fetch APIconst fetchJSONWithToken = { const token = let initWithToken = init if token !== null && token !== undefined initWithToken = ; return } shouldRefreshToken refreshToken fetch: fetchJSONWithToken
// myapp.js // This API will be called with Bearer token in Authorization header and if it// returns 401 with message 'Token has expired', request to /api/refresh-token// will be issued and then the request to /api/data will be automatically// repeated with the new token