]> git.kaiwu.me - njs.git/commitdiff
Types: updated Fetch API types.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 10 Feb 2023 02:34:51 +0000 (18:34 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 10 Feb 2023 02:34:51 +0000 (18:34 -0800)
test/ts/test.ts
ts/ngx_core.d.ts

index b7464163af40f98f98c54879c5b3c3d10727c2f9..5522f4a1f8c8cc06900e7883dc08bbad04ec3a0e 100644 (file)
@@ -96,12 +96,21 @@ async function http_module(r: NginxHTTPRequest) {
         let out: Array<string> = reply.headers.getAll("foo");
         let has: boolean = reply.headers.has("foo");
 
+        reply.headers.append("foo", "xxx");
+        reply.headers.delete("xxx");
+        reply.headers.forEach((name, value) => { /* do something. */ });
+
         return reply.text()
     })
     .then(body => r.return(200, body))
     .catch(e => r.return(501, e.message))
 
+
     let response = await ngx.fetch('http://nginx.org/');
+    let response2 = new Response("xxx", {headers: {"Content-Type": "text/plain"}, status: 404});
+
+    let req = new Request("http://nginx.org", {method: "POST", headers: new Headers(["Foo", "bar"])});
+    let response3 = await ngx.fetch(req);
 
     // js_body_filter
     r.sendBuffer(Buffer.from("xxx"), {last:true});
