Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit f162180

Browse files
fix: allow passing gax instance to client constructor (#102)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: https://github.com/googleapis/googleapis-gen/commit/f16a1d224f00a630ea43d6a9a1a31f566f45cdea Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZjE2YTFkMjI0ZjAwYTYzMGVhNDNkNmE5YTFhMzFmNTY2ZjQ1Y2RlYSJ9 feat: accept google-gax instance as a parameter Please see the documentation of the client constructor for details. PiperOrigin-RevId: 470332808 Source-Link: googleapis/googleapis@d4a2367 Source-Link: https://github.com/googleapis/googleapis-gen/commit/e97a1ac204ead4fe7341f91e72db7c6ac6016341 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9
1 parent 54959ca commit f162180

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/v1beta1/private_catalog_client.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {
20+
import type * as gax from 'google-gax';
21+
import type {
2222
Callback,
2323
CallOptions,
2424
Descriptors,
2525
ClientOptions,
2626
PaginationCallback,
2727
GaxCall,
2828
} from 'google-gax';
29-
3029
import {Transform} from 'stream';
3130
import * as protos from '../../protos/protos';
3231
import jsonProtos = require('../../protos/protos.json');
@@ -36,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3635
* This file defines retry strategy and timeouts for all API methods in this library.
3736
*/
3837
import * as gapicConfig from './private_catalog_client_config.json';
39-
4038
const version = require('../../../package.json').version;
4139

4240
/**
@@ -116,8 +114,18 @@ export class PrivateCatalogClient {
116114
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
117115
* For more information, please check the
118116
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
117+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
118+
* need to avoid loading the default gRPC version and want to use the fallback
119+
* HTTP implementation. Load only fallback version and pass it to the constructor:
120+
* ```
121+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
122+
* const client = new PrivateCatalogClient({fallback: 'rest'}, gax);
123+
* ```
119124
*/
120-
constructor(opts?: ClientOptions) {
125+
constructor(
126+
opts?: ClientOptions,
127+
gaxInstance?: typeof gax | typeof gax.fallback
128+
) {
121129
// Ensure that options include all the required fields.
122130
const staticMembers = this.constructor as typeof PrivateCatalogClient;
123131
const servicePath =
@@ -137,8 +145,13 @@ export class PrivateCatalogClient {
137145
opts['scopes'] = staticMembers.scopes;
138146
}
139147

148+
// Load google-gax module synchronously if needed
149+
if (!gaxInstance) {
150+
gaxInstance = require('google-gax') as typeof gax;
151+
}
152+
140153
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
141-
this._gaxModule = opts.fallback ? gax.fallback : gax;
154+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
142155

143156
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
144157
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -228,7 +241,7 @@ export class PrivateCatalogClient {
228241
this.innerApiCalls = {};
229242

230243
// Add a warn function to the client constructor so it can be easily tested.
231-
this.warn = gax.warn;
244+
this.warn = this._gaxModule.warn;
232245
}
233246

234247
/**
@@ -454,7 +467,7 @@ export class PrivateCatalogClient {
454467
options.otherArgs = options.otherArgs || {};
455468
options.otherArgs.headers = options.otherArgs.headers || {};
456469
options.otherArgs.headers['x-goog-request-params'] =
457-
gax.routingHeader.fromParams({
470+
this._gaxModule.routingHeader.fromParams({
458471
resource: request.resource || '',
459472
});
460473
this.initialize();
@@ -501,7 +514,7 @@ export class PrivateCatalogClient {
501514
options.otherArgs = options.otherArgs || {};
502515
options.otherArgs.headers = options.otherArgs.headers || {};
503516
options.otherArgs.headers['x-goog-request-params'] =
504-
gax.routingHeader.fromParams({
517+
this._gaxModule.routingHeader.fromParams({
505518
resource: request.resource || '',
506519
});
507520
const defaultCallSettings = this._defaults['searchCatalogs'];
@@ -557,7 +570,7 @@ export class PrivateCatalogClient {
557570
options.otherArgs = options.otherArgs || {};
558571
options.otherArgs.headers = options.otherArgs.headers || {};
559572
options.otherArgs.headers['x-goog-request-params'] =
560-
gax.routingHeader.fromParams({
573+
this._gaxModule.routingHeader.fromParams({
561574
resource: request.resource || '',
562575
});
563576
const defaultCallSettings = this._defaults['searchCatalogs'];
@@ -672,7 +685,7 @@ export class PrivateCatalogClient {
672685
options.otherArgs = options.otherArgs || {};
673686
options.otherArgs.headers = options.otherArgs.headers || {};
674687
options.otherArgs.headers['x-goog-request-params'] =
675-
gax.routingHeader.fromParams({
688+
this._gaxModule.routingHeader.fromParams({
676689
resource: request.resource || '',
677690
});
678691
this.initialize();
@@ -720,7 +733,7 @@ export class PrivateCatalogClient {
720733
options.otherArgs = options.otherArgs || {};
721734
options.otherArgs.headers = options.otherArgs.headers || {};
722735
options.otherArgs.headers['x-goog-request-params'] =
723-
gax.routingHeader.fromParams({
736+
this._gaxModule.routingHeader.fromParams({
724737
resource: request.resource || '',
725738
});
726739
const defaultCallSettings = this._defaults['searchProducts'];
@@ -777,7 +790,7 @@ export class PrivateCatalogClient {
777790
options.otherArgs = options.otherArgs || {};
778791
options.otherArgs.headers = options.otherArgs.headers || {};
779792
options.otherArgs.headers['x-goog-request-params'] =
780-
gax.routingHeader.fromParams({
793+
this._gaxModule.routingHeader.fromParams({
781794
resource: request.resource || '',
782795
});
783796
const defaultCallSettings = this._defaults['searchProducts'];
@@ -892,7 +905,7 @@ export class PrivateCatalogClient {
892905
options.otherArgs = options.otherArgs || {};
893906
options.otherArgs.headers = options.otherArgs.headers || {};
894907
options.otherArgs.headers['x-goog-request-params'] =
895-
gax.routingHeader.fromParams({
908+
this._gaxModule.routingHeader.fromParams({
896909
resource: request.resource || '',
897910
});
898911
this.initialize();
@@ -940,7 +953,7 @@ export class PrivateCatalogClient {
940953
options.otherArgs = options.otherArgs || {};
941954
options.otherArgs.headers = options.otherArgs.headers || {};
942955
options.otherArgs.headers['x-goog-request-params'] =
943-
gax.routingHeader.fromParams({
956+
this._gaxModule.routingHeader.fromParams({
944957
resource: request.resource || '',
945958
});
946959
const defaultCallSettings = this._defaults['searchVersions'];
@@ -997,7 +1010,7 @@ export class PrivateCatalogClient {
9971010
options.otherArgs = options.otherArgs || {};
9981011
options.otherArgs.headers = options.otherArgs.headers || {};
9991012
options.otherArgs.headers['x-goog-request-params'] =
1000-
gax.routingHeader.fromParams({
1013+
this._gaxModule.routingHeader.fromParams({
10011014
resource: request.resource || '',
10021015
});
10031016
const defaultCallSettings = this._defaults['searchVersions'];

0 commit comments

Comments
 (0)