Skip to content

API design decisions ​

A few naming and ergonomics choices are intentional and locked for stability. They are documented here so the asymmetries do not read as accidental.

  • searchParams vs params. Query values use searchParams (plus searchSchema); path values use params (plus paramsSchema). The searchParams key deliberately mirrors the web platform's URL.searchParams / URLSearchParams and the equivalent option in ky, so the value key (searchParams) and its schema (searchSchema) use slightly different stems. Platform alignment is preferred over internal symmetry.
  • json / form / body. Structured bodies go through json (validated against bodySchema) or form; body is the raw escape hatch (string, Blob, ArrayBuffer, typed array, ReadableStream) sent as-is with no validation or Content-Type inference. Exactly one may be set per request; none is accepted at instance level.
  • responseType is per-call only. responseType lives on the per-request options, not on create() / extend() instance options. It changes the return type of a call ('blob' gives Blob, 'sse' gives AsyncIterable<MessageEvent>, and so on), which cannot be expressed at instance-creation time without losing type safety. Set it on each call instead.
  • ValifetchError.cause is unknown. Matching the standard Error.cause, the cause option accepts any thrown value, not just an Error, so non-Error throws pass through without wrapping.
  • Hook signatures stay positional, following ky 1.x. (ky 2 moved to state-object arguments; valifetch deliberately does not.) beforeRequest(request, options), afterResponse(request, options, response), afterParseResponse(data, response, request), beforeRetry(state) and beforeError(error) are not being unified into a single state-object signature. Matching ky's conventions is deliberate, and a hook that throws propagates that error as-is (never wrapped) to abort the request. beforeError only ever sees ValifetchErrors.
  • Entry-point rule. valifetch (.) is the core runtime plus core types; valifetch/types is the superset of every public type; ./error, ./auth and ./mock are runtime subpaths. JSR publishes ., ./auth and ./mock (disjoint symbol sets); ./error and ./types are npm-only because they only re-export symbols already in ., ./auth or ./mock.
  • options(url, requestOptions). The HTTP-verb method is deliberately named options (matching the OPTIONS HTTP verb); its parameter is internally named requestOptions to avoid colliding with the method name.
  • Retry counters use different bases on purpose. RetryOptions.delay(attempt) is 0-based (delay(0) computes the first retry's delay). BeforeRetryState.retryCount and the attempt field on a DebugEvent of type 'retry' are both 1-based: they describe "the retry about to be performed", which reads more naturally in hook and log code than a 0-based count would.

See also Versioning and stability.

Released under the MIT License. Built with Claude Code.