query-string 7.1.2 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/base.d.ts ADDED
@@ -0,0 +1,561 @@
1
+ export type ParseOptions = {
2
+ /**
3
+ Decode the keys and values. URI components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
4
+
5
+ @default true
6
+ */
7
+ readonly decode?: boolean;
8
+
9
+ /**
10
+ @default 'none'
11
+
12
+ - `bracket`: Parse arrays with bracket representation:
13
+
14
+ ```
15
+ import queryString from 'query-string';
16
+
17
+ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
18
+ //=> {foo: ['1', '2', '3']}
19
+ ```
20
+
21
+ - `index`: Parse arrays with index representation:
22
+
23
+ ```
24
+ import queryString from 'query-string';
25
+
26
+ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
27
+ //=> {foo: ['1', '2', '3']}
28
+ ```
29
+
30
+ - `comma`: Parse arrays with elements separated by comma:
31
+
32
+ ```
33
+ import queryString from 'query-string';
34
+
35
+ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
36
+ //=> {foo: ['1', '2', '3']}
37
+ ```
38
+
39
+ - `separator`: Parse arrays with elements separated by a custom character:
40
+
41
+ ```
42
+ import queryString from 'query-string';
43
+
44
+ queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
45
+ //=> {foo: ['1', '2', '3']}
46
+ ```
47
+
48
+ - `bracket-separator`: Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
49
+
50
+ ```
51
+ import queryString from 'query-string';
52
+
53
+ queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
54
+ //=> {foo: []}
55
+
56
+ queryString.parse('foo[]=', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
57
+ //=> {foo: ['']}
58
+
59
+ queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
60
+ //=> {foo: ['1']}
61
+
62
+ queryString.parse('foo[]=1|2|3', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
63
+ //=> {foo: ['1', '2', '3']}
64
+
65
+ queryString.parse('foo[]=1||3|||6', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
66
+ //=> {foo: ['1', '', 3, '', '', '6']}
67
+
68
+ queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
69
+ //=> {foo: ['1', '2', '3'], bar: 'fluffy', baz:['4']}
70
+ ```
71
+
72
+ - `colon-list-separator`: Parse arrays with parameter names that are explicitly marked with `:list`:
73
+
74
+ ```
75
+ import queryString from 'query-string';
76
+
77
+ queryString.parse('foo:list=one&foo:list=two', {arrayFormat: 'colon-list-separator'});
78
+ //=> {foo: ['one', 'two']}
79
+ ```
80
+
81
+ - `none`: Parse arrays with elements using duplicate keys:
82
+
83
+ ```
84
+ import queryString from 'query-string';
85
+
86
+ queryString.parse('foo=1&foo=2&foo=3');
87
+ //=> {foo: ['1', '2', '3']}
88
+ ```
89
+ */
90
+ readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'colon-list-separator' | 'none';
91
+
92
+ /**
93
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
94
+
95
+ @default ,
96
+ */
97
+ readonly arrayFormatSeparator?: string;
98
+
99
+ /**
100
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
101
+
102
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
103
+
104
+ @default true
105
+
106
+ @example
107
+ ```
108
+ import queryString from 'query-string';
109
+
110
+ const order = ['c', 'a', 'b'];
111
+
112
+ queryString.parse('?a=one&b=two&c=three', {
113
+ sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
114
+ });
115
+ //=> {c: 'three', a: 'one', b: 'two'}
116
+ ```
117
+
118
+ @example
119
+ ```
120
+ import queryString from 'query-string';
121
+
122
+ queryString.parse('?a=one&c=three&b=two', {sort: false});
123
+ //=> {a: 'one', c: 'three', b: 'two'}
124
+ ```
125
+ */
126
+ readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
127
+
128
+ /**
129
+ Parse the value as a number type instead of string type if it's a number.
130
+
131
+ @default false
132
+
133
+ @example
134
+ ```
135
+ import queryString from 'query-string';
136
+
137
+ queryString.parse('foo=1', {parseNumbers: true});
138
+ //=> {foo: 1}
139
+ ```
140
+ */
141
+ readonly parseNumbers?: boolean;
142
+
143
+ /**
144
+ Parse the value as a boolean type instead of string type if it's a boolean.
145
+
146
+ @default false
147
+
148
+ @example
149
+ ```
150
+ import queryString from 'query-string';
151
+
152
+ queryString.parse('foo=true', {parseBooleans: true});
153
+ //=> {foo: true}
154
+ ```
155
+ */
156
+ readonly parseBooleans?: boolean;
157
+
158
+ /**
159
+ Parse the fragment identifier from the URL and add it to result object.
160
+
161
+ @default false
162
+
163
+ @example
164
+ ```
165
+ import queryString from 'query-string';
166
+
167
+ queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
168
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
169
+ ```
170
+ */
171
+ readonly parseFragmentIdentifier?: boolean;
172
+ };
173
+
174
+ export type ParsedQuery<T = string> = Record<string, T | undefined | Array<T | undefined>>;
175
+
176
+ /**
177
+ Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
178
+
179
+ The returned object is created with [`Object.create(null)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) and thus does not have a `prototype`.
180
+
181
+ @param query - The query string to parse.
182
+ */
183
+ export function parse(query: string, options: {parseBooleans: true; parseNumbers: true} & ParseOptions): ParsedQuery<string | boolean | number>;
184
+ export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery<string | boolean>;
185
+ export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery<string | number>;
186
+ export function parse(query: string, options?: ParseOptions): ParsedQuery;
187
+
188
+ export type ParsedUrl = {
189
+ readonly url: string;
190
+ readonly query: ParsedQuery;
191
+
192
+ /**
193
+ The fragment identifier of the URL.
194
+
195
+ Present when the `parseFragmentIdentifier` option is `true`.
196
+ */
197
+ readonly fragmentIdentifier?: string;
198
+ };
199
+
200
+ /**
201
+ Extract the URL and the query string as an object.
202
+
203
+ If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
204
+
205
+ @param url - The URL to parse.
206
+
207
+ @example
208
+ ```
209
+ import queryString from 'query-string';
210
+
211
+ queryString.parseUrl('https://foo.bar?foo=bar');
212
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
213
+
214
+ queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
215
+ //=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
216
+ ```
217
+ */
218
+ export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
219
+
220
+ export type StringifyOptions = {
221
+ /**
222
+ Strictly encode URI components with [`strict-uri-encode`](https://github.com/kevva/strict-uri-encode). It uses [`encodeURIComponent`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to `false`. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
223
+
224
+ @default true
225
+ */
226
+ readonly strict?: boolean;
227
+
228
+ /**
229
+ [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
230
+
231
+ @default true
232
+ */
233
+ readonly encode?: boolean;
234
+
235
+ /**
236
+ @default 'none'
237
+
238
+ - `bracket`: Serialize arrays using bracket representation:
239
+
240
+ ```
241
+ import queryString from 'query-string';
242
+
243
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
244
+ //=> 'foo[]=1&foo[]=2&foo[]=3'
245
+ ```
246
+
247
+ - `index`: Serialize arrays using index representation:
248
+
249
+ ```
250
+ import queryString from 'query-string';
251
+
252
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
253
+ //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
254
+ ```
255
+
256
+ - `comma`: Serialize arrays by separating elements with comma:
257
+
258
+ ```
259
+ import queryString from 'query-string';
260
+
261
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
262
+ //=> 'foo=1,2,3'
263
+
264
+ queryString.stringify({foo: [1, null, '']}, {arrayFormat: 'comma'});
265
+ //=> 'foo=1,,'
266
+ // Note that typing information for null values is lost
267
+ // and `.parse('foo=1,,')` would return `{foo: [1, '', '']}`.
268
+ ```
269
+
270
+ - `separator`: Serialize arrays by separating elements with character:
271
+
272
+ ```
273
+ import queryString from 'query-string';
274
+
275
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
276
+ //=> 'foo=1|2|3'
277
+ ```
278
+
279
+ - `bracket-separator`: Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
280
+
281
+ ```
282
+ import queryString from 'query-string';
283
+
284
+ queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
285
+ //=> 'foo[]'
286
+
287
+ queryString.stringify({foo: ['']}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
288
+ //=> 'foo[]='
289
+
290
+ queryString.stringify({foo: [1]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
291
+ //=> 'foo[]=1'
292
+
293
+ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
294
+ //=> 'foo[]=1|2|3'
295
+
296
+ queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
297
+ //=> 'foo[]=1||3|||6'
298
+
299
+ queryString.stringify({foo: [1, '', 3, null, null, 6]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true});
300
+ //=> 'foo[]=1||3|6'
301
+
302
+ queryString.stringify({foo: [1, 2, 3], bar: 'fluffy', baz: [4]}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
303
+ //=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
304
+ ```
305
+
306
+ - `colon-list-separator`: Serialize arrays with parameter names that are explicitly marked with `:list`:
307
+
308
+ ```js
309
+ import queryString from 'query-string';
310
+
311
+ queryString.stringify({foo: ['one', 'two']}, {arrayFormat: 'colon-list-separator'});
312
+ //=> 'foo:list=one&foo:list=two'
313
+ ```
314
+
315
+ - `none`: Serialize arrays by using duplicate keys:
316
+
317
+ ```
318
+ import queryString from 'query-string';
319
+
320
+ queryString.stringify({foo: [1, 2, 3]});
321
+ //=> 'foo=1&foo=2&foo=3'
322
+ ```
323
+ */
324
+ readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'bracket-separator' | 'colon-list-separator' | 'none';
325
+
326
+ /**
327
+ The character used to separate array elements when using `{arrayFormat: 'separator'}`.
328
+
329
+ @default ,
330
+ */
331
+ readonly arrayFormatSeparator?: string;
332
+
333
+ /**
334
+ Supports both `Function` as a custom sorting function or `false` to disable sorting.
335
+
336
+ If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
337
+
338
+ @default true
339
+
340
+ @example
341
+ ```
342
+ import queryString from 'query-string';
343
+
344
+ const order = ['c', 'a', 'b'];
345
+
346
+ queryString.stringify({a: 1, b: 2, c: 3}, {
347
+ sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
348
+ });
349
+ //=> 'c=3&a=1&b=2'
350
+ ```
351
+
352
+ @example
353
+ ```
354
+ import queryString from 'query-string';
355
+
356
+ queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
357
+ //=> 'b=1&c=2&a=3'
358
+ ```
359
+ */
360
+ readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
361
+
362
+ /**
363
+ Skip keys with `null` as the value.
364
+
365
+ Note that keys with `undefined` as the value are always skipped.
366
+
367
+ @default false
368
+
369
+ @example
370
+ ```
371
+ import queryString from 'query-string';
372
+
373
+ queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
374
+ skipNull: true
375
+ });
376
+ //=> 'a=1&d=4'
377
+
378
+ queryString.stringify({a: undefined, b: null}, {
379
+ skipNull: true
380
+ });
381
+ //=> ''
382
+ ```
383
+ */
384
+ readonly skipNull?: boolean;
385
+
386
+ /**
387
+ Skip keys with an empty string as the value.
388
+
389
+ @default false
390
+
391
+ @example
392
+ ```
393
+ import queryString from 'query-string';
394
+
395
+ queryString.stringify({a: 1, b: '', c: '', d: 4}, {
396
+ skipEmptyString: true
397
+ });
398
+ //=> 'a=1&d=4'
399
+ ```
400
+
401
+ @example
402
+ ```
403
+ import queryString from 'query-string';
404
+
405
+ queryString.stringify({a: '', b: ''}, {
406
+ skipEmptyString: true
407
+ });
408
+ //=> ''
409
+ ```
410
+ */
411
+ readonly skipEmptyString?: boolean;
412
+ };
413
+
414
+ export type Stringifiable = string | boolean | number | null | undefined; // eslint-disable-line @typescript-eslint/ban-types
415
+
416
+ export type StringifiableRecord = Record<
417
+ string,
418
+ Stringifiable | readonly Stringifiable[]
419
+ >;
420
+
421
+ /**
422
+ Stringify an object into a query string and sort the keys.
423
+ */
424
+ export function stringify(
425
+ // TODO: Use the below instead when the following TS issues are fixed:
426
+ // - https://github.com/microsoft/TypeScript/issues/15300
427
+ // - https://github.com/microsoft/TypeScript/issues/42021
428
+ // Context: https://github.com/sindresorhus/query-string/issues/298
429
+ // object: StringifiableRecord,
430
+ object: Record<string, any>,
431
+ options?: StringifyOptions
432
+ ): string;
433
+
434
+ /**
435
+ Extract a query string from a URL that can be passed into `.parse()`.
436
+
437
+ Note: This behaviour can be changed with the `skipNull` option.
438
+ */
439
+ export function extract(url: string): string;
440
+
441
+ export type UrlObject = {
442
+ readonly url: string;
443
+
444
+ /**
445
+ Overrides queries in the `url` property.
446
+ */
447
+ readonly query?: StringifiableRecord;
448
+
449
+ /**
450
+ Overrides the fragment identifier in the `url` property.
451
+ */
452
+ readonly fragmentIdentifier?: string;
453
+ };
454
+
455
+ /**
456
+ Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
457
+
458
+ Query items in the `query` property overrides queries in the `url` property.
459
+
460
+ The `fragmentIdentifier` property overrides the fragment identifier in the `url` property.
461
+
462
+ @example
463
+ ```
464
+ queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
465
+ //=> 'https://foo.bar?foo=bar'
466
+
467
+ queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
468
+ //=> 'https://foo.bar?foo=bar'
469
+
470
+ queryString.stringifyUrl({
471
+ url: 'https://foo.bar',
472
+ query: {
473
+ top: 'foo'
474
+ },
475
+ fragmentIdentifier: 'bar'
476
+ });
477
+ //=> 'https://foo.bar?top=foo#bar'
478
+ ```
479
+ */
480
+ export function stringifyUrl(
481
+ object: UrlObject,
482
+ options?: StringifyOptions
483
+ ): string;
484
+
485
+ /**
486
+ Pick query parameters from a URL.
487
+
488
+ @param url - The URL containing the query parameters to pick.
489
+ @param keys - The names of the query parameters to keep. All other query parameters will be removed from the URL.
490
+ @param filter - A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
491
+
492
+ @returns The URL with the picked query parameters.
493
+
494
+ @example
495
+ ```
496
+ queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']);
497
+ //=> 'https://foo.bar?foo=1#hello'
498
+
499
+ queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
500
+ //=> 'https://foo.bar?bar=2#hello'
501
+ ```
502
+ */
503
+ export function pick(
504
+ url: string,
505
+ keys: readonly string[],
506
+ options?: ParseOptions & StringifyOptions
507
+ ): string;
508
+ export function pick(
509
+ url: string,
510
+ filter: (key: string, value: string | boolean | number) => boolean,
511
+ options?: {parseBooleans: true; parseNumbers: true} & ParseOptions & StringifyOptions
512
+ ): string;
513
+ export function pick(
514
+ url: string,
515
+ filter: (key: string, value: string | boolean) => boolean,
516
+ options?: {parseBooleans: true} & ParseOptions & StringifyOptions
517
+ ): string;
518
+ export function pick(
519
+ url: string,
520
+ filter: (key: string, value: string | number) => boolean,
521
+ options?: {parseNumbers: true} & ParseOptions & StringifyOptions
522
+ ): string;
523
+
524
+ /**
525
+ Exclude query parameters from a URL. Like `.pick()` but reversed.
526
+
527
+ @param url - The URL containing the query parameters to exclude.
528
+ @param keys - The names of the query parameters to remove. All other query parameters will remain in the URL.
529
+ @param filter - A filter predicate that will be provided the name of each query parameter and its value. The `parseNumbers` and `parseBooleans` options also affect `value`.
530
+
531
+ @returns The URL without the excluded the query parameters.
532
+
533
+ @example
534
+ ```
535
+ queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']);
536
+ //=> 'https://foo.bar?bar=2#hello'
537
+
538
+ queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
539
+ //=> 'https://foo.bar?foo=1#hello'
540
+ ```
541
+ */
542
+ export function exclude(
543
+ url: string,
544
+ keys: readonly string[],
545
+ options?: ParseOptions & StringifyOptions
546
+ ): string;
547
+ export function exclude(
548
+ url: string,
549
+ filter: (key: string, value: string | boolean | number) => boolean,
550
+ options?: {parseBooleans: true; parseNumbers: true} & ParseOptions & StringifyOptions
551
+ ): string;
552
+ export function exclude(
553
+ url: string,
554
+ filter: (key: string, value: string | boolean) => boolean,
555
+ options?: {parseBooleans: true} & ParseOptions & StringifyOptions
556
+ ): string;
557
+ export function exclude(
558
+ url: string,
559
+ filter: (key: string, value: string | number) => boolean,
560
+ options?: {parseNumbers: true} & ParseOptions & StringifyOptions
561
+ ): string;