hooks.ts
This file
Path
test-d/hooks.ts
62 lines · 2.7 KiB · TypeScript
Last changed by
Let a `beforeRetry` hook change per-attempt optionsSindre Sorhusbefore adoption242c327
History of this fileTake it
Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
line 1why, one step back1import {expectTypeOf} from 'expect-type';Let a `beforeRetry` hook change per-attempt optionsSindre Sorhusbefore adoption242c327
lines 2–4why, one step back2import type {3 BeforeErrorHook, BeforeRetryHook, BeforeRetryUpdate, Hooks, InitHook,4} from 'ky';Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
lines 5–26why, one step back56const hookList = [() => undefined] as const;7const beforeErrorHook: BeforeErrorHook = ({error}) => error;8const beforeErrorHooks = [beforeErrorHook] as const;9const hooks: Hooks = {10 init: hookList,11 beforeRequest: hookList,12 beforeRetry: hookList,13 beforeError: beforeErrorHooks,14 afterResponse: hookList,15};1617expectTypeOf(hooks).toEqualTypeOf<Hooks>();1819const initHook: InitHook = options => {20 options.hooks?.beforeRequest?.push(() => undefined);21 if (options.hooks) {22 options.hooks.beforeRequest = undefined;23 }24};2526expectTypeOf(initHook).toEqualTypeOf<InitHook>();Let a `beforeRetry` hook change per-attempt optionsSindre Sorhusbefore adoption242c327
lines 27–62why, one step back2728const retryOptionsHook: BeforeRetryHook = () => ({options: {onUploadProgress: undefined, timeout: false}});29const asyncRetryOptionsHook: BeforeRetryHook = async () => ({options: {throwHttpErrors: false}});30// `undefined` restores the default for every option, so it must be accepted with `exactOptionalPropertyTypes`.31const restoreDefaultsHook: BeforeRetryHook = () => ({32 options: {33 onUploadProgress: undefined,34 onDownloadProgress: undefined,35 timeout: undefined,36 fetch: undefined,37 throwHttpErrors: undefined,38 },39});40expectTypeOf(retryOptionsHook).toEqualTypeOf<BeforeRetryHook>();41expectTypeOf(asyncRetryOptionsHook).toEqualTypeOf<BeforeRetryHook>();42expectTypeOf(restoreDefaultsHook).toEqualTypeOf<BeforeRetryHook>();4344const withoutUploadProgress = (): BeforeRetryUpdate => ({options: {onUploadProgress: undefined}});45const separateFunctionHook: BeforeRetryHook = withoutUploadProgress;46expectTypeOf(separateFunctionHook).toEqualTypeOf<BeforeRetryHook>();47// @ts-expect-error The `body` option cannot be returned from a hook.48const invalidUpdate: BeforeRetryUpdate = {options: {body: 'payload'}};49expectTypeOf(invalidUpdate).toEqualTypeOf<BeforeRetryUpdate>();5051// @ts-expect-error Request properties must be changed through the request.52const bodyOptionsHook: BeforeRetryHook = () => ({options: {body: 'payload'}});53// @ts-expect-error The `retry` option cannot be returned from a hook.54const retryLimitOptionsHook: BeforeRetryHook = () => ({options: {retry: 2}});55// @ts-expect-error The `timeout` option must be a number or `false`.56const invalidTimeoutOptionsHook: BeforeRetryHook = () => ({options: {timeout: 'soon'}});57// @ts-expect-error `null` is not accepted.58const nullFetchOptionsHook: BeforeRetryHook = () => ({options: {fetch: null}});59expectTypeOf(bodyOptionsHook).toEqualTypeOf<BeforeRetryHook>();60expectTypeOf(retryLimitOptionsHook).toEqualTypeOf<BeforeRetryHook>();61expectTypeOf(invalidTimeoutOptionsHook).toEqualTypeOf<BeforeRetryHook>();62expectTypeOf(nullFetchOptionsHook).toEqualTypeOf<BeforeRetryHook>();