Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit eafbadd

Browse files
fix: do not modify options object, use defaultScopes (#669)
Regenerated the library using [gapic-generator-typescript](https://github.com/googleapis/gapic-generator-typescript) v1.2.1.
1 parent 8ca406d commit eafbadd

File tree

7 files changed

+160
-112
lines changed

7 files changed

+160
-112
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@google-cloud/common": "^3.0.0",
51-
"google-gax": "^2.1.0",
51+
"google-gax": "^2.9.2",
5252
"protobufjs": "^6.8.6",
5353
"pumpify": "^2.0.0",
5454
"stream-events": "^1.0.4",

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Object.defineProperty(
3636
);
3737

3838
const SpeechClient = v1.SpeechClient;
39+
type SpeechClient = v1.SpeechClient;
3940
export {v1, v1p1beta1, SpeechClient};
4041
// For compatibility with JavaScript libraries we need to provide this default export:
4142
// tslint:disable-next-line no-default-export

src/v1/speech_client.ts

+68-45
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ export class SpeechClient {
5757
/**
5858
* Construct an instance of SpeechClient.
5959
*
60-
* @param {object} [options] - The configuration object. See the subsequent
61-
* parameters for more details.
60+
* @param {object} [options] - The configuration object.
61+
* The options accepted by the constructor are described in detail
62+
* in [this document](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#creating-the-client-instance).
63+
* The common options are:
6264
* @param {object} [options.credentials] - Credentials object.
6365
* @param {string} [options.credentials.client_email]
6466
* @param {string} [options.credentials.private_key]
@@ -78,42 +80,33 @@ export class SpeechClient {
7880
* your project ID will be detected automatically.
7981
* @param {string} [options.apiEndpoint] - The domain name of the
8082
* API remote host.
83+
* @param {gax.ClientConfig} [options.clientConfig] - client configuration override.
84+
* TODO(@alexander-fenster): link to gax documentation.
85+
* @param {boolean} fallback - Use HTTP fallback mode.
86+
* In fallback mode, a special browser-compatible transport implementation is used
87+
* instead of gRPC transport. In browser context (if the `window` object is defined)
88+
* the fallback mode is enabled automatically; set `options.fallback` to `false`
89+
* if you need to override this behavior.
8190
*/
82-
8391
constructor(opts?: ClientOptions) {
84-
// Ensure that options include the service address and port.
92+
// Ensure that options include all the required fields.
8593
const staticMembers = this.constructor as typeof SpeechClient;
8694
const servicePath =
87-
opts && opts.servicePath
88-
? opts.servicePath
89-
: opts && opts.apiEndpoint
90-
? opts.apiEndpoint
91-
: staticMembers.servicePath;
92-
const port = opts && opts.port ? opts.port : staticMembers.port;
93-
94-
if (!opts) {
95-
opts = {servicePath, port};
95+
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
96+
const port = opts?.port || staticMembers.port;
97+
const clientConfig = opts?.clientConfig ?? {};
98+
const fallback = opts?.fallback ?? typeof window !== 'undefined';
99+
opts = Object.assign({servicePath, port, clientConfig, fallback}, opts);
100+
101+
// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
102+
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) {
103+
opts['scopes'] = staticMembers.scopes;
96104
}
97-
opts.servicePath = opts.servicePath || servicePath;
98-
opts.port = opts.port || port;
99-
100-
// users can override the config from client side, like retry codes name.
101-
// The detailed structure of the clientConfig can be found here: https://github.com/googleapis/gax-nodejs/blob/master/src/gax.ts#L546
102-
// The way to override client config for Showcase API:
103-
//
104-
// const customConfig = {"interfaces": {"google.showcase.v1beta1.Echo": {"methods": {"Echo": {"retry_codes_name": "idempotent", "retry_params_name": "default"}}}}}
105-
// const showcaseClient = new showcaseClient({ projectId, customConfig });
106-
opts.clientConfig = opts.clientConfig || {};
107-
108-
// If we're running in browser, it's OK to omit `fallback` since
109-
// google-gax has `browser` field in its `package.json`.
110-
// For Electron (which does not respect `browser` field),
111-
// pass `{fallback: true}` to the SpeechClient constructor.
105+
106+
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
112107
this._gaxModule = opts.fallback ? gax.fallback : gax;
113108

114-
// Create a `gaxGrpc` object, with any grpc-specific options
115-
// sent to the client.
116-
opts.scopes = (this.constructor as typeof SpeechClient).scopes;
109+
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
117110
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
118111

119112
// Save options to use in initialize() method.
@@ -122,6 +115,11 @@ export class SpeechClient {
122115
// Save the auth object to the client, for use by other methods.
123116
this.auth = this._gaxGrpc.auth as gax.GoogleAuth;
124117

118+
// Set the default scopes in auth client if needed.
119+
if (servicePath === staticMembers.servicePath) {
120+
this.auth.defaultScopes = staticMembers.scopes;
121+
}
122+
125123
// Determine the client header string.
126124
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
127125
if (typeof process !== 'undefined' && 'versions' in process) {
@@ -274,6 +272,7 @@ export class SpeechClient {
274272

275273
/**
276274
* The DNS address for this API service.
275+
* @returns {string} The DNS address for this service.
277276
*/
278277
static get servicePath() {
279278
return 'speech.googleapis.com';
@@ -282,13 +281,15 @@ export class SpeechClient {
282281
/**
283282
* The DNS address for this API service - same as servicePath(),
284283
* exists for compatibility reasons.
284+
* @returns {string} The DNS address for this service.
285285
*/
286286
static get apiEndpoint() {
287287
return 'speech.googleapis.com';
288288
}
289289

290290
/**
291291
* The port for this API service.
292+
* @returns {number} The default port for this service.
292293
*/
293294
static get port() {
294295
return 443;
@@ -297,6 +298,7 @@ export class SpeechClient {
297298
/**
298299
* The scopes needed to make gRPC calls for every method defined
299300
* in this service.
301+
* @returns {string[]} List of default scopes.
300302
*/
301303
static get scopes() {
302304
return ['https://www.googleapis.com/auth/cloud-platform'];
@@ -306,8 +308,7 @@ export class SpeechClient {
306308
getProjectId(callback: Callback<string, undefined, undefined>): void;
307309
/**
308310
* Return the project ID used by this class.
309-
* @param {function(Error, string)} callback - the callback to
310-
* be called with the current project Id.
311+
* @returns {Promise} A promise that resolves to string containing the project ID.
311312
*/
312313
getProjectId(
313314
callback?: Callback<string, undefined, undefined>
@@ -364,7 +365,11 @@ export class SpeechClient {
364365
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
365366
* @returns {Promise} - The promise which resolves to an array.
366367
* The first element of the array is an object representing [RecognizeResponse]{@link google.cloud.speech.v1.RecognizeResponse}.
367-
* The promise has a method named "cancel" which cancels the ongoing API call.
368+
* Please see the
369+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
370+
* for more details and examples.
371+
* @example
372+
* const [response] = await client.recognize(request);
368373
*/
369374
recognize(
370375
request: protos.google.cloud.speech.v1.IRecognizeRequest,
@@ -410,6 +415,15 @@ export class SpeechClient {
410415
* An object stream which is both readable and writable. It accepts objects
411416
* representing [StreamingRecognizeRequest]{@link google.cloud.speech.v1.StreamingRecognizeRequest} for write() method, and
412417
* will emit objects representing [StreamingRecognizeResponse]{@link google.cloud.speech.v1.StreamingRecognizeResponse} on 'data' event asynchronously.
418+
* Please see the
419+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#bi-directional-streaming)
420+
* for more details and examples.
421+
* @example
422+
* const stream = client.streamingRecognize();
423+
* stream.on('data', (response) => { ... });
424+
* stream.on('end', () => { ... });
425+
* stream.write(request);
426+
* stream.end();
413427
*/
414428
_streamingRecognize(options?: gax.CallOptions): gax.CancellableStream {
415429
this.initialize();
@@ -470,8 +484,15 @@ export class SpeechClient {
470484
* @param {object} [options]
471485
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
472486
* @returns {Promise} - The promise which resolves to an array.
473-
* The first element of the array is an object representing [Operation]{@link google.longrunning.Operation}.
474-
* The promise has a method named "cancel" which cancels the ongoing API call.
487+
* The first element of the array is an object representing
488+
* a long running operation. Its `promise()` method returns a promise
489+
* you can `await` for.
490+
* Please see the
491+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#long-running-operations)
492+
* for more details and examples.
493+
* @example
494+
* const [operation] = await client.longRunningRecognize(request);
495+
* const [response] = await operation.promise();
475496
*/
476497
longRunningRecognize(
477498
request: protos.google.cloud.speech.v1.ILongRunningRecognizeRequest,
@@ -516,18 +537,19 @@ export class SpeechClient {
516537
return this.innerApiCalls.longRunningRecognize(request, options, callback);
517538
}
518539
/**
519-
* Check the status of the long running operation returned by the longRunningRecognize() method.
540+
* Check the status of the long running operation returned by `longRunningRecognize()`.
520541
* @param {String} name
521542
* The operation name that will be passed.
522543
* @returns {Promise} - The promise which resolves to an object.
523544
* The decoded operation object has result and metadata field to get information from.
524-
*
525-
* @example:
526-
* const decodedOperation = await checkLongRunningRecognizeProgress(name);
527-
* console.log(decodedOperation.result);
528-
* console.log(decodedOperation.done);
529-
* console.log(decodedOperation.metadata);
530-
*
545+
* Please see the
546+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#long-running-operations)
547+
* for more details and examples.
548+
* @example
549+
* const decodedOperation = await checkLongRunningRecognizeProgress(name);
550+
* console.log(decodedOperation.result);
551+
* console.log(decodedOperation.done);
552+
* console.log(decodedOperation.metadata);
531553
*/
532554
async checkLongRunningRecognizeProgress(
533555
name: string
@@ -553,9 +575,10 @@ export class SpeechClient {
553575
}
554576

555577
/**
556-
* Terminate the GRPC channel and close the client.
578+
* Terminate the gRPC channel and close the client.
557579
*
558580
* The client will no longer be usable and all future behavior is undefined.
581+
* @returns {Promise} A promise that resolves when the client is closed.
559582
*/
560583
close(): Promise<void> {
561584
this.initialize();

0 commit comments

Comments
 (0)