Skip to content
Myr

index.ts

This file

Path

source/index.ts

114 lines · 4.2 KiB · TypeScript

Last changed by

Let a `beforeRetry` hook change per-attempt optionsSindre Sorhusbefore adoption242c327

History of this file

Take it

The walk stopped after 200 commits: lines older than 37fc250 point at it.

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 1–3why, one step back
1/*! MIT License © Sindre Sorhus */
23import {Ky} from './core/Ky.js';

TweaksSindre Sorhusbefore adoption3ded2aa

lines 4–5why, one step back
4import {
5 requestMethods,

RefactorSindre Sorhusbefore adoptiona5ed57b

line 6why, one step back
6 getSupportedResponseTypes,

TweaksSindre Sorhusbefore adoption3ded2aa

lines 7–9why, one step back
7 stop,
8 retry,9} from './core/constants.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 10–11why, one step back
10import type {KyInstance} from './types/ky.js';
11import type {Input, Options} from './types/options.js';

TweaksSindre Sorhusbefore adoption3ded2aa

line 12why, one step back
12import type {ResponsePromise} from './types/ResponsePromise.js';

Explain a missing response and give `extend(fn)` a deep copy of the parent defaultsSindre Sorhusbefore adoption5b7080a

line 13why, one step back
13import {cloneDeep, validateAndMerge} from './utils/merge.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 14–15why, one step back
14import {type Mutable} from './utils/types.js';
15

TweaksSindre Sorhusbefore adoption3ded2aa

lines 16–22why, one step back
16// Errors while setting up the request, such as invalid options or a throwing `init` hook, reject the returned promise instead of throwing, the same way `fetch()` reports them, so callers only have one error channel to handle.
17const createRequest = (input: Input, getOptions: () => Options): ResponsePromise => {18 try {19 return Ky.create(input, getOptions());20 } catch (error: unknown) {21 // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors -- A hook can throw any value, and it must be preserved exactly.22 const result = Promise.reject(error) as ResponsePromise;

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 23–24why, one step back
23 // Matches `Ky.create()`, which only exposes `.bytes()` when the environment implements it.
24 for (const type of getSupportedResponseTypes()) {

TweaksSindre Sorhusbefore adoption3ded2aa

lines 25–32why, one step back
25 // Returning the same promise marks it as handled, so a body method call does not also report an unhandled rejection.
26 result[type] = async () => result as never;27 }2829 return result;30 }31};32

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 33–34why, one step back
33const createInstance = (defaults?: Partial<Options>): KyInstance => {
34 // eslint-disable-next-line @typescript-eslint/promise-function-async

TweaksSindre Sorhusbefore adoption3ded2aa

line 35why, one step back
35 const ky: Partial<Mutable<KyInstance>> = (input: Input, options?: Options) => createRequest(input, () => validateAndMerge(defaults, options));

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 36–38why, one step back
36
37 for (const method of requestMethods) {38 // eslint-disable-next-line @typescript-eslint/promise-function-async

TweaksSindre Sorhusbefore adoption3ded2aa

line 39why, one step back
39 ky[method] = (input: Input, options?: Options) => createRequest(input, () => validateAndMerge(defaults, options, {method}));

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 40–44why, one step back
40 }
4142 ky.create = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(newDefaults));43 ky.extend = (newDefaults?: Partial<Options> | ((parentDefaults: Partial<Options>) => Partial<Options>)) => {44 if (typeof newDefaults === 'function') {

Run `beforeError` for a `beforeRetry` abort and share the body-wrapping checkSindre Sorhusbefore adoption55599c8

line 45why, one step back
45 // Pass a copy so mutations inside the callback cannot leak into this instance's defaults. Deep, because options like `context` are merged shallowly, so a nested value would still point at the parent's object. Non-plain values such as a `FormData` body are shared, the same way the `hooks.init` copies share them.

Explain a missing response and give `extend(fn)` a deep copy of the parent defaultsSindre Sorhusbefore adoption5b7080a

line 46why, one step back
46 newDefaults = newDefaults(validateAndMerge(cloneDeep(defaults)));

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 47–52why, one step back
47 }
4849 return createInstance(validateAndMerge(defaults, newDefaults));50 };5152 ky.stop = stop;

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

line 53why, one step back
53 ky.retry = retry;

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 54–68why, one step back
54
55 return ky as KyInstance;56};5758const ky = createInstance();5960export default ky;6162export type {KyInstance} from './types/ky.js';6364export type {65 Input,66 Options,67 NormalizedOptions,68 RetryOptions,

Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4

line 69why, one step back
69 ShouldRetryState,

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 70–75why, one step back
70 SearchParamsOption,
71 Progress,72} from './types/options.js';7374export type {75 Hooks,

Add `init` hook (#841)Sindre Sorhusbefore adoption87c6740

line 76why, one step back
76 InitHook,

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 77–80why, one step back
77 BeforeRequestHook,
78 BeforeRequestState,79 BeforeRetryHook,80 BeforeRetryState,

Let a `beforeRetry` hook change per-attempt optionsSindre Sorhusbefore adoption242c327

line 81why, one step back
81 BeforeRetryUpdate,

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 82–88why, one step back
82 BeforeErrorHook,
83 BeforeErrorState,84 AfterResponseHook,85 AfterResponseState,86} from './types/hooks.js';8788export type {ResponsePromise} from './types/ResponsePromise.js';

Add Standard Schema validation for `.json()` (#830)Sindre Sorhusbefore adoption94741a9

lines 89–93why, one step back
89export type {
90 StandardSchemaV1,91 StandardSchemaV1InferOutput,92 StandardSchemaV1Issue,93} from './types/standard-schema.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 94–95why, one step back
94export type {KyRequest} from './types/request.js';
95export type {KyResponse} from './types/response.js';

Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b

line 96why, one step back
96export {KyError} from './errors/KyError.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 97why, one step back
97export {HTTPError} from './errors/HTTPError.js';

Add Standard Schema validation for `.json()` (#830)Sindre Sorhusbefore adoption94741a9

line 98why, one step back
98export {SchemaValidationError} from './errors/SchemaValidationError.js';

Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80

line 99why, one step back
99export {NetworkError} from './errors/NetworkError.js';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 100why, one step back
100export {TimeoutError} from './errors/TimeoutError.js';

Add `maxResponseSize` optionSindre Sorhusbefore adoption0d59458

line 101why, one step back
101export {ResponseSizeError} from './errors/ResponseSizeError.js';

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

lines 102–105why, one step back
102export {ForceRetryError} from './errors/ForceRetryError.js';
103export {104 isKyError,105 isHTTPError,

Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80

line 106why, one step back
106 isNetworkError,

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

line 107why, one step back
107 isTimeoutError,

Add `maxResponseSize` optionSindre Sorhusbefore adoption0d59458

line 108why, one step back
108 isResponseSizeError,

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

lines 109–110why, one step back
109 isForceRetryError,
110} from './utils/type-guards.js';

Add `replaceOption` helper for `.extend()` (#846)Sindre Sorhusbefore adoptionbb8412e

line 111why, one step back
111export {replaceOption} from './utils/merge.js';

Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4

lines 112–114why, one step back
112
113// Intentionally not exporting this for now as it's just an implementation detail and we don't want to commit to a certain API yet at least.114// export {NonError} from './errors/NonError.js';