Skip to content

Commit 8cccd8a

Browse files
szmarczaksindresorhus
authored andcommittedJul 25, 2018
Expose assignOptions (#530)
1 parent 2649270 commit 8cccd8a

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed
 

‎advanced-creation.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Configure a new `got` instance with the provided settings.<br>
1111

1212
##### [options](readme.md#options)
1313

14-
To inherit from parent, set it as `got.defaults.options` or use [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals).
14+
To inherit from parent, set it as `got.defaults.options` or use [`got.assignOptions(defaults.options, options)`](readme.md#gotassignoptionsparentoptions-newoptions).<br>
15+
**Note**: Avoid using [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) as it doesn't work recursively.
1516

1617
##### methods
1718

@@ -53,10 +54,9 @@ const settings = {
5354
return next(options);
5455
},
5556
methods: got.defaults.methods,
56-
options: {
57-
...got.defaults.options,
57+
options: got.assignOptions(got.defaults.options, {
5858
json: true
59-
}
59+
})
6060
};
6161

6262
const jsonGot = got.create(settings);
@@ -99,12 +99,11 @@ const unchangedGot = got.create(defaults);
9999
const settings = {
100100
handler: got.defaults.handler,
101101
methods: got.defaults.methods,
102-
options: {
103-
...got.defaults.options,
102+
options: got.assignOptions(got.defaults.options, {
104103
headers: {
105104
unicorn: 'rainbow'
106105
}
107-
}
106+
})
108107
};
109108

110109
const unicorn = got.create(settings);

‎readme.md

+12
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ client.get('/demo');
398398

399399
*Need more control over the behavior of Got? Check out the [`got.create()`](advanced-creation.md).*
400400

401+
#### got.assignOptions(parentOptions, newOptions)
402+
403+
Extends parent options. Avoid using [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) as it doesn't work recursively:
404+
405+
```js
406+
const a = {headers: {cat: 'meow'}};
407+
const b = {headers: {dog: 'woof'}};
408+
409+
{...a, ...b} // => {headers: {dog: 'woof'}}
410+
got.assignOptions(a, b) // => {headers: {cat: 'meow', dog: 'woof'}}
411+
```
412+
401413
## Errors
402414

403415
Each error contains (if available) `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier.

‎source/create.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const create = defaults => {
4646
got.stream[method] = (url, options) => got.stream(url, {...options, method});
4747
}
4848

49-
Object.assign(got, errors);
49+
Object.assign(got, {...errors, assignOptions});
5050
Object.defineProperty(got, 'defaults', {
5151
value: deepFreeze(defaults),
5252
writable: false,

‎test/create.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ test('no tampering with defaults', t => {
105105
const instance = got.create({
106106
handler: got.defaults.handler,
107107
methods: got.defaults.methods,
108-
options: {
109-
...got.defaults.options,
108+
options: got.assignOptions(got.defaults.options, {
110109
baseUrl: 'example'
111-
}
110+
})
112111
});
113112

114113
const instance2 = instance.create({

0 commit comments

Comments
 (0)
Please sign in to comment.