Skip to content

Commit 7b3cbfd

Browse files
committed
Revert serachParameters to searchParams
1 parent 3c23eea commit 7b3cbfd

File tree

6 files changed

+79
-53
lines changed

6 files changed

+79
-53
lines changed

source/core/options.ts

+33-13
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,18 @@ All parsing methods supported by Got.
498498
*/
499499
export type ResponseType = 'json' | 'buffer' | 'text';
500500

501-
export type InternalsType = Except<Options, 'followRedirects' | 'auth' | 'toJSON' | 'merge' | 'createNativeRequestOptions' | 'getRequestFunction' | 'getFallbackRequestFunction' | 'freeze'>;
501+
type OptionsToSkip =
502+
'searchParameters' |
503+
'followRedirects' |
504+
'auth' |
505+
'toJSON' |
506+
'merge' |
507+
'createNativeRequestOptions' |
508+
'getRequestFunction' |
509+
'getFallbackRequestFunction' |
510+
'freeze';
511+
512+
export type InternalsType = Except<Options, OptionsToSkip>;
502513

503514
export type OptionsError = NodeJS.ErrnoException & {options: Options};
504515

@@ -545,7 +556,7 @@ const defaultInternals: Options['_internals'] = {
545556
json: undefined,
546557
cookieJar: undefined,
547558
ignoreInvalidCookies: false,
548-
searchParameters: undefined,
559+
searchParams: undefined,
549560
dnsLookup: undefined,
550561
dnsCache: undefined,
551562
context: {},
@@ -689,7 +700,7 @@ const cloneInternals = (internals: typeof defaultInternals): typeof defaultInter
689700
beforeRetry: [...hooks.beforeRetry],
690701
afterResponse: [...hooks.afterResponse]
691702
},
692-
searchParameters: internals.searchParameters ? new URLSearchParams(internals.searchParameters as URLSearchParams) : undefined,
703+
searchParameters: internals.searchParams ? new URLSearchParams(internals.searchParams as URLSearchParams) : undefined,
693704
pagination: {...internals.pagination}
694705
};
695706

@@ -1164,9 +1175,9 @@ export default class Options {
11641175
this._internals.password = '';
11651176
}
11661177

