normalize.ts
This file
Path
source/utils/normalize.ts
100 lines · 4.2 KiB · TypeScript
Last changed by
RefactorSindre Sorhusbefore adoptiona5ed57b
History of this fileTake 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–2why, one step back1import {requestMethods} from '../core/constants.js';2import type {RetryOptions} from '../types/retry.js';Harden retrySindre Sorhusbefore adoptionc426f19
line 3why, one step back3import type {HttpMethod, InternalOptions, RequestHttpMethod} from '../types/options.js';RefactorSindre Sorhusbefore adoptiona5ed57b
line 4why, one step back4import {isNonArrayObject, isNonNegativeNumber} from './is.js';Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 5–6why, one step back56export const normalizeRequestMethod = (input: string): string =>Fix mixed-case standard `method` values not being uppercasedSindre Sorhusbefore adoption8950864
line 7why, one step back7 requestMethods.includes(input.toLowerCase() as RequestHttpMethod) ? input.toUpperCase() : input;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 8why, one step back8Respect custom HTTP method case when matching retriesSindre Sorhusbefore adoptione4269f0
lines 9–16why, one step back9const caseInsensitiveMethods = new Set<string>([...requestMethods, 'options', 'trace']);1011/**12Normalizes common methods for retry matching while preserving case-sensitive custom methods.13*/14export const normalizeRetryMethod = (method: string): string =>15 caseInsensitiveMethods.has(method.toLowerCase()) ? method.toLowerCase() : method;16Add QUERY method support (#873)Sindre Sorhusbefore adoption6edddd9
line 17why, one step back17const retryMethods: HttpMethod[] = ['get', 'put', 'head', 'delete', 'options', 'trace', 'query'];Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 18–22why, one step back1819const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];2021const retryAfterStatusCodes = [413, 429, 503];22Harden retrySindre Sorhusbefore adoptionc426f19
line 23why, one step back23type InternalRetryOptions = InternalOptions['retry'];Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4
lines 24–25why, one step back2425const defaultRetryOptions: InternalRetryOptions = {Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 26–32why, one step back26 limit: 2,27 methods: retryMethods,28 statusCodes: retryStatusCodes,29 afterStatusCodes: retryAfterStatusCodes,30 maxRetryAfter: Number.POSITIVE_INFINITY,31 backoffLimit: Number.POSITIVE_INFINITY,32 delay: attemptCount => 0.3 * (2 ** (attemptCount - 1)) * 1000,Add `retry.jitter` option (#760)Sindre Sorhusbefore adoption081c924
line 33why, one step back33 jitter: undefined,Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4
line 34why, one step back34 retryOnTimeout: false,Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 35–36why, one step back35};36Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4
line 37why, one step back37export const normalizeRetryOptions = (retry: number | RetryOptions = {}): InternalRetryOptions => {Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 38why, one step back38 if (typeof retry === 'number') {Simplify request state and retry normalizationSindre Sorhusbefore adoptionb28ba90
line 39why, one step back39 retry = {limit: retry};Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 40–41why, one step back40 }41RefactorSindre Sorhusbefore adoptiona5ed57b
line 42why, one step back42 if (!isNonArrayObject(retry)) {Harden retrySindre Sorhusbefore adoptionc426f19
lines 43–45why, one step back43 throw new TypeError('`retry` must be a number or an object');44 }45Simplify request state and retry normalizationSindre Sorhusbefore adoptionb28ba90
lines 46–49why, one step back46 const normalizedRetry = {47 ...defaultRetryOptions,48 ...Object.fromEntries(Object.entries(retry).filter(([, value]) => value !== undefined)),49 };RefactorSindre Sorhusbefore adoptiona5ed57b
lines 50–53why, one step back50 // Validates the retry limit. An omitted one already got the default above, since `undefined` values are filtered out.51 if (!Number.isInteger(normalizedRetry.limit) || normalizedRetry.limit < 0) {52 throw new TypeError('`retry.limit` must be a finite, non-negative integer');53 }Harden retrySindre Sorhusbefore adoptionc426f19
line 54why, one step back54Simplify request state and retry normalizationSindre Sorhusbefore adoptionb28ba90
lines 55–59why, one step back55 for (const key of ['methods', 'statusCodes', 'afterStatusCodes'] as const) {56 if (!Array.isArray(normalizedRetry[key])) {57 // eslint-disable-next-line unicorn/prefer-type-error -- Preserve the existing error type.58 throw new Error(`retry.${key} must be an array`);59 }Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 60why, one step back60 }Fix handling of uppercase `retry.methods` and improve typing (#789)Martin Vokrinekbefore adoptioncec10a9
line 61why, one step back61Count a compressed response as unknown, deep-copy context, and isolate `init` hook assignmentsSindre Sorhusbefore adoptiond89c8c8
lines 62–73why, one step back62 // A mistyped entry only ever fails to match, so `statusCodes: ['429']` quietly disabled retrying instead of reporting the typo.63 for (const [key, isValid] of [64 ['methods', (value: unknown) => typeof value === 'string'],65 ['statusCodes', (value: unknown) => Number.isInteger(value)],66 ['afterStatusCodes', (value: unknown) => Number.isInteger(value)],67 ] as const) {68 if (normalizedRetry[key].some(value => !isValid(value))) {69 throw new TypeError(`\`retry.${key}\` must only contain ${key === 'methods' ? 'strings' : 'numbers'}`);70 }71 }7273 // Both limits are passed to `Math.min()`, so a non-number silently turns every delay into `NaN`, which `setTimeout()` clamps to 1ms. That defeats the whole point of the limits.Validate the retry options and forward every fetch-only extensionSindre Sorhusbefore adoptiond9774cf
line 74why, one step back74 for (const key of ['maxRetryAfter', 'backoffLimit'] as const) {RefactorSindre Sorhusbefore adoptiona5ed57b
line 75why, one step back75 if (!isNonNegativeNumber(normalizedRetry[key])) {Validate the retry options and forward every fetch-only extensionSindre Sorhusbefore adoptiond9774cf
lines 76–90why, one step back76 throw new TypeError(`\`retry.${key}\` must be a non-negative number or \`Infinity\``);77 }78 }7980 // These run while deciding what to do with the original failure, so an invalid shape would replace the `HTTPError`/`NetworkError` that caused the retry with a `TypeError` from deep inside Ky.81 for (const key of ['delay', 'shouldRetry'] as const) {82 if (normalizedRetry[key] !== undefined && typeof normalizedRetry[key] !== 'function') {83 throw new TypeError(`\`retry.${key}\` must be a function`);84 }85 }8687 if (normalizedRetry.jitter !== undefined && typeof normalizedRetry.jitter !== 'boolean' && typeof normalizedRetry.jitter !== 'function') {88 throw new TypeError('`retry.jitter` must be a boolean or a function');89 }90TweaksSindre Sorhusbefore adoption3ded2aa
lines 91–94why, one step back91 if (typeof normalizedRetry.retryOnTimeout !== 'boolean') {92 throw new TypeError('`retry.retryOnTimeout` must be a boolean');93 }94Simplify request state and retry normalizationSindre Sorhusbefore adoptionb28ba90
lines 95–97why, one step back95 normalizedRetry.methods = normalizedRetry.methods.map(method => normalizeRetryMethod(method));96 normalizedRetry.statusCodes = [...normalizedRetry.statusCodes];97 normalizedRetry.afterStatusCodes = [...normalizedRetry.afterStatusCodes];Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 98why, one step back98Simplify request state and retry normalizationSindre Sorhusbefore adoptionb28ba90
line 99why, one step back99 return normalizedRetry;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 100why, one step back100};