A wrapper around the fetch API that automatically handles authentication token refreshing. Current stable version is 0.9.0. It intercepts fetch responses, detects token expiry via a configurable `shouldRefreshToken` callback, performs token refresh via a `refreshToken` function, and retries the original request. Unlike manual token management, refresh-fetch abstracts the retry logic and ensures concurrent requests are serialized during token refresh. Requires ES6 Promise and fetch. Suitable for single-page apps using bearer tokens with expiry.
npm install refresh-fetchNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates configuring refresh-fetch with fetchJSON, handling 401 errors, and making a request.
Change import from `import fetchJSON from 'refresh-fetch'` to `import { fetchJSON } from 'refresh-fetch'`Rename `retryToken` to `refreshToken` in configuration
Always use the returned function for authenticated requests; do not call original fetch
Always attach a .catch handler to avoid unhandled promise rejections
Pass a fetch polyfill like node-fetch: `import fetch from 'node-fetch'; const rf = configureRefreshFetch({ fetch, ... })`Use optional chaining: `error.response?.status === 401`
Pass `window.fetch.bind(window)` or use an arrow function wrapper `(url, opts) => fetch(url, opts)`