]> git.kaiwu.me - njs.git/commitdiff
Types: added definitions for fetch method.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 11 Feb 2021 14:39:13 +0000 (14:39 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 11 Feb 2021 14:39:13 +0000 (14:39 +0000)
test/ts/test.ts
ts/ngx_core.d.ts

index 65cf52f74de14f660041bbf33713b64bf6423dac..ef24d70fc1c22107848f59aac40722d0cf5fc3a3 100644 (file)
@@ -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() {
index 258a27fcc5c19c751a1c5c456e06f7f626e1af40..4a02f149f44eca768a54f31e0e4710eec535fac5 100644 (file)
@@ -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;