Skip to content

Commit

Permalink
Expose assignOptions (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Jul 25, 2018
1 parent 2649270 commit 8cccd8a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
13 changes: 6 additions & 7 deletions advanced-creation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Configure a new `got` instance with the provided settings.<br>

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

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).
To inherit from parent, set it as `got.defaults.options` or use [`got.assignOptions(defaults.options, options)`](readme.md#gotassignoptionsparentoptions-newoptions).<br>
**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.

##### methods

Expand Down Expand Up @@ -53,10 +54,9 @@ const settings = {
return next(options);
},
methods: got.defaults.methods,
options: {
...got.defaults.options,
options: got.assignOptions(got.defaults.options, {
json: true
}
})
};

const jsonGot = got.create(settings);
Expand Down Expand Up @@ -99,12 +99,11 @@ const unchangedGot = got.create(defaults);
const settings = {
handler: got.defaults.handler,
methods: got.defaults.methods,
options: {
...got.defaults.options,
options: got.assignOptions(got.defaults.options, {
headers: {
unicorn: 'rainbow'
}
}
})
};

const unicorn = got.create(settings);
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,18 @@ client.get('/demo');

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

#### got.assignOptions(parentOptions, newOptions)

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:

```js
const a = {headers: {cat: 'meow'}};
const b = {headers: {dog: 'woof'}};
{...a, ...b} // => {headers: {dog: 'woof'}}
got.assignOptions(a, b) // => {headers: {cat: 'meow', dog: 'woof'}}
```

## Errors

Each error contains (if available) `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier.
Expand Down
2 changes: 1 addition & 1 deletion source/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const create = defaults => {
got.stream[method] = (url, options) => got.stream(url, {...options, method});
}

Object.assign(got, errors);
Object.assign(got, {...errors, assignOptions});
Object.defineProperty(got, 'defaults', {
value: deepFreeze(defaults),
writable: false,
Expand Down
5 changes: 2 additions & 3 deletions test/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ test('no tampering with defaults', t => {
const instance = got.create({
handler: got.defaults.handler,
methods: got.defaults.methods,
options: {
...got.defaults.options,
options: got.assignOptions(got.defaults.options, {
baseUrl: 'example'
}
})
});

const instance2 = instance.create({
Expand Down

0 comments on commit 8cccd8a

Please sign in to comment.