@@ -57,8 +57,10 @@ export class SpeechClient {
57
57
/**
58
58
* Construct an instance of SpeechClient.
59
59
*
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:
62
64
* @param {object } [options.credentials] - Credentials object.
63
65
* @param {string } [options.credentials.client_email]
64
66
* @param {string } [options.credentials.private_key]
@@ -78,42 +80,33 @@ export class SpeechClient {
78
80
* your project ID will be detected automatically.
79
81
* @param {string } [options.apiEndpoint] - The domain name of the
80
82
* 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.
81
90
*/
82
-
83
91
constructor ( opts ?: ClientOptions ) {
84
- // Ensure that options include the service address and port .
92
+ // Ensure that options include all the required fields .
85
93
const staticMembers = this . constructor as typeof SpeechClient ;
86
94
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 ;
96
104
}
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.
112
107
this . _gaxModule = opts . fallback ? gax . fallback : gax ;
113
108
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.
117
110
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
118
111
119
112
// Save options to use in initialize() method.
@@ -122,6 +115,11 @@ export class SpeechClient {
122
115
// Save the auth object to the client, for use by other methods.
123
116
this . auth = this . _gaxGrpc . auth as gax . GoogleAuth ;
124
117
118
+ // Set the default scopes in auth client if needed.
119
+ if ( servicePath === staticMembers . servicePath ) {
120
+ this . auth . defaultScopes = staticMembers . scopes ;
121
+ }
122
+
125
123
// Determine the client header string.
126
124
const clientHeader = [ `gax/${ this . _gaxModule . version } ` , `gapic/${ version } ` ] ;
127
125
if ( typeof process !== 'undefined' && 'versions' in process ) {
@@ -274,6 +272,7 @@ export class SpeechClient {
274
272
275
273
/**
276
274
* The DNS address for this API service.
275
+ * @returns {string } The DNS address for this service.
277
276
*/
278
277
static get servicePath ( ) {
279
278
return 'speech.googleapis.com' ;
@@ -282,13 +281,15 @@ export class SpeechClient {
282
281
/**
283
282
* The DNS address for this API service - same as servicePath(),
284
283
* exists for compatibility reasons.
284
+ * @returns {string } The DNS address for this service.
285
285
*/
286
286
static get apiEndpoint ( ) {
287
287
return 'speech.googleapis.com' ;
288
288
}
289
289
290
290
/**
291
291
* The port for this API service.
292
+ * @returns {number } The default port for this service.
292
293
*/
293
294
static get port ( ) {
294
295
return 443 ;
@@ -297,6 +298,7 @@ export class SpeechClient {
297
298
/**
298
299
* The scopes needed to make gRPC calls for every method defined
299
300
* in this service.
301
+ * @returns {string[] } List of default scopes.
300
302
*/
301
303
static get scopes ( ) {
302
304
return [ 'https://www.googleapis.com/auth/cloud-platform' ] ;
@@ -306,8 +308,7 @@ export class SpeechClient {
306
308
getProjectId ( callback : Callback < string , undefined , undefined > ) : void ;
307
309
/**
308
310
* 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.
311
312
*/
312
313
getProjectId (
313
314
callback ?: Callback < string , undefined , undefined >
@@ -364,7 +365,11 @@ export class SpeechClient {
364
365
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
365
366
* @returns {Promise } - The promise which resolves to an array.
366
367
* 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);
368
373
*/
369
374
recognize (
370
375
request : protos . google . cloud . speech . v1 . IRecognizeRequest ,
@@ -410,6 +415,15 @@ export class SpeechClient {
410
415
* An object stream which is both readable and writable. It accepts objects
411
416
* representing [StreamingRecognizeRequest]{@link google.cloud.speech.v1.StreamingRecognizeRequest} for write() method, and
412
417
* 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();
413
427
*/
414
428
_streamingRecognize ( options ?: gax . CallOptions ) : gax . CancellableStream {
415
429
this . initialize ( ) ;
@@ -470,8 +484,15 @@ export class SpeechClient {
470
484
* @param {object } [options]
471
485
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
472
486
* @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();
475
496
*/
476
497
longRunningRecognize (
477
498
request : protos . google . cloud . speech . v1 . ILongRunningRecognizeRequest ,
@@ -516,18 +537,19 @@ export class SpeechClient {
516
537
return this . innerApiCalls . longRunningRecognize ( request , options , callback ) ;
517
538
}
518
539
/**
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()` .
520
541
* @param {String } name
521
542
* The operation name that will be passed.
522
543
* @returns {Promise } - The promise which resolves to an object.
523
544
* 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);
531
553
*/
532
554
async checkLongRunningRecognizeProgress (
533
555
name : string
@@ -553,9 +575,10 @@ export class SpeechClient {
553
575
}
554
576
555
577
/**
556
- * Terminate the GRPC channel and close the client.
578
+ * Terminate the gRPC channel and close the client.
557
579
*
558
580
* 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.
559
582
*/
560
583
close ( ) : Promise < void > {
561
584
this . initialize ( ) ;
0 commit comments