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

Commit 33e1faa

Browse files
fix: allow passing gax instance to client constructor (#407)
- [ ] 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 54ef38f commit 33e1faa

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/v1beta1/phishing_protection_service_v1_beta1_client.ts

Lines changed: 26 additions & 7 deletions
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 './phishing_protection_service_v1_beta1_client_config.json';
31-
3236
const version = require('../../../package.json').version;
3337

3438
/**
@@ -88,8 +92,18 @@ export class PhishingProtectionServiceV1Beta1Client {
8892
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
8993
* For more information, please check the
9094
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
95+
* @param {gax} [gaxInstance]: loaded instance of `google-gax`. Useful if you
96+
* need to avoid loading the default gRPC version and want to use the fallback
97+
* HTTP implementation. Load only fallback version and pass it to the constructor:
98+
* ```
99+
* const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
100+
* const client = new PhishingProtectionServiceV1Beta1Client({fallback: 'rest'}, gax);
101+
* ```
91102
*/
92-
constructor(opts?: ClientOptions) {
103+
constructor(
104+
opts?: ClientOptions,
105+
gaxInstance?: typeof gax | typeof gax.fallback
106+
) {
93107
// Ensure that options include all the required fields.
94108
const staticMembers = this
95109
.constructor as typeof PhishingProtectionServiceV1Beta1Client;
@@ -110,8 +124,13 @@ export class PhishingProtectionServiceV1Beta1Client {
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);
@@ -174,7 +193,7 @@ export class PhishingProtectionServiceV1Beta1Client {
174193
this.innerApiCalls = {};
175194

176195
// Add a warn function to the client constructor so it can be easily tested.
177-
this.warn = gax.warn;
196+
this.warn = this._gaxModule.warn;
178197
}
179198

180199
/**
@@ -393,7 +412,7 @@ export class PhishingProtectionServiceV1Beta1Client {
393412
options.otherArgs = options.otherArgs || {};
394413
options.otherArgs.headers = options.otherArgs.headers || {};
395414
options.otherArgs.headers['x-goog-request-params'] =
396-
gax.routingHeader.fromParams({
415+
this._gaxModule.routingHeader.fromParams({
397416
parent: request.parent || '',
398417
});
399418
this.initialize();

0 commit comments

Comments
 (0)