Registry / testing / fetch-defaults

fetch-defaults

JSON →
library1.0.0jsnpmunverified

Functional and immutable mixin for the Fetch API that sets a default base URL and/or default options (e.g., headers) on fetch calls. Version 1.0.0 is stable and follows semantic versioning. It does not modify input objects and supports both browser and Node.js environments when paired with a polyfill like node-fetch or fetch-off. Its key differentiator is immutability: options are merged recursively without mutation, and it does not pollute prototypes.

npm install fetch-defaults
INSTALL
IMPORT
SIG · FETCH-DEFAULTS
F
fetch-defaults
testingjavascriptv1.0.0
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.

fetchDefaults
✓ import fetchDefaults from 'fetch-defaults'
Default export is the fetchDefaults function. Use default import with ESM.
fetchDefaults
✓ const fetchDefaults = require('fetch-defaults')
✗ const fetchDefaults = require('fetch-defaults').default
CommonJS require returns the function directly, not an object with default property.
fetchDefaults
✓ import fetchDefaults from 'fetch-defaults'
✗ import { fetchDefaults } from 'fetch-defaults'
This module does not export a named export; only a default export.

Demonstrates creating a customized fetch with a base URL and default Authorization header, then making a relative request.

import fetchDefaults from 'fetch-defaults'; // Create a custom fetch with base URL and default options const apiFetch = fetchDefaults(fetch, 'https://api.example.com', { headers: { Authorization: `Bearer ${process.env.API_TOKEN ?? ''}` }, mode: 'cors' }); // Use the custom fetch async function getModels() { const response = await apiFetch('/v1/models', { method: 'GET' }); const data = await response.json(); console.log(data); } getModels().catch(console.error);
Debug
Known issues
gotchaNode.js does not have native fetch; you must provide a polyfill.
fix
Install and pass a fetch implementation like node-fetch or fetch-off.
affects: >=1.0.0
gotchaOptions are merged recursively, but arrays are replaced, not concatenated.
fix
If you need to merge arrays, handle that manually before passing options.
affects: >=1.0.0
gotchaThe base URL must be absolute when passed; only appended to relative paths.
fix
Ensure the base URL ends with a slash if you want to avoid path concatenation issues.
affects: >=1.0.0
deprecatedLicense is AGPL-3.0-only (Lesser Affero GPL), which may impose restrictions.
fix
Consider using MIT-licensed alternatives like 'wretch' or 'redaxios' if AGPL is problematic.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetchDefaults is not a function
Using named import instead of default import in ESM, or using .default in CommonJS.
fix
Use `import fetchDefaults from 'fetch-defaults'` (ESM) or `const fetchDefaults = require('fetch-defaults')` (CommonJS).
Uncaught ReferenceError: fetch is not defined
Running in Node.js without a fetch polyfill.
fix
Install and import node-fetch or fetch-off before using fetchDefaults.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
fetch-offoptionalNode.js does not have a built-in Fetch API; any compatible library works, but fetch-off is explicitly recommended in the README.
node-fetchoptionalAlternative Fetch API implementation for Node.js, mentioned as a compatible option.
Agent activity
4 hits · last 30 days
node
4
Resources
fetch-defaults — npm install fetch-defaults · libregistry