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

Commit a6427f0

Browse files
fix: allow passing gax instance to client constructor (#688)
- [ ] 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 f3c627d commit a6427f0

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

src/v1/language_service_client.ts

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

3438
/**
@@ -88,8 +92,18 @@ export class LanguageServiceClient {
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 LanguageServiceClient({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.constructor as typeof LanguageServiceClient;
95109
const servicePath =
@@ -109,8 +123,13 @@ export class LanguageServiceClient {
109123
opts['scopes'] = staticMembers.scopes;
110124
}
111125

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

115134
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
116135
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -164,7 +183,7 @@ export class LanguageServiceClient {
164183
this.innerApiCalls = {};
165184

166185
// Add a warn function to the client constructor so it can be easily tested.
167-
this.warn = gax.warn;
186+
this.warn = this._gaxModule.warn;
168187
}
169188

170189
/**

src/v1beta2/language_service_client.ts

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

3438
/**
@@ -88,8 +92,18 @@ export class LanguageServiceClient {
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 LanguageServiceClient({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.constructor as typeof LanguageServiceClient;
95109
const servicePath =
@@ -109,8 +123,13 @@ export class LanguageServiceClient {
109123
opts['scopes'] = staticMembers.scopes;
110124
}
111125

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

115134
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
116135
this._gaxGrpc = new this._gaxModule.GrpcClient(opts);
@@ -164,7 +183,7 @@ export class LanguageServiceClient {
164183
this.innerApiCalls = {};
165184

166185
// Add a warn function to the client constructor so it can be easily tested.
167-
this.warn = gax.warn;
186+
this.warn = this._gaxModule.warn;
168187
}
169188

170189
/**

0 commit comments

Comments
 (0)