diff --git a/packages/google-cloud-run/src/v2/revisions_client.ts b/packages/google-cloud-run/src/v2/revisions_client.ts index f966d0a973c..5da0b46ef2e 100644 --- a/packages/google-cloud-run/src/v2/revisions_client.ts +++ b/packages/google-cloud-run/src/v2/revisions_client.ts @@ -17,8 +17,8 @@ // ** All changes to this file may be overwritten. ** /* global window */ -import * as gax from 'google-gax'; -import { +import type * as gax from 'google-gax'; +import type { Callback, CallOptions, Descriptors, @@ -28,7 +28,6 @@ import { PaginationCallback, GaxCall, } from 'google-gax'; - import {Transform} from 'stream'; import * as protos from '../../protos/protos'; import jsonProtos = require('../../protos/protos.json'); @@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json'); * This file defines retry strategy and timeouts for all API methods in this library. */ import * as gapicConfig from './revisions_client_config.json'; -import {operationsProtos} from 'google-gax'; const version = require('../../../package.json').version; /** @@ -99,8 +97,18 @@ export class RevisionsClient { * Pass "rest" to use HTTP/1.1 REST API instead of gRPC. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. + * @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you + * need to avoid loading the default gRPC version and want to use the fallback + * HTTP implementation. Load only fallback version and pass it to the constructor: + * ``` + * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC + * const client = new RevisionsClient({fallback: 'rest'}, gax); + * ``` */ - constructor(opts?: ClientOptions) { + constructor( + opts?: ClientOptions, + gaxInstance?: typeof gax | typeof gax.fallback + ) { // Ensure that options include all the required fields. const staticMembers = this.constructor as typeof RevisionsClient; const servicePath = @@ -120,8 +128,13 @@ export class RevisionsClient { opts['scopes'] = staticMembers.scopes; } + // Load google-gax module synchronously if needed + if (!gaxInstance) { + gaxInstance = require('google-gax') as typeof gax; + } + // Choose either gRPC or proto-over-HTTP implementation of google-gax. - this._gaxModule = opts.fallback ? gax.fallback : gax; + this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance; // Create a `gaxGrpc` object, with any grpc-specific options sent to the client. this._gaxGrpc = new this._gaxModule.GrpcClient(opts); @@ -250,7 +263,7 @@ export class RevisionsClient { this.innerApiCalls = {}; // Add a warn function to the client constructor so it can be easily tested. - this.warn = gax.warn; + this.warn = this._gaxModule.warn; } /** @@ -467,7 +480,7 @@ export class RevisionsClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.getRevision(request, options, callback); } @@ -590,7 +603,7 @@ export class RevisionsClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.deleteRevision(request, options, callback); } @@ -614,11 +627,12 @@ export class RevisionsClient { protos.google.cloud.run.v2.Revision > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.deleteRevision, this._gaxModule.createDefaultBackoffSettings() @@ -731,7 +745,7 @@ export class RevisionsClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.listRevisions(request, options, callback); } @@ -786,7 +800,7 @@ export class RevisionsClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); const defaultCallSettings = this._defaults['listRevisions']; const callSettings = defaultCallSettings.merge(options); this.initialize(); @@ -850,7 +864,7 @@ export class RevisionsClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); const defaultCallSettings = this._defaults['listRevisions']; const callSettings = defaultCallSettings.merge(options); this.initialize(); diff --git a/packages/google-cloud-run/src/v2/services_client.ts b/packages/google-cloud-run/src/v2/services_client.ts index 7522e559966..761335803f4 100644 --- a/packages/google-cloud-run/src/v2/services_client.ts +++ b/packages/google-cloud-run/src/v2/services_client.ts @@ -17,8 +17,8 @@ // ** All changes to this file may be overwritten. ** /* global window */ -import * as gax from 'google-gax'; -import { +import type * as gax from 'google-gax'; +import type { Callback, CallOptions, Descriptors, @@ -28,7 +28,6 @@ import { PaginationCallback, GaxCall, } from 'google-gax'; - import {Transform} from 'stream'; import * as protos from '../../protos/protos'; import jsonProtos = require('../../protos/protos.json'); @@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json'); * This file defines retry strategy and timeouts for all API methods in this library. */ import * as gapicConfig from './services_client_config.json'; -import {operationsProtos} from 'google-gax'; const version = require('../../../package.json').version; /** @@ -99,8 +97,18 @@ export class ServicesClient { * Pass "rest" to use HTTP/1.1 REST API instead of gRPC. * For more information, please check the * {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}. + * @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you + * need to avoid loading the default gRPC version and want to use the fallback + * HTTP implementation. Load only fallback version and pass it to the constructor: + * ``` + * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC + * const client = new ServicesClient({fallback: 'rest'}, gax); + * ``` */ - constructor(opts?: ClientOptions) { + constructor( + opts?: ClientOptions, + gaxInstance?: typeof gax | typeof gax.fallback + ) { // Ensure that options include all the required fields. const staticMembers = this.constructor as typeof ServicesClient; const servicePath = @@ -120,8 +128,13 @@ export class ServicesClient { opts['scopes'] = staticMembers.scopes; } + // Load google-gax module synchronously if needed + if (!gaxInstance) { + gaxInstance = require('google-gax') as typeof gax; + } + // Choose either gRPC or proto-over-HTTP implementation of google-gax. - this._gaxModule = opts.fallback ? gax.fallback : gax; + this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance; // Create a `gaxGrpc` object, with any grpc-specific options sent to the client. this._gaxGrpc = new this._gaxModule.GrpcClient(opts); @@ -269,7 +282,7 @@ export class ServicesClient { this.innerApiCalls = {}; // Add a warn function to the client constructor so it can be easily tested. - this.warn = gax.warn; + this.warn = this._gaxModule.warn; } /** @@ -490,7 +503,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.getService(request, options, callback); } @@ -576,7 +589,7 @@ export class ServicesClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -672,7 +685,7 @@ export class ServicesClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -763,7 +776,7 @@ export class ServicesClient { options.otherArgs = options.otherArgs || {}; options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams({ + this._gaxModule.routingHeader.fromParams({ resource: request.resource || '', }); this.initialize(); @@ -889,7 +902,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.createService(request, options, callback); } @@ -913,11 +926,12 @@ export class ServicesClient { protos.google.cloud.run.v2.Service > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.createService, this._gaxModule.createDefaultBackoffSettings() @@ -1044,7 +1058,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.updateService(request, options, callback); } @@ -1068,11 +1082,12 @@ export class ServicesClient { protos.google.cloud.run.v2.Service > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.updateService, this._gaxModule.createDefaultBackoffSettings() @@ -1201,7 +1216,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.deleteService(request, options, callback); } @@ -1225,11 +1240,12 @@ export class ServicesClient { protos.google.cloud.run.v2.Service > > { - const request = new operationsProtos.google.longrunning.GetOperationRequest( - {name} - ); + const request = + new this._gaxModule.operationsProtos.google.longrunning.GetOperationRequest( + {name} + ); const [operation] = await this.operationsClient.getOperation(request); - const decodeOperation = new gax.Operation( + const decodeOperation = new this._gaxModule.Operation( operation, this.descriptors.longrunning.deleteService, this._gaxModule.createDefaultBackoffSettings() @@ -1341,7 +1357,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); this.initialize(); return this.innerApiCalls.listServices(request, options, callback); } @@ -1395,7 +1411,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); const defaultCallSettings = this._defaults['listServices']; const callSettings = defaultCallSettings.merge(options); this.initialize(); @@ -1458,7 +1474,7 @@ export class ServicesClient { } } options.otherArgs.headers['x-goog-request-params'] = - gax.routingHeader.fromParams(routingParameter); + this._gaxModule.routingHeader.fromParams(routingParameter); const defaultCallSettings = this._defaults['listServices']; const callSettings = defaultCallSettings.merge(options); this.initialize();