Skip to content

Migrating from ky ​

valifetch is deliberately close to ky: both wrap native fetch, both have instances, retries and hooks. The biggest differences are that valifetch returns parsed data instead of a response promise with .json(), and that it validates with Valibot.

Versions

Option names below are shown for ky 1.x and ky 2.x where they differ. ky 2 renamed prefixUrl to prefix and moved its hooks to state-object arguments; valifetch's hooks use the positional signatures ky 1 had.

The shape of a call ​

ts
import ky from 'ky';

const user = await ky.get('https://api.example.com/users/1').json<User>();
ts
import valifetch from 'valifetch';

const user = await valifetch.get('https://api.example.com/users/1', {
  responseSchema: UserSchema, // validated and typed
});
// or, without validation:
const user2 = await valifetch.get<User>('https://api.example.com/users/1');
kyvalifetch
.json()Default; the call resolves to the parsed JSON
.text()responseType: 'text'
.blob()responseType: 'blob'
.arrayBuffer()responseType: 'arrayBuffer'
.formData()responseType: 'formData'
The Response itself (await ky(...))responseType: 'raw'
response.bodyresponseType: 'stream'
ky.create() / ky.extend()valifetch.create() / instance.extend()
ky(url, options)instance.callable() then api(url, options)

Options ​

kyvalifetchNotes
prefixUrl (ky 1) / prefix (ky 2)prefixUrlPaths always stay under it: absolute inputs become path segments, a .. escape throws a TypeError, and a prefix with ? or # throws
searchParamssearchParamsSame name. Instance values merge with per-request values; undefined / null removes a key
jsonjsonOptionally validated by bodySchema
body (string, Blob, stream, FormData, ...)body for raw payloads, form for FormData / URLSearchParamsjson, form and body are mutually exclusive
headersheaders
timeouttimeout0 disables it (ky uses false)
retry: number / retry: { limit, methods, statusCodes }retry: number / retry: { limit, methods, statusCodes }Same names
retry.delay(attemptCount)retry.delay(attempt)0-based; returns milliseconds
retry.shouldRetryretry.shouldRetrySee Retry for the context shape
retry.maxRetryAfterretry.maxRetryAfterBehaviour differs: ky clamps the wait to maxRetryAfter and still retries; valifetch skips the retry and surfaces the response (an HTTP_ERROR, or the response with throwHttpErrors: false). valifetch defaults to 60_000 ms. See Retry
retry.backoffLimit, retry.jitter, retry.retryOnTimeout, retry.afterStatusCodesnonevalifetch caps the default backoff at 30 s, adds 20% jitter, and honours Retry-After
throwHttpErrors: booleanthrowHttpErrors: booleanvalifetch does not accept a function
hookshooksSee below
onDownloadProgressonDownloadProgressEvent is { loaded, total, percent }
onUploadProgress (ky 2)noneSee File upload
signalsignal, plus .cancel() on the returned promise
totalTimeout, maxResponseSize, parseJson, stringifyJson, fetch, contextnone
(none)responseSchema, bodySchema, paramsSchema, searchSchemaValidation
(none)params with :name path segments
(none)dedupe, debug, priority, sseMaxEventSize

Hooks ​

The five hook names match ky's, with the addition of afterParseResponse. valifetch hooks are plain arrays and use positional arguments.

ky 1 hookky 2 hook argumentvalifetch
beforeRequest(request, options)({ request, options, retryCount })beforeRequest(request, options)
afterResponse(request, options, response)({ request, options, response, retryCount })afterResponse(request, options, response)
beforeRetry({ request, options, error, retryCount })same state objectbeforeRetry(state) where state is a RetryContext plus options
beforeError(error)({ error, ... })beforeError(error), receives ValifetchError only
(none)(none)afterParseResponse(data, response, request)
init hooksinit hooksnone

Differences that matter:

  • Short-circuiting beforeRetry. Return the exported stop sentinel to give up retrying. Returning a Request replaces the request for the remaining attempts.
  • beforeRequest runs once per call. beforeRetry runs inside the retry loop.
  • Returning a Response from beforeRequest bypasses fetch (valifetch's valifetch/mock is built on this) and still flows through afterResponse.
  • Hooks concatenate on extend(), parent first.
  • Hook errors propagate unwrapped.

Errors ​

kyvalifetch
HTTPError (error.response)ValifetchError with code: 'HTTP_ERROR'; error.response, error.status, and the already-parsed error.responseBody
TimeoutErrorcode: 'TIMEOUT_ERROR'
network TypeErrorcode: 'NETWORK_ERROR'
AbortErrorcode: 'ABORT_ERROR'
a JSON parse failurecode: 'PARSE_ERROR'
(none)code: 'VALIDATION_ERROR' with error.validation = { target, issues, input }
ts
// ky
try {
  await ky.post(url, { json }).json();
} catch (error) {
  if (error instanceof HTTPError) {
    const body = await error.response.json();
  }
}

// valifetch: the body is already parsed
try {
  await valifetch.post(url, { json });
} catch (error) {
  if (error instanceof ValifetchError && error.isHttpError) {
    const body = error.responseBody;
  }
}

A worked example ​

ts
const api = ky.create({
  prefixUrl: 'https://api.example.com',
  timeout: 10_000,
  retry: { limit: 3 },
  hooks: {
    beforeRequest: [
      (request) => request.headers.set('Authorization', `Bearer ${token()}`),
    ],
  },
});

const users = (await api
  .get('users', { searchParams: { page: 1 } })
  .json()) as User[];
ts
import valifetch from 'valifetch';
import { bearerAuth } from 'valifetch/auth';
import * as v from 'valibot';

const api = valifetch.create({
  prefixUrl: 'https://api.example.com',
  timeout: 10_000,
  retry: { limit: 3 },
  hooks: { beforeRequest: [bearerAuth(token)] },
});

const users = await api.get('/users', {
  searchParams: { page: 1 },
  responseSchema: v.array(UserSchema),
});

Note the leading / is fine in valifetch: prefixUrl and the path are joined with the slashes normalised.

Released under the MIT License. Built with Claude Code.