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 './translation_service_client_config.json' ;
41
- import { operationsProtos } from 'google-gax' ;
42
40
const version = require ( '../../../package.json' ) . version ;
43
41
44
42
/**
@@ -99,8 +97,18 @@ export class TranslationServiceClient {
99
97
* Pass "rest" to use HTTP/1.1 REST API instead of gRPC.
100
98
* For more information, please check the
101
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 TranslationServiceClient({fallback: 'rest'}, gax);
106
+ * ```
102
107
*/
103
- constructor ( opts ?: ClientOptions ) {
108
+ constructor (
109
+ opts ?: ClientOptions ,
110
+ gaxInstance ?: typeof gax | typeof gax . fallback
111
+ ) {
104
112
// Ensure that options include all the required fields.
105
113
const staticMembers = this . constructor as typeof TranslationServiceClient ;
106
114
const servicePath =
@@ -120,8 +128,13 @@ export class TranslationServiceClient {
120
128
opts [ 'scopes' ] = staticMembers . scopes ;
121
129
}
122
130
131
+ // Load google-gax module synchronously if needed
132
+ if ( ! gaxInstance ) {
133
+ gaxInstance = require ( 'google-gax' ) as typeof gax ;
134
+ }
135
+
123
136
// Choose either gRPC or proto-over-HTTP implementation of google-gax.
124
- this . _gaxModule = opts . fallback ? gax . fallback : gax ;
137
+ this . _gaxModule = opts . fallback ? gaxInstance . fallback : gaxInstance ;
125
138
126
139
// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
127
140
this . _gaxGrpc = new this . _gaxModule . GrpcClient ( opts ) ;
@@ -296,7 +309,7 @@ export class TranslationServiceClient {
296
309
this . innerApiCalls = { } ;
297
310
298
311
// Add a warn function to the client constructor so it can be easily tested.
299
- this . warn = gax . warn ;
312
+ this . warn = this . _gaxModule . warn ;
300
313
}
301
314
302
315
/**
@@ -577,7 +590,7 @@ export class TranslationServiceClient {
577
590
options . otherArgs = options . otherArgs || { } ;
578
591
options . otherArgs . headers = options . otherArgs . headers || { } ;
579
592
options . otherArgs . headers [ 'x-goog-request-params' ] =
580
- gax . routingHeader . fromParams ( {
593
+ this . _gaxModule . routingHeader . fromParams ( {
581
594
parent : request . parent || '' ,
582
595
} ) ;
583
596
this . initialize ( ) ;
@@ -703,7 +716,7 @@ export class TranslationServiceClient {
703
716
options . otherArgs = options . otherArgs || { } ;
704
717
options . otherArgs . headers = options . otherArgs . headers || { } ;
705
718
options . otherArgs . headers [ 'x-goog-request-params' ] =
706
- gax . routingHeader . fromParams ( {
719
+ this . _gaxModule . routingHeader . fromParams ( {
707
720
parent : request . parent || '' ,
708
721
} ) ;
709
722
this . initialize ( ) ;
@@ -830,7 +843,7 @@ export class TranslationServiceClient {
830
843
options . otherArgs = options . otherArgs || { } ;
831
844
options . otherArgs . headers = options . otherArgs . headers || { } ;
832
845
options . otherArgs . headers [ 'x-goog-request-params' ] =
833
- gax . routingHeader . fromParams ( {
846
+ this . _gaxModule . routingHeader . fromParams ( {
834
847
parent : request . parent || '' ,
835
848
} ) ;
836
849
this . initialize ( ) ;
@@ -978,7 +991,7 @@ export class TranslationServiceClient {
978
991
options . otherArgs = options . otherArgs || { } ;
979
992
options . otherArgs . headers = options . otherArgs . headers || { } ;
980
993
options . otherArgs . headers [ 'x-goog-request-params' ] =
981
- gax . routingHeader . fromParams ( {
994
+ this . _gaxModule . routingHeader . fromParams ( {
982
995
parent : request . parent || '' ,
983
996
} ) ;
984
997
this . initialize ( ) ;
@@ -1064,7 +1077,7 @@ export class TranslationServiceClient {
1064
1077
options . otherArgs = options . otherArgs || { } ;
1065
1078
options . otherArgs . headers = options . otherArgs . headers || { } ;
1066
1079
options . otherArgs . headers [ 'x-goog-request-params' ] =
1067
- gax . routingHeader . fromParams ( {
1080
+ this . _gaxModule . routingHeader . fromParams ( {
1068
1081
name : request . name || '' ,
1069
1082
} ) ;
1070
1083
this . initialize ( ) ;
@@ -1224,7 +1237,7 @@ export class TranslationServiceClient {
1224
1237
options . otherArgs = options . otherArgs || { } ;
1225
1238
options . otherArgs . headers = options . otherArgs . headers || { } ;
1226
1239
options . otherArgs . headers [ 'x-goog-request-params' ] =
1227
- gax . routingHeader . fromParams ( {
1240
+ this . _gaxModule . routingHeader . fromParams ( {
1228
1241
parent : request . parent || '' ,
1229
1242
} ) ;
1230
1243
this . initialize ( ) ;
@@ -1250,11 +1263,12 @@ export class TranslationServiceClient {
1250
1263
protos . google . cloud . translation . v3 . BatchTranslateMetadata
1251
1264
>
1252
1265
> {
1253
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1254
- { name}
1255
- ) ;
1266
+ const request =
1267
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1268
+ { name}
1269
+ ) ;
1256
1270
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1257
- const decodeOperation = new gax . Operation (
1271
+ const decodeOperation = new this . _gaxModule . Operation (
1258
1272
operation ,
1259
1273
this . descriptors . longrunning . batchTranslateText ,
1260
1274
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1420,7 +1434,7 @@ export class TranslationServiceClient {
1420
1434
options . otherArgs = options . otherArgs || { } ;
1421
1435
options . otherArgs . headers = options . otherArgs . headers || { } ;
1422
1436
options . otherArgs . headers [ 'x-goog-request-params' ] =
1423
- gax . routingHeader . fromParams ( {
1437
+ this . _gaxModule . routingHeader . fromParams ( {
1424
1438
parent : request . parent || '' ,
1425
1439
} ) ;
1426
1440
this . initialize ( ) ;
@@ -1450,11 +1464,12 @@ export class TranslationServiceClient {
1450
1464
protos . google . cloud . translation . v3 . BatchTranslateDocumentMetadata
1451
1465
>
1452
1466
> {
1453
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1454
- { name}
1455
- ) ;
1467
+ const request =
1468
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1469
+ { name}
1470
+ ) ;
1456
1471
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1457
- const decodeOperation = new gax . Operation (
1472
+ const decodeOperation = new this . _gaxModule . Operation (
1458
1473
operation ,
1459
1474
this . descriptors . longrunning . batchTranslateDocument ,
1460
1475
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1564,7 +1579,7 @@ export class TranslationServiceClient {
1564
1579
options . otherArgs = options . otherArgs || { } ;
1565
1580
options . otherArgs . headers = options . otherArgs . headers || { } ;
1566
1581
options . otherArgs . headers [ 'x-goog-request-params' ] =
1567
- gax . routingHeader . fromParams ( {
1582
+ this . _gaxModule . routingHeader . fromParams ( {
1568
1583
parent : request . parent || '' ,
1569
1584
} ) ;
1570
1585
this . initialize ( ) ;
@@ -1590,11 +1605,12 @@ export class TranslationServiceClient {
1590
1605
protos . google . cloud . translation . v3 . CreateGlossaryMetadata
1591
1606
>
1592
1607
> {
1593
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1594
- { name}
1595
- ) ;
1608
+ const request =
1609
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1610
+ { name}
1611
+ ) ;
1596
1612
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1597
- const decodeOperation = new gax . Operation (
1613
+ const decodeOperation = new this . _gaxModule . Operation (
1598
1614
operation ,
1599
1615
this . descriptors . longrunning . createGlossary ,
1600
1616
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1703,7 +1719,7 @@ export class TranslationServiceClient {
1703
1719
options . otherArgs = options . otherArgs || { } ;
1704
1720
options . otherArgs . headers = options . otherArgs . headers || { } ;
1705
1721
options . otherArgs . headers [ 'x-goog-request-params' ] =
1706
- gax . routingHeader . fromParams ( {
1722
+ this . _gaxModule . routingHeader . fromParams ( {
1707
1723
name : request . name || '' ,
1708
1724
} ) ;
1709
1725
this . initialize ( ) ;
@@ -1729,11 +1745,12 @@ export class TranslationServiceClient {
1729
1745
protos . google . cloud . translation . v3 . DeleteGlossaryMetadata
1730
1746
>
1731
1747
> {
1732
- const request = new operationsProtos . google . longrunning . GetOperationRequest (
1733
- { name}
1734
- ) ;
1748
+ const request =
1749
+ new this . _gaxModule . operationsProtos . google . longrunning . GetOperationRequest (
1750
+ { name}
1751
+ ) ;
1735
1752
const [ operation ] = await this . operationsClient . getOperation ( request ) ;
1736
- const decodeOperation = new gax . Operation (
1753
+ const decodeOperation = new this . _gaxModule . Operation (
1737
1754
operation ,
1738
1755
this . descriptors . longrunning . deleteGlossary ,
1739
1756
this . _gaxModule . createDefaultBackoffSettings ( )
@@ -1857,7 +1874,7 @@ export class TranslationServiceClient {
1857
1874
options . otherArgs = options . otherArgs || { } ;
1858
1875
options . otherArgs . headers = options . otherArgs . headers || { } ;
1859
1876
options . otherArgs . headers [ 'x-goog-request-params' ] =
1860
- gax . routingHeader . fromParams ( {
1877
+ this . _gaxModule . routingHeader . fromParams ( {
1861
1878
parent : request . parent || '' ,
1862
1879
} ) ;
1863
1880
this . initialize ( ) ;
@@ -1916,7 +1933,7 @@ export class TranslationServiceClient {
1916
1933
options . otherArgs = options . otherArgs || { } ;
1917
1934
options . otherArgs . headers = options . otherArgs . headers || { } ;
1918
1935
options . otherArgs . headers [ 'x-goog-request-params' ] =
1919
- gax . routingHeader . fromParams ( {
1936
+ this . _gaxModule . routingHeader . fromParams ( {
1920
1937
parent : request . parent || '' ,
1921
1938
} ) ;
1922
1939
const defaultCallSettings = this . _defaults [ 'listGlossaries' ] ;
@@ -1984,7 +2001,7 @@ export class TranslationServiceClient {
1984
2001
options . otherArgs = options . otherArgs || { } ;
1985
2002
options . otherArgs . headers = options . otherArgs . headers || { } ;
1986
2003
options . otherArgs . headers [ 'x-goog-request-params' ] =
1987
- gax . routingHeader . fromParams ( {
2004
+ this . _gaxModule . routingHeader . fromParams ( {
1988
2005
parent : request . parent || '' ,
1989
2006
} ) ;
1990
2007
const defaultCallSettings = this . _defaults [ 'listGlossaries' ] ;
0 commit comments