-
-
Notifications
You must be signed in to change notification settings - Fork 957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve merging options #539
Improve merging options #539
Conversation
Option merging is now consistent, so null header removal is done during options normalization. null could not be used to indicate property removal because null is a significant value for some settings.
We have a narrow set of rules for merging objects and lodash's more aggressive merge may have unintended consequences when the values being merged are not plain objects.
got.assignOptions()
got.assignOptions()
and document it
got.assignOptions()
and document itgot.assignOptions()
and document it
got.assignOptions()
and document itgot.assignOptions()
and document it
b9db06e
to
3c294d0
Compare
@@ -124,7 +124,7 @@ Default: `{}` | |||
|
|||
Request headers. | |||
|
|||
Existing headers will be overwritten. Headers set to `null` or `undefined` will be omitted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed that part ("or undefined
") to not confuse people with the behaviour of got.mergeOptions
, where setting to undefined
may keep the old value.
Not sure I see the point of this. Example use-case? I generally prefer not exposing
Can you leave it where it is for now so we get a diff?
👍 |
Simple example: const {beforeRequest} = options.hooks;
const handler = function (options) {
options.headers['some-data'] = this.data; // this = options.hooks.beforeRequest
};
beforeRequest.data = 'someData';
beforeRequest.push(handler);
Working on it. |
Your example doesn't really convince me otherwise. That problem can be solved without using |
Right, I'll revert that :) |
3c6ccad
to
31e97f9
Compare
got.assignOptions()
and document it
test/headers.js
Outdated
@@ -142,12 +142,22 @@ test('remove null value headers', async t => { | |||
t.false(Reflect.has(headers, 'user-agent')); | |||
}); | |||
|
|||
test('remove undefined value headers', async t => { | |||
test('setting a header to undefined keeps the old value', async t => { | |||
const {body} = await got(s.url, { | |||
headers: { | |||
foo: undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be 'user-agent': undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, must missed that when was merging with master. Fixed.
@@ -403,30 +403,28 @@ client.get('/demo'); | |||
|
|||
*Need more control over the behavior of Got? Check out the [`got.create()`](advanced-creation.md).* | |||
|
|||
**Both `got.extend(options)` and `got.create(options)` will freeze the instance's default options. For `got.extend()`, the instance's default options are the result of `got.mergeOptions`, which effectively copies plain `Object` and `Array` values. Therefore, you should treat objects passed to these methods as immutable.** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this removed? I guess we could shorten it a little bit, but I like to keep the note about you should treat objects passed to these methods as immutable.
somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I consider it's no longer needed, because the defaults are cloned, so your object is safe, nothing will change :) This proves it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Compared to #538:
defaults
on instance creation,merge()
clones array properties too, which is useful when hooks need to accesshooks.beforeRequest.someProperty
,merge()
is a different file now,[]
instead ofnew Array
inmerge()
,use strict
tomerge.js
,merge()
is now an arrow function.