Got: Fix `got.mergeOptions` docs

Created on 12 Feb 2020  Â·  6Comments  Â·  Source: sindresorhus/got

Describe the bug

  • Node.js version:
  • OS & version:

Actual behavior

The objects get merged like with a normal spread operation {...defaultOptions, ...newOptions}
...

Expected behavior

The result of got.mergeOptions should be {foo: 'x', bar: 'y'}
...

Code to reproduce

import got, { Options } from 'got';

const defaultOptions: Options = {
  responseType: 'json',
  json: {
    foo: 'x',
  },
};

const newOptions: Options = {
  json: {
    bar: 'y',
  },
};

console.log(got.mergeOptions(defaultOptions, newOptions).json);
// {bar: 'y'}
console.log(got.mergeOptions(newOptions, defaultOptions).json);
// {foo: 'x'}

Checklist

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.
documentation ✭ help wanted ✭

Most helpful comment

Got it. I could take a look and have a crack at it.

All 6 comments

This is actually the correct behavior. It's like you would want to merge body: 'a' and body: 'b' and expect body: 'ab' - these are two different payloads.

The documentation needs to be fixed

It sounds like this statement in the documentation has some caveats:

got.mergeOptions(parentOptions, newOptions)

  • If the new property is a plain object:

    • If the parent property is a plain object too, both values are merged recursively into a new object.

So for example form, json, and body wouldn't get "merged" but instead it would use values from properties coming from the newOptions object, is that correct?

yup

Got it. I could take a look and have a crack at it.

Fixed in 157e02bcb613ab506829326e7af96e8155b4e186

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joolfe picture joolfe  Â·  3Comments

dominusmars picture dominusmars  Â·  3Comments

quocnguyen picture quocnguyen  Â·  4Comments

lukechu10 picture lukechu10  Â·  3Comments

carvallegro picture carvallegro  Â·  4Comments