When using an extended got instance, with default searchParams and using a URL with a querystring, got will duplicate the default parameters and overwrite the URL's querystring.
See test code below, documentation says that passing in a searchParams option will overwrite the url's querystring but in this case got overwrites _and_ duplicates parameters.
According to [docs] got should delete the urls querystring and overwrite it with the searchParams option.
const got = require('got').extend({
searchParams: new URLSearchParams({ foo: '123' }),
// simple hook to print the final URL
hooks: {
beforeRequest: [
({ url }) => console.log('url %s', url)
]
}
});
(async () => {
await got('https://example.com?bar=456'); // https://example.com/?foo=123&foo=123
await got(new URL('https://example.com?bar=456')); // https://example.com/?foo=123&foo=123
await got('https://example.com', { searchParams: new URLSearchParams({ bar: '456' }) }); // https://example.com/?bar=456&foo=123
await got(new URL('https://example.com'), { searchParams: new URLSearchParams({ bar: '456' }) }); // https://example.com/?bar=456&foo=123
})()
await got('https://example.com', { searchParams: new URLSearchParams({ bar: '456' }) }); // https://example.com/?bar=456&foo=123
await got(new URL('https://example.com'), { searchParams: new URLSearchParams({ bar: '456' }) }); // https://example.com/?bar=456&foo=123
The last two examples are perfectly valid. The first two indicate a bug.
Please note that in the first two you define options.searchParams through the defauts, so the URLs will be https://example.com/?foo=123 in both cases. To bypass this, simply pass searchParams as undefined precisely.