Skip to content

Plugins ​

The opt-in middleware, each on its own import path. Conceptual guides: Plugins overview · Write your own. Per-plugin: timeout · retry · abort-previous · swr · schema.

withTimeout ​

ts
// @itsy/corgi/timeout          (hand-rolled, 2022-safe)
// @itsy/corgi/timeout-modern   (AbortSignal.any + AbortSignal.timeout, Baseline 2024)
function withTimeout(ms: number): Plugin;

Per-attempt deadline, body included, tagged ORDER.timeout. Both modules export the same signature and behaviour. ms of 0/Infinity disables it. Detect with isTimeoutError.

withRetry ​

ts
// @itsy/corgi/retry
function withRetry(options?: RetryOptions | number): Plugin;

interface RetryOptions {
  retries?: number; //   default 2 (up to 3 total tries)
  backoff?: number; //   base ms, default 300
  maxDelay?: number; //  default 10_000
  methods?: readonly string[]; //  default: the idempotent set
  statuses?: readonly number[]; // default: 408/429/500/502/503/504
  onRetry?: (info: { attempt: number; delay: number; error?: unknown; response?: Response }) => void;
}

A bare number is shorthand for { retries: n }. Tagged ORDER.retry. Only retries idempotent methods with replayable bodies. A Retry-After header (seconds or an HTTP-date) overrides computed backoff, capped at maxDelay — fractional seconds included (0.001 → 1ms). That's contract, not a parsing accident: it's the lever tests use to exercise retry against a client whose config they can't touch. Full behaviour: retry.

abortPrevious ​

ts
// @itsy/corgi/abort-previous
function abortPrevious(): AbortPrevious;

interface AbortPrevious extends Plugin {
  abort(reason?: unknown): void;
}

Aborts the previous in-flight request when a new one starts. Tagged ORDER.cancel (outermost). Stateful — keep it at the client level. abort() cancels the newest call without starting one, rejecting it with reason (an AbortError by default). Full behaviour: abort-previous.

swr ​

ts
// @itsy/corgi/swr
function swr(options: SwrOptions | number): Plugin;

interface SwrOptions {
  fresh: number; //         serve-from-memory window, ms
  stale?: number; //        grace window added after `fresh`, default 0
  errorBackoff?: number; //  wait between failed refreshes, default 0
  accept?: (response: Response) => boolean | Promise<boolean>; // default: response.ok
  onError?: (info: { url: string; error?: unknown; response?: Response }) => void;
}

A bare number is shorthand for { fresh: n } (0 = pure in-flight dedupe). GETs only, tagged ORDER.swr. Fresh hits skip the network; stale hits are served instantly while a detached background refresh updates the store. Stateful — keyed by the final url alone, so don't share one client across users. Full behaviour: swr.

schema ​

Not middleware, but a transform. Grouped here as an opt-in add-on.

ts
// @itsy/corgi/schema
function schema<S extends StandardSchemaV1>(
  s: S,
): (value: unknown, response?: Response) => Promise<StandardSchemaV1.InferOutput<S>>;

function parseWith<S extends StandardSchemaV1>(s: S, input: Promise<unknown>): Promise<StandardSchemaV1.InferOutput<S>>;

class ValidationError extends Error {
  readonly name: 'ValidationError';
  readonly issues: readonly StandardSchemaV1.Issue[];
}

function isValidationError(error: unknown): error is ValidationError;

schema(s) returns a transform that validates the parsed value against any Standard Schema validator and infers the output type. parseWith is the standalone .then() form. Full behaviour: schema.

Released under the MIT License.