Registry / testing / fetch-absolute

fetch-absolute

JSON →
library1.0.0jsnpmunverified

Fetch wrapper that prepends a default base URL to all fetch calls. Version 1.0.0, released as a stable package. It works with both browser's native fetch and node-fetch. Simplifies testing and server-side code by avoiding repetitive URL prefixes. Unlike node-fetch or isomorphic-fetch, it adds a base URL layer without modifying the underlying fetch implementation.

npm install fetch-absolute
INSTALL
IMPORT
SIG · FETCH-ABSOLUTE
F
fetch-absolute
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.

fetchAbsolute (default)
✓ import fetchAbsolute from 'fetch-absolute';
✗ const { fetchAbsolute } = require('fetch-absolute');
Package exports a single default function; named import is incorrect.
require usage
✓ const fetchAbsolute = require('fetch-absolute');
✗ const fetchAbsolute = require('fetch-absolute').default;
CommonJS require returns the default export directly, no .default needed.
Usage with node-fetch
✓ const fetch = require('node-fetch'); const fetchApi = require('fetch-absolute')(fetch)('http://localhost:3030');
✗ const fetchApi = require('fetch-absolute')('http://localhost:3030');
fetch-absolute requires a fetch function as the first argument; omitting it will cause a TypeError.

Shows how to create an absolute-fetch wrapper using node-fetch and make a request.

const fetch = require('node-fetch'); const fetchAbsolute = require('fetch-absolute'); const fetchApi = fetchAbsolute(fetch)('http://localhost:3030'); async function example() { const response = await fetchApi('/api/data'); const data = await response.json(); console.log(data); } example();
Debug
Known issues
gotchaRequests not starting with '/' are treated as relative; absolute URLs in requests are not automatically prefixed.
fix
Ensure all request paths start with '/' to have the base URL prepended.
affects: <=1.0.0
gotchaDoes not handle pathname merging (e.g. ends with '/' or not); base URL and request path are concatenated directly.
fix
Manually ensure base URL ends with '/' if needed, or request path starts correctly.
affects: <=1.0.0
Errors
Common errors & fixes
TypeError: fetch is not a function
Missing or invalid fetch argument passed to fetchAbsolute.
fix
Ensure you pass a valid fetch function: const fetchAbsolute = require('fetch-absolute'); const fetch = require('node-fetch'); const fetchApi = fetchAbsolute(fetch)('http://localhost:3030');
Error: Only absolute URLs are allowed
Request path doesn't start with '/' and is not an absolute URL.
fix
Prefix your request path with '/': fetchApi('/api/endpoint')
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
fetch-absolute — npm install fetch-absolute · libregistry