Registry / testing / memoized-fetch

memoized-fetch

JSON →
library1.0.0jsnpmunverified

A lightweight wrapper around window.fetch that memoizes JSON responses using in-memory cache and sessionStorage for persistence. Version 1.0.0 is the latest stable release. Unlike raw fetch, it automatically parses JSON and caches results keyed by request arguments (URL, options, and an optional cache-busting token). It is designed for browser-only use, ships TypeScript type definitions, and enforces that the third argument must be a string, number, boolean, Date, null, or undefined. It differentiates by providing simple cache control without external dependencies, but lacks features like TTL, cache size limits, or error handling for non-JSON responses.

npm install memoized-fetch
INSTALL
IMPORT
SIG · MEMOIZED-FETCH
M
memoized-fetch
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.

memoizedFetch
import memoizedFetch from 'memoized-fetch'
const memoizedFetch = require('memoized-fetch')
Package ships as ESM only; default import is the function. CommonJS require will fail in modern bundlers.
memoizedFetch
import type memoizedFetch from 'memoized-fetch'
TypeScript types are included; you can import the type for type annotations.
default
import memoizedFetch from 'memoized-fetch'
import { memoizedFetch } from 'memoized-fetch'
The package has a single default export, not a named export. Named import will be undefined.

Demonstrates basic usage: network request on first call, cached response on subsequent identical calls, and cache busting with a third argument.

import memoizedFetch from 'memoized-fetch'; async function run() { // First call fetches from network const data = await memoizedFetch('https://api.example.com/data', { headers: { 'Accept': 'application/json' } }); console.log('First fetch:', data); // Second call returns cached response (in-memory or sessionStorage) const cachedData = await memoizedFetch('https://api.example.com/data', { headers: { 'Accept': 'application/json' } }); console.log('Cached:', cachedData); // Force fresh fetch by providing a cache-busting token const freshData = await memoizedFetch('https://api.example.com/data', {}, 'cache-buster-123'); console.log('Fresh fetch:', freshData); } run().catch(console.error);
Debug
Known issues
gotchaThe third argument to memoizedFetch must be a string, number, boolean, Date, null, or undefined. Passing an object, array, or other type will throw an error.
fix
Ensure cache-busting token is one of the allowed types.
affects: >=1.0.0
gotchamemoizedFetch only works in browser environments because it relies on window.fetch and sessionStorage. Using in Node.js will throw a ReferenceError.
fix
Use only in browser contexts; consider a polyfill for fetch and storage if needed.
affects: >=1.0.0
gotchaThe function automatically calls .json() on the response. If the response is not valid JSON, the promise will reject with a SyntaxError from JSON.parse.
fix
Ensure endpoints return JSON or handle errors with try/catch.
affects: >=1.0.0
deprecatedNo breaking changes or deprecations known as of version 1.0.0. The package is simple and stable.
fix
No action needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: memoizedFetch is not a function
Incorrect import using named import syntax instead of default.
fix
Change to 'import memoizedFetch from 'memoized-fetch''.
ReferenceError: fetch is not defined
Using memoizedFetch in a non-browser environment (Node.js or SSR).
fix
Ensure code runs in a browser or polyfill fetch and sessionStorage.
Error: The memo type must be a string, number, boolean, Date, null, or undefined. Got object.
Passing an object or array as the third argument (cache-busting token).
fix
Pass a primitive or Date instance, or null/undefined.
SyntaxError: Unexpected token < in JSON at position 0
The fetched response is not valid JSON (e.g., HTML error page).
fix
Check that the endpoint returns JSON and handle non-JSON responses.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources