aboutsummaryrefslogtreecommitdiff
path: root/ts/njs_modules/querystring.d.ts
blob: d4229fdfb13e13456f5ab9272ca1bbec3ab595b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/// <reference path="../njs_core.d.ts" />

declare module "querystring" {

    export interface ParsedUrlQuery {
        [key: string]: string | string[] | undefined;
    }

    export interface ParsedUrlQueryInput {
        [key: string]: string | number | boolean | string[] | number[] | boolean[] | null | undefined;
    }

    interface ParseOptions {
        /**
         * Function used to decode percent-encoded characters in the query string.
         * Defaults to `querystring.unescape()`.
         */
        decodeURIComponent?: (str: string) => string;

        /**
         * The maximum number of keys to parse; defaults to `1000`.
         * The `0` value removes limitations for counting keys.
         */
        maxKeys?: number;
    }

    interface StringifyOptions {
        /**
         * The function to use when converting URL-unsafe characters to percent-encoding in the
         * query string; defaults to `querystring.escape()`.
         */
        encodeURIComponent?: (str: string) => string;
    }

    interface QueryString {
        /**
         * Performs URL encoding of the given string `str`, returns an escaped query string.
         * The method is used by `querystring.stringify()` and should not be used directly.
         *
         * @param str The query string to escape.
         * @return The escaped query string.
         */
        escape(str: string): string;

        /**
         * Parses the query string URL and returns an object.
         *
         * By default, percent-encoded characters within the query string are assumed to use the
         * UTF-8 encoding, invalid UTF-8 sequences will be replaced with the `U+FFFD` replacement
         * character.
         *
         * @param query The query string.
         * @param separator The substring for delimiting key and value pairs in the query string; defaults to `'&'`.
         * @param equal The substring for delimiting keys and values in the query string, defaults to `'='`.
         * @param options An object optionally specifying `decodeURIComponent` function and `maxKeys` number.
         * @return An object containing the components of the query string.
         */
        parse(query: string, separator?: string, equal?: string, options?: ParseOptions): ParsedUrlQuery;

        /**
         * An alias for `querystring.parse()`.
         */
        decode(query: string, separator?: string, equal?: string, options?: ParseOptions): ParsedUrlQuery;

        /**
         * Serializes an object and returns a URL query string.
         *
         * By default, characters that require percent-encoding within the query string are encoded
         * as UTF-8. If other encoding is required, then `encodeURIComponent` option should be
         * specified.
         *
         * @param obj The data to convert to a query string.
         * @param separator The substring for delimiting key and value pairs in the query string; defaults to `'&'`.
         * @param equal The substring for delimiting keys and values in the query string; defaults to `'='`.
         * @param options An object optionally specifying `encodeURIComponent` function.
         * @return A query string.
         */
        stringify(obj: ParsedUrlQueryInput, separator?: string, equal?: string, options?: StringifyOptions): string;

        /**
         * An alias for `querystring.stringify()`.
         */
        encode(obj: ParsedUrlQueryInput, separator?: string, equal?: string, options?: StringifyOptions): string;

        /**
         * Performs decoding of URL percent-encoded characters of the string `str`, returns an
         * unescaped query string. The method is used by `querystring.parse()` and should not be
         * used directly.
         *
         * @param str An escaped query string.
         * @return An unescaped string.
         */
        unescape(str: string): string;
    }

    const querystring: QueryString;

    // It's exported like this because njs doesn't support named imports.
    // TODO: Replace NjsFS with individual named exports as soon as njs supports named imports.
    export default querystring;
}