import type { ReadonlyDeep } from "type-fest"; import type { CacheSpec, ContentForId } from "./00_CacheSpec.js"; import type { AnyParams } from "./01_Params.js"; import type { AnyValidators } from "./02_Validators.js"; import type { ConsumerRequest } from "./03_ConsumerRequest.js"; import type { ProducerResultResource, ProducerResultResourceObject } from "./04_ProducerResult.js"; import type { IsSingleType } from "./utils.js"; /** * A producer paired with a consumer request: invoked with the full request, * the producer is expected to return a {@link RequestPairedProducerResult} * for that request's `id`. * * The shape of `RequestPairedProducer` depends on whether the cache's `Spec` * is a *single id type* or a *multi-id type* (a union of {@link CacheSpec}s): * * - **Single-id-type mode** (one `CacheSpec` variant — the most common case): * the producer is a plain non-generic function. The (id, content) * correlation is trivially preserved because there's only one possible * content type. * * - **Multi-id-type mode** (a union of `CacheSpec`s): the producer is generic * over the request's specific id, so its return type is required to match * the spec variant that id selects. Implementing such a producer directly * is awkward (TypeScript can't narrow the function's free type parameter * based on runtime checks on `req.id`), so users are encouraged to build * one with the {@link producerByIdType} helper, which handles per-variant * dispatch and contains the necessary internal cast. * * Implementations of this type MUST NOT be `instanceof Error`, as instanceof * Error is used elsewhere to detect if the result could not be returned. */ export type RequestPairedProducer = IsSingleType extends true ? SingleIdTypeRequestPairedProducer : MultiIdTypeRequestPairedProducer; /** * The single-id-type form of {@link RequestPairedProducer}: a non-generic * function whose return need only be valid for the spec's one variant. */ export type SingleIdTypeRequestPairedProducer = (req: ReadonlyDeep>, options?: { signal?: AbortSignal; }) => Promise>; /** * The multi-id-type form of {@link RequestPairedProducer}: generic over the * request's specific id, so the return type's content is required to match * the spec variant that id selects. * * This is the form that internal code (`wrapProducer`, * `requestPairedProducerResultToResources`, etc.) operates against. In * single-id-type mode the loose user-facing form is coerced to this one * inside `wrapProducer`; that coercion is sound because all ids in single- * id-type mode share the same content type, so any valid loose result is * also a valid result for an arbitrary requested id. */ export type MultiIdTypeRequestPairedProducer = (req: ReadonlyDeep>, options?: { signal?: AbortSignal; }) => Promise>; /** * A producer result that will be processed along with a corresponding request. * Because the request will have indicated the id, the producer can leave that * out (and `wrapProducer` will use the request's `id` to fill it in). However, * it must still set `vary`, if the result varied on any request params. * * The shape is a discriminated union over `Spec`: each variant pairs an `id` * (now made optional) with the matching `content`. So a producer for a cache * with `Spec = (story:..., Story) | (collection:..., Story[])` must return * either a Story-shaped or Collection-shaped result, but cannot return a * Story-content paired with a `collection:` id. * * Supplemental resources may correspond to any spec variant (so a fetch for a * collection can attach the individual stories as supplementals). * * Implementations of this type MUST NOT be `instanceof Error`, as instanceof * Error is used elsewhere to detect if the result could not be returned. */ export type RequestPairedProducerResult = Id extends unknown ? // For each valid id type, make a paired { id?, content } object, Omit, Params, Validators>, "id"> & { id?: Id; supplementalResources?: ProducerResultResource[]; } : never; //# sourceMappingURL=05_RequestPairedProducer.d.ts.map