cachedFetch
Fetches a public GET URL through the data cache (see the file comment).
Fetches a public GET URL through the data cache (see the file comment). Private data (headers that name a user) is refused: the cache is shared, so an answer for one user must never reach another.
Import#
ts
import { cachedFetch } from "@shadow-js/dusk/server";
Signature#
ts
async function cachedFetch<T = unknown>(
input: string | URL,
init: CachedFetchInit | undefined,
options: CachedFetchOptions,
): Promise<T>
Parameters#
| Name | Description |
|---|---|
input |
The URL. |
init |
Method (GET only), extra headers (not authorization, cookie or proxy-authorization; the other headers are part of the cache key), signal. |
options |
The tags and the lifetime. |
Returns#
The parsed JSON answer, or the text.
Throws#
- TypeError for a non-GET method, a private header or a URL with credentials; an Error for a non-OK answer (not cached).
Examples#
ts
const rates = await cachedFetch("https://api.example/rates", undefined, { revalidate: 300 });