17
17
// ** All changes to this file may be overwritten. **
18
18
19
19
/* 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' ;
22
27
23
28
import * as protos from '../../protos/protos' ;
24
29
import jsonProtos = require( '../../protos/protos.json' ) ;
@@ -28,7 +33,6 @@ import jsonProtos = require('../../protos/protos.json');
28
33
* This file defines retry strategy and timeouts for all API methods in this library.
29
34
*/
30
35
import * as gapicConfig from './web_risk_service_client_config.json' ;
31
-
32
36
const version = require ( '../../../package.json' ) . version ;
33
37
34
38
/**
@@ -89,8 +93,18 @@ export class WebRiskServiceClient {
89
93
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
90
94
* For more information, please check the
91
95
* {@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 WebRiskServiceClient({fallback: 'rest'}, gax);
102
+ * ```
92
103
*/
93
- constructor ( opts ?: ClientOptions ) {
104
+ constructor (
105
+ opts ?: ClientOptions ,
106
+ gaxInstance ?: typeof gax | typeof gax . fallback
107
+ ) {
94
108
// Ensure that options include all the required fields.
95
109
const staticMembers = this . constructor as typeof WebRiskServiceClient ;
96
110
const servicePath =
@@ -110,8 +124,13 @@ export class WebRiskServiceClient {
110
124
opts [ 'scopes' ] = staticMembers . scopes ;
111
125
}
112
126
127
+ // Load google-gax module synchronously if needed
128
+ if ( ! gaxInstance ) {
129
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
130
+ }
131
+
113
132
// 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 ;
115
134
116
135
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
117
136
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -174,7 +193,7 @@ export class WebRiskServiceClient {
174
193
this . innerApiCalls = { } ;
175
194
176
195
// 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 ;
178
197
}
179
198
180
199
/**
@@ -663,8 +682,8 @@ export class WebRiskServiceClient {
663
682
options . otherArgs = options . otherArgs || { } ;
664
683
options . otherArgs . headers = options . otherArgs . headers || { } ;
665
684
options . otherArgs . headers [ 'x-goog-request-params' ] =
666
- gax . routingHeader . fromParams ( {
667
- parent : request . parent || '' ,
685
+ this . _gaxModule . routingHeader . fromParams ( {
686
+ parent : request . parent ?? '' ,
668
687
} ) ;
669
688
this . initialize ( ) ;
670
689
return this . innerApiCalls . createSubmission ( request , options , callback ) ;
0 commit comments