Module · source
source Module
The source directory is the root of the Ky library implementation. It contains the runtime feature-detection layer (source/core/constants.ts) and the core request-execution engine (source/core/Ky.ts), along with the surrounding utilities, error types, and type definitions that the public ky entry point composes into the final HTTP client.
The two files surfaced here form the backbone of the runtime: constants.ts defines compile-once feature probes and configuration tables (supported HTTP methods, response shortcut types, retry defaults, option registries), while Ky.ts defines the Ky class that wires those constants together with hooks, timeouts, retries, and schema validation to produce a ResponsePromise.
Feature detection (source/core/constants.ts)
A set of module-level constants probe the host environment exactly once at import time. They are intended to be referenced later by Ky and the body/stream utilities to gate optional functionality.
supportsRequestStreams— Performs a non-trivial probe: it constructs aRequestwith aReadableStreambody and aduplexgetter, then checks whether theduplexoption was read and that no automaticContent-Typewas attached. It also catches the QQBrowser/iOS"unsupported BodyInit type"error and returnsfalsein that case.supportsAbortController— Simpletypeof globalThis.AbortController === 'function'check.supportsAbortSignal— Requires bothAbortSignaland the staticAbortSignal.anymethod to exist, so it gates use of signal composition rather than just cancellation.supportsResponseStreams— Checks for a globalReadableStream.supportsFormData— Checks for a globalFormData.
Request/response configuration tables
responseTypes— Maps Ky's response shortcut names toAcceptheader values:json→application/json,text→text/*,formData→multipart/form-data, andarrayBuffer/blob/bytes→*/*. The comment onbytesnotes it is feature-checked at runtime before being exposed (the actual probe lives in test code astypeof Response.prototype.bytes === 'function').requestMethods,responseTypes,validate,retry,kyOptionKeys,requestOptionsRegistry— Static tables describing the supported HTTP verbs, validation defaults, retry defaults, and which option keys belong to Ky vs. the underlyingRequest.maxSafeTimeout,usualFormBoundarySize,stop— Numeric/sentinel constants used by timeout handling, body size estimation, and the hook short-circuit protocol.
Retry primitives
type ForceRetryOptionsandclass RetryMarker— Declared together inconstants.ts.RetryMarkerholds anoptionsfield set in itsconstructorand is used as an in-band signal that a hook has requested a retry with overridden options. It pairs with theForceRetryErrorimported inKy.ts.
The Ky class (source/core/Ky.ts)
Ky is the per-request state machine. Each request instantiates one through the static create method (split across four ranges in the source — create (part 1..4) — covering option normalization, request construction, hook invocation, and response handling).
Key instance state:
request— The underlyingRequestobject that will be passed tofetch.#abortController— Private controller used to implementtimeoutand to abort on retry, in combination with thesupportsAbortController/supportsAbortSignalprobes.#retryCount— Tracks retry attempts against theretrydefaults fromconstants.ts.#input— The originalInput(URL/Request/string) the caller supplied.#normalizeSearchParams— Private helper that reconcilessearchParamsoptions with any URL query string, using thedeletedParametersSymbolimported fromutils/merge.js.
Internal helpers in Ky.ts
A cluster of module-private helpers above the class supports request lifecycle concerns:
maxErrorResponseBodySize,timedOutResponseData,createTextDecoder— Bound the amount of response body retained on errors and decode timeout placeholder data.prefixUrlRenamedErrorMessage,invalidSchemaMessage— Static error messages thrown for misuse.cloneRetryOptions,cloneSearchParametersForInitHook,cloneInitHookOptions— Defensive copies passed tobeforeRequest/beforeRetryhooks so user mutations don't leak into Ky's normalized state.isRequestInstance,isResponseInstance,objectToString—Object.prototype.toString-based type guards that work across realms.validateJsonWithSchema— Bridges Standard Schema (StandardSchemaV1, imported from../types/standard-schema.js) into the.json()path, raisingSchemaValidationErroron failure.
Declarations
| const | supportsRequestStreams | :4 |
| const | supportsAbortController | :34 |
| const | supportsAbortSignal | :35 |
| const | supportsResponseStreams | :36 |
| const | supportsFormData | :37 |
| const | requestMethods | :39 |
| const | validate | :41 |
| const | responseTypes | :46 |
| const | maxSafeTimeout | :58 |
| const | usualFormBoundarySize | :61 |
| const | stop | :66 |
| type | ForceRetryOptions | :71 |
| class | RetryMarker | :153 |
| field | options | :154 |
| method | constructor | :156 |
| const | retry | :244 |
| const | kyOptionKeys | :246 |
| const | requestOptionsRegistry | :269 |
| const | maxErrorResponseBodySize | :44 |
| const | prefixUrlRenamedErrorMessage | :45 |
| const | timedOutResponseData | :46 |
| const | createTextDecoder | :48 |
| const | invalidSchemaMessage | :60 |
| const | cloneRetryOptions | :62 |
| const | objectToString | :76 |
| const | isRequestInstance | :78 |
| const | isResponseInstance | :84 |
| const | cloneSearchParametersForInitHook | :87 |
| function | cloneInitHookOptions | :96 |
| const | validateJsonWithSchema | :112 |
| class | Ky | :142 |
| method | create (part 1) | :143 |
| method | create (part 2) | :205 |
| method | create (part 3) | :267 |
| method | create (part 4) | :329 |
| method | #normalizeSearchParams | :340 |
| field | request | :349 |
| field | #abortController | :350 |
| field | #retryCount | :351 |
| field | #input | :353 |
| field | #options | :354 |
| field | #originalRequest | :355 |
| field | #userProvidedAbortSignal | :356 |
| field | #beforeRetryHookErrors | :357 |
| field | #cachedNormalizedOptions | :358 |
| field | #startTime | :359 |
| field | #returnedResponseFromBeforeRetryHook | :360 |
| field | #responseRequests | :361 |
| method | constructor (part 1) | :364 |
| method | constructor (part 2) | :418 |
| method | constructor (part 3) | :472 |
| method | #calculateDelay | :487 |
| method | #calculateRetryDelay | :504 |
| method | #decorateResponse | :592 |
| method | #getResponseData | :609 |
| method | #getErrorDataTimeout | :635 |
| method | #isJsonContentType | :649 |
| method | #readResponseText | :655 |
| method | #parseJson | :719 |
| method | #cancelBody | :740 |