The objects get merged like with a normal spread operation {...defaultOptions, ...newOptions}
...
The result of got.mergeOptions should be {foo: 'x', bar: 'y'}
...
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'}
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
Most helpful comment
Got it. I could take a look and have a crack at it.