Skip to content
Myr

Fix a search parameter removed with `undefined` staying in the input URL when a later layer re-adds it

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Re-adding a key in a later `searchParams` layer dropped the deletion marker, so the key was never removed from the input URL and the old value was sent alongside the new one. The marker now persists, and a merged `URLSearchParams` layer applies its own deletions before its entries so re-added values are kept.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 7c1272b · Browse files · 4 files+114 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+114 added−10 removed

  • tests:+106 added−0 removed
  • other:+8 added−10 removed

4 files+114 added−10 removed

modified:source/types/options.ts+1 added-1 removed
114 unmodified lines
115 Search parameters to include in the request URL. Setting this will merge with any existing search parameters in the input URL.116117 Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).118119 When passing an object, setting a value to `undefined` deletes the parameter, while `null` values are preserved and converted to the string `'null'`.119 When passing an object, setting a value to `undefined` deletes the parameter, including from the input URL, even when a later option layer adds the parameter again. `null` values are preserved and converted to the string `'null'`.120121 When `input` is a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) with a body, the body is sent as a stream, which requires [request stream support](https://caniuse.com/wf-fetch-request-streams) and HTTP/2 for HTTPS connections (in Chromium-based browsers). In environments without request stream support, when `keepalive` is true, or when the effective mode is `'no-cors'`, the inherited body is dropped. A compatible body passed explicitly with the `body` option is still used.122 */123 searchParams?: SearchParamsOption;
371 unmodified lines
modified:source/utils/merge.ts+6 added-8 removed
180 unmodified lines
181export const deletedParametersSymbol = Symbol('deletedParameters');182183const appendSearchParameters = (target: any, source: any): URLSearchParams => {184 const result = new URLSearchParams() as URLSearchParams & {[deletedParametersSymbol]?: Set<string>};185 // Deleted keys stay marked even when a later layer re-adds the key, so the key is still removed from the input URL before the re-added value is appended.186 const deleted = new Set<string>();187188 for (const input of [target, source]) {189 if (input === undefined) {190 continue;191 }192193 if (input instanceof URLSearchParams) {193 for (const [key, value] of input.entries()) {194 result.append(key, value);195 deleted.delete(key);196 }197194 // A merged `URLSearchParams` already applied its own deletions before any re-added entries, so its deletions only apply to what was merged before it.195 const inputDeleted = (input as any)[deletedParametersSymbol] as Set<string> | undefined;196 if (inputDeleted) {197 for (const key of inputDeleted) {198 result.delete(key);199 deleted.add(key);200 }201 }202203 for (const [key, value] of input.entries()) {204 result.append(key, value);205 }206 } else if (Array.isArray(input)) {207 for (const pair of input) {208 if (!Array.isArray(pair) || pair.length !== 2) {209 throw new TypeError('Array search parameters must be provided in [[key, value], ...] format');210 }211212 result.append(String(pair[0]), String(pair[1]));212 deleted.delete(String(pair[0]));213 }214 } else if (isObject(input)) {215 for (const [key, value] of Object.entries(input)) {216 if (value === undefined) {217 result.delete(key);218 deleted.add(key);219 } else {220 result.append(key, String(value));221 deleted.delete(key);221 }222 }223 } else {224 // String225 const parameters = new URLSearchParams(input);226 for (const [key, value] of parameters.entries()) {227 result.append(key, value);229 deleted.delete(key);228 }229 }230 }231
145 unmodified lines
modified:test/main.ts+106 added
1805 unmodified lines
1806 const url = new URL(await response.text(), server.url);1807 t.is(url.searchParams.get('foo'), '2');1808});18091810test('re-adding a key after an earlier deletion still removes it from the input URL', async t => {1811 const server = await createHttpTestServer(t);18121813 server.get('/', (request, response) => {1814 response.end(request.url);1815 });18161817 const api = ky.create({searchParams: {page: undefined}});18181819 const response = await api.get(`${server.url}?page=5&keep=1`, {1820 searchParams: {page: '2'},1821 });18221823 const url = new URL(await response.text(), server.url);1824 t.deepEqual(url.searchParams.getAll('page'), ['2']);1825 t.is(url.searchParams.get('keep'), '1');1826});18271828test('re-adding a key after an earlier deletion through .extend() still removes it from the input URL', async t => {1829 const server = await createHttpTestServer(t);18301831 server.get('/', (request, response) => {1832 response.end(request.url);1833 });18341835 const api = ky.create({searchParams: {page: '1'}})1836 .extend({searchParams: {page: undefined}})1837 .extend({searchParams: {page: '2'}});18381839 const response = await api.get(`${server.url}?page=5`);18401841 const url = new URL(await response.text(), server.url);1842 t.deepEqual(url.searchParams.getAll('page'), ['2']);1843});18441845test('re-adding a key with an array after an earlier deletion still removes it from the input URL', async t => {1846 const server = await createHttpTestServer(t);18471848 server.get('/', (request, response) => {1849 response.end(request.url);1850 });18511852 const api = ky.create({searchParams: {page: undefined}});18531854 const response = await api.get(`${server.url}?page=5`, {1855 searchParams: [['page', '2'], ['page', '3']],1856 });18571858 const url = new URL(await response.text(), server.url);1859 t.deepEqual(url.searchParams.getAll('page'), ['2', '3']);1860});18611862test('re-adding a key with a string after an earlier deletion still removes it from the input URL', async t => {1863 const server = await createHttpTestServer(t);18641865 server.get('/', (request, response) => {1866 response.end(request.url);1867 });18681869 const api = ky.create({searchParams: {page: undefined}});18701871 const response = await api.get(`${server.url}?page=5`, {1872 searchParams: 'page=2',1873 });18741875 const url = new URL(await response.text(), server.url);1876 t.deepEqual(url.searchParams.getAll('page'), ['2']);1877});18781879test('a deletion after a re-add still wins', async t => {1880 const server = await createHttpTestServer(t);18811882 server.get('/', (request, response) => {1883 response.end(request.url);1884 });18851886 const api = ky.create({searchParams: {page: undefined}})1887 .extend({searchParams: {page: '2'}})1888 .extend({searchParams: {page: undefined}});18891890 const response = await api.get(`${server.url}?page=5&keep=1`);18911892 const url = new URL(await response.text(), server.url);1893 t.false(url.searchParams.has('page'));1894 t.is(url.searchParams.get('keep'), '1');1895});18961897test('merging a URLSearchParams layer that deleted and re-added a key keeps the re-added value', async t => {1898 const server = await createHttpTestServer(t);18991900 server.get('/', (request, response) => {1901 response.end(request.url);1902 });19031904 const api = ky.create({searchParams: {page: '1'}})1905 .extend({searchParams: {page: undefined}})1906 .extend({searchParams: {page: '2'}})1907 .extend({searchParams: {other: '3'}});19081909 const response = await api.get(`${server.url}?page=5`);19101911 const url = new URL(await response.text(), server.url);1912 t.deepEqual(url.searchParams.getAll('page'), ['2']);1913 t.is(url.searchParams.get('other'), '3');1914});19151916test('deletion from a replaceOption(...) boundary', async t => {1917 const server = await createHttpTestServer(t);19181919 server.get('/', (request, response) => {
1671 unmodified lines
modified:readme.md+1 added-1 removed
195 unmodified lines
196Search parameters to include in the request URL. Setting this will merge with any existing search parameters in the input URL.197198Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).199200When passing an object, setting a value to `undefined` deletes the parameter, while `null` values are preserved and converted to the string `'null'`.200When passing an object, setting a value to `undefined` deletes the parameter, including from the input URL, even when a later option layer adds the parameter again. `null` values are preserved and converted to the string `'null'`.201202When `input` is a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) with a body, the body is sent as a stream, which requires [request stream support](https://caniuse.com/wf-fetch-request-streams) and HTTP/2 for HTTPS connections (in Chromium-based browsers). In environments without request stream support, when `keepalive` is true, or when the effective mode is `'no-cors'`, the inherited body is dropped. A compatible body passed explicitly with the `body` option is still used.203204##### baseUrl
1652 unmodified lines