[njs] Added TypeScript description for querystring module.

Dmitry Volyntsev xeioex at nginx.com
Tue Oct 20 15:50:25 UTC 2020


details:   https://hg.nginx.org/njs/rev/e7fb6e22acb7
branches:  
changeset: 1544:e7fb6e22acb7
user:      Jakub Jirutka <jakub at jirutka.cz>
date:      Fri Oct 09 17:05:47 2020 +0200
description:
Added TypeScript description for querystring module.

diffstat:

 src/ts/querystring.d.ts |  101 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/ts/test.ts         |   10 ++++
 2 files changed, 111 insertions(+), 0 deletions(-)

diffs (133 lines):

diff -r 6b84320e31d4 -r e7fb6e22acb7 src/ts/querystring.d.ts
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ts/querystring.d.ts	Fri Oct 09 17:05:47 2020 +0200
@@ -0,0 +1,101 @@
+/// <reference path="njs_core.d.ts" />
+
+declare module "querystring" {
+
+    export interface ParsedUrlQuery {
+        [key: string]: string | string[] | undefined;
+    }
+
+    export interface ParsedUrlQueryInput {
+        [key: string]: NjsStringLike | number | boolean | NjsStringLike[] | number[] | boolean[] | null | undefined;
+    }
+
+    interface ParseOptions {
+        /**
+         * Function used to decode percent-encoded characters in the query string.
+         * Defaults to `querystring.unescape()`.
+         */
+        decodeURIComponent?: (str: NjsStringLike) => 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: NjsStringLike) => 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: NjsStringLike): 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: NjsStringLike, separator?: NjsStringLike, equal?: NjsStringLike, options?: ParseOptions): ParsedUrlQuery;
+
+        /**
+         * An alias for `querystring.parse()`.
+         */
+        decode(query: NjsStringLike, separator?: NjsStringLike, equal?: NjsStringLike, 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?: NjsStringLike, equal?: NjsStringLike, options?: StringifyOptions): string;
+
+        /**
+         * An alias for `querystring.stringify()`.
+         */
+        encode(obj: ParsedUrlQueryInput, separator?: NjsStringLike, equal?: NjsStringLike, 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: NjsStringLike): 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;
+}
diff -r 6b84320e31d4 -r e7fb6e22acb7 test/ts/test.ts
--- a/test/ts/test.ts	Fri Oct 09 02:33:31 2020 +0200
+++ b/test/ts/test.ts	Fri Oct 09 17:05:47 2020 +0200
@@ -1,7 +1,9 @@
 /// <reference path="../../build/ts/ngx_http_js_module.d.ts" />
 /// <reference path="../../build/ts/fs.d.ts" />
+/// <reference path="../../build/ts/querystring.d.ts" />
 
 import fs from 'fs';
+import qs from 'querystring';
 
 function http_module(r: NginxHTTPRequest) {
     var bs: NjsByteString;
@@ -71,6 +73,14 @@ function fs_module() {
     s = fs.readFileSync(Buffer.from('/path'), {encoding:'hex'});
 }
 
+function qs_module(str: NjsByteString) {
+    var o;
+    var s:string;
+
+    o = qs.parse(str);
+    s = qs.stringify(o);
+}
+
 function buffer(b: Buffer) {
     var s:string;
 


More information about the nginx-devel mailing list