1717// ** All changes to this file may be overwritten. **
1818
1919/* global window */
20- import * as gax from 'google-gax' ;
21- import {
20+ import type * as gax from 'google-gax' ;
21+ import type {
2222 Callback ,
2323 CallOptions ,
2424 Descriptors ,
@@ -35,7 +35,6 @@ import jsonProtos = require('../../protos/protos.json');
3535 * This file defines retry strategy and timeouts for all API methods in this library.
3636 */
3737import * as gapicConfig from './image_annotator_client_config.json' ;
38- import { operationsProtos } from 'google-gax' ;
3938const version = require ( '../../../package.json' ) . version ;
4039
4140/**
@@ -98,8 +97,18 @@ export class ImageAnnotatorClient {
9897 * Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
9998 * For more information, please check the
10099 * {@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+ * ```
101107 */
102- constructor ( opts ?: ClientOptions ) {
108+ constructor (
109+ opts ?: ClientOptions ,
110+ gaxInstance ?: typeof gax | typeof gax . fallback
111+ ) {
103112 // Ensure that options include all the required fields.
104113 const staticMembers = this . constructor as typeof ImageAnnotatorClient ;
105114 const servicePath =
@@ -119,8 +128,13 @@ export class ImageAnnotatorClient {
119128 opts [ 'scopes' ] = staticMembers . scopes ;
120129 }
121130
131+ // Load google-gax module synchronously if needed
132+ if ( ! gaxInstance ) {
133+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
134+ }
135+
122136 // 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 ;
124138
125139 // Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
126140 this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -248,7 +262,7 @@ export class ImageAnnotatorClient {
248262 this . innerApiCalls = { } ;
249263
250264 // 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 ;
252266 }
253267
254268 /**
@@ -472,7 +486,7 @@ export class ImageAnnotatorClient {
472486 options . otherArgs = options . otherArgs || { } ;
473487 options . otherArgs . headers = options . otherArgs . headers || { } ;
474488 options . otherArgs . headers [ 'x-goog-request-params' ] =
475- gax . routingHeader . fromParams ( {
489+ this . _gaxModule . routingHeader . fromParams ( {
476490 parent : request . parent || '' ,
477491 } ) ;
478492 this . initialize ( ) ;
@@ -583,7 +597,7 @@ export class ImageAnnotatorClient {
583597 options . otherArgs = options . otherArgs || { } ;
584598 options . otherArgs . headers = options . otherArgs . headers || { } ;
585599 options . otherArgs . headers [ 'x-goog-request-params' ] =
586- gax . routingHeader . fromParams ( {
600+ this . _gaxModule . routingHeader . fromParams ( {
587601 parent : request . parent || '' ,
588602 } ) ;
589603 this . initialize ( ) ;
@@ -710,7 +724,7 @@ export class ImageAnnotatorClient {
710724 options . otherArgs = options . otherArgs || { } ;
711725 options . otherArgs . headers = options . otherArgs . headers || { } ;
712726 options . otherArgs . headers [ 'x-goog-request-params' ] =
713- gax . routingHeader . fromParams ( {
727+ this . _gaxModule . routingHeader . fromParams ( {
714728 parent : request . parent || '' ,
715729 } ) ;
716730 this . initialize ( ) ;
@@ -740,11 +754,12 @@ export class ImageAnnotatorClient {
740754 protos . google . cloud . vision . v1 . OperationMetadata
741755 >
742756 > {
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+ ) ;
746761 const [ operation ] = await this . operationsClient . getOperation ( request ) ;
747- const decodeOperation = new gax . Operation (
762+ const decodeOperation = new this . _gaxModule . Operation (
748763 operation ,
749764 this . descriptors . longrunning . asyncBatchAnnotateImages ,
750765 this . _gaxModule . createDefaultBackoffSettings ( )
@@ -869,7 +884,7 @@ export class ImageAnnotatorClient {
869884 options . otherArgs = options . otherArgs || { } ;
870885 options . otherArgs . headers = options . otherArgs . headers || { } ;
871886 options . otherArgs . headers [ 'x-goog-request-params' ] =
872- gax . routingHeader . fromParams ( {
887+ this . _gaxModule . routingHeader . fromParams ( {
873888 parent : request . parent || '' ,
874889 } ) ;
875890 this . initialize ( ) ;
@@ -899,11 +914,12 @@ export class ImageAnnotatorClient {
899914 protos . google . cloud . vision . v1 . OperationMetadata
900915 >
901916 > {
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+ ) ;
905921 const [ operation ] = await this . operationsClient . getOperation ( request ) ;
906- const decodeOperation = new gax . Operation (
922+ const decodeOperation = new this . _gaxModule . Operation (
907923 operation ,
908924 this . descriptors . longrunning . asyncBatchAnnotateFiles ,
909925 this . _gaxModule . createDefaultBackoffSettings ( )
0 commit comments