Skip to content

Commit ab603a8

Browse files
committed
Document supported value types for stringify()
Closes #312
1 parent ec67fea commit ab603a8

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

base.d.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export type ParseOptions = {
184184
185185
You can also provide a custom function to transform the value. The function will receive the raw string and should return the desired parsed result (see Example 4).
186186
187-
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`. (See Example 5.)
187+
NOTE: Array types (`string[]`, `number[]`) are ignored if `arrayFormat` is set to `'none'`.
188188
189189
@default {}
190190
@@ -325,9 +325,10 @@ export type ParsedUrl = {
325325
/**
326326
Extract the URL and the query string as an object.
327327
328-
If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
329-
330328
@param url - The URL to parse.
329+
@returns An object with a `url` and `query` property.
330+
331+
If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
331332
332333
@example
333334
```
@@ -536,15 +537,33 @@ export type StringifyOptions = {
536537
readonly skipEmptyString?: boolean;
537538
};
538539

540+
/**
541+
Supported value types for query string parameters.
542+
543+
Note: `Symbol`, functions, and objects (except arrays) are not supported and will throw an error when stringified.
544+
*/
539545
export type Stringifiable = string | boolean | number | bigint | null | undefined; // eslint-disable-line @typescript-eslint/ban-types
540546

547+
/**
548+
A record of stringifiable values.
549+
*/
541550
export type StringifiableRecord = Record<
542551
string,
543552
Stringifiable | readonly Stringifiable[]
544553
>;
545554

546555
/**
547-
Stringify an object into a query string and sort the keys.
556+
Stringify an object into a query string and sorting the keys.
557+
558+
@param object - Object to stringify. Supported value types are: `string`, `number`, `bigint`, `boolean`, `null`, `undefined`, and arrays of these types. Other types like `Symbol`, functions, or objects (except arrays) will throw an error.
559+
560+
@example
561+
```
562+
import queryString from 'query-string';
563+
564+
queryString.stringify({foo: 'bar', baz: 42, qux: true});
565+
//=> 'baz=42&foo=bar&qux=true'
566+
```
548567
*/
549568
export function stringify(
550569
// TODO: Use the below instead when the following TS issues are fixed:
@@ -559,7 +578,13 @@ export function stringify(
559578
/**
560579
Extract a query string from a URL that can be passed into `.parse()`.
561580
562-
Note: This behaviour can be changed with the `skipNull` option.
581+
@example
582+
```
583+
import queryString from 'query-string';
584+
585+
queryString.extract('https://foo.bar?foo=bar');
586+
//=> 'foo=bar'
587+
```
563588
*/
564589
export function extract(url: string): string;
565590

@@ -580,6 +605,10 @@ export type UrlObject = {
580605
/**
581606
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)
582607
608+
The `options` are the same as for `.stringify()`.
609+
610+
@returns A string with the URL and a query string.
611+
583612
Query items in the `query` property overrides queries in the `url` property.
584613
585614
The `fragmentIdentifier` property overrides the fragment identifier in the `url` property.

readme.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ Parse a query string into an object. Leading `?` or `#` are ignored, so you can
5252

5353
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`.
5454

55+
```js
56+
queryString.parse('?foo=bar');
57+
//=> {foo: 'bar'}
58+
59+
queryString.parse('#token=secret&name=jhon');
60+
//=> {token: 'secret', name: 'jhon'}
61+
```
62+
5563
#### options
5664

5765
Type: `object`
@@ -318,6 +326,8 @@ Parse the value as a boolean type instead of string type if it's a boolean.
318326
319327
Stringify an object into a query string and sorting the keys.
320328
329+
**Supported value types:** `string`, `number`, `bigint`, `boolean`, `null`, `undefined`, and arrays of these types. Other types like `Symbol`, functions, or objects (except arrays) will throw an error.
330+
321331
#### options
322332
323333
Type: `object`
@@ -516,7 +526,10 @@ queryString.stringify({a: '', b: ''}, {
516526
517527
Extract a query string from a URL that can be passed into `.parse()`.
518528
519-
Note: This behaviour can be changed with the `skipNull` option.
529+
```js
530+
queryString.extract('https://foo.bar?foo=bar');
531+
//=> 'foo=bar'
532+
```
520533
521534
### .parseUrl(string, options?)
522535

0 commit comments

Comments
 (0)