Skip to content

Commit d426160

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

File tree

9 files changed

+851
-595
lines changed

9 files changed

+851
-595
lines changed

packages/google-cloud-automl/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"precompile": "gts clean"
4444
},
4545
"dependencies": {
46-
"google-gax": "^2.1.0"
46+
"google-gax": "^2.9.2"
4747
},
4848
"devDependencies": {
4949
"@types/mocha": "^8.0.0",

packages/google-cloud-automl/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import * as v1 from './v1';
2020
import * as v1beta1 from './v1beta1';
2121

2222
const AutoMlClient = v1.AutoMlClient;
23+
type AutoMlClient = v1.AutoMlClient;
2324
const PredictionServiceClient = v1.PredictionServiceClient;
25+
type PredictionServiceClient = v1.PredictionServiceClient;
2426

2527
export {v1, v1beta1, AutoMlClient, PredictionServiceClient};
2628
export default {v1, v1beta1, AutoMlClient, PredictionServiceClient};

packages/google-cloud-automl/src/v1/auto_ml_client.ts

+313-212
Large diffs are not rendered by default.

packages/google-cloud-automl/src/v1/prediction_service_client.ts

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

118-
// Create a `gaxGrpc` object, with any grpc-specific options
119-
// sent to the client.
120-
opts.scopes = (this.constructor as typeof PredictionServiceClient).scopes;
113+
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
121114
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
122115

123116
// Save options to use in initialize() method.
@@ -126,6 +119,11 @@ export class PredictionServiceClient {
126119
// Save the auth object to the client, for use by other methods.
127120
this.auth = this._gaxGrpc.auth as gax.GoogleAuth;
128121

122+
// Set the default scopes in auth client if needed.
123+
if (servicePath === staticMembers.servicePath) {
124+
this.auth.defaultScopes = staticMembers.scopes;
125+
}
126+
129127
// Determine the client header string.
130128
const clientHeader = [`gax/${this._gaxModule.version}`, `gapic/${version}`];
131129
if (typeof process !== 'undefined' && 'versions' in process) {
@@ -281,6 +279,7 @@ export class PredictionServiceClient {
281279

282280
/**
283281
* The DNS address for this API service.
282+
* @returns {string} The DNS address for this service.
284283
*/
285284
static get servicePath() {
286285
return 'automl.googleapis.com';
@@ -289,13 +288,15 @@ export class PredictionServiceClient {
289288
/**
290289
* The DNS address for this API service - same as servicePath(),
291290
* exists for compatibility reasons.
291+
* @returns {string} The DNS address for this service.
292292
*/
293293
static get apiEndpoint() {
294294
return 'automl.googleapis.com';
295295
}
296296

297297
/**
298298
* The port for this API service.
299+
* @returns {number} The default port for this service.
299300
*/
300301
static get port() {
301302
return 443;
@@ -304,6 +305,7 @@ export class PredictionServiceClient {
304305
/**
305306
* The scopes needed to make gRPC calls for every method defined
306307
* in this service.
308+
* @returns {string[]} List of default scopes.
307309
*/
308310
static get scopes() {
309311
return ['https://www.googleapis.com/auth/cloud-platform'];
@@ -313,8 +315,7 @@ export class PredictionServiceClient {
313315
getProjectId(callback: Callback<string, undefined, undefined>): void;
314316
/**
315317
* Return the project ID used by this class.
316-
* @param {function(Error, string)} callback - the callback to
317-
* be called with the current project Id.
318+
* @returns {Promise} A promise that resolves to string containing the project ID.
318319
*/
319320
getProjectId(
320321
callback?: Callback<string, undefined, undefined>
@@ -437,7 +438,11 @@ export class PredictionServiceClient {
437438
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
438439
* @returns {Promise} - The promise which resolves to an array.
439440
* The first element of the array is an object representing [PredictResponse]{@link google.cloud.automl.v1.PredictResponse}.
440-
* The promise has a method named "cancel" which cancels the ongoing API call.
441+
* Please see the
442+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#regular-methods)
443+
* for more details and examples.
444+
* @example
445+
* const [response] = await client.predict(request);
441446
*/
442447
predict(
443448
request: protos.google.cloud.automl.v1.IPredictRequest,
@@ -630,8 +635,15 @@ export class PredictionServiceClient {
630635
* @param {object} [options]
631636
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
632637
* @returns {Promise} - The promise which resolves to an array.
633-
* The first element of the array is an object representing [Operation]{@link google.longrunning.Operation}.
634-
* The promise has a method named "cancel" which cancels the ongoing API call.
638+
* The first element of the array is an object representing
639+
* a long running operation. Its `promise()` method returns a promise
640+
* you can `await` for.
641+
* Please see the
642+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#long-running-operations)
643+
* for more details and examples.
644+
* @example
645+
* const [operation] = await client.batchPredict(request);
646+
* const [response] = await operation.promise();
635647
*/
636648
batchPredict(
637649
request: protos.google.cloud.automl.v1.IBatchPredictRequest,
@@ -683,18 +695,19 @@ export class PredictionServiceClient {
683695
return this.innerApiCalls.batchPredict(request, options, callback);
684696
}
685697
/**
686-
* Check the status of the long running operation returned by the batchPredict() method.
698+
* Check the status of the long running operation returned by `batchPredict()`.
687699
* @param {String} name
688700
* The operation name that will be passed.
689701
* @returns {Promise} - The promise which resolves to an object.
690702
* The decoded operation object has result and metadata field to get information from.
691-
*
692-
* @example:
693-
* const decodedOperation = await checkBatchPredictProgress(name);
694-
* console.log(decodedOperation.result);
695-
* console.log(decodedOperation.done);
696-
* console.log(decodedOperation.metadata);
697-
*
703+
* Please see the
704+
* [documentation](https://github.com/googleapis/gax-nodejs/blob/master/client-libraries.md#long-running-operations)
705+
* for more details and examples.
706+
* @example
707+
* const decodedOperation = await checkBatchPredictProgress(name);
708+
* console.log(decodedOperation.result);
709+
* console.log(decodedOperation.done);
710+
* console.log(decodedOperation.metadata);
698711
*/
699712
async checkBatchPredictProgress(
700713
name: string
@@ -971,9 +984,10 @@ export class PredictionServiceClient {
971984
}
972985

973986
/**
974-
* Terminate the GRPC channel and close the client.
987+
* Terminate the gRPC channel and close the client.
975988
*
976989
* The client will no longer be usable and all future behavior is undefined.
990+
* @returns {Promise} A promise that resolves when the client is closed.
977991
*/
978992
close(): Promise<void> {
979993
this.initialize();

0 commit comments

Comments
 (0)