Registry / storage / fetch-cached

fetch-cached

JSON →
library2.0.3jsnpmunverified

A flexible cache wrapper around fetch that allows you to plug in any fetch implementation (e.g., node-fetch) and any cache backend (e.g., Redis, in-memory). Current stable version is 2.0.3. Releases are infrequent. Key differentiators: the cache interface is simple (get/set returning Promises), and cached responses are rehydrated with ok, url, status, statusText, .json(), and .text() methods. It does not ship its own caching logic but instead delegates to user-provided store.

npm install fetch-cached
INSTALL
IMPORT
SIG · FETCH-CACHED
F
fetch-cached
storagejavascriptv2.0.3
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.

fetchCached
import fetchCached from 'fetch-cached'
import { fetchCached } from 'fetch-cached'
Package exports a default function, not a named export.
require('fetch-cached')
const fetchCached = require('fetch-cached')
const { fetchCached } = require('fetch-cached')
CommonJS require returns the default export directly.
fetchCached (type)
import type { FetchCacheOptions } from 'fetch-cached'
Types are included for TypeScript; use import type for options if needed.

Shows a minimal setup using an in-memory cache with node-fetch; includes async/await usage.

import fetchCached from 'fetch-cached'; import nodeFetch from 'node-fetch'; // In-memory cache (simple object) const memoryCache = new Map(); const cache = { get: (key) => Promise.resolve(memoryCache.get(key) || null), set: (key, value) => { memoryCache.set(key, value); return Promise.resolve(); } }; const fetch = fetchCached({ fetch: nodeFetch, cache: cache }); async function main() { const response = await fetch('https://api.github.com'); const data = await response.json(); console.log(data); } main().catch(console.error);
Debug
Known issues
gotchaCached responses are recreated objects with .json() and .text() methods; they are not real Response objects. Do not rely on Response-specific properties like .headers, .body, or .clone().
fix
Only use .json() or .text() on cached responses; avoid using .headers or .body.
affects: <=2.0.3
deprecatednode-fetch v2 is used in examples; newer versions may differ in API. The package works with any fetch implementation but does not update examples.
fix
Use node-fetch v3 (ESM-only) or native fetch in Node 18+. Adjust imports accordingly.
affects: >=2.0.0
gotchaThe cache key is the URL string. If you pass different options (e.g., headers) to fetch, they are ignored for caching; the same URL always returns the same cached response.
fix
Implement your own cache key logic by wrapping the fetch function if request-specific caching is needed.
affects: <=2.0.3
breakingVersion 2.0.0 changed the API from a constructor to a factory function. If you were using 'new FetchCached(options)' it will break.
fix
Use fetchCached(options) directly without 'new'.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: fetchCached is not a function
Importing the package as a named export instead of default.
fix
Use default import: import fetchCached from 'fetch-cached'
TypeError: response.json is not a function
Trying to call .json() on a cached response that was already consumed or not a proper Response.
fix
Ensure you call .json() only once on a cached response; do not attempt to use .headers or .body.
Error: The "fetch" option must be provided.
Missing fetch implementation in options, or the option is undefined.
fix
Pass a fetch function (e.g., node-fetch) as the fetch option.
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
node-fetchoptionalrequired for server-side fetch; browser has native fetch
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
1
Resources
fetch-cached — npm install fetch-cached · libregistry