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
Provenance
- Commit
91ea8a9- Tree
f6b3e8a- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
7c1272b
4 files changed+114 added−10 removed
modified:source/types/options.ts+1 added-1 removed
114 unmodified lines115 Search parameters to include in the request URL. Setting this will merge with any existing search parameters in the input URL.116 117 Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).118 119- 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'`.120 121 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 linesmodified:source/utils/merge.ts+6 added-8 removed
180 unmodified lines181 export const deletedParametersSymbol = Symbol('deletedParameters');182 183 const 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>();187 188 for (const input of [target, source]) {189 if (input === undefined) {190 continue;191 }192 193 if (input instanceof URLSearchParams) {193- for (const [key, value] of input.entries()) {194- result.append(key, value);195- deleted.delete(key);196- }197-194+ // 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 }202+203+ 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 }211 212 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 linesmodified:test/main.ts+106 added
1805 unmodified lines1806 const url = new URL(await response.text(), server.url);1807 t.is(url.searchParams.get('foo'), '2');1808 });1809 1810+test('re-adding a key after an earlier deletion still removes it from the input URL', async t => {1811+ const server = await createHttpTestServer(t);1812+1813+ server.get('/', (request, response) => {1814+ response.end(request.url);1815+ });1816+1817+ const api = ky.create({searchParams: {page: undefined}});1818+1819+ const response = await api.get(`${server.url}?page=5&keep=1`, {1820+ searchParams: {page: '2'},1821+ });1822+1823+ 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+});1827+1828+test('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);1830+1831+ server.get('/', (request, response) => {1832+ response.end(request.url);1833+ });1834+1835+ const api = ky.create({searchParams: {page: '1'}})1836+ .extend({searchParams: {page: undefined}})1837+ .extend({searchParams: {page: '2'}});1838+1839+ const response = await api.get(`${server.url}?page=5`);1840+1841+ const url = new URL(await response.text(), server.url);1842+ t.deepEqual(url.searchParams.getAll('page'), ['2']);1843+});1844+1845+test('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);1847+1848+ server.get('/', (request, response) => {1849+ response.end(request.url);1850+ });1851+1852+ const api = ky.create({searchParams: {page: undefined}});1853+1854+ const response = await api.get(`${server.url}?page=5`, {1855+ searchParams: [['page', '2'], ['page', '3']],1856+ });1857+1858+ const url = new URL(await response.text(), server.url);1859+ t.deepEqual(url.searchParams.getAll('page'), ['2', '3']);1860+});1861+1862+test('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);1864+1865+ server.get('/', (request, response) => {1866+ response.end(request.url);1867+ });1868+1869+ const api = ky.create({searchParams: {page: undefined}});1870+1871+ const response = await api.get(`${server.url}?page=5`, {1872+ searchParams: 'page=2',1873+ });1874+1875+ const url = new URL(await response.text(), server.url);1876+ t.deepEqual(url.searchParams.getAll('page'), ['2']);1877+});1878+1879+test('a deletion after a re-add still wins', async t => {1880+ const server = await createHttpTestServer(t);1881+1882+ server.get('/', (request, response) => {1883+ response.end(request.url);1884+ });1885+1886+ const api = ky.create({searchParams: {page: undefined}})1887+ .extend({searchParams: {page: '2'}})1888+ .extend({searchParams: {page: undefined}});1889+1890+ const response = await api.get(`${server.url}?page=5&keep=1`);1891+1892+ 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+});1896+1897+test('merging a URLSearchParams layer that deleted and re-added a key keeps the re-added value', async t => {1898+ const server = await createHttpTestServer(t);1899+1900+ server.get('/', (request, response) => {1901+ response.end(request.url);1902+ });1903+1904+ const api = ky.create({searchParams: {page: '1'}})1905+ .extend({searchParams: {page: undefined}})1906+ .extend({searchParams: {page: '2'}})1907+ .extend({searchParams: {other: '3'}});1908+1909+ const response = await api.get(`${server.url}?page=5`);1910+1911+ 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+});1915+1916 test('deletion from a replaceOption(...) boundary', async t => {1917 const server = await createHttpTestServer(t);1918 1919 server.get('/', (request, response) => {1671 unmodified linesmodified:readme.md+1 added-1 removed
195 unmodified lines196 Search parameters to include in the request URL. Setting this will merge with any existing search parameters in the input URL.197 198 Accepts any value supported by [`URLSearchParams()`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams).199 200-When passing an object, setting a value to `undefined` deletes the parameter, while `null` values are preserved and converted to the string `'null'`.200+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'`.201 202 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.203 204 ##### baseUrl1652 unmodified lines