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 ,
@@ -35,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
35
35
* This file defines retry strategy and timeouts for all API methods in this library.
36
36
*/
37
37
import * as gapicConfig from './image_annotator_client_config.json' ;
38
- import { operationsProtos } from 'google-gax' ;
39
38
const version = require ( '../../../package.json' ) . version ;
40
39
41
40
/**
@@ -98,8 +97,18 @@ export class ImageAnnotatorClient {
98
97
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
99
98
* For more information, please check the
100
99
* {@link https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#http11-rest-api-mode documentation}.
100
+ * @param {gax } [gaxInstance]: loaded instance of `google-gax`. Useful if you
101
+ * need to avoid loading the default gRPC version and want to use the fallback
102
+ * HTTP implementation. Load only fallback version and pass it to the constructor:
103
+ * ```
104
+ * const gax = require('google-gax/build/src/fallback'); // avoids loading google-gax with gRPC
105
+ * const client = new ImageAnnotatorClient({fallback: 'rest'}, gax);
106
+ * ```
101
107
*/
102
- constructor ( opts ?: ClientOptions ) {
108
+ constructor (
109
+ opts ?: ClientOptions ,
110
+ gaxInstance ?: typeof gax | typeof gax . fallback
111
+ ) {
103
112
// Ensure that options include all the required fields.
104
113
const staticMembers = this . constructor as typeof ImageAnnotatorClient ;
105
114
const servicePath =
@@ -119,8 +128,13 @@ export class ImageAnnotatorClient {
119
128
opts [ 'scopes' ] = staticMembers . scopes ;
120
129
}
121
130
131
+ // Load google-gax module synchronously if needed
132
+ if ( ! gaxInstance ) {
133
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
134
+ }
135
+
122
136
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
123
- this . _gaxModule = opts . fallback ? gax . fallback : gax ;
137
+ this . _gaxModule = opts . fallback ? gaxInstance . fallback : gaxInstance ;
124
138
125
139
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
126
140
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -248,7 +262,7 @@ export class ImageAnnotatorClient {
248
262
this . innerApiCalls = { } ;
249
263
250
264
// Add a warn function to the client constructor so it can be easily tested.
251
- this . warn = gax . warn ;
265
+ this . warn = this . _gaxModule . warn ;
252
266
}
253
267
254
268
/**
@@ -472,7 +486,7 @@ export class ImageAnnotatorClient {
472
486
options . otherArgs = options . otherArgs || { } ;
473
487
options . otherArgs . headers = options . otherArgs . headers || { } ;
474
488
options . otherArgs . headers [ 'x-goog-request-params' ] =
475
- gax . routingHeader . fromParams ( {
489
+ this . _gaxModule . routingHeader . fromParams ( {
476
490
parent : request . parent || '' ,
477
491
} ) ;
478
492
this . initialize ( ) ;
@@ -583,7 +597,7 @@ export class ImageAnnotatorClient {
583
597
options . otherArgs = options . otherArgs || { } ;
584
598
options . otherArgs . headers = options . otherArgs . headers || { } ;
585
599
options . otherArgs . headers [ 'x-goog-request-params' ] =
586
- gax . routingHeader . fromParams ( {
600
+ this . _gaxModule . routingHeader . fromParams ( {
587
601
parent : request . parent || '' ,
588
602
} ) ;
589
603
this . initialize ( ) ;
@@ -710,7 +724,7 @@ export class ImageAnnotatorClient {
710
724
options . otherArgs = options . otherArgs || { } ;
711
725
options . otherArgs . headers = options . otherArgs . headers || { } ;
712
726
options . otherArgs . headers [ 'x-goog-request-params' ] =
713
- gax . routingHeader . fromParams ( {
727
+ this . _gaxModule . routingHeader . fromParams ( {
714
728
parent : request . parent || '' ,
715
729
} ) ;
716
730
this . initialize ( ) ;
@@ -740,11 +754,12 @@ export class ImageAnnotatorClient {
740
754
protos . google . cloud . vision . v1 . OperationMetadata
741
755
>
742
756
> {
743
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
744
- { name}
745
- ) ;
757
+ const request =
758
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
759
+ { name}
760
+ ) ;
746
761
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
747
- const decodeOperation = new gax . Operation (
762
+ const decodeOperation = new this . _gaxModule . Operation (
748
763
operation ,
749
764
this . descriptors . longrunning . asyncBatchAnnotateImages ,
750
765
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -869,7 +884,7 @@ export class ImageAnnotatorClient {
869
884
options . otherArgs = options . otherArgs || { } ;
870
885
options . otherArgs . headers = options . otherArgs . headers || { } ;
871
886
options . otherArgs . headers [ 'x-goog-request-params' ] =
872
- gax . routingHeader . fromParams ( {
887
+ this . _gaxModule . routingHeader . fromParams ( {
873
888
parent : request . parent || '' ,
874
889
} ) ;
875
890
this . initialize ( ) ;
@@ -899,11 +914,12 @@ export class ImageAnnotatorClient {
899
914
protos . google . cloud . vision . v1 . OperationMetadata
900
915
>
901
916
> {
902
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
903
- { name}
904
- ) ;
917
+ const request =
918
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
919
+ { name}
920
+ ) ;
905
921
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
906
- const decodeOperation = new gax . Operation (
922
+ const decodeOperation = new this . _gaxModule . Operation (
907
923
operation ,
908
924
this . descriptors . longrunning . asyncBatchAnnotateFiles ,
909
925
this . _gaxModule . createDefaultBackoffSettings ( )
0 commit comments