Fix `searchParams` option merging with `URLSearchParams` instances (#755)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent 2b0a100 · Browse files · 2 files·+137 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+137 added−0 removed
- tests:+80 added−0 removed
- other:+57 added−0 removed
Provenance
- Commit
c847eec- Tree
3881841- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2b0a100
2 files changed+137 added−0 removed
modified:source/utils/merge.ts+57 added
42 unmodified lines434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949 unmodified lines10410510610710810911011111211311411511611711811912012112212312412511 unmodified lines13713813914014114214314414514614714811 unmodified lines42 unmodified lines beforeError: newHookValue(original, incoming, 'beforeError'), });
const appendSearchParameters = (target: any, source: any): URLSearchParams => { const result = new URLSearchParams();
for (const input of [target, source]) { if (input === undefined) { continue; }
if (input instanceof URLSearchParams) { for (const [key, value] of input.entries()) { result.append(key, value); } } else if (Array.isArray(input)) { for (const pair of input) { if (!Array.isArray(pair) || pair.length !== 2) { throw new TypeError('Array search parameters must be provided in [[key, value], ...] format'); }
result.append(String(pair[0]), String(pair[1])); } } else if (isObject(input)) { for (const [key, value] of Object.entries(input)) { if (value !== undefined) { result.append(key, String(value)); } } } else { // String const parameters = new URLSearchParams(input); for (const [key, value] of parameters.entries()) { result.append(key, value); } } }
return result;};
// TODO: Make this strongly-typed (no `any`).export const deepMerge = <T>(...sources: Array<Partial<T> | undefined>): T => { let returnValue: any = {}; let headers = {}; let hooks = {}; let searchParameters: any; const signals: AbortSignal[] = [];
for (const source of sources) { if (Array.isArray(source)) {9 unmodified lines signals.push(value); continue; }
// Special handling for searchParams if (key === 'searchParams') { if (value === undefined || value === null) { // Explicit undefined or null removes searchParams searchParameters = undefined; } else { // First source: keep as-is to preserve type (string/object/URLSearchParams) // Subsequent sources: merge and convert to URLSearchParams searchParameters = searchParameters === undefined ? value : appendSearchParameters(searchParameters, value); }
continue; }
if (isObject(value) && key in returnValue) { value = deepMerge(returnValue[key], value); }
11 unmodified lines } } }
if (searchParameters !== undefined) { returnValue.searchParams = searchParameters; }
if (signals.length > 0) { if (signals.length === 1) { returnValue.signal = signals[0]; } else if (supportsAbortSignal) {11 unmodified linesmodified:test/main.ts+80 added
433 unmodified lines434 435 await server.close();436 });437 438+test('merges plain object searchParams with URLSearchParams', async t => {439+ const server = await createHttpTestServer();440+ server.get('/', (request, response) => {441+ response.end(request.url);442+ });443+444+ const client = ky.create({searchParams: {api: '123'}});445+ const response = await client.get(server.url, {446+ searchParams: new URLSearchParams({_limit_: '1'}),447+ });448+449+ const url = await response.text();450+ t.true(url.includes('api=123'), `URL should contain api=123, got: ${url}`);451+ t.true(url.includes('_limit_=1'), `URL should contain _limit_=1, got: ${url}`);452+ t.false(url.includes('[object Object]'), `URL should not contain [object Object], got: ${url}`);453+ t.false(url.includes('headers'), `URL should not contain 'headers', got: ${url}`);454+455+ await server.close();456+});457+458+test('merges URLSearchParams with plain object searchParams', async t => {459+ const server = await createHttpTestServer();460+ server.get('/', (request, response) => {461+ response.end(request.url);462+ });463+464+ const client = ky.create({searchParams: new URLSearchParams({api: '123'})});465+ const response = await client.get(server.url, {466+ searchParams: {_limit_: '1'},467+ });468+469+ const url = await response.text();470+ t.true(url.includes('api=123'), `URL should contain api=123, got: ${url}`);471+ t.true(url.includes('_limit_=1'), `URL should contain _limit_=1, got: ${url}`);472+ t.false(url.includes('[object Object]'), `URL should not contain [object Object], got: ${url}`);473+474+ await server.close();475+});476+477+test('merges URLSearchParams with URLSearchParams', async t => {478+ const server = await createHttpTestServer();479+ server.get('/', (request, response) => {480+ response.end(request.url);481+ });482+483+ const client = ky.create({searchParams: new URLSearchParams({api: '123'})});484+ const response = await client.get(server.url, {485+ searchParams: new URLSearchParams({_limit_: '1'}),486+ });487+488+ const url = await response.text();489+ t.true(url.includes('api=123'), `URL should contain api=123, got: ${url}`);490+ t.true(url.includes('_limit_=1'), `URL should contain _limit_=1, got: ${url}`);491+ t.false(url.includes('[object Object]'), `URL should not contain [object Object], got: ${url}`);492+493+ await server.close();494+});495+496+test('merges searchParams with duplicate keys', async t => {497+ const server = await createHttpTestServer();498+ server.get('/', (request, response) => {499+ response.end(request.url);500+ });501+502+ const client = ky.create({searchParams: new URLSearchParams({filter: 'active'})});503+ const response = await client.get(server.url, {504+ searchParams: new URLSearchParams({filter: 'recent', _limit_: '10'}),505+ });506+507+ const urlString = await response.text();508+ const url = new URL(urlString, server.url);509+ const filterValues = url.searchParams.getAll('filter');510+511+ t.deepEqual(filterValues.sort(), ['active', 'recent'], `Both filter values should be present, got: ${JSON.stringify(filterValues)}`);512+ t.is(url.searchParams.get('_limit_'), '10', `URL should contain _limit_=10, got: ${urlString}`);513+ t.false(urlString.includes('[object Object]'), `URL should not contain [object Object], got: ${urlString}`);514+515+ await server.close();516+});517+518 test('throwHttpErrors option', async t => {519 const server = await createHttpTestServer();520 server.get('/', (_request, response) => {521 response.sendStatus(500);524 unmodified lines