Skip to content

Commit 525d8f6

Browse files
fix: allow passing gax instance to client constructor (#140)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 470911839 Source-Link: googleapis/googleapis@3527566 Source-Link: googleapis/googleapis-gen@f16a1d2 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: googleapis/googleapis-gen@e97a1ac Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZTk3YTFhYzIwNGVhZDRmZTczNDFmOTFlNzJkYjdjNmFjNjAxNjM0MSJ9
1 parent a0a23cd commit 525d8f6

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

packages/google-cloud-policytroubleshooter/src/v1/iam_checker_client.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
// ** All changes to this file may be overwritten. **
1818

1919
/* global window */
20-
import * as gax from 'google-gax';
21-
import {Callback, CallOptions, Descriptors, ClientOptions} from 'google-gax';
20+
import type * as gax from 'google-gax';
21+
import type {
22+
Callback,
23+
CallOptions,
24+
Descriptors,
25+
ClientOptions,
26+
} from 'google-gax';
2227

2328
import * as protos from '../../protos/protos';
2429
import jsonProtos = require('../../protos/protos.json');
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
2833
* This file defines retry strategy and timeouts for all API methods in this library.
2934
*/
3035
import * as gapicConfig from './iam_checker_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -89,8 +93,18 @@ export class IamCheckerClient {
8993
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9094
* For more information, please check the
9195
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
96+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
97+
* need to avoid loading the default gRPC version and want to use the fallback
98+
* HTTP implementation. Load only fallback version and pass it to the constructor:
99+
* ```
100+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
101+
* const client = new IamCheckerClient({fallback: 'rest'}, gax);
102+
* ```
92103
*/
93-
constructor(opts?: ClientOptions) {
104+
constructor(
105+
opts?: ClientOptions,
106+
gaxInstance?: typeof gax | typeof gax.fallback
107+
) {
94108
// Ensure that options include all the required fields.
95109
const staticMembers = this.constructor as typeof IamCheckerClient;
96110
const servicePath =
@@ -110,8 +124,13 @@ export class IamCheckerClient {
110124
opts['scopes'] = staticMembers.scopes;
111125
}
112126

127+
// Load google-gax module synchronously if needed
128+
if (!gaxInstance) {
129+
gaxInstance = require('google-gax') as typeof gax;
130+
}
131+
113132
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
114-
this._gaxModule = opts.fallback ? gax.fallback : gax;
133+
this._gaxModule = opts.fallback ? gaxInstance.fallback : gaxInstance;
115134

116135
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
117136
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -165,7 +184,7 @@ export class IamCheckerClient {
165184
this.innerApiCalls = {};
166185

167186
// Add a warn function to the client constructor so it can be easily tested.
168-
this.warn = gax.warn;
187+
this.warn = this._gaxModule.warn;
169188
}
170189

171190
/**

0 commit comments

Comments
 (0)