Skip to Content
✨ New: Auth Plugin. JWT, OAuth2, API Key, and more authentication strategies out of the box. Read more... 🎉
Plugins NewCache Plugin

Cache Plugin

The official caching plugin for endpoint-fetcher. Provides TTL-based caching with LRU eviction and custom storage support.

Installation

npm install @endpoint-fetcher/cache

Requires endpoint-fetcher v3.0.0 or higher.

Quick Start

Wrap endpoint return types with CachingWrapper<T> to enable cache metadata and control methods:

import { createApiClient, get } from 'endpoint-fetcher'; import { cache, CachingWrapper } from '@endpoint-fetcher/cache'; const api = createApiClient({ getAll: get<void, CachingWrapper<User[]>>('/users'), }, { baseUrl: 'https://api.example.com', plugins: [cache({ ttl: 300 })], }); const result = await api.getAll(); console.log(result.data); // User[] console.log(result.isStale); // boolean console.log(result.expiresAt); // Date

CachingWrapper<T>

Every cached response includes:

MemberTypeDescription
dataTThe actual API response data
cachedAtDateWhen the original fetch happened
expiresAtDateWhen the entry expires
isStalebooleanWhether the current time has passed the TTL
refresh()() => Promise<CachingWrapper<T>>Force a network re-fetch and update the cache
invalidate()() => voidRemove this entry from the cache
const result = await api.getAll(); // Force a fresh fetch const fresh = await result.refresh(); // Stale-while-revalidate pattern if (result.isStale) { result.refresh().then(fresh => render(fresh.data)); } render(result.data);

Configuration

OptionTypeDefaultDescription
ttlnumber300Time-to-live in seconds
maxSizenumberInfinityMax cache entries before LRU eviction
methodsstring[]['GET']HTTP methods to cache
storageCacheStorageIn-memoryCustom storage adapter
keyGeneratorFunctionDefaultCustom cache key logic

Custom Storage

Persist cache across sessions with a custom storage adapter:

plugins: [ cache({ ttl: 3600, storage: { get: (key) => JSON.parse(localStorage.getItem(key) || 'null'), set: (key, val) => localStorage.setItem(key, JSON.stringify(val)), delete: (key) => localStorage.removeItem(key), keys: () => Object.keys(localStorage), clear: () => localStorage.clear(), }, }), ]

Global Cache Management

The cache plugin exposes methods on client.plugins.cache:

MethodSignatureDescription
clear()() => voidClear all cache entries
invalidate(method, path, input)(string, string, any) => voidInvalidate a specific endpoint
invalidateKey(key)(string) => voidInvalidate by raw cache key
// Clear everything (e.g., on logout) api.plugins.cache.clear(); // Invalidate a specific endpoint api.plugins.cache.invalidate('GET', '/users', undefined); api.plugins.cache.invalidate('GET', `/users/${userId}`, { id: userId });

Troubleshooting

Data is never cached — Ensure the return type uses CachingWrapper<T>. If you use just T, the plugin skips caching. Also check that the HTTP method is in methods (default: ['GET']).

Cache persists after refresh — The default storage is in-memory and clears on page load. If you use a localStorage adapter, data persists until TTL expires or you call invalidate() / clear().