normalize.ts
This file
Path
source/utils/normalize.ts
45 lines · 1.3 KiB · TypeScript
Last changed by
Add `retry.delay` option to control the time between retries (#533)Russell Clareybefore adoptionc6181ef
History of this fileTake it
Move to TypeScript (#330)Olivier Louvignesbefore adoption2d8d68a
line 1why, one step back1import {requestMethods} from '../core/constants.js';Rename some internal types and upgrade dev dependenciesSindre Sorhusbefore adoption2049aff
line 2why, one step back2import type {HttpMethod} from '../types/options.js';Move to TypeScript (#330)Olivier Louvignesbefore adoption2d8d68a
lines 3–5why, one step back3import type {RetryOptions} from '../types/retry.js';45export const normalizeRequestMethod = (input: string): string =>Rename some internal types and upgrade dev dependenciesSindre Sorhusbefore adoption2049aff
line 6why, one step back6 requestMethods.includes(input as HttpMethod) ? input.toUpperCase() : input;Move to TypeScript (#330)Olivier Louvignesbefore adoption2d8d68a
lines 7–18why, one step back78const retryMethods = ['get', 'put', 'head', 'delete', 'options', 'trace'];910const retryStatusCodes = [408, 413, 429, 500, 502, 503, 504];1112const retryAfterStatusCodes = [413, 429, 503];1314const defaultRetryOptions: Required<RetryOptions> = {15 limit: 2,16 methods: retryMethods,17 statusCodes: retryStatusCodes,18 afterStatusCodes: retryAfterStatusCodes,Rename some internal types and upgrade dev dependenciesSindre Sorhusbefore adoption2049aff
line 19why, one step back19 maxRetryAfter: Number.POSITIVE_INFINITY,Add `retry.backoffLimit` option (#454)Frank van Wijkbefore adoptionab19baf
line 20why, one step back20 backoffLimit: Number.POSITIVE_INFINITY,Add `retry.delay` option to control the time between retries (#533)Russell Clareybefore adoptionc6181ef
line 21why, one step back21 delay: attemptCount => 0.3 * (2 ** (attemptCount - 1)) * 1000,Move to TypeScript (#330)Olivier Louvignesbefore adoption2d8d68a
lines 22–27why, one step back22};2324export const normalizeRetryOptions = (retry: number | RetryOptions = {}): Required<RetryOptions> => {25 if (typeof retry === 'number') {26 return {27 ...defaultRetryOptions,Rename some internal types and upgrade dev dependenciesSindre Sorhusbefore adoption2049aff
line 28why, one step back28 limit: retry,Move to TypeScript (#330)Olivier Louvignesbefore adoption2d8d68a
lines 29–42why, one step back29 };30 }3132 if (retry.methods && !Array.isArray(retry.methods)) {33 throw new Error('retry.methods must be an array');34 }3536 if (retry.statusCodes && !Array.isArray(retry.statusCodes)) {37 throw new Error('retry.statusCodes must be an array');38 }3940 return {41 ...defaultRetryOptions,42 ...retry,Rename some internal types and upgrade dev dependenciesSindre Sorhusbefore adoption2049aff
line 43why, one step back43 afterStatusCodes: retryAfterStatusCodes,Move to TypeScript (#330)Olivier Louvignesbefore adoption2d8d68a
lines 44–45why, one step back44 };45};