{"id":47579,"library":"fetch-defaults","title":"fetch-defaults","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/moll/js-fetch-defaults","tags":["javascript","fetch"],"install":[{"cmd":"npm install fetch-defaults","lang":"bash","label":"npm"},{"cmd":"yarn add fetch-defaults","lang":"bash","label":"yarn"},{"cmd":"pnpm add fetch-defaults","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Node.js does not have a built-in Fetch API; any compatible library works, but fetch-off is explicitly recommended in the README.","package":"fetch-off","optional":true},{"reason":"Alternative Fetch API implementation for Node.js, mentioned as a compatible option.","package":"node-fetch","optional":true}],"imports":[{"note":"Default export is the fetchDefaults function. Use default import with ESM.","wrong":null,"symbol":"fetchDefaults","correct":"import fetchDefaults from 'fetch-defaults'"},{"note":"CommonJS require returns the function directly, not an object with default property.","wrong":"const fetchDefaults = require('fetch-defaults').default","symbol":"fetchDefaults","correct":"const fetchDefaults = require('fetch-defaults')"},{"note":"This module does not export a named export; only a default export.","wrong":"import { fetchDefaults } from 'fetch-defaults'","symbol":"fetchDefaults","correct":"import fetchDefaults from 'fetch-defaults'"}],"quickstart":{"code":"import fetchDefaults from 'fetch-defaults';\n\n// Create a custom fetch with base URL and default options\nconst apiFetch = fetchDefaults(fetch, 'https://api.example.com', {\n  headers: { Authorization: `Bearer ${process.env.API_TOKEN ?? ''}` },\n  mode: 'cors'\n});\n\n// Use the custom fetch\nasync function getModels() {\n  const response = await apiFetch('/v1/models', { method: 'GET' });\n  const data = await response.json();\n  console.log(data);\n}\n\ngetModels().catch(console.error);","lang":"typescript","description":"Demonstrates creating a customized fetch with a base URL and default Authorization header, then making a relative request."},"warnings":[{"fix":"Install and pass a fetch implementation like node-fetch or fetch-off.","message":"Node.js does not have native fetch; you must provide a polyfill.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you need to merge arrays, handle that manually before passing options.","message":"Options are merged recursively, but arrays are replaced, not concatenated.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the base URL ends with a slash if you want to avoid path concatenation issues.","message":"The base URL must be absolute when passed; only appended to relative paths.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider using MIT-licensed alternatives like 'wretch' or 'redaxios' if AGPL is problematic.","message":"License is AGPL-3.0-only (Lesser Affero GPL), which may impose restrictions.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.0':27 'and/or':18 'api':11 'base':16 'browser':43 'call':25 'default':3,15,19 'differenti':62 'e.g':21 'environ':46 'fetch':2,10,24,55,58,78 'fetch-default':1 'fetch-off':57 'follow':31 'function':4 'header':22 'immut':6,64 'input':38 'javascript':77 'key':61 'like':52 'merg':67 'mixin':7 'modifi':37 'mutat':70 'node':54 'node-fetch':53 'node.js':45 'object':39 'option':20,65 'pair':48 'pollut':75 'polyfil':51 'prototyp':76 'recurs':68 'semant':32 'set':13 'stabl':29 'support':41 'url':17 'version':26,33 'without':69","created_at":"2026-06-07T16:52:14.416760+00:00","updated_at":"2026-06-07T16:52:14.416760+00:00","problems":[{"fix":"Use `import fetchDefaults from 'fetch-defaults'` (ESM) or `const fetchDefaults = require('fetch-defaults')` (CommonJS).","cause":"Using named import instead of default import in ESM, or using .default in CommonJS.","error":"TypeError: fetchDefaults is not a function"},{"fix":"Install and import node-fetch or fetch-off before using fetchDefaults.","cause":"Running in Node.js without a fetch polyfill.","error":"Uncaught ReferenceError: fetch is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/moll/js-fetch-defaults","github":"git://github.com/moll/js-fetch-defaults","docs":null,"changelog":null,"pypi":null,"npm":"fetch-defaults","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}