index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. export 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. @default true
  5. */
  6. readonly decode?: boolean;
  7. /**
  8. @default 'none'
  9. - `bracket`: Parse arrays with bracket representation:
  10. ```
  11. import queryString = require('query-string');
  12. queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
  13. //=> {foo: ['1', '2', '3']}
  14. ```
  15. - `index`: Parse arrays with index representation:
  16. ```
  17. import queryString = require('query-string');
  18. queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
  19. //=> {foo: ['1', '2', '3']}
  20. ```
  21. - `comma`: Parse arrays with elements separated by comma:
  22. ```
  23. import queryString = require('query-string');
  24. queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
  25. //=> {foo: ['1', '2', '3']}
  26. ```
  27. - `separator`: Parse arrays with elements separated by a custom character:
  28. ```
  29. import queryString = require('query-string');
  30. queryString.parse('foo=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
  31. //=> {foo: ['1', '2', '3']}
  32. ```
  33. - `none`: Parse arrays with elements using duplicate keys:
  34. ```
  35. import queryString = require('query-string');
  36. queryString.parse('foo=1&foo=2&foo=3');
  37. //=> {foo: ['1', '2', '3']}
  38. ```
  39. */
  40. readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
  41. /**
  42. The character used to separate array elements when using `{arrayFormat: 'separator'}`.
  43. @default ,
  44. */
  45. readonly arrayFormatSeparator?: string;
  46. /**
  47. Supports both `Function` as a custom sorting function or `false` to disable sorting.
  48. If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
  49. @default true
  50. @example
  51. ```
  52. import queryString = require('query-string');
  53. const order = ['c', 'a', 'b'];
  54. queryString.parse('?a=one&b=two&c=three', {
  55. sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
  56. });
  57. //=> {c: 'three', a: 'one', b: 'two'}
  58. ```
  59. @example
  60. ```
  61. import queryString = require('query-string');
  62. queryString.parse('?a=one&c=three&b=two', {sort: false});
  63. //=> {a: 'one', c: 'three', b: 'two'}
  64. ```
  65. */
  66. readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
  67. /**
  68. Parse the value as a number type instead of string type if it's a number.
  69. @default false
  70. @example
  71. ```
  72. import queryString = require('query-string');
  73. queryString.parse('foo=1', {parseNumbers: true});
  74. //=> {foo: 1}
  75. ```
  76. */
  77. readonly parseNumbers?: boolean;
  78. /**
  79. Parse the value as a boolean type instead of string type if it's a boolean.
  80. @default false
  81. @example
  82. ```
  83. import queryString = require('query-string');
  84. queryString.parse('foo=true', {parseBooleans: true});
  85. //=> {foo: true}
  86. ```
  87. */
  88. readonly parseBooleans?: boolean;
  89. /**
  90. Parse the fragment identifier from the URL and add it to result object.
  91. @default false
  92. @example
  93. ```
  94. import queryString = require('query-string');
  95. queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
  96. //=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
  97. ```
  98. */
  99. readonly parseFragmentIdentifier?: boolean;
  100. }
  101. export interface ParsedQuery<T = string> {
  102. [key: string]: T | T[] | null;
  103. }
  104. /**
  105. Parse a query string into an object. Leading `?` or `#` are ignored, so you can pass `location.search` or `location.hash` directly.
  106. 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`.
  107. @param query - The query string to parse.
  108. */
  109. export function parse(query: string, options: {parseBooleans: true, parseNumbers: true} & ParseOptions): ParsedQuery<string | boolean | number>;
  110. export function parse(query: string, options: {parseBooleans: true} & ParseOptions): ParsedQuery<string | boolean>;
  111. export function parse(query: string, options: {parseNumbers: true} & ParseOptions): ParsedQuery<string | number>;
  112. export function parse(query: string, options?: ParseOptions): ParsedQuery;
  113. export interface ParsedUrl {
  114. readonly url: string;
  115. readonly query: ParsedQuery;
  116. /**
  117. The fragment identifier of the URL.
  118. Present when the `parseFragmentIdentifier` option is `true`.
  119. */
  120. readonly fragmentIdentifier?: string;
  121. }
  122. /**
  123. Extract the URL and the query string as an object.
  124. If the `parseFragmentIdentifier` option is `true`, the object will also contain a `fragmentIdentifier` property.
  125. @param url - The URL to parse.
  126. @example
  127. ```
  128. import queryString = require('query-string');
  129. queryString.parseUrl('https://foo.bar?foo=bar');
  130. //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
  131. queryString.parseUrl('https://foo.bar?foo=bar#xyz', {parseFragmentIdentifier: true});
  132. //=> {url: 'https://foo.bar', query: {foo: 'bar'}, fragmentIdentifier: 'xyz'}
  133. ```
  134. */
  135. export function parseUrl(url: string, options?: ParseOptions): ParsedUrl;
  136. export interface StringifyOptions {
  137. /**
  138. 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.
  139. @default true
  140. */
  141. readonly strict?: boolean;
  142. /**
  143. [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
  144. @default true
  145. */
  146. readonly encode?: boolean;
  147. /**
  148. @default 'none'
  149. - `bracket`: Serialize arrays using bracket representation:
  150. ```
  151. import queryString = require('query-string');
  152. queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
  153. //=> 'foo[]=1&foo[]=2&foo[]=3'
  154. ```
  155. - `index`: Serialize arrays using index representation:
  156. ```
  157. import queryString = require('query-string');
  158. queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
  159. //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
  160. ```
  161. - `comma`: Serialize arrays by separating elements with comma:
  162. ```
  163. import queryString = require('query-string');
  164. queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
  165. //=> 'foo=1,2,3'
  166. ```
  167. - `separator`: Serialize arrays by separating elements with character:
  168. ```
  169. import queryString = require('query-string');
  170. queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'separator', arrayFormatSeparator: '|'});
  171. //=> 'foo=1|2|3'
  172. ```
  173. - `none`: Serialize arrays by using duplicate keys:
  174. ```
  175. import queryString = require('query-string');
  176. queryString.stringify({foo: [1, 2, 3]});
  177. //=> 'foo=1&foo=2&foo=3'
  178. ```
  179. */
  180. readonly arrayFormat?: 'bracket' | 'index' | 'comma' | 'separator' | 'none';
  181. /**
  182. The character used to separate array elements when using `{arrayFormat: 'separator'}`.
  183. @default ,
  184. */
  185. readonly arrayFormatSeparator?: string;
  186. /**
  187. Supports both `Function` as a custom sorting function or `false` to disable sorting.
  188. If omitted, keys are sorted using `Array#sort`, which means, converting them to strings and comparing strings in Unicode code point order.
  189. @default true
  190. @example
  191. ```
  192. import queryString = require('query-string');
  193. const order = ['c', 'a', 'b'];
  194. queryString.stringify({a: 1, b: 2, c: 3}, {
  195. sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
  196. });
  197. //=> 'c=3&a=1&b=2'
  198. ```
  199. @example
  200. ```
  201. import queryString = require('query-string');
  202. queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
  203. //=> 'b=1&c=2&a=3'
  204. ```
  205. */
  206. readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
  207. /**
  208. Skip keys with `null` as the value.
  209. Note that keys with `undefined` as the value are always skipped.
  210. @default false
  211. @example
  212. ```
  213. import queryString = require('query-string');
  214. queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
  215. skipNull: true
  216. });
  217. //=> 'a=1&d=4'
  218. queryString.stringify({a: undefined, b: null}, {
  219. skipNull: true
  220. });
  221. //=> ''
  222. ```
  223. */
  224. readonly skipNull?: boolean;
  225. /**
  226. Skip keys with an empty string as the value.
  227. @default false
  228. @example
  229. ```
  230. import queryString = require('query-string');
  231. queryString.stringify({a: 1, b: '', c: '', d: 4}, {
  232. skipEmptyString: true
  233. });
  234. //=> 'a=1&d=4'
  235. ```
  236. @example
  237. ```
  238. import queryString = require('query-string');
  239. queryString.stringify({a: '', b: ''}, {
  240. skipEmptyString: true
  241. });
  242. //=> ''
  243. ```
  244. */
  245. readonly skipEmptyString?: boolean;
  246. }
  247. export type Stringifiable = string | boolean | number | null | undefined;
  248. export type StringifiableRecord = Record<
  249. string,
  250. Stringifiable | readonly Stringifiable[]
  251. >;
  252. /**
  253. Stringify an object into a query string and sort the keys.
  254. */
  255. export function stringify(
  256. // TODO: Use the below instead when the following TS issues are fixed:
  257. // - https://github.com/microsoft/TypeScript/issues/15300
  258. // - https://github.com/microsoft/TypeScript/issues/42021
  259. // Context: https://github.com/sindresorhus/query-string/issues/298
  260. // object: StringifiableRecord,
  261. object: Record<string, any>,
  262. options?: StringifyOptions
  263. ): string;
  264. /**
  265. Extract a query string from a URL that can be passed into `.parse()`.
  266. Note: This behaviour can be changed with the `skipNull` option.
  267. */
  268. export function extract(url: string): string;
  269. export interface UrlObject {
  270. readonly url: string;
  271. /**
  272. Overrides queries in the `url` property.
  273. */
  274. readonly query?: StringifiableRecord;
  275. /**
  276. Overrides the fragment identifier in the `url` property.
  277. */
  278. readonly fragmentIdentifier?: string;
  279. }
  280. /**
  281. 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)
  282. Query items in the `query` property overrides queries in the `url` property.
  283. The `fragmentIdentifier` property overrides the fragment identifier in the `url` property.
  284. @example
  285. ```
  286. queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
  287. //=> 'https://foo.bar?foo=bar'
  288. queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
  289. //=> 'https://foo.bar?foo=bar'
  290. queryString.stringifyUrl({
  291. url: 'https://foo.bar',
  292. query: {
  293. top: 'foo'
  294. },
  295. fragmentIdentifier: 'bar'
  296. });
  297. //=> 'https://foo.bar?top=foo#bar'
  298. ```
  299. */
  300. export function stringifyUrl(
  301. object: UrlObject,
  302. options?: StringifyOptions
  303. ): string;
  304. /**
  305. Pick query parameters from a URL.
  306. @param url - The URL containing the query parameters to pick.
  307. @param keys - The names of the query parameters to keep. All other query parameters will be removed from the URL.
  308. @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`.
  309. @returns The URL with the picked query parameters.
  310. @example
  311. ```
  312. queryString.pick('https://foo.bar?foo=1&bar=2#hello', ['foo']);
  313. //=> 'https://foo.bar?foo=1#hello'
  314. queryString.pick('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
  315. //=> 'https://foo.bar?bar=2#hello'
  316. ```
  317. */
  318. export function pick(
  319. url: string,
  320. keys: readonly string[],
  321. options?: ParseOptions & StringifyOptions
  322. ): string
  323. export function pick(
  324. url: string,
  325. filter: (key: string, value: string | boolean | number) => boolean,
  326. options?: {parseBooleans: true, parseNumbers: true} & ParseOptions & StringifyOptions
  327. ): string
  328. export function pick(
  329. url: string,
  330. filter: (key: string, value: string | boolean) => boolean,
  331. options?: {parseBooleans: true} & ParseOptions & StringifyOptions
  332. ): string
  333. export function pick(
  334. url: string,
  335. filter: (key: string, value: string | number) => boolean,
  336. options?: {parseNumbers: true} & ParseOptions & StringifyOptions
  337. ): string
  338. /**
  339. Exclude query parameters from a URL. Like `.pick()` but reversed.
  340. @param url - The URL containing the query parameters to exclude.
  341. @param keys - The names of the query parameters to remove. All other query parameters will remain in the URL.
  342. @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`.
  343. @returns The URL without the excluded the query parameters.
  344. @example
  345. ```
  346. queryString.exclude('https://foo.bar?foo=1&bar=2#hello', ['foo']);
  347. //=> 'https://foo.bar?bar=2#hello'
  348. queryString.exclude('https://foo.bar?foo=1&bar=2#hello', (name, value) => value === 2, {parseNumbers: true});
  349. //=> 'https://foo.bar?foo=1#hello'
  350. ```
  351. */
  352. export function exclude(
  353. url: string,
  354. keys: readonly string[],
  355. options?: ParseOptions & StringifyOptions
  356. ): string
  357. export function exclude(
  358. url: string,
  359. filter: (key: string, value: string | boolean | number) => boolean,
  360. options?: {parseBooleans: true, parseNumbers: true} & ParseOptions & StringifyOptions
  361. ): string
  362. export function exclude(
  363. url: string,
  364. filter: (key: string, value: string | boolean) => boolean,
  365. options?: {parseBooleans: true} & ParseOptions & StringifyOptions
  366. ): string
  367. export function exclude(
  368. url: string,
  369. filter: (key: string, value: string | number) => boolean,
  370. options?: {parseNumbers: true} & ParseOptions & StringifyOptions
  371. ): string