-
-
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
Fix reusing Got options #1127
Fix reusing Got options #1127
Conversation
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); |
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.
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); |
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.
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); |
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.
no
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 so many changes? Just move
got/source/normalize-arguments.ts
Lines 313 to 317 in 62b3388
if (hasUrl) { | |
options = mergeOptions(defaults?.options ?? {}, options ?? {}); | |
} else { | |
options = mergeOptions(defaults?.options ?? {}, url as object, options ?? {}); | |
} |
before that if
.
options
parameter instead of modifiying the passed object directly
source/normalize-arguments.ts
Outdated
} 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); | ||
} | ||
} |
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'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
options
parameter instead of modifiying the passed object directly
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 |
There are two |
I would prefer to keep the passed objects immutable, but since you're the maintainer, it's up to you. |
Me too.
No, I just give some ideas and execute them. The maintainer is @sindresorhus :)
What do you mean? The rewrite fixes both of these issues. |
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. |
Your fix unfortunately breaks a lot of other tests. And FYI, running
I have no clue why (EDIT: typescript-eslint/typescript-eslint/issues/1746) For now I use |
|
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)); |
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.
t.notThrows(() => normalizeArguments('http://localhost', options)); | |
t.notThrows(() => { | |
normalizeArguments('http://localhost', options); | |
}); |
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 |
Fixed in #1051 |
Restores the original
options.url
after calling the init hooks.Fixes #1118
Checklist