84 |
86 |
|
* your project ID will be detected automatically. |
85 |
87 |
|
* @param {string} [options.apiEndpoint] - The domain name of the |
86 |
88 |
|
* API remote host. |
|
89 |
+ |
* @param {gax.ClientConfig} [options.clientConfig] - client configuration override. |
|
90 |
+ |
* TODO(@alexander-fenster): link to gax documentation. |
|
91 |
+ |
* @param {boolean} fallback - Use HTTP fallback mode. |
|
92 |
+ |
* In fallback mode, a special browser-compatible transport implementation is used |
|
93 |
+ |
* instead of gRPC transport. In browser context (if the `window` object is defined) |
|
94 |
+ |
* the fallback mode is enabled automatically; set `options.fallback` to `false` |
|
95 |
+ |
* if you need to override this behavior. |
87 |
96 |
|
*/ |
88 |
|
- |
|
89 |
97 |
|
constructor(opts?: ClientOptions) { |
90 |
|
- |
// Ensure that options include the service address and port. |
|
98 |
+ |
// Ensure that options include all the required fields. |
91 |
99 |
|
const staticMembers = this.constructor as typeof SpannerClient; |
92 |
100 |
|
const servicePath = |
93 |
|
- |
opts && opts.servicePath |
94 |
|
- |
? opts.servicePath |
95 |
|
- |
: opts && opts.apiEndpoint |
96 |
|
- |
? opts.apiEndpoint |
97 |
|
- |
: staticMembers.servicePath; |
98 |
|
- |
const port = opts && opts.port ? opts.port : staticMembers.port; |
|
101 |
+ |
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath; |
|
102 |
+ |
const port = opts?.port || staticMembers.port; |
|
103 |
+ |
const clientConfig = opts?.clientConfig ?? {}; |
|
104 |
+ |
const fallback = opts?.fallback ?? typeof window !== 'undefined'; |
|
105 |
+ |
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts); |
99 |
106 |
|
|
100 |
|
- |
if (!opts) { |
101 |
|
- |
opts = {servicePath, port}; |
|
107 |
+ |
// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case. |
|
108 |
+ |
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) { |
|
109 |
+ |
opts['scopes'] = staticMembers.scopes; |
102 |
110 |
|
} |
103 |
|
- |
opts.servicePath = opts.servicePath || servicePath; |
104 |
|
- |
opts.port = opts.port || port; |
105 |
|
- |
|
106 |
|
- |
// users can override the config from client side, like retry codes name. |
107 |
|
- |
// The detailed structure of the clientConfig can be found here: https://github.com/googleapis/gax-nodejs/blob/master/src/gax.ts#L546 |
108 |
|
- |
// The way to override client config for Showcase API: |
109 |
|
- |
// |
110 |
|
- |
// const customConfig = {"interfaces": {"google.showcase.v1beta1.Echo": {"methods": {"Echo": {"retry_codes_name": "idempotent", "retry_params_name": "default"}}}}} |
111 |
|
- |
// const showcaseClient = new showcaseClient({ projectId, customConfig }); |
112 |
|
- |
opts.clientConfig = opts.clientConfig || {}; |
113 |
111 |
|
|
114 |
|
- |
// If we're running in browser, it's OK to omit `fallback` since |
115 |
|
- |
// google-gax has `browser` field in its `package.json`. |
116 |
|
- |
// For Electron (which does not respect `browser` field), |
117 |
|
- |
// pass `{fallback: true}` to the SpannerClient constructor. |
|
112 |
+ |
// Choose either gRPC or proto-over-HTTP implementation of google-gax. |
118 |
113 |
|
this._gaxModule = opts.fallback ? gax.fallback : gax; |
119 |
114 |
|
|
120 |
|
- |
// Create a `gaxGrpc` object, with any grpc-specific options |
121 |
|
- |
// sent to the client. |
122 |
|
- |
opts.scopes = (this.constructor as typeof SpannerClient).scopes; |
|
115 |
+ |
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client. |
123 |
116 |
|
this._gaxGrpc = new this._gaxModule.GrpcClient(opts); |
124 |
117 |
|
|
125 |
118 |
|
// Save options to use in initialize() method. |