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 {
20
+ import type * as gax from 'google-gax' ;
21
+ import type {
22
22
Callback ,
23
23
CallOptions ,
24
24
Descriptors ,
@@ -28,7 +28,6 @@ import {
28
28
PaginationCallback ,
29
29
GaxCall ,
30
30
} from 'google-gax' ;
31
-
32
31
import { Transform } from 'stream' ;
33
32
import * as protos from '../../protos/protos' ;
34
33
import jsonProtos = require( '../../protos/protos.json' ) ;
@@ -38,7 +37,6 @@ import jsonProtos = require('../../protos/protos.json');
38
37
* This file defines retry strategy and timeouts for all API methods in this library.
39
38
*/
40
39
import * as gapicConfig from './service_usage_client_config.json' ;
41
- import { operationsProtos } from 'google-gax' ;
42
40
const version = require ( '../../../package.json' ) . version ;
43
41
44
42
/**
@@ -102,8 +100,18 @@ export class ServiceUsageClient {
102
100
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
103
101
* For more information, please check the
104
102
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
103
+ * @param {gax } [gaxInstance]: loaded instance of `google-gax`. Useful if you
104
+ * need to avoid loading the default gRPC version and want to use the fallback
105
+ * HTTP implementation. Load only fallback version and pass it to the constructor:
106
+ * ```
107
+ * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
108
+ * const client = new ServiceUsageClient({fallback: 'rest'}, gax);
109
+ * ```
105
110
*/
106
- constructor ( opts ?: ClientOptions ) {
111
+ constructor (
112
+ opts ?: ClientOptions ,
113
+ gaxInstance ?: typeof gax | typeof gax . fallback
114
+ ) {
107
115
// Ensure that options include all the required fields.
108
116
const staticMembers = this . constructor as typeof ServiceUsageClient ;
109
117
const servicePath =
@@ -123,8 +131,13 @@ export class ServiceUsageClient {
123
131
opts [ 'scopes' ] = staticMembers . scopes ;
124
132
}
125
133
134
+ // Load google-gax module synchronously if needed
135
+ if ( ! gaxInstance ) {
136
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
137
+ }
138
+
126
139
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
127
- this . _gaxModule = opts . fallback ? gax . fallback : gax ;
140
+ this . _gaxModule = opts . fallback ? gaxInstance . fallback : gaxInstance ;
128
141
129
142
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
130
143
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -250,7 +263,7 @@ export class ServiceUsageClient {
250
263
this . innerApiCalls = { } ;
251
264
252
265
// Add a warn function to the client constructor so it can be easily tested.
253
- this . warn = gax . warn ;
266
+ this . warn = this . _gaxModule . warn ;
254
267
}
255
268
256
269
/**
@@ -465,7 +478,7 @@ export class ServiceUsageClient {
465
478
options . otherArgs = options . otherArgs || { } ;
466
479
options . otherArgs . headers = options . otherArgs . headers || { } ;
467
480
options . otherArgs . headers [ 'x-goog-request-params' ] =
468
- gax . routingHeader . fromParams ( {
481
+ this . _gaxModule . routingHeader . fromParams ( {
469
482
name : request . name || '' ,
470
483
} ) ;
471
484
this . initialize ( ) ;
@@ -568,7 +581,7 @@ export class ServiceUsageClient {
568
581
options . otherArgs = options . otherArgs || { } ;
569
582
options . otherArgs . headers = options . otherArgs . headers || { } ;
570
583
options . otherArgs . headers [ 'x-goog-request-params' ] =
571
- gax . routingHeader . fromParams ( {
584
+ this . _gaxModule . routingHeader . fromParams ( {
572
585
parent : request . parent || '' ,
573
586
} ) ;
574
587
this . initialize ( ) ;
@@ -682,7 +695,7 @@ export class ServiceUsageClient {
682
695
options . otherArgs = options . otherArgs || { } ;
683
696
options . otherArgs . headers = options . otherArgs . headers || { } ;
684
697
options . otherArgs . headers [ 'x-goog-request-params' ] =
685
- gax . routingHeader . fromParams ( {
698
+ this . _gaxModule . routingHeader . fromParams ( {
686
699
name : request . name || '' ,
687
700
} ) ;
688
701
this . initialize ( ) ;
@@ -708,11 +721,12 @@ export class ServiceUsageClient {
708
721
protos . google . api . serviceusage . v1 . OperationMetadata
709
722
>
710
723
> {
711
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
712
- { name}
713
- ) ;
724
+ const request =
725
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
726
+ { name}
727
+ ) ;
714
728
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
715
- const decodeOperation = new gax . Operation (
729
+ const decodeOperation = new this . _gaxModule . Operation (
716
730
operation ,
717
731
this . descriptors . longrunning . enableService ,
718
732
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -839,7 +853,7 @@ export class ServiceUsageClient {
839
853
options . otherArgs = options . otherArgs || { } ;
840
854
options . otherArgs . headers = options . otherArgs . headers || { } ;
841
855
options . otherArgs . headers [ 'x-goog-request-params' ] =
842
- gax . routingHeader . fromParams ( {
856
+ this . _gaxModule . routingHeader . fromParams ( {
843
857
name : request . name || '' ,
844
858
} ) ;
845
859
this . initialize ( ) ;
@@ -865,11 +879,12 @@ export class ServiceUsageClient {
865
879
protos . google . api . serviceusage . v1 . OperationMetadata
866
880
>
867
881
> {
868
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
869
- { name}
870
- ) ;
882
+ const request =
883
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
884
+ { name}
885
+ ) ;
871
886
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
872
- const decodeOperation = new gax . Operation (
887
+ const decodeOperation = new this . _gaxModule . Operation (
873
888
operation ,
874
889
this . descriptors . longrunning . disableService ,
875
890
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -995,7 +1010,7 @@ export class ServiceUsageClient {
995
1010
options . otherArgs = options . otherArgs || { } ;
996
1011
options . otherArgs . headers = options . otherArgs . headers || { } ;
997
1012
options . otherArgs . headers [ 'x-goog-request-params' ] =
998
- gax . routingHeader . fromParams ( {
1013
+ this . _gaxModule . routingHeader . fromParams ( {
999
1014
parent : request . parent || '' ,
1000
1015
} ) ;
1001
1016
this . initialize ( ) ;
@@ -1021,11 +1036,12 @@ export class ServiceUsageClient {
1021
1036
protos . google . api . serviceusage . v1 . OperationMetadata
1022
1037
>
1023
1038
> {
1024
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1025
- { name}
1026
- ) ;
1039
+ const request =
1040
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1041
+ { name}
1042
+ ) ;
1027
1043
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1028
- const decodeOperation = new gax . Operation (
1044
+ const decodeOperation = new this . _gaxModule . Operation (
1029
1045
operation ,
1030
1046
this . descriptors . longrunning . batchEnableServices ,
1031
1047
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1148,7 +1164,7 @@ export class ServiceUsageClient {
1148
1164
options . otherArgs = options . otherArgs || { } ;
1149
1165
options . otherArgs . headers = options . otherArgs . headers || { } ;
1150
1166
options . otherArgs . headers [ 'x-goog-request-params' ] =
1151
- gax . routingHeader . fromParams ( {
1167
+ this . _gaxModule . routingHeader . fromParams ( {
1152
1168
parent : request . parent || '' ,
1153
1169
} ) ;
1154
1170
this . initialize ( ) ;
@@ -1195,7 +1211,7 @@ export class ServiceUsageClient {
1195
1211
options . otherArgs = options . otherArgs || { } ;
1196
1212
options . otherArgs . headers = options . otherArgs . headers || { } ;
1197
1213
options . otherArgs . headers [ 'x-goog-request-params' ] =
1198
- gax . routingHeader . fromParams ( {
1214
+ this . _gaxModule . routingHeader . fromParams ( {
1199
1215
parent : request . parent || '' ,
1200
1216
} ) ;
1201
1217
const defaultCallSettings = this . _defaults [ 'listServices' ] ;
@@ -1251,7 +1267,7 @@ export class ServiceUsageClient {
1251
1267
options . otherArgs = options . otherArgs || { } ;
1252
1268
options . otherArgs . headers = options . otherArgs . headers || { } ;
1253
1269
options . otherArgs . headers [ 'x-goog-request-params' ] =
1254
- gax . routingHeader . fromParams ( {
1270
+ this . _gaxModule . routingHeader . fromParams ( {
1255
1271
parent : request . parent || '' ,
1256
1272
} ) ;
1257
1273
const defaultCallSettings = this . _defaults [ 'listServices' ] ;
0 commit comments