[njs] Types: updated Fetch API types.
Dmitry Volyntsev
xeioex at nginx.com
Fri Feb 10 05:07:33 UTC 2023
details: https://hg.nginx.org/njs/rev/29fbf8f85c09
branches:
changeset: 2047:29fbf8f85c09
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Thu Feb 09 18:34:51 2023 -0800
description:
Types: updated Fetch API types.
diffstat:
test/ts/test.ts | 9 +++
ts/ngx_core.d.ts | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 154 insertions(+), 8 deletions(-)
diffs (231 lines):
diff -r 8b0350c4b19e -r 29fbf8f85c09 test/ts/test.ts
--- a/test/ts/test.ts Thu Feb 09 18:34:51 2023 -0800
+++ b/test/ts/test.ts Thu Feb 09 18:34:51 2023 -0800
@@ -96,12 +96,21 @@ async function http_module(r: NginxHTTPR
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});
diff -r 8b0350c4b19e -r 29fbf8f85c09 ts/ngx_core.d.ts
--- a/ts/ngx_core.d.ts Thu Feb 09 18:34:51 2023 -0800
+++ b/ts/ngx_core.d.ts Thu Feb 09 18:34:51 2023 -0800
@@ -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.
@@ -12,14 +36,119 @@ interface NgxResponseHeaders {
*/
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";
}
-interface NgxResponse {
+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 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.
@@ -30,15 +159,23 @@ interface NgxResponse {
*/
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
* parsing the body text as JSON.
*/
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;
More information about the nginx-devel
mailing list