index.d.ts 16 KB

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