1export interface 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 queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'}); 16 //=> {foo: ['1', '2', '3']} 17 ``` 18 19 - `index`: Parse arrays with index representation: 20 21 ``` 22 queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'}); 23 //=> {foo: ['1', '2', '3']} 24 ``` 25 26 - `comma`: Parse arrays with elements separated by comma: 27 28 ``` 29 queryString.parse('foo=1,2,3', {arrayFormat: 'comma'}); 30 //=> {foo: ['1', '2', '3']} 31 ``` 32 33 - `none`: Parse arrays with elements using duplicate keys: 34 35 ``` 36 queryString.parse('foo=1&foo=2&foo=3'); 37 //=> {foo: ['1', '2', '3']} 38 ``` 39 */ 40 readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none'; 41 42 /** 43 Supports both `Function` as a custom sorting function or `false` to disable sorting. 44 45 If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order. 46 47 @default true 48 49 @example 50 ``` 51 const order = ['c', 'a', 'b']; 52 53 queryString.parse('?a=one&b=two&c=three', { 54 sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight) 55 }); 56 // => {c: 'three', a: 'one', b: 'two'} 57 ``` 58 59 queryString.parse('?a=one&c=three&b=two', {sort: false}); 60 // => {a: 'one', c: 'three', b: 'two'} 61 ``` 62 */ 63 readonly sort?: ((itemLeft: string, itemRight: string) => number) | false; 64 65 /** 66 Parse the value as a number type instead of string type if it's a number. 67 68 @default false 69 70 @example 71 ```js 72 queryString.parse('foo=1', {parseNumbers: true}); 73 //=> {foo: 1} 74 ``` 75 */ 76 readonly parseNumbers?: boolean; 77 78 /** 79 Parse the value as a boolean type instead of string type if it's a boolean. 80 81 @default false 82 83 @example 84 ``` 85 queryString.parse('foo=true', {parseBooleans: true}); 86 //=> {foo: true} 87 ``` 88 */ 89 readonly parseBooleans?: boolean; 90} 91 92export interface ParsedQuery<T = string> { 93 [key: string]: T | T[] | null | undefined; 94} 95 96/** 97Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly. 98 99The 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`. 100 101@param query - The query string to parse. 102*/ 103export function parse(query: string, options: {parseBooleans: true, parseNumbers: true} & ParseOptions): ParsedQuery<string | boolean | number>; 104export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery<string | boolean>; 105export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery<string | number>; 106export function parse(query: string, options?: ParseOptions): ParsedQuery; 107 108export interface ParsedUrl { 109 readonly url: string; 110 readonly query: ParsedQuery; 111} 112 113/** 114Extract the URL and the query string as an object. 115 116@param url - The URL to parse. 117 118@example 119``` 120queryString.parseUrl('https://foo.bar?foo=bar'); 121//=> {url: 'https://foo.bar', query: {foo: 'bar'}} 122``` 123*/ 124export function parseUrl(url: string, options?: ParseOptions): ParsedUrl; 125 126export interface StringifyOptions { 127 /** 128 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. 129 130 @default true 131 */ 132 readonly strict?: boolean; 133 134 /** 135 [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values. 136 137 @default true 138 */ 139 readonly encode?: boolean; 140 141 /** 142 @default 'none' 143 144 - `bracket`: Serialize arrays using bracket representation: 145 146 ``` 147 queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'}); 148 //=> 'foo[]=1&foo[]=2&foo[]=3' 149 ``` 150 151 - `index`: Serialize arrays using index representation: 152 153 ``` 154 queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'}); 155 //=> 'foo[0]=1&foo[1]=2&foo[2]=3' 156 ``` 157 158 - `comma`: Serialize arrays by separating elements with comma: 159 160 ``` 161 queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'}); 162 //=> 'foo=1,2,3' 163 ``` 164 165 - `none`: Serialize arrays by using duplicate keys: 166 167 ``` 168 queryString.stringify({foo: [1, 2, 3]}); 169 //=> 'foo=1&foo=2&foo=3' 170 ``` 171 */ 172 readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'none'; 173 174 /** 175 Supports both `Function` as a custom sorting function or `false` to disable sorting. 176 177 If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order. 178 179 @default true 180 181 @example 182 ``` 183 const order = ['c', 'a', 'b']; 184 185 queryString.stringify({a: 1, b: 2, c: 3}, { 186 sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight) 187 }); 188 // => 'c=3&a=1&b=2' 189 190 queryString.stringify({b: 1, c: 2, a: 3}, {sort: false}); 191 // => 'b=1&c=2&a=3' 192 ``` 193 */ 194 readonly sort?: ((itemLeft: string, itemRight: string) => number) | false; 195} 196 197/** 198Stringify an object into a query string and sort the keys. 199*/ 200export function stringify( 201 object: {[key: string]: any}, 202 options?: StringifyOptions 203): string; 204 205/** 206Extract a query string from a URL that can be passed into `.parse()`. 207*/ 208export function extract(url: string): string; 209