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
Provenance
- Commit
add0703- Tree
8391338- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
8f28eac
3 files changed+87 added−8 removed
modified:source/core/Ky.ts+8 added-7 removed
434 unmodified lines435 this.request = new globalThis.Request(this.#input, this.#options);436 437 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;440 441+ 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+ }447+448 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 lines470 }471 }472 }473 466- 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- }472-474 // 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 linesmodified:source/utils/merge.ts+8 added-1 removed
86 unmodified lines87 };88 89 export 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+ }97+98+ return copy as T;99 }100 101 if (value instanceof globalThis.Headers) {102 return new globalThis.Headers(value) as T;210 unmodified linesmodified:test/main.ts+71 added
1116 unmodified lines1117 const url = new URL(await response.text(), server.url);1118 t.false(url.searchParams.has('foo'));1119 });1120 1121+test('init hook preserves merged URLSearchParams deletions', async t => {1122+ const server = await createHttpTestServer(t);1123+1124+ server.get('/', (request, response) => {1125+ response.end(request.url);1126+ });1127+1128+ 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+ });1136+1137+ const response = await api.get(`${server.url}?foo=from-url&bar=2`);1138+ const url = new URL(await response.text(), server.url);1139+1140+ t.false(url.searchParams.has('foo'));1141+ t.is(url.searchParams.get('bar'), '2');1142+});1143+1144+test('init hook preserves merged plain object deletions', async t => {1145+ const server = await createHttpTestServer(t);1146+1147+ server.get('/', (request, response) => {1148+ response.end(request.url);1149+ });1150+1151+ 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+ });1159+1160+ const response = await api.get(`${server.url}?foo=from-url&bar=2`);1161+ const url = new URL(await response.text(), server.url);1162+1163+ t.false(url.searchParams.has('foo'));1164+ t.is(url.searchParams.get('bar'), '2');1165+});1166+1167+test('init hook can re-add deleted URLSearchParams keys in place', async t => {1168+ const server = await createHttpTestServer(t);1169+1170+ server.get('/', (request, response) => {1171+ response.end(request.url);1172+ });1173+1174+ 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+ });1184+1185+ const response = await api.get(`${server.url}?foo=from-url&bar=2`);1186+ const url = new URL(await response.text(), server.url);1187+1188+ t.deepEqual(url.searchParams.getAll('foo'), ['2']);1189+ t.is(url.searchParams.get('bar'), '2');1190+});1191+1192 test('re-adding a key after an earlier deletion across merge layers', async t => {1193 const server = await createHttpTestServer(t);1194 1195 server.get('/', (request, response) => {1288 unmodified lines