import type { CacheMode, Hyperjump } from "./hyperjump.d.ts"; import type { PrioritySignal } from "./priority.d.ts"; import type { FetchRecord } from "./fetch-record.d.ts"; /** * Anything that can answer a request: a transport, a layer, or a whole * stack. A stack is a fetcher, so it can be wrapped again. */ export interface Fetcher { fetch(request: Request, ctx: FetchContext): Promise; } /** A fetcher that wraps another. `inner` is what it wraps, so a stack can be walked. */ export interface Layer extends Fetcher { readonly inner: Fetcher; } /** Builds a layer around a fetcher. {@link compose} chains them. */ export type Decorator = (inner: Fetcher) => Out; /** * What the session knows about a request, which goes down the stack with * it. A layer can give the one below it a changed copy, like * `{ ...ctx, priority }`, but these are the only fields. What aborts a * request is its own `signal`. */ export type FetchContext = { readonly session: Hyperjump; /** How the HTTP cache handles the request, from `fetch(url, { cache })` */ readonly cacheMode: CacheMode; /** * The request's priority, from `fetch(url, { priority })`. Read `value`, and * listen for `"change"` to follow it while a request waits. */ readonly priority: PrioritySignal; /** The request's record. A layer says what it did with `record.mark(name, detail)`. */ readonly record: FetchRecord; }; /** * Chain decorators, the first one outermost, so they read the way the stack * does from the top: `compose(a(), b())(transport)` is `a()(b()(transport))`. */ export const compose: (...decorators: Decorator[]) => (inner: Fetcher) => Fetcher; /** A fetcher from a function, for a transport or a test */ export const fetcher: (fn: (request: Request, ctx: FetchContext) => Promise | Response) => Fetcher; /** A layer from a function, for one that doesn't need a class, like one that adds a header */ export const layer: (fn: (request: Request, ctx: FetchContext, inner: Fetcher) => Promise) => Decorator; /** The fetchers in a stack, from the top down, through each layer's `inner` */ export const layersOf: (stack: Fetcher) => IteratorObject; /** The first layer of a class in a stack, like `findLayer(stack, HttpCache)` */ export const findLayer: (stack: Fetcher, type: abstract new (...args: any[]) => T) => T | undefined;