Registry / storage / fetchache

fetchache

JSON →
library0.1.6jsnpmunverified

Cross-platform fetch wrapper that adds HTTP caching support to any fetch implementation using a key-value cache. Version 0.1.6 is the latest stable release. It follows HTTP Caching (RFC 7234) and Conditional Requests (RFC 7232) standards, allowing server-like caching behavior in non-browser environments (Node.js, workers). Unlike simple memoization, it respects Cache-Control headers, ETags, and expiration. Ships TypeScript types. Requires Node >= 16. Low release cadence; maintained by Ardatan/whatwg-node ecosystem.

npm install fetchache
INSTALL
IMPORT
SIG · FETCHACHE
F
fetchache
storagejavascriptv0.1.6
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.

fetchFactory
✓ import { fetchFactory } from 'fetchache'
✗ const fetchFactory = require('fetchache').fetchFactory
Package is ESM-only; CommonJS require() will fail. Use named import.
Response
✓ import { Response } from 'fetchache'
✗ import { Response } from '@whatwg-node/fetch'
fetchache re-exports Response for convenience, but you must provide your own fetch implementation.
fetchWithCache
✓ const fetchWithCache = fetchFactory({ fetch, Response, cache })
✗ const fetchWithCache = fetchFactory({ fetch, cache })
You must pass both fetch and Response constructors, not just fetch. Otherwise errors occur.

Creates a cached fetch using an in-memory Map, demonstrating the minimal setup with @whatwg-node/fetch.

import { fetchFactory } from 'fetchache'; import { fetch, Response } from '@whatwg-node/fetch'; const cache = new Map<string, string>(); const cacheImpl = { get: async (key: string) => cache.get(key) ?? undefined, set: async (key: string, value: string) => { cache.set(key, value); }, }; const fetchWithCache = fetchFactory({ fetch, Response, cache: cacheImpl }); const response = await fetchWithCache('https://jsonplaceholder.typicode.com/todos/1'); const data = await response.json(); console.log(data);
Debug
Known issues
gotchaMust provide both fetch and Response implementations; missing one causes cryptic errors.
fix
Always pass both fetch and Response. Use @whatwg-node/fetch for Node.js.
affects: >=0.1.0
gotchaCache implementation must match the expected interface: get and set with async string keys and values.
fix
Implement cache { get(key: string): Promise<string|undefined>; set(key: string, value: string): Promise<void> }.
affects: >=0.1.0
gotchaPackage is ESM-only; using require() throws 'ERR_REQUIRE_ESM'.
fix
Use import syntax or dynamic import(). Set 'type':'module' in package.json if running as CommonJS.
affects: >=0.1.0
gotchaThe cache key includes full URL and method; sensitive headers are not cached.
fix
Understand that cache keys are derived from the request; do not rely on manual key generation.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: fetch is not a constructor
Passing a native fetch or a fetch that is not a constructor (e.g., from undici or cross-fetch).
fix
Use @whatwg-node/fetch which exports a constructor-compatible fetch. Alternatively, wrap fetch using the factory pattern: const fetchFunc = (...args) => fetch(...args);
ERR_REQUIRE_ESM
Using require('fetchache') in a CommonJS file.
fix
Change to import { fetchFactory } from 'fetchache' or use dynamic import().
TypeError: cache.get is not a function
Passed cache object without implementing the get method.
fix
Ensure cache implements { get: (key: string) => Promise<string | undefined> }.
Upgrade
Version history
0.1.6latest on npm
Audit
Dependencies
@whatwg-node/fetchoptionalRecommended fetch and Response polyfill for Node.js; otherwise must provide custom fetch/Response.
Agent activity
21 hits · last 30 days
node
16
Amazon
3
Resources
fetchache — npm install fetchache · libregistry