Skip to content
Myr

normalized-options.ts

This file

Path

test-d/normalized-options.ts

117 lines · 5.0 KiB · TypeScript

Last changed by

Improve docsSindre Sorhusbefore adoption8778439

History of this file

Take it

Harden retrySindre Sorhusbefore adoptionc426f19

lines 1–2why, one step back
1import {expectTypeOf} from 'expect-type';
2import type {

Fix `hooks: {x: undefined}` and `signal: undefined` failing to type-check with `exactOptionalPropertyTypes`Sindre Sorhusbefore adoptionee806e0

line 3why, one step back
3 BeforeRequestHook,

Harden retrySindre Sorhusbefore adoptionc426f19

lines 4–8why, one step back
4 BeforeRequestState,
5 HTTPError,6 NormalizedOptions,7 Options,8 RetryOptions,

Document that `null` is intentionally rejected by the `searchParams` typeSindre Sorhusbefore adoptionb99fb79

line 9why, one step back
9 SearchParamsOption,

Harden retrySindre Sorhusbefore adoptionc426f19

lines 10–21why, one step back
10} from 'ky';
1112declare module 'ky' {13 interface Options { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- Declaration merging requires an interface.14 customOption?: string;15 }1617 interface NormalizedOptions { // eslint-disable-line @typescript-eslint/consistent-type-definitions -- Declaration merging requires an interface.18 customOption?: string;19 }20}21

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 22–33why, one step back
22type ExpectedNormalizedRetryOptions = {
23 limit: number;24 methods: string[];25 statusCodes: number[];26 afterStatusCodes: number[];27 maxRetryAfter: number;28 backoffLimit: number;29 delay: (attemptCount: number) => number;30 jitter: boolean | ((delay: number) => number) | undefined;31 retryOnTimeout: boolean;32 shouldRetry?: RetryOptions['shouldRetry'];33};

Harden retrySindre Sorhusbefore adoptionc426f19

lines 34–69why, one step back
34type ExpectedProgressCallbacks = {
35 readonly onDownloadProgress?: NonNullable<Options['onDownloadProgress']>;36 readonly onUploadProgress?: NonNullable<Options['onUploadProgress']>;37};3839declare const options: NormalizedOptions;40declare const beforeRequestState: BeforeRequestState;41declare const httpError: HTTPError;42const contextKey = 'test';4344expectTypeOf(options.headers).toEqualTypeOf<Headers>();45expectTypeOf(options.retry).branded.toEqualTypeOf<ExpectedNormalizedRetryOptions>();46expectTypeOf<Readonly<Pick<NormalizedOptions, keyof ExpectedProgressCallbacks>>>().branded.toEqualTypeOf<ExpectedProgressCallbacks>();4748options.headers.set('x-test', 'value');49options.context[contextKey] = true;50options.retry.limit = 0;51options.retry.statusCodes.push(500);5253// @ts-expect-error - The normalized options object is frozen.54options.method = 'POST';5556// @ts-expect-error - Inherited request options are frozen too.57options.cache = 'no-store';5859// @ts-expect-error - The normalized options object is frozen.60options.context = {};6162// @ts-expect-error - The normalized options object is frozen.63options.retry = {...options.retry};6465// @ts-expect-error - Module-augmented options passed to hooks are frozen too.66beforeRequestState.options.customOption = 'value';6768// @ts-expect-error - Module-augmented options attached to errors are frozen too.69httpError.options.customOption = 'value';

Fix `hooks: {x: undefined}` and `signal: undefined` failing to type-check with `exactOptionalPropertyTypes`Sindre Sorhusbefore adoptionee806e0

lines 70–73why, one step back
70
71// Documented ways to remove inherited values must type-check with `exactOptionalPropertyTypes`.72const withoutHooks: Options = {hooks: {beforeRequest: undefined, afterResponse: []}};73const withoutSignal: Options = {signal: undefined};

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 74–88why, one step back
74const defaultRetries: Options = {retry: undefined};
75expectTypeOf(defaultRetries.retry).toMatchTypeOf<RetryOptions | number | undefined>();76const withoutContainers: Options = {headers: undefined, hooks: undefined, context: undefined};77expectTypeOf(withoutContainers.headers).toMatchTypeOf<Options['headers']>();78expectTypeOf(withoutContainers.hooks).toMatchTypeOf<Options['hooks']>();79expectTypeOf(withoutContainers.context).toMatchTypeOf<Options['context']>();80const defaultHttpErrors: Options = {throwHttpErrors: undefined};81expectTypeOf(defaultHttpErrors.throwHttpErrors).toMatchTypeOf<Options['throwHttpErrors']>();82const withoutJsonCallbacks: Options = {parseJson: undefined, stringifyJson: undefined};83expectTypeOf(withoutJsonCallbacks.parseJson).toMatchTypeOf<Options['parseJson']>();84expectTypeOf(withoutJsonCallbacks.stringifyJson).toMatchTypeOf<Options['stringifyJson']>();85const withoutProgressCallbacks: Options = {onDownloadProgress: undefined, onUploadProgress: undefined};86expectTypeOf(withoutProgressCallbacks.onDownloadProgress).toMatchTypeOf<Options['onDownloadProgress']>();87expectTypeOf(withoutProgressCallbacks.onUploadProgress).toMatchTypeOf<Options['onUploadProgress']>();88expectTypeOf(withoutHooks.hooks?.beforeRequest).toEqualTypeOf<readonly BeforeRequestHook[] | undefined>();

Improve docsSindre Sorhusbefore adoption8778439

line 89why, one step back
89// `null` is accepted for `RequestInit` compatibility, so a `RequestInit` can be forwarded to Ky.

Fix `hooks: {x: undefined}` and `signal: undefined` failing to type-check with `exactOptionalPropertyTypes`Sindre Sorhusbefore adoptionee806e0

lines 90–91why, one step back
90// eslint-disable-next-line @typescript-eslint/no-restricted-types
91expectTypeOf(withoutSignal.signal).toEqualTypeOf<AbortSignal | null | undefined>();

Fix `shouldRetry` type rejecting functions that return nothingSindre Sorhusbefore adoption178767e

lines 92–109why, one step back
92
93// `shouldRetry` may return nothing to fall back to the default retry logic, like every hook type.94const shouldRetryOptions: Options = {95 retry: {96 shouldRetry() {97 // Nothing to decide here.98 },99 },100};101const asyncShouldRetryOptions: Options = {102 retry: {103 async shouldRetry() {104 await Promise.resolve();105 },106 },107};108expectTypeOf(shouldRetryOptions.retry).toMatchTypeOf<RetryOptions | number | undefined>();109expectTypeOf(asyncShouldRetryOptions.retry).toMatchTypeOf<RetryOptions | number | undefined>();

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 110–112why, one step back
110
111const withoutShouldRetry: RetryOptions = {shouldRetry: undefined};112expectTypeOf(withoutShouldRetry.shouldRetry).toMatchTypeOf<RetryOptions['shouldRetry']>();

Document that `null` is intentionally rejected by the `searchParams` typeSindre Sorhusbefore adoptionb99fb79

lines 113–117why, one step back
113
114// `null` is rejected in `searchParams` objects on purpose, even though the runtime sends it as the string `'null'`.115// @ts-expect-error - `null` is not an allowed search parameter value.116const searchParametersWithNull: Options = {searchParams: {foo: null}};117expectTypeOf(searchParametersWithNull.searchParams).toMatchTypeOf<SearchParamsOption | undefined>();