Skip to content
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

Merged
merged 14 commits into from
Aug 2, 2018

Conversation

szmarczak
Copy link
Collaborator

@szmarczak szmarczak commented Jul 27, 2018

Compared to #538:

  • clones defaults on instance creation,
  • more clear docs a bit,
  • merge() clones array properties too, which is useful when hooks need to access hooks.beforeRequest.someProperty,
  • merge() is a different file now,
  • [] instead of new Array in merge(),
  • added use strict to merge.js,
  • merge() is now an arrow function.

Jonathan Stewmon and others added 3 commits July 26, 2018 11:06
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.
@szmarczak szmarczak changed the title Better got.assignOptions() Rework got.assignOptions() and document it Jul 27, 2018
@szmarczak szmarczak requested a review from sindresorhus July 27, 2018 12:20
@szmarczak szmarczak changed the title Rework got.assignOptions() and document it [WIP] Rework got.assignOptions() and document it Jul 30, 2018
@szmarczak szmarczak reopened this Jul 30, 2018
@szmarczak szmarczak changed the title [WIP] Rework got.assignOptions() and document it Rework got.assignOptions() and document it Jul 30, 2018
@szmarczak szmarczak force-pushed the assignOptions-mergeWith branch from b9db06e to 3c294d0 Compare July 30, 2018 14:40
@@ -124,7 +124,7 @@ Default: `{}`

Request headers.

Existing headers will be overwritten. Headers set to `null` or `undefined` will be omitted.
Copy link
Collaborator Author

@szmarczak szmarczak Jul 30, 2018

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.

Repository owner deleted a comment from szmarczak Jul 30, 2018
Repository owner deleted a comment from szmarczak Jul 30, 2018
Repository owner deleted a comment from szmarczak Jul 30, 2018
Repository owner deleted a comment from szmarczak Jul 30, 2018
@szmarczak szmarczak requested a review from brandon93s July 31, 2018 09:48
@sindresorhus
Copy link
Owner

hooks are called by reference,

Not sure I see the point of this. Example use-case? I generally prefer not exposing this unless absolutely necessary.

merge() is a different file now,

Can you leave it where it is for now so we get a diff?

[] instead of new Array in merge().

👍

@szmarczak
Copy link
Collaborator Author

Not sure I see the point of this. Example use-case? I generally prefer not exposing this unless absolutely necessary.

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);

Can you leave it where it is for now so we get a diff?

Working on it.

@sindresorhus
Copy link
Owner

Your example doesn't really convince me otherwise. That problem can be solved without using this too. I think it should be up to users to bind methods if they want to use this.

@szmarczak
Copy link
Collaborator Author

I think it should be up to users to bind methods if they want to use this.

Right, I'll revert that :)

@szmarczak szmarczak force-pushed the assignOptions-mergeWith branch from 3c6ccad to 31e97f9 Compare July 31, 2018 10:31
@szmarczak szmarczak changed the title Rework got.assignOptions() and document it Improve merging options Jul 31, 2018
@szmarczak szmarczak removed the request for review from sindresorhus July 31, 2018 11:58
@szmarczak szmarczak requested review from sindresorhus and removed request for brandon93s July 31, 2018 11:58
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
Copy link
Owner

@sindresorhus sindresorhus Aug 2, 2018

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?

Copy link
Collaborator Author

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.**
Copy link
Owner

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.

Copy link
Collaborator Author

@szmarczak szmarczak Aug 2, 2018

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sindresorhus sindresorhus merged commit af5c3fd into sindresorhus:master Aug 2, 2018
@szmarczak szmarczak deleted the assignOptions-mergeWith branch January 17, 2019 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants