Registry / devops / fetch-default

fetch-default

JSON →
library1.0.18jsnpmunverified

Adds global default properties to the web fetch API, allowing developers to set default headers, methods, and lifecycle hooks (beforeSend, dataFilter, fail) without wrapping every fetch call. Stable version 1.0.18 with infrequent updates. Differentiates from similar utilities by offering beforeSend and dataFilter interceptors, and by extending the global fetch object rather than requiring a proxy or wrapper.

npm install fetch-default
INSTALL
IMPORT
SIG · FETCH-DEFAULT
F
fetch-default
devopsjavascriptv1.0.18
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.

fetch-default
import 'fetch-default'
import fetchDefault from 'fetch-default'
This module is a side-effect import; it modifies the global fetch object. Do not use a default import.
require
require('fetch-default')
const fetchDefault = require('fetch-default')
The module must be required for its side effects; assigning the result is unnecessary.
fetch.default()
fetch.default({method:'POST'})
fetch.default({method:'POST'}, callback)
fetch.default() takes a single options object, not multiple arguments.

Demonstrates how to import fetch-default, set global defaults via fetch.default(), and make a fetch call with per-request overrides.

// Install: npm i fetch-default --save // Import for side effects (modifies global fetch) import 'fetch-default'; // Set default options fetch.default({ method: 'GET', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, credentials: 'include', beforeSend() { // Modify this.uri before request console.log('Sending request to:', this.uri); }, dataFilter(response) { if (!response.ok) { throw new Error(`HTTP ${response.status}`); } return response.json(); }, fail(e) { console.error('Request failed:', e); } }); // Use fetch normally - defaults apply automatically fetch('/api/data', { method: 'POST', // overrides default method for this call body: JSON.stringify({key: 'value'}) }) .then(data => console.log(data));
Debug
Known issues
gotchafetch.default() modifies the global fetch object; all subsequent fetch calls will be affected. Make sure to call fetch.default() early in your app's lifecycle.
fix
Call fetch.default() once at application startup, not inside components or request handlers.
affects: >=1.0.0
gotchaThe beforeSend and dataFilter hooks are called with `this` bound to the request options object. Using arrow functions will break `this` references.
fix
Use regular function expressions for hooks: `beforeSend() { ... }` instead of `beforeSend: () => { ... }`.
affects: >=1.0.0
gotchadataFilter expects a synchronous return or a promise; if you return `response.json()` (async), the subsequent .then() will receive the parsed data, not the original Response.
fix
In dataFilter, return the transformed value synchronously or return a promise (e.g., `response.json()`). The fetch promise will resolve with that value.
affects: >=1.0.0
deprecatedThe `fetch.default({ ... })` signature is not standard; this is a proprietary enhancement. Future versions or other libraries may conflict.
fix
Be aware that using fetch.default() makes your code reliant on this specific package. If you switch to another fetch enhancer, remove fetch.default() calls.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetch.default is not a function
The package was not imported before calling fetch.default().
fix
Add `import 'fetch-default'` or `require('fetch-default')` at the top of your entry file.
Uncaught (in promise) TypeError: Failed to fetch
The dataFilter hook threw an error or the fail hook was not defined to catch network errors.
fix
Ensure dataFilter returns a valid value (or throws) and that fail(e) is defined to handle exceptions.
TypeError: Cannot read property 'uri' of undefined
Inside beforeSend, `this.uri` is undefined because the request URI was not set or the hook is bound incorrectly.
fix
Do not use arrow functions in beforeSend; use `function()` syntax so `this` points to the request options.
Upgrade
Version history
1.0.18latest on npm
Audit
Dependencies

No dependency data recorded yet.

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