1167-
if (this._internals.searchParameters) {
1168-
url.search = (this._internals.searchParameters as URLSearchParams).toString();
1169-
this._internals.searchParameters = undefined;
1178+
if (this._internals.searchParams) {
1179+
url.search = (this._internals.searchParams as URLSearchParams).toString();
1180+
this._internals.searchParams = undefined;
11701181
}
11711182

11721183
if (url.hostname === 'unix') {
@@ -1260,21 +1271,21 @@ export default class Options {
12601271
//=> 'key=a&key=b'
12611272
```
12621273
*/
1263-
get searchParameters(): string | SearchParameters | URLSearchParams | undefined {
1274+
get searchParams(): string | SearchParameters | URLSearchParams | undefined {
12641275
if (this._internals.url) {
12651276
return (this._internals.url as URL).searchParams;
12661277
}
12671278

1268-
return this._internals.searchParameters;
1279+
return this._internals.searchParams;
12691280
}
12701281

1271-
set searchParameters(value: string | SearchParameters | URLSearchParams | undefined) {
1282+
set searchParams(value: string | SearchParameters | URLSearchParams | undefined) {
12721283
assert.any([is.string, is.object, is.undefined], value);
12731284

12741285
const url = this._internals.url as URL;
12751286

12761287
if (value === undefined) {
1277-
this._internals.searchParameters = undefined;
1288+
this._internals.searchParams = undefined;
12781289

12791290
if (url) {
12801291
url.search = '';
@@ -1283,7 +1294,7 @@ export default class Options {
12831294
return;
12841295
}
12851296

1286-
let searchParameters = (this.searchParameters ?? new URLSearchParams()) as URLSearchParams;
1297+
let searchParameters = (this.searchParams ?? new URLSearchParams()) as URLSearchParams;
12871298
let updated;
12881299

12891300
if (is.string(value) || (value instanceof URLSearchParams)) {
@@ -1315,10 +1326,18 @@ export default class Options {
13151326
}
13161327

13171328
if (!url) {
1318-
this._internals.searchParameters = searchParameters;
1329+
this._internals.searchParams = searchParameters;
13191330
}
13201331
}
13211332

1333+
get searchParameters() {
1334+
throw new Error('The `searchParameters` option does not exist. Use `searchParams` instead.');
1335+
}
1336+
1337+
set searchParameters(_value: unknown) {
1338+
throw new Error('The `searchParameters` option does not exist. Use `searchParams` instead.');
1339+
}
1340+
13221341
get dnsLookup(): CacheableLookup['lookup'] | undefined {
13231342
return this._internals.dnsLookup;
13241343
}
@@ -2168,12 +2187,13 @@ const nonEnumerableProperties = new Set([
21682187
// Getters that always throw
21692188
'auth',
21702189
'followRedirects',
2190+
'searchParameters',
21712191

21722192
// May contain sensitive data
21732193
'username',
21742194
'password',
21752195
'headers',
2176-
'searchParameters',
2196+
'searchParams',
21772197
'url',
21782198

21792199
// Privates

test/arguments.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ test('overrides `searchParams` from options', withServer, async (t, server, got)
123123
const {body} = await got(
124124
'?drop=this',
125125
{
126-
searchParameters: {
126+
searchParams: {
127127
test: 'wow'
128128
}
129129
}
@@ -136,7 +136,7 @@ test('does not duplicate `searchParams`', withServer, async (t, server, got) =>
136136
server.get('/', echoUrl);
137137

138138
const instance = got.extend({
139-
searchParameters: new URLSearchParams({foo: '123'})
139+
searchParams: new URLSearchParams({foo: '123'})
140140
});
141141

142142
const body = await instance('?bar=456').text();
@@ -148,7 +148,7 @@ test('escapes `searchParams` parameter values', withServer, async (t, server, go
148148
server.get('/', echoUrl);
149149

150150
const {body} = await got({
151-
searchParameters: {
151+
searchParams: {
152152
test: 'it’s ok'
153153
}
154154
});
@@ -159,15 +159,16 @@ test('escapes `searchParams` parameter values', withServer, async (t, server, go
159159
test('the `searchParams` option can be a URLSearchParams', withServer, async (t, server, got) => {
160160
server.get('/', echoUrl);
161161

162-
const searchParameters = new URLSearchParams({test: 'wow'});
163-
const {body} = await got({searchParameters});
162+
// eslint-disable-next-line unicorn/prevent-abbreviations
163+
const searchParams = new URLSearchParams({test: 'wow'});
164+
const {body} = await got({searchParams});
164165
t.is(body, '/?test=wow');
165166
});
166167

167168
test('ignores empty searchParams object', withServer, async (t, server, got) => {
168169
server.get('/test', echoUrl);
169170

170-
t.is((await got('test', {searchParameters: {}})).requestUrl.toString(), `${server.url}/test`);
171+
t.is((await got('test', {searchParams: {}})).requestUrl.toString(), `${server.url}/test`);
171172
});
172173

173174
test('throws when passing body with a non payload method', async t => {
@@ -289,9 +290,9 @@ test('`prefixUrl` can be changed if the URL contains the old one', withServer, a
289290
t.is(body, '/');
290291
});
291292

292-
test('throws if the `searchParameters` value is invalid', async t => {
293+
test('throws if the `searchParams` value is invalid', async t => {
293294
await t.throwsAsync(got('https://example.com', {
294-
searchParameters: {
295+
searchParams: {
295296
// @ts-expect-error Error tests
296297
foo: []
297298
}
@@ -392,15 +393,16 @@ test('throws a helpful error when passing `followRedirects`', async t => {
392393

393394
test('merges `searchParams` instances', t => {
394395
const instance = got.extend({
395-
searchParameters: new URLSearchParams('a=1')
396+
searchParams: new URLSearchParams('a=1')
396397
}, {
397-
searchParameters: new URLSearchParams('b=2')
398+
searchParams: new URLSearchParams('b=2')
398399
});
399400

400-
const searchParameters = instance.defaults.options.searchParameters as URLSearchParams;
401+
// eslint-disable-next-line unicorn/prevent-abbreviations
402+
const searchParams = instance.defaults.options.searchParams as URLSearchParams;
401403

402-
t.is(searchParameters.get('a'), '1');
403-
t.is(searchParameters.get('b'), '2');
404+
t.is(searchParams.get('a'), '1');
405+
t.is(searchParams.get('b'), '2');
404406
});
405407

406408
test('throws a helpful error when passing `auth`', async t => {
@@ -477,7 +479,7 @@ test('encodes query string included in input', t => {
477479
test('normalizes search params included in options', t => {
478480
const {url} = new Options({
479481
url: new URL('https://example.com'),
480-
searchParameters: 'a=b c'
482+
searchParams: 'a=b c'
481483
});
482484

483485
t.is(url!.search, '?a=b+c');

test/http.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ test('`searchParams` option', withServer, async (t, server, got) => {
136136
response.end('recent');
137137
});
138138

139-
t.is((await got({searchParameters: {recent: true}})).body, 'recent');
140-
t.is((await got({searchParameters: 'recent=true'})).body, 'recent');
139+
t.is((await got({searchParams: {recent: true}})).body, 'recent');
140+
t.is((await got({searchParams: 'recent=true'})).body, 'recent');
141141
});
142142

143143
test('response contains url', withServer, async (t, server, got) => {

test/normalize-arguments.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ test('should merge options replacing responseType', t => {
1313

1414
test('no duplicated searchParams values', t => {
1515
const options = new Options({
16-
searchParameters: 'string=true&noDuplication=true'
16+
searchParams: 'string=true&noDuplication=true'
1717
}, {
18-
searchParameters: new URLSearchParams({
18+
searchParams: new URLSearchParams({
1919
instance: 'true',
2020
noDuplication: 'true'
2121
})
2222
});
2323

24-
const searchParameters = options.searchParameters as URLSearchParams;
24+
// eslint-disable-next-line unicorn/prevent-abbreviations
25+
const searchParams = options.searchParams as URLSearchParams;
2526

26-
t.is(searchParameters.get('string'), 'true');
27-
t.is(searchParameters.get('instance'), 'true');
28-
t.is(searchParameters.getAll('noDuplication').length, 1);
27+
t.is(searchParams.get('string'), 'true');
28+
t.is(searchParams.get('instance'), 'true');
29+
t.is(searchParams.getAll('noDuplication').length, 1);
2930
});
3031

3132
test('should copy non-numerable properties', t => {
@@ -77,7 +78,7 @@ test('should get username and password from the merged options', t => {
7778
test('null value in search params means empty', t => {
7879
const options = new Options({
7980
url: new URL('http://localhost'),
80-
searchParameters: {
81+
searchParams: {
8182
foo: null
8283
}
8384
});
@@ -88,7 +89,7 @@ test('null value in search params means empty', t => {
8889
test('undefined value in search params means it does not exist', t => {
8990
const options = new Options({
9091
url: new URL('http://localhost'),
91-
searchParameters: {
92+
searchParams: {
9293
foo: undefined
9394
}
9495
});

test/pagination.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const resetPagination = {
1919

2020
const attachHandler = (server: ExtendedHttpTestServer, count: number): void => {
2121
server.get('/', (request, response) => {
22-
const searchParameters = new URLSearchParams(request.url.split('?')[1]);
23-
const page = Number(searchParameters.get('page')) || 1;
22+
// eslint-disable-next-line unicorn/prevent-abbreviations
23+
const searchParams = new URLSearchParams(request.url.split('?')[1]);
24+
const page = Number(searchParams.get('page')) || 1;
2425

2526
if (page < count) {
2627
response.setHeader('link', `<${server.url}/?page=${page + 1}>; rel="next"`);
@@ -548,7 +549,7 @@ test('next url in json response', withServer, async (t, server, got) => {
548549
}
549550

550551
const all = await got.paginate.all('', {
551-
searchParameters: {
552+
searchParams: {
552553
page: 0
553554
},
554555
responseType: 'json',
@@ -566,7 +567,7 @@ test('next url in json response', withServer, async (t, server, got) => {
566567
return {
567568
url: next,
568569
prefixUrl: '',
569-
searchParameters: undefined
570+
searchParams: undefined
570571
};
571572
}
572573
}
@@ -580,7 +581,7 @@ test('next url in json response', withServer, async (t, server, got) => {
580581
]);
581582
});
582583

583-
test('pagination using searchParameters', withServer, async (t, server, got) => {
584+
test('pagination using searchParams', withServer, async (t, server, got) => {
584585
server.get('/', (request, response) => {
585586
const parameters = new URLSearchParams(request.url.slice(2));
586587
const page = Number(parameters.get('page') ?? 0);
@@ -597,7 +598,7 @@ test('pagination using searchParameters', withServer, async (t, server, got) =>
597598
}
598599

599600
const all = await got.paginate.all('', {
600-
searchParameters: {
601+
searchParams: {
601602
page: 0
602603
},
603604
responseType: 'json',
@@ -607,15 +608,16 @@ test('pagination using searchParameters', withServer, async (t, server, got) =>
607608
},
608609
paginate: ({response}) => {
609610
const {next} = response.body;
610-
const searchParameters = response.request.options.searchParameters as URLSearchParams;
611-
const previousPage = Number(searchParameters.get('page'));
611+
// eslint-disable-next-line unicorn/prevent-abbreviations
612+
const searchParams = response.request.options.searchParams as URLSearchParams;
613+
const previousPage = Number(searchParams.get('page'));
612614

613615
if (!next) {
614616
return false;
615617
}
616618

617619
return {
618-
searchParameters: {
620+
searchParams: {
619621
page: previousPage + 1
620622
}
621623
};
@@ -631,7 +633,7 @@ test('pagination using searchParameters', withServer, async (t, server, got) =>
631633
]);
632634
});
633635

634-
test('pagination using extended searchParameters', withServer, async (t, server, got) => {
636+
test('pagination using extended searchParams', withServer, async (t, server, got) => {
635637
server.get('/', (request, response) => {
636638
const parameters = new URLSearchParams(request.url.slice(2));
637639
const page = Number(parameters.get('page') ?? 0);
@@ -648,13 +650,13 @@ test('pagination using extended searchParameters', withServer, async (t, server,
648650
}
649651

650652
const client = got.extend({
651-
searchParameters: {
653+
searchParams: {
652654
limit: 10
653655
}
654656
});
655657

656658
const all = await client.paginate.all('', {
657-
searchParameters: {
659+
searchParams: {
658660
page: 0
659661
},
660662
responseType: 'json',
@@ -664,15 +666,16 @@ test('pagination using extended searchParameters', withServer, async (t, server,
664666
},
665667
paginate: ({response}) => {
666668
const {next} = response.body;
667-
const searchParameters = response.request.options.searchParameters as URLSearchParams;
668-
const previousPage = Number(searchParameters.get('page'));
669+
// eslint-disable-next-line unicorn/prevent-abbreviations
670+
const searchParams = response.request.options.searchParams as URLSearchParams;
671+
const previousPage = Number(searchParams.get('page'));
669672

670673
if (!next) {
671674
return false;
672675
}
673676

674677
return {
675-
searchParameters: {
678+
searchParams: {
676679
page: previousPage + 1
677680
}
678681
};

test/redirects.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ test('searchParams are not breaking redirects', withServer, async (t, server, go
111111
response.end();
112112
});
113113

114-
t.is((await got('relativeSearchParam', {searchParameters: 'bang=1'})).body, 'reached');
114+
t.is((await got('relativeSearchParam', {searchParams: 'bang=1'})).body, 'reached');
115115
});
116116

117117
test('redirects GET and HEAD requests', withServer, async (t, server, got) => {

0 commit comments

Comments
 (0)