[njs] Types: added definitions for fetch method.

Dmitry Volyntsev xeioex at nginx.com
Thu Feb 11 15:05:39 UTC 2021


details:   https://hg.nginx.org/njs/rev/374dec48b9e4
branches:  
changeset: 1602:374dec48b9e4
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Feb 11 14:39:13 2021 +0000
description:
Types: added definitions for fetch method.

diffstat:

 test/ts/test.ts  |   11 +++++
 ts/ngx_core.d.ts |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+), 0 deletions(-)

diffs (143 lines):

diff -r 9631d5c88caf -r 374dec48b9e4 test/ts/test.ts
--- a/test/ts/test.ts	Thu Feb 11 14:30:59 2021 +0000
+++ b/test/ts/test.ts	Thu Feb 11 14:39:13 2021 +0000
@@ -82,6 +82,17 @@ function http_module(r: NginxHTTPRequest
 
     // r.responseBuffer
     r.responseBuffer?.equals(Buffer.from([1]));
+
+    ngx.fetch('http://nginx.org/', {method:'POST', headers:{Foo:'bar'}})
+    .then(reply => {
+        if (reply.headers.get('foo')) {
+            throw 'oops'
+        };
+
+        return reply.text()
+    })
+    .then(body => r.return(200, body))
+    .catch(e => r.return(501, e.message))
 }
 
 function fs_module() {
diff -r 9631d5c88caf -r 374dec48b9e4 ts/ngx_core.d.ts
--- a/ts/ngx_core.d.ts	Thu Feb 11 14:30:59 2021 +0000
+++ b/ts/ngx_core.d.ts	Thu Feb 11 14:39:13 2021 +0000
@@ -1,3 +1,102 @@
+interface NgxResponseHeaders {
+    /**
+     * Returns a string containing the values of all headers
+     * with the specified name separated by a comma and a space.
+     * @param name A name of the header.
+     */
+    get(name:NjsStringLike): NjsByteString;
+    /**
+     * Returns an array containing the values of all headers
+     * with the specified name.
+     * @param name A name of the header.
+     */
+    getAll(name:NjsStringLike): NjsByteString;
+    /**
+     * Returns a boolean value indicating whether a header with
+     * the specified name exists.
+     * @param name A name of the header.
+     */
+    has(name:NjsStringLike): NjsByteString;
+}
+
+interface NgxResponse {
+    /**
+     * Takes a Response stream and reads it to completion.
+     * Returns a Promise that resolves with an ArrayBuffer.
+     */
+    arrayBuffer(): Promise<ArrayBuffer>;
+    /**
+     * A boolean value, true if the body has been used.
+     */
+    readonly bodyUsed: boolean;
+    /**
+     * Takes a Response stream and reads it to completion.
+     * Returns a Promise that resolves with the result of
+     * parsing the body text as JSON.
+     */
+    json(): Promise<Object>;
+    /**
+     * The NgxResponseHeaders object associated with the response.
+     */
+    headers: NgxResponseHeaders;
+    /**
+     * A boolean value, true if the response was successful
+     * (status in the range 200-299).
+     */
+    readonly ok: boolean;
+    /**
+     * A boolean value, true if the response is the result
+     * of a redirect.
+     */
+    readonly redirected: boolean;
+    /**
+     * The status code of the response.
+     */
+    readonly status: number;
+    /**
+     * The status message corresponding to the status code.
+     */
+    readonly statusText: NjsByteString;
+    /**
+     * Takes a Response stream and reads it to completion.
+     * Returns a Promise that resolves with a string.
+     */
+    text(): Promise<NjsByteString>;
+    /**
+     * The type of the response.
+     */
+    readonly type: NjsByteString;
+    /**
+     * Response url.
+     */
+    readonly url: NjsByteString;
+}
+
+interface NgxFetchOptions {
+    /**
+     * Request body, by default is empty.
+     */
+    body?: NjsStringLike,
+    /**
+     * The buffer size for reading the response, by default is 4096.
+     * Nginx specific.
+     */
+    buffer_size?: Number,
+    /**
+     * Request headers object.
+     */
+    headers?: Object,
+    /**
+     * The maximum size of the response body in bytes, by default is 32768.
+     * Nginx specific.
+     */
+    max_response_body_size?: Number,
+    /**
+     * Request method, by default the GET method is used.
+     */
+    method?: NjsStringLike;
+}
+
 interface NgxObject {
     readonly INFO: number;
     readonly WARN: number;
@@ -9,6 +108,15 @@ interface NgxObject {
      * @param message Message to log.
      */
     log(level: number, message: NjsStringOrBuffer): void;
+    /**
+     * Makes a request to fetch an URL.
+     * Returns a Promise that resolves with the NgxResponse object.
+     * Only the http:// scheme is supported, redirects are not handled.
+     * @param url URL of a resource to fetch.
+     * @param options An object containing additional settings.
+     * @since 0.5.1
+     */
+    fetch(url: NjsStringOrBuffer, options?: NgxFetchOptions): Promise<NgxResponse>;
 }
 
 declare const ngx: NgxObject;


More information about the nginx-devel mailing list