Skip to content

mergeOptions isn't merging URLSearchParams instances #1011

@alexghr

Description

@alexghr

Describe the bug

  • Node.js version: -
  • OS & version: -

got.mergeOptions isn't merging instances of URLSearchParams. According to the README is should merge them.

Actual behavior

It looks like later instances of URLSearchParams just overwrite earlier values. (see code sample)

Expected behavior

It should merge the two URLSearchParams instances.

Code to reproduce

const got = require('got');
got.mergeOptions({ searchParams: new URLSearchParams({ foo: 1 }) }, { searchParams: new URLSearchParams({ bar: 2 }) });
// => { ... searchParams: URLSearchParams { 'bar' => '2' } }

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

I think some code got deleted in #921.

merge.ts got moved https://github.com/sindresorhus/got/pull/921/files#diff-5e2f9cb0db248d822e1b167d5ccf8e07. Before the move it had code to deal with instances of URLSearchParams:

got/source/merge.ts

Lines 13 to 21 in 9f4fe33

if (targetValue instanceof URLSearchParams && sourceValue instanceof URLSearchParams) {
const params = new URLSearchParams();
const append = (value: string, key: string): void => params.append(key, value);
targetValue.forEach(append);
sourceValue.forEach(append);
// @ts-ignore https://github.com/microsoft/TypeScript/issues/31661
target[key] = params;

After the move it's missing the code to handle URLSearchParams: https://github.com/szmarczak/got/blob/a7e73f2fe89b1eca00d2f8fff71dffb2caf68c16/source/utils/merge.ts

Metadata

Metadata

Assignees

Labels

bugSomething does not work as it shouldregressionSomething does not work anymore✭ help wanted ✭

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions