From 44f479b1c9cb1276da2d1c2f0bedb06994132230 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Mon, 12 Oct 2020 14:53:30 -0700 Subject: [PATCH] Add ClientOptions class --- javascript/net/grpc/web/BUILD.bazel | 12 +++++ javascript/net/grpc/web/clientoptions.js | 50 +++++++++++++++++++ javascript/net/grpc/web/grpcwebclientbase.js | 5 +- .../web/streambodyclientreadablestream.js | 6 +-- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 javascript/net/grpc/web/clientoptions.js diff --git a/javascript/net/grpc/web/BUILD.bazel b/javascript/net/grpc/web/BUILD.bazel index 4cd859a4..9a7a97ac 100644 --- a/javascript/net/grpc/web/BUILD.bazel +++ b/javascript/net/grpc/web/BUILD.bazel @@ -52,6 +52,17 @@ closure_js_library( visibility = ["//visibility:public"], ) +closure_js_library( + name = "clientoptions", + srcs = [ + "clientoptions.js", + ], + visibility = ["//visibility:public"], + deps = [ + ":interceptor", + ], +) + closure_js_library( name = "clientreadablestream", srcs = [ @@ -106,6 +117,7 @@ closure_js_library( visibility = ["//visibility:public"], deps = [ ":abstractclientbase", + ":clientoptions", ":clientreadablestream", ":clientunarycallimpl", ":error", diff --git a/javascript/net/grpc/web/clientoptions.js b/javascript/net/grpc/web/clientoptions.js new file mode 100644 index 00000000..bbf7fd24 --- /dev/null +++ b/javascript/net/grpc/web/clientoptions.js @@ -0,0 +1,50 @@ +goog.module('grpc.web.ClientOptions'); +goog.module.declareLegacyNamespace(); + +const {StreamInterceptor, UnaryInterceptor} = goog.require('grpc.web.Interceptor'); + + +/** + * Options that are availavle during the client construction. + * @record + */ +class ClientOptions { + constructor() { + /** + * Whether to use the HttpCors library to pack http headers into a special + * url query param $httpHeaders= so that browsers can bypass CORS OPTIONS + * requests. + * @type {boolean|undefined} + */ + this.suppressCorsPreflight; + + /** + * Whether to turn on XMLHttpRequest's withCredentials flag. + * @type {boolean|undefined} + */ + this.withCredentials; + + /** + * Unary interceptors. Note that they are only available in grpcweb and + * grpcwebtext mode + * @type {!Array|undefined} + */ + this.unaryInterceptors; + + /** + * Stream interceptors. Note that they are only available in grpcweb and + * grpcwebtext mode + * @type {!Array|undefined} + */ + this.streamInterceptors; + + /** + * Protocol buffer format for open source gRPC-Web. This attribute should be + * specified by the gRPC-Web build rule by default. + * @type {string|undefined} + */ + this.format; + } +} + +exports = ClientOptions; diff --git a/javascript/net/grpc/web/grpcwebclientbase.js b/javascript/net/grpc/web/grpcwebclientbase.js index 6f93883b..869cb502 100644 --- a/javascript/net/grpc/web/grpcwebclientbase.js +++ b/javascript/net/grpc/web/grpcwebclientbase.js @@ -29,6 +29,7 @@ goog.module.declareLegacyNamespace(); const AbstractClientBase = goog.require('grpc.web.AbstractClientBase'); +const ClientOptions = goog.requireType('grpc.web.ClientOptions'); const ClientReadableStream = goog.require('grpc.web.ClientReadableStream'); const ClientUnaryCallImpl = goog.require('grpc.web.ClientUnaryCallImpl'); const Error = goog.require('grpc.web.Error'); @@ -52,9 +53,9 @@ const {StreamInterceptor, UnaryInterceptor} = goog.require('grpc.web.Interceptor */ class GrpcWebClientBase { /** - * @param {?Object=} options + * @param {!ClientOptions=} options */ - constructor(options) { + constructor(options = {}) { /** * @const * @private {string} diff --git a/javascript/net/grpc/web/streambodyclientreadablestream.js b/javascript/net/grpc/web/streambodyclientreadablestream.js index 1883248e..8e8da51f 100644 --- a/javascript/net/grpc/web/streambodyclientreadablestream.js +++ b/javascript/net/grpc/web/streambodyclientreadablestream.js @@ -144,11 +144,11 @@ class StreamBodyClientReadableStream { if (grpcStatus == StatusCode.OK) { this.sendDataCallbacks_(responseMessage); } else { - callback( + this.sendErrorCallbacks_( /** @type {!GrpcWebError} */ ({ code: grpcStatus, - }), - responseMessage); + message: response, + })); } } else { let rawResponse;