Closed asomethings closed 3 years ago
Would you be able to submit a pull request with one or more failing tests? That would help to get this fixed faster.
@sindresorhus Sorry for the late reply. I just found out that it seems to be working as intended referring to normalize-arguments.ts test. It seems the test intends to reset the searchParams by putting undefined
value in it.
Can you confirm if it's intended behaviour?
@asomethings It could be intended behavior, but I still don't like it as it's ambiguous. Anyway #1667 (merged, yay!) is much more stricter when providing options. In order to retain the previous options, simply omit them (or, if defined, use the delete
operator).
Currently, only these options accept undefined
: searchParams
, cookieJar
and responseType
.
The following will reset those values:
instance(..., {searchParams: undefined}})
instance(..., {cookieJar: undefined}})
instance(..., {responseType: undefined}})
instance(..., {prefixUrl: ''})
instance(..., {agent: {http: undefined, https: undefined, http2: undefined}})
instance(..., {context: {token: undefined, ...}})
instance(..., {httpsOptions: {rejectUnauthorized: undefined, ...}})
instance(..., {cacheOptions: {immutableMinTimeToLive: undefined, ...}})
instance(..., {headers: {'user-agent': undefined, ...}})
instance(..., {timeout: {request: undefined, ...}})
In order to reset other options such as retry
and pagination
:
instance(..., {retry: got.defaults.options.retry})
instance(..., {pagination: got.defaults.options.pagination})
Note that instance(..., {hooks: got.defaults.options.hooks})
has no effect. There are two ways to reset hooks
:
const createEmptyHooks = () => {
return {
init: [],
beforeRequest: [],
beforeError: [],
beforeRedirect: [],
beforeRetry: [],
afterResponse: []
};
};
// 1.
const secondInstance = instance.extend({mutableDefaults: true});
secondInstance.defaults.options.hooks = emptyHooks;
await secondInstance(...);
// 2.
await instance(..., {
hooks: {
init: [
(options, self) => {
self.hooks = createEmptyHooks();
}
]
}
});
We could implement instance(..., {hooks: undefined})
, but you may ask how to reset particular hooks? Well, you need to use the above. So hooks
doesn't accept undefined
in order to be more transparent.
Note that the above comment refers to the upcoming version of Got (12).
Describe the bug
Actual behavior
Does not merge
searchParams
whensearchParams: undefined
making a request. But, sends defaultsearchParams
whenExpected behavior
Should pass default
searchParams
even though requestedsearchParams
is undefinedCode to reproduce
Checklist