index cb5e47f5ec274f0d5ab89e7a9f0c017904548412..d9cbb785f5042873abb35d2a1d1a3b3d7bcd6cb9 100644 (file)
@@ -1,4 +1,28 @@
-interface NgxResponseHeaders {
+type NgxHeaders = Headers | Object | [NjsFixedSizeArray<2, NjsStringLike>];
+
+declare class Headers {
+    /**
+     * Appends a new value into an existing header in the Headers object,
+     * or adds the header if it does not already exist.
+     * @param name A name of the header.
+     * @param value A value of the header.
+     * @since 0.7.10
+     */
+    append(name:NjsStringLike, value: NjsStringLike): void;
+    /**
+     * Headers constructors.
+     *
+     * @param init is an optional initialization object.
+     * @returns returns Headers object.
+     * @since 0.7.10
+     */
+    constructor(init?: Object | [NjsFixedSizeArray<2, NjsStringLike>]);
+    /**
+     * Deletes a header from the Headers object.
+     * @param name A name of the header to be deleted.
+     * @since 0.7.10
+     */
+    delete(name:NjsStringLike): void;
     /**
      * Returns a string containing the values of all headers
      * with the specified name separated by a comma and a space.
@@ -11,15 +35,120 @@ interface NgxResponseHeaders {
      * @param name A name of the header.
      */
     getAll(name:NjsStringLike): Array<NjsByteString>;
+    /**
+     * Executes a provided function once for each key/value
+     * pair in the Headers object.
+     * @param fn the function to be envoked.
+     * @since 0.7.10
+     */
+    forEach(fn:(name: NjsStringLike, value: NjsStringLike) => void): void;
     /**
      * Returns a boolean value indicating whether a header with
      * the specified name exists.
      * @param name A name of the header.
      */
     has(name:NjsStringLike): boolean;
+    /**
+     * Sets a new value for an existing header inside the Headers object,
+     * or adds the header if it does not already exist.
+     * @param name A name of the header.
+     * @param value A value of the header.
+     * @since 0.7.10
+     */
+    set(name:NjsStringLike, value: NjsStringLike): void;
+}
+
+interface NgxRequestOptions {
+    /**
+     * Request body, by default is empty.
+     */
+    body?: NjsStringLike;
+    /**
+     * Cache mode, by default is "default".
+     */
+    cache?: "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached";
+    /**
+     * Credentials, by default is "same-origin".
+     */
+    credentials?: "omit" | "same-origin" | "include";
+    /**
+     * Request headers.
+     */
+    headers?: NgxHeaders;
+    /**
+     * Request method, by default the GET method is used.
+     */
+    method?: NjsStringLike;
+    /**
+     * Mode, by default is "no-cors".
+     */
+    mode?: "same-origin" | "no-cors" | "cors";
+}
+
+declare class Request {
+    /**
+     * Returns a Promise that resolves with an body as ArrayBuffer.
+     */
+    arrayBuffer(): Promise<ArrayBuffer>;
+    /**
+     * A boolean value, true if the body has been used.
+     */
+    readonly bodyUsed: boolean;
+    /**
+     * Cache mode.
+     */
+    readonly cache: NjsByteString;
+    /**
+     * Request constructors.
+     *
+     * @param init is an optional initialization object.
+     * @returns returns Headers object.
+     * @since 0.7.10
+     */
+    constructor(input: NjsStringLike | Request, options?: NgxRequestOptions);
+    /**
+     * Credentials.
+     */
+    readonly credentials: NjsByteString;
+    /**
+     * Returns a Promise that resolves with an result of applying of
+     * JSON.parse() to a body.
+     */
+    json(): Promise<Object>;
+    /**
+     * The Headers object associated with the request.
+     */
+    headers: Headers;
+    /**
+     * Request mode.
+     */
+    readonly mode: NjsByteString;
+    /**
+     * Returns a Promise that resolves with an body as String.
+     */
+    text(): Promise<NjsByteString>;
+    /**
+     * Request url.
+     */
+    readonly url: NjsByteString;
 }
 
-interface NgxResponse {
+interface NgxResponseOptions {
+    /**
+     * Request headers.
+     */
+    headers?: NgxHeaders;
+    /**
+     * Response status, 200 by default.
+     */
+    status?: number;
+    /**
+     * Response status test, '' by default.
+     */
+    statusText?: NjsStringLike;
+}
+
+declare class Response {
     /**
      * Takes a Response stream and reads it to completion.
      * Returns a Promise that resolves with an ArrayBuffer.
@@ -29,6 +158,14 @@ interface NgxResponse {
      * A boolean value, true if the body has been used.
      */
     readonly bodyUsed: boolean;
+    /**
+     * Response constructors.
+     *
+     * @param init is an optional initialization object.
+     * @returns returns Headers object.
+     * @since 0.7.10
+     */
+    constructor(body?: NjsStringLike, options?: NgxResponseOptions);
     /**
      * Takes a Response stream and reads it to completion.
      * Returns a Promise that resolves with the result of
@@ -36,9 +173,9 @@ interface NgxResponse {
      */
     json(): Promise<Object>;
     /**
-     * The NgxResponseHeaders object associated with the response.
+     * The Headers object associated with the response.
      */
-    headers: NgxResponseHeaders;
+    headers: Headers;
     /**
      * A boolean value, true if the response was successful
      * (status in the range 200-299).
@@ -86,7 +223,7 @@ interface NgxFetchOptions {
     /**
      * Request headers object.
      */
-    headers?: Object,
+    headers?: NgxHeaders;
     /**
      * The maximum size of the response body in bytes, by default is 1048576 (32768 before 0.7.4).
      * Nginx specific.
@@ -118,13 +255,13 @@ interface NgxObject {
     log(level: number, message: NjsStringOrBuffer): void;
     /**
      * Makes a request to fetch an URL.
-     * Returns a Promise that resolves with the NgxResponse object.
+     * Returns a Promise that resolves with the Response object.
      * Since 0.7.0 HTTPS is supported, redirects are not handled.
-     * @param url URL of a resource to fetch.
+     * @param init URL of a resource to fetch or a Request object.
      * @param options An object containing additional settings.
      * @since 0.5.1
      */
-    fetch(url: NjsStringOrBuffer, options?: NgxFetchOptions): Promise<NgxResponse>;
+    fetch(init: NjsStringOrBuffer | Request, options?: NgxFetchOptions): Promise<Response>;
 }
 
 declare const ngx: NgxObject;