UNPKG

6.49 kBTypeScriptView Raw
1/**
2 * The `querystring` module provides utilities for parsing and formatting URL
3 * query strings. It can be accessed using:
4 *
5 * ```js
6 * const querystring = require('querystring');
7 * ```
8 *
9 * The `querystring` API is considered Legacy. While it is still maintained,
10 * new code should use the `URLSearchParams` API instead.
11 * @deprecated Legacy
12 * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/querystring.js)
13 */
14declare module 'querystring' {
15 interface StringifyOptions {
16 encodeURIComponent?: ((str: string) => string) | undefined;
17 }
18 interface ParseOptions {
19 maxKeys?: number | undefined;
20 decodeURIComponent?: ((str: string) => string) | undefined;
21 }
22 interface ParsedUrlQuery extends NodeJS.Dict<string | string[]> {}
23 interface ParsedUrlQueryInput extends NodeJS.Dict<string | number | boolean | ReadonlyArray<string> | ReadonlyArray<number> | ReadonlyArray<boolean> | null> {}
24 /**
25 * The `querystring.stringify()` method produces a URL query string from a
26 * given `obj` by iterating through the object's "own properties".
27 *
28 * It serializes the following types of values passed in `obj`:[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) |
29 * [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) |
30 * [bigint](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) |
31 * [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) |
32 * [string\[\]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) |
33 * [number\[\]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) |
34 * [bigint\[\]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) |
35 * [boolean\[\]](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) The numeric values must be finite. Any other input values will be coerced to
36 * empty strings.
37 *
38 * ```js
39 * querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
40 * // Returns 'foo=bar&#x26;baz=qux&#x26;baz=quux&#x26;corge='
41 *
42 * querystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');
43 * // Returns 'foo:bar;baz:qux'
44 * ```
45 *
46 * By default, characters requiring percent-encoding within the query string will
47 * be encoded as UTF-8\. If an alternative encoding is required, then an alternative`encodeURIComponent` option will need to be specified:
48 *
49 * ```js
50 * // Assuming gbkEncodeURIComponent function already exists,
51 *
52 * querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
53 * { encodeURIComponent: gbkEncodeURIComponent });
54 * ```
55 * @since v0.1.25
56 * @param obj The object to serialize into a URL query string
57 * @param [sep='&'] The substring used to delimit key and value pairs in the query string.
58 * @param [eq='='] . The substring used to delimit keys and values in the query string.
59 */
60 function stringify(obj?: ParsedUrlQueryInput, sep?: string, eq?: string, options?: StringifyOptions): string;
61 /**
62 * The `querystring.parse()` method parses a URL query string (`str`) into a
63 * collection of key and value pairs.
64 *
65 * For example, the query string `'foo=bar&#x26;abc=xyz&#x26;abc=123'` is parsed into:
66 *
67 * ```js
68 * {
69 * foo: 'bar',
70 * abc: ['xyz', '123']
71 * }
72 * ```
73 *
74 * The object returned by the `querystring.parse()` method _does not_prototypically inherit from the JavaScript `Object`. This means that typical`Object` methods such as `obj.toString()`,
75 * `obj.hasOwnProperty()`, and others
76 * are not defined and _will not work_.
77 *
78 * By default, percent-encoded characters within the query string will be assumed
79 * to use UTF-8 encoding. If an alternative character encoding is used, then an
80 * alternative `decodeURIComponent` option will need to be specified:
81 *
82 * ```js
83 * // Assuming gbkDecodeURIComponent function already exists...
84 *
85 * querystring.parse('w=%D6%D0%CE%C4&#x26;foo=bar', null, null,
86 * { decodeURIComponent: gbkDecodeURIComponent });
87 * ```
88 * @since v0.1.25
89 * @param str The URL query string to parse
90 * @param [sep='&'] The substring used to delimit key and value pairs in the query string.
91 * @param [eq='='] . The substring used to delimit keys and values in the query string.
92 */
93 function parse(str: string, sep?: string, eq?: string, options?: ParseOptions): ParsedUrlQuery;
94 /**
95 * The querystring.encode() function is an alias for querystring.stringify().
96 */
97 const encode: typeof stringify;
98 /**
99 * The querystring.decode() function is an alias for querystring.parse().
100 */
101 const decode: typeof parse;
102 /**
103 * The `querystring.escape()` method performs URL percent-encoding on the given`str` in a manner that is optimized for the specific requirements of URL
104 * query strings.
105 *
106 * The `querystring.escape()` method is used by `querystring.stringify()` and is
107 * generally not expected to be used directly. It is exported primarily to allow
108 * application code to provide a replacement percent-encoding implementation if
109 * necessary by assigning `querystring.escape` to an alternative function.
110 * @since v0.1.25
111 */
112 function escape(str: string): string;
113 /**
114 * The `querystring.unescape()` method performs decoding of URL percent-encoded
115 * characters on the given `str`.
116 *
117 * The `querystring.unescape()` method is used by `querystring.parse()` and is
118 * generally not expected to be used directly. It is exported primarily to allow
119 * application code to provide a replacement decoding implementation if
120 * necessary by assigning `querystring.unescape` to an alternative function.
121 *
122 * By default, the `querystring.unescape()` method will attempt to use the
123 * JavaScript built-in `decodeURIComponent()` method to decode. If that fails,
124 * a safer equivalent that does not throw on malformed URLs will be used.
125 * @since v0.1.25
126 */
127 function unescape(str: string): string;
128}
129declare module 'node:querystring' {
130 export * from 'querystring';
131}
132
\No newline at end of file