CachedAPIProviderclass

new CachedAPIProvider<P, R>(source: APIProvider<P, R>, maxAge: number = AVOID_REFRESH)
ParamType
sourceAPIProvider<P, R>
The source provider whose results are cached. required
maxAgenumber
Default maximum age in milliseconds for call() (defaults to AVOID_REFRESH). Defaults to AVOID_REFRESH
Return
CachedAPIProvider<P, R>
API provider wrapper that serves requests through an APICache so repeated calls reuse cached results.
PropertyType
.maxAgenumber
The maximum age used when calling call(), defaulting to AVOID_REFRESH (only refresh if invalidated or still loading).
- Not used for refresh() calls, which always refetch immediately. readonly

API provider wrapper that serves requests through an APICache so repeated calls reuse cached results.

  • On call(...), triggers cache.refresh(maxAge) for the endpoint+payload before awaiting cache.call(...).
  • invalidate, invalidateAll, refresh, and refreshAll pass through to the underlying cache and use this.maxAge as the default refresh timing.

A wrapping provider that serves calls through an APICache. Repeated calls for the same endpoint and payload skip the network and return the cached value, and it exposes invalidate / refresh helpers for cache control.

The constructor takes the source provider and an optional default maxAge (defaults to AVOID_REFRESH — only refetch when a value is missing or invalidated).

Usage

ts
import { CachedAPIProvider, ValidationAPIProvider, ClientAPIProvider } from "shelving/api"

const provider = new CachedAPIProvider(
  new ValidationAPIProvider(new ClientAPIProvider({ url: "https://api.example.com" }))
)

await provider.call(getUser, { id: "u_1" }) // fetches
await provider.call(getUser, { id: "u_1" }) // returns cached

provider.invalidate(getUser, { id: "u_1" }) // mark stale
provider.refresh(getUser, { id: "u_1" })    // re-fetch eagerly

invalidateAll(endpoint) and refreshAll(endpoint) act on every cached payload for an endpoint at once.

Examples

const api = new CachedAPIProvider(source);
 const result = await api.call(endpoint, payload);

Methods

Go

CachedAPIProvider.invalidate()method

Invalidate the cached result for a specific endpoint and payload.

invalidate(endpoint: Endpoint<PP, RR>, payload: PP): void
Go

CachedAPIProvider.invalidateAll()method

Invalidate every cached result for an endpoint, across all payloads.

invalidateAll(endpoint: Endpoint<PP, RR>): void
Go

CachedAPIProvider.refresh()method

Refresh the cached result for a specific endpoint and payload.

refresh(endpoint: Endpoint<PP, RR>, payload: PP): void
Go

CachedAPIProvider.refreshAll()method

Refresh every cached result for an endpoint, across all payloads.

refreshAll(endpoint: Endpoint<PP, RR>): void