/** * smartFetch — consumer-side Soma Check helper. * * Drop-in replacement for `fetch()` that automatically: * 1. Stores the `X-Soma-Hash` from every response * 2. Sends `If-Soma-Hash` on subsequent calls to the same URL * 3. Returns cached content when the server responds `{ unchanged: true }` * * When data hasn't changed, the response body is replayed from the consumer's * local cache — zero provider cost, zero parsing overhead. * * @example * ```ts * import { createSmartFetch } from "soma-sense"; * * const sfetch = createSmartFetch(); * * // First call — normal paid fetch * const r1 = await sfetch("https://api.example.com/price?symbol=BTC"); * const price1 = await r1.json(); * * // Second call — sends If-Soma-Hash automatically * // If unchanged, returns the cached body with { unchanged: true, dataHash, ... } * const r2 = await sfetch("https://api.example.com/price?symbol=BTC"); * if (r2.somaCheck?.unchanged) { * // Zero-cost: use cached body * const price2 = r2.somaCheck.cachedBody; * } * ``` */ import { SomaCheckHashStore } from '../core/soma-check.js'; export interface SmartFetchConfig { /** Custom hash store — defaults to in-memory `SomaCheckHashStore`. */ hashStore?: SomaCheckHashStore; /** * Override the underlying fetch implementation. Defaults to globalThis.fetch. * Useful for testing or custom transports. */ fetchImpl?: typeof fetch; /** * Cache body keyed by URL — when server responds `unchanged: true`, * return the cached body from this store. Defaults to an in-memory Map. */ bodyCache?: Map; /** Callback invoked whenever Soma Check saves a request (0-cost hit). */ onSaved?: (url: string, dataHash: string) => void; } export interface SomaCheckMeta { /** True if the server responded with `unchanged: true` and we served from cache. */ unchanged: boolean; /** The current dataHash as known to the server. */ dataHash: string | null; /** The cached body, if `unchanged` is true. */ cachedBody?: unknown; /** Age of the server's cached entry, if provided. */ age?: number; /** Whether the server's cached entry was still fresh. */ fresh?: boolean; } /** A `Response` augmented with Soma Check metadata. */ export interface SmartResponse extends Response { somaCheck?: SomaCheckMeta; } /** * Normalize a URL to a stable cache key — strips trailing slashes and * orders query params so `?a=1&b=2` and `?b=2&a=1` hit the same entry. */ export declare function hashKeyForUrl(url: string): string; /** * Create a smart fetch function with its own hash store + body cache. * Multiple callers can share a single instance, or create separate ones. */ export declare function createSmartFetch(config?: SmartFetchConfig): (input: string | URL | Request, init?: RequestInit) => Promise; /** * Default shared smart fetch — convenient for quick use but all callers * in a process share one cache. For isolation, use `createSmartFetch()`. */ export declare const smartFetch: (input: string | URL | Request, init?: RequestInit) => Promise; //# sourceMappingURL=smart-fetch.d.ts.map