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

Fix reusing Got options #1127

Closed
wants to merge 10 commits into from
Closed

Fix reusing Got options #1127

wants to merge 10 commits into from

Conversation

mpern
Copy link

@mpern mpern commented Mar 21, 2020

Restores the original options.url after calling the init hooks.

Fixes #1118

Checklist

  • I have read the documentation.
  • I have included a pull request description of my changes.
  • I have included some tests.
  • If it's a new feature, I have included documentation updates.

@mpern mpern changed the title Bugfix/1118 Fix #1118 and #1050 Mar 21, 2020
@sindresorhus
Copy link
Owner

Can you give the PR a more descriptive title?

test/hooks.ts Outdated
@@ -193,7 +193,7 @@ test('init is called with options', withServer, async (t, server, got) => {
init: [
options => {
t.is(options.url, undefined);
t.is(options.context, context);
t.deepEqual(options.context, context);
Copy link
Collaborator

Choose a reason for hiding this comment

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

no

test/hooks.ts Outdated
@@ -211,7 +211,7 @@ test('init from defaults is called with options', withServer, async (t, server,
init: [
options => {
t.is(options.url, undefined);
t.is(options.context, context);
t.deepEqual(options.context, context);
Copy link
Collaborator

Choose a reason for hiding this comment

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

no

test/post.ts Outdated
@@ -264,7 +264,7 @@ test('the `form` payload is not touched', withServer, async (t, server, got) =>
hooks: {
beforeRequest: [
options => {
t.is(options.form, object);
t.deepEqual(options.form, object);
Copy link
Collaborator

Choose a reason for hiding this comment

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

no

Copy link
Collaborator

@szmarczak szmarczak left a comment

Choose a reason for hiding this comment

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

Why so many changes? Just move

if (hasUrl) {
options = mergeOptions(defaults?.options ?? {}, options ?? {});
} else {
options = mergeOptions(defaults?.options ?? {}, url as object, options ?? {});
}

before that if.

@mpern mpern changed the title Fix #1118 and #1050 Use a clone of the options parameter instead of modifiying the passed object directly Mar 21, 2020
Comment on lines 303 to 311
} else {
runInitHooks(defaults?.options.hooks.init, url as Options);
runInitHooks((url as Options).hooks?.init, url as Options);

if (options) {
runInitHooks(defaults?.options.hooks.init, options);
runInitHooks(options.hooks?.init, options);
}
}
Copy link
Author

Choose a reason for hiding this comment

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

I'm not 100% sure why runInitHooks was called twice once for the url as Options object and once for the options parameter.

Since I merge both objects now anyway if necessary (see line 297), I don't think that split is necessary anymore

@szmarczak szmarczak changed the title Use a clone of the options parameter instead of modifiying the passed object directly Fix reusing Got options Mar 21, 2020
@szmarczak
Copy link
Collaborator

szmarczak commented Mar 21, 2020

Actually my thought was a bad solution too, I think I fixed it. Pushed to this PR. It breaks on frozen options though, but that's fixed in #1051

@szmarczak
Copy link
Collaborator

szmarczak commented Mar 21, 2020

There are two runInitHooks: one for the defaults, one for the options. It's called twice because the options aren't merged yet.

@mpern
Copy link
Author

mpern commented Mar 21, 2020

I would prefer to keep the passed objects immutable, but since you're the maintainer, it's up to you.
Maybe something for the rewrite?

@szmarczak
Copy link
Collaborator

I would prefer to keep the passed objects immutable

Me too.

since you're the maintainer, it's up to you.

No, I just give some ideas and execute them. The maintainer is @sindresorhus :)

Maybe something for the rewrite?

What do you mean? The rewrite fixes both of these issues.

@szmarczak
Copy link
Collaborator

I don't know why Travis doesn't see my commits inside this PR...

@mpern Can you run the tests locally and confirm if my changes fix the issue? Feel free to experiment around that.

@mpern
Copy link
Author

mpern commented Mar 21, 2020

Your fix unfortunately breaks a lot of other tests.

And FYI, running npm test locally doesn't work, xo fails:

  source/index.ts:undefined:undefined
  ✖    0:0   Parsing error: Cannot read property name of undefined

I have no clue why (EDIT: typescript-eslint/typescript-eslint/issues/1746)

For now I use npm run build && npx ava

@szmarczak
Copy link
Collaborator

szmarczak commented Mar 21, 2020

And FYI, running npm test locally doesn't work, xo fails:

rm -rf node_modules
npm install

@szmarczak
Copy link
Collaborator

I don't know why the tests fail either. I'll look more into this tomorrow.

test('should be able to reuse options', t => {
const options: Options = {};
normalizeArguments('http://localhost', options);
t.notThrows(() => normalizeArguments('http://localhost', options));
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
t.notThrows(() => normalizeArguments('http://localhost', options));
t.notThrows(() => {
normalizeArguments('http://localhost', options);
});

@jaulz
Copy link
Contributor

jaulz commented Mar 24, 2020

By the way, this also influences the behaviour of the paginate mechanism since we also reuse Got options there.

@szmarczak
Copy link
Collaborator

By the way, this also influences the behaviour of the paginate mechanism since we also reuse Got options there.

Generally the whole Got project since it sets options.url whenever you provide an init hook.

@szmarczak
Copy link
Collaborator

Fixed in #1051

@szmarczak szmarczak closed this Apr 12, 2020
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.

Cannot reuse an options object
4 participants