Registry / devops / fetch-compose

fetch-compose

JSON →
library1.0.1jsnpmunverified

A set of higher-order functions for the WHATWG Fetch API, providing composable middleware such as JSON handling, query string injection, timeout, retry (via vercel/fetch-retry), and JWT token support. Version 1.0.1 is the latest stable release, last updated in 2020. It re-exports cross-fetch for cross-platform compatibility (browser, Node, React Native) and includes TypeScript definitions. Key differentiators: opinionated default configuration via createFetch(), first-class JWT support with token refresh and request retry, and a functional composition approach inspired by higher-order functions.

npm install fetch-compose
INSTALL
IMPORT
SIG · FETCH-COMPOSE
F
fetch-compose
devopsjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createFetch
import createFetch from 'fetch-compose'
const createFetch = require('fetch-compose').default
Default export provides the composed fetch with JSON, query, throw-on-non-ok, and timeout middleware
fetch
import { fetch } from 'fetch-compose'
import fetch from 'fetch-compose'
Named export for the bare cross-fetch (with AbortController polyfill only when needed)
withRetry
import { withRetry } from 'fetch-compose'
Re-exported from fetch-retry
withJSON
import { withJSON } from 'fetch-compose'
import withJSON from 'fetch-compose'
Named HOF to automatically set Content-Type and parse JSON responses
withTimeout
import { withTimeout } from 'fetch-compose'
Named HOF that takes a timeout duration (default 10s) and uses AbortController
withJWTToken
import { withJWTToken } from 'fetch-compose'
Named HOF that requires a provider object with getToken and optional refreshToken

Creates a composed fetch with default middleware (JSON, query, throw on non-ok, timeout) and shows usage with options like query parameters and JSON body.

import createFetch, { withRetry } from 'fetch-compose'; // Create the default fetch with JSON support, query params, throw on non-ok, and 10s timeout. const fetch = createFetch(); // Or add retry capability: // const fetch = withRetry(createFetch()); async function main() { try { const response = await fetch('https://api.example.com/data', { method: 'POST', body: { key: 'value' }, query: { token: process.env.API_TOKEN ?? '' } }); console.log(await response.json()); } catch (error) { console.error('Fetch failed:', error); } } main();
Debug
Known issues
breakingfetch-compose versions prior to 1.0.0 had different export names (e.g., 'withJson' instead of 'withJSON').
fix
Update imports to use camelCase with uppercase JSON: import { withJSON } from 'fetch-compose'
affects: <1.0.0
deprecatedThe default export createFetch with no arguments uses a hardcoded timeout of 10 seconds. This may not be suitable for all use cases.
fix
Pass custom timeout via withTimeout: createFetch({ timeout: 5000 }) or use withTimeout(fetch, 5000)
affects: >=1.0.0
gotchawithJWTToken requires a provider object with getToken() and optionally refreshToken(). Without refreshToken, token expiration will not automatically retry.
fix
Ensure provider implements refreshToken() if you want automatic retry on 401 responses.
affects: >=1.0.0
gotchaThe provided fetch implementation uses cross-fetch, which adds node-fetch polyfill. On Node.js 18+ with native fetch, this may cause unexpected behavior if you also have native fetch.
fix
Consider using only native fetch on modern Node.js versions, or explicitly import cross-fetch directly.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetch is not a function
Importing the named export { fetch } incorrectly as default.
fix
Use import { fetch } from 'fetch-compose' (named import) instead of import fetch from 'fetch-compose'.
ReferenceError: AbortController is not defined
Older Node.js (<14) or some browsers lack global AbortController.
fix
Ensure you are using fetch-compose which polyfills AbortController via abortcontroller-polyfill, or install it separately.
Error: The user aborted a request.
AbortController signal triggered due to timeout.
fix
Increase timeout with withTimeout(fetch, 30000) or adjust default in createFetch({ timeout: 30000 }).
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
cross-fetchoptionalPolyfill for WHATWG Fetch API to support Node.js and React Native
fetch-retryoptionalAdds retry logic to fetch requests
abortcontroller-polyfilloptionalPolyfills AbortController for environments lacking native support
Agent activity
6 hits · last 30 days
node
6
Resources
fetch-compose — npm install fetch-compose · libregistry