Skip to content
Myr

Fix init hook URLSearchParams deletions

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

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 8f28eac · Browse files · 3 files+87 added−8 removed
This commit

State

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

Diff stats

3 files+87 added−8 removed

  • tests:+71 added−0 removed
  • other:+16 added−8 removed

3 files+87 added−8 removed

modified:source/core/Ky.ts+8 added-7 removed
434 unmodified lines
435 this.request = new globalThis.Request(this.#input, this.#options);436437 if (hasSearchParameters(this.#options.searchParams)) {438 const url = new URL(this.request.url);439 const deleted = (this.#options.searchParams as any)?.[deletedParametersSymbol] as Set<string> | undefined;440441 if (deleted) {442 // Remove keys from the input URL first so later searchParams entries can intentionally re-add them.443 for (const key of deleted) {444 url.searchParams.delete(key);445 }446 }447448 if (typeof this.#options.searchParams === 'string') {449 const stringSearchParameters = this.#options.searchParams.replace(/^\?/, '');450 if (stringSearchParameters !== '') {451 url.search = url.search ? `${url.search}&${stringSearchParameters}` : `?${stringSearchParameters}`;
18 unmodified lines
470 }471 }472 }473466 const deleted = (this.#options.searchParams as any)?.[deletedParametersSymbol] as Set<string> | undefined;467 if (deleted) {468 for (const key of deleted) {469 url.searchParams.delete(key);470 }471 }472474 // Recreate request with the updated URL. We already have all options in this.#options, including duplex.475 this.request = new globalThis.Request(url, this.#options as RequestInit);476 }477
565 unmodified lines
modified:source/utils/merge.ts+8 added-1 removed
86 unmodified lines
87};8889export const cloneShallow = <T>(value: T): T => {90 if (value instanceof URLSearchParams) {91 return new URLSearchParams(value) as T;91 const copy = new URLSearchParams(value) as URLSearchParams & {[deletedParametersSymbol]?: Set<string>};92 const deleted = (value as URLSearchParams & {[deletedParametersSymbol]?: Set<string>})[deletedParametersSymbol];93 if (deleted) {94 // Preserve internal deletion markers so init-hook cloning does not resurrect params removed during option merging.95 copy[deletedParametersSymbol] = new Set(deleted);96 }9798 return copy as T;99 }100101 if (value instanceof globalThis.Headers) {102 return new globalThis.Headers(value) as T;
210 unmodified lines
modified:test/main.ts+71 added
1116 unmodified lines
1117 const url = new URL(await response.text(), server.url);1118 t.false(url.searchParams.has('foo'));1119});11201121test('init hook preserves merged URLSearchParams deletions', async t => {1122 const server = await createHttpTestServer(t);11231124 server.get('/', (request, response) => {1125 response.end(request.url);1126 });11271128 const api = ky.create({searchParams: new URLSearchParams({foo: '1'})}).extend({1129 searchParams: {foo: undefined},1130 hooks: {1131 init: [1132 () => {}, // eslint-disable-line @typescript-eslint/no-empty-function1133 ],1134 },1135 });11361137 const response = await api.get(`${server.url}?foo=from-url&bar=2`);1138 const url = new URL(await response.text(), server.url);11391140 t.false(url.searchParams.has('foo'));1141 t.is(url.searchParams.get('bar'), '2');1142});11431144test('init hook preserves merged plain object deletions', async t => {1145 const server = await createHttpTestServer(t);11461147 server.get('/', (request, response) => {1148 response.end(request.url);1149 });11501151 const api = ky.create({searchParams: {foo: '1'}}).extend({1152 searchParams: {foo: undefined},1153 hooks: {1154 init: [1155 () => {}, // eslint-disable-line @typescript-eslint/no-empty-function1156 ],1157 },1158 });11591160 const response = await api.get(`${server.url}?foo=from-url&bar=2`);1161 const url = new URL(await response.text(), server.url);11621163 t.false(url.searchParams.has('foo'));1164 t.is(url.searchParams.get('bar'), '2');1165});11661167test('init hook can re-add deleted URLSearchParams keys in place', async t => {1168 const server = await createHttpTestServer(t);11691170 server.get('/', (request, response) => {1171 response.end(request.url);1172 });11731174 const api = ky.create({searchParams: new URLSearchParams({foo: '1'})}).extend({1175 searchParams: {foo: undefined},1176 hooks: {1177 init: [1178 options => {1179 (options.searchParams as URLSearchParams).append('foo', '2');1180 },1181 ],1182 },1183 });11841185 const response = await api.get(`${server.url}?foo=from-url&bar=2`);1186 const url = new URL(await response.text(), server.url);11871188 t.deepEqual(url.searchParams.getAll('foo'), ['2']);1189 t.is(url.searchParams.get('bar'), '2');1190});11911192test('re-adding a key after an earlier deletion across merge layers', async t => {1193 const server = await createHttpTestServer(t);11941195 server.get('/', (request, response) => {
1288 unmodified lines