Registry / devops / fetch-wrap

fetch-wrap

JSON →
library0.1.2jsnpmunverified

Extend the WHATWG fetch API with middleware in a transparent, recursive manner. Current stable version is 0.1.2. It allows chaining of middleware functions that can modify URLs, options, response handling, and error catching. Key differentiators include built-in middleware for URL parameters, conditional options by URL pattern, JSON serialization/deserialization, and logging. Supports any fetch implementation (native, ponyfill, polyfill) and is isomorphic. Unit tested and benchmarked against plain fetch.

npm install fetch-wrap
INSTALL
IMPORT
SIG · FETCH-WRAP
F
fetch-wrap
devopsjavascriptv0.1.2
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.

fetchWrap
import fetchWrap from 'fetch-wrap'
const fetchWrap = require('fetch-wrap')
ESM default import; CommonJS require also works but is not recommended for modern code.
middleware
import { urlParams, optionsByUrlPattern, sendJson, receiveJson, logger } from 'fetch-wrap/middleware'
import middleware from 'fetch-wrap/middleware'
Named exports from subpath; avoid default import as it may fail.
merge
import { merge } from 'fetch-wrap'
import merge from 'fetch-wrap'
merge is a named export, not a default export.

Shows how to create an enhanced fetch with URL parameter substitution, automatic JSON parsing, and logging.

import fetchWrap from 'fetch-wrap'; import { urlParams, receiveJson, logger } from 'fetch-wrap/middleware'; import fetch from 'node-fetch'; // or any fetch implementation const enhancedFetch = fetchWrap(fetch, [ urlParams({ host: 'localhost' }), receiveJson(), logger() ]); enhancedFetch('http://{host}:3000/data.json?limit=10') .then(data => console.log('Received:', data)) .catch(err => console.error('Error:', err));
Debug
Known issues
deprecatedThe built-in middleware 'fetchWrap/middleware' is not officially documented in the README for version 0.1.2; may change.
fix
Refer to source code or consider pinning version; watch for breaking changes.
affects: >=0.1.0
gotchaThe 'receiveJson' middleware throws on HTTP errors by default; ensure you handle promises properly.
fix
Add a .catch handler or use try/catch with async/await.
affects: >=0.1.0
gotchaThe 'urlParams' middleware replaces tokens in the URL but does not escape special characters; avoid user input in tokens.
fix
Sanitize parameters before passing to urlParams.
affects: >=0.1.0
gotchaMiddleware order matters; for example, receiveJson should come after any middleware that modifies the response.
fix
Plan middleware order carefully: URL/header mods first, then JSON handling, then logging.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: fetchWrap is not a function
Failed to import default export from 'fetch-wrap' in CommonJS.
fix
Use const fetchWrap = require('fetch-wrap').default; or switch to ESM import.
Error: Cannot find module 'fetch-wrap/middleware'
Attempted to import middleware using a path that doesn't exist or import as default.
fix
Use correct import: import { urlParams, receiveJson } from 'fetch-wrap/middleware';
Error: output is not a function
Passed a non-function to fetchWrap; middleware must be functions.
fix
Ensure each middleware is a function with signature (url, options, innerFetch) => ...
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetch-wrap — npm install fetch-wrap · libregistry