1/5
Easy#patchfetchretryoptionsmerge

Keep a numeric retry limit when extend() merges an object

sindresorhus/ky#867 · @chatman-media · MIT · licence

a slice of a larger PR (2 files) · see it all

description
### Problem retry accepts a number as shorthand for {limit: number} (per the docs: *"If retry is a number, it will be used as limit and other defaults will remain in place."*). However, when a numeric retry is set on a parent instance and then extended with an **object**, the numeric limit is silently dropped and falls back to the default (2): ``js import ky from 'ky'; const api = ky.create({retry: 3}); // Intent: keep limit 3, only narrow the retriable methods. const extended = api.extend({retry: {methods: ['get']}}); // Bug: extended retries with the default limit of 2, not 3. ` The reverse direction (object on the parent, number on the child) and the all-object case both work correctly — only the *number → object* merge loses data. ### Cause In deepMerge, retry is merged like any other nested object. When the parent value is the numeric shorthand (3) and the incoming value is an object, the recursion ends up as deepMerge(3, {methods: ['get']}). A non-object source is skipped entirely by deepMerge, so the 3 is discarded and only {methods: ['get']} survives — the limit is lost. ### Fix Expand the numeric retry shorthand to {limit: number} before the deep merge, so extending it with an object preserves the limit. This mirrors the documented shorthand semantics and leaves every other case (object → object, object → number replacement, replaceOption) unchanged. `js deepMerge({retry: 3}, {retry: {methods: ['get']}}) // before: {retry: {methods: ['get']}} // after: {retry: {limit: 3, methods: ['get']}} ` ### Test Added a test in test/retry.ts that sets retry: 3 on a parent, extends it with retry: {methods: ['get']}, and asserts the request is attempted limit + 1 times. It fails on main` (3 attempts) and passes with the fix (4 attempts).
2 commits · 1 file
  • 1dec9aaFix `extend()` dropping numeric `retry` limit when merging with an object
  • 61b90edScope retry shorthand expansion to root options merge

// files changed

Msource/utils/merge.ts+14−2
@@ -204,7 +204,7 @@ const appendSearchParameters = (target: any, source: any): URLSearchParams => {
204204};
205205
206206// TODO: Make this strongly-typed (no `any`).
207export const deepMerge = <T>(...sources: Array<Partial<T> | undefined>): T => {
207const deepMergeInternal = <T>(isRoot: boolean, ...sources: Array<Partial<T> | undefined>): T => {
208208 let returnValue: any = {};
209209 let headers: KyHeadersInit = {};
210210 let hooks = {};
@@ -264,8 +264,17 @@ export const deepMerge = <T>(...sources: Array<Partial<T> | undefined>): T => {
264264 continue;
265265 }
266266
267 // `retry` accepts a number as shorthand for `{limit: number}`. Expand it before
268 // merging so extending a numeric `retry` with an object keeps the limit instead
269 // of dropping it (e.g. `ky.create({retry: 3}).extend({retry: {methods: ['get']}})`).
270 // Scoped to the root options level so it never rewrites nested user data that
271 // happens to contain a `retry` key (e.g. a `json` request body).
272 if (isRoot && key === 'retry' && isObject(value) && !isReplace && typeof returnValue[key] === 'number') {
273 returnValue = {...returnValue, [key]: {limit: returnValue[key]}};
274 }
275
267276 if (isObject(value) && !isReplace && key in returnValue) {
268 value = deepMerge(returnValue[key], value);
277 value = deepMergeInternal(false, returnValue[key], value);
269278 }
270279
271280 returnValue = {...returnValue, [key]: value};
@@ -310,3 +319,6 @@ export const deepMerge = <T>(...sources: Array<Partial<T> | undefined>): T => {
310319
311320 return returnValue;
312321};
322
323export const deepMerge = <T>(...sources: Array<Partial<T> | undefined>): T =>
324 deepMergeInternal<T>(true, ...sources);