@@ -20,15 +20,7 @@ import * as protobuf from 'protobufjs';
20
20
import * as gax from './gax' ;
21
21
import * as routingHeader from './routingHeader' ;
22
22
import { Status } from './status' ;
23
- import {
24
- GoogleAuth ,
25
- OAuth2Client ,
26
- Compute ,
27
- JWT ,
28
- UserRefreshClient ,
29
- GoogleAuthOptions ,
30
- BaseExternalAccountClient ,
31
- } from 'google-auth-library' ;
23
+ import { GoogleAuth , AuthClient } from 'google-auth-library' ;
32
24
import { OperationsClientBuilder } from './operationsClient' ;
33
25
import type { GrpcClientOptions , ClientStubOptions } from './grpc' ;
34
26
import { GaxCall , GRPCCall } from './apitypes' ;
@@ -85,15 +77,16 @@ export interface ServiceMethods {
85
77
[ name : string ] : protobuf . Method ;
86
78
}
87
79
88
- export type AuthClient =
89
- | OAuth2Client
90
- | Compute
91
- | JWT
92
- | UserRefreshClient
93
- | BaseExternalAccountClient ;
80
+ /**
81
+ * @deprecated use `GoogleAuth` here instead
82
+ */
83
+ type deprecatedAuthClientAlias = AuthClient ;
94
84
95
85
export class GrpcClient {
96
- auth ?: OAuth2Client | GoogleAuth ;
86
+ auth ?: GoogleAuth < AuthClient > | deprecatedAuthClientAlias ;
87
+ /**
88
+ * @deprecated use {@link GrpcClient.auth} instead
89
+ */
97
90
authClient ?: AuthClient ;
98
91
fallback : boolean ;
99
92
grpcVersion : string ;
@@ -114,33 +107,36 @@ export class GrpcClient {
114
107
* gRPC-fallback version of GrpcClient
115
108
* Implements GrpcClient API for a browser using grpc-fallback protocol (sends serialized protobuf to HTTP/1 $rpc endpoint).
116
109
*
117
- * @param {Object= } options.auth - An instance of OAuth2Client to use in browser, or an instance of GoogleAuth from google-auth-library
118
- * to use in Node.js. Required for browser, optional for Node.js.
119
- * @constructor
110
+ * @param options {@link GrpcClientOptions }
120
111
*/
121
-
122
112
constructor (
123
- options : ( GrpcClientOptions | { auth : OAuth2Client } ) & {
113
+ options : (
114
+ | GrpcClientOptions
115
+ | {
116
+ /**
117
+ * @deprecated - use `authClient` for `AuthClient`s instead
118
+ */
119
+ auth : AuthClient ;
120
+ }
121
+ ) & {
124
122
/**
125
123
* Fallback mode to use instead of gRPC.
126
124
* A string is accepted for compatibility, all non-empty string values enable the HTTP REST fallback.
127
125
*/
128
126
fallback ?: boolean | string ;
129
127
} = { } ,
130
128
) {
131
- if ( ! isNodeJS ( ) ) {
132
- if ( ! options . auth ) {
133
- throw new Error (
134
- JSON . stringify ( options ) +
135
- 'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Use OAuth2Client from google-auth-library.' ,
136
- ) ;
137
- }
138
- this . auth = options . auth as OAuth2Client ;
129
+ if ( options . auth ) {
130
+ this . auth = options . auth ;
131
+ } else if ( 'authClient' in options ) {
132
+ this . auth = options . authClient ;
139
133
} else {
140
- this . auth =
141
- ( options . auth as GoogleAuth ) ||
142
- new GoogleAuth ( options as GoogleAuthOptions ) ;
134
+ this . auth = new GoogleAuth ( {
135
+ authClient : options . auth ,
136
+ ...options ,
137
+ } ) ;
143
138
}
139
+
144
140
this . fallback = options . fallback ? true : false ;
145
141
this . grpcVersion = require ( '../../package.json' ) . version ;
146
142
this . httpRules = ( options as GrpcClientOptions ) . httpRules ;
@@ -266,7 +262,7 @@ export class GrpcClient {
266
262
267
263
/**
268
264
* gRPC-fallback version of createStub
269
- * Creates a gRPC-fallback stub with authentication headers built from supplied OAuth2Client instance
265
+ * Creates a gRPC-fallback stub with authentication headers built from supplied `AuthClient` instance
270
266
*
271
267
* @param {function } CreateStub - The constructor function of the stub.
272
268
* @param {Object } service - A protobufjs Service object (as returned by lookupService)
@@ -284,7 +280,7 @@ export class GrpcClient {
284
280
) {
285
281
if ( ! this . authClient ) {
286
282
if ( this . auth && 'getClient' in this . auth ) {
287
- this . authClient = ( await this . auth . getClient ( ) ) as AuthClient ;
283
+ this . authClient = await this . auth . getClient ( ) ;
288
284
} else if ( this . auth && 'getRequestHeaders' in this . auth ) {
289
285
this . authClient = this . auth ;
290
286
}
@@ -342,7 +338,7 @@ export class GrpcClient {
342
338
protocol ,
343
339
servicePath ,
344
340
servicePort ,
345
- this . authClient ,
341
+ this . auth || this . authClient ,
346
342
encoder ,
347
343
decoder ,
348
344
this . numericEnums ,
@@ -425,6 +421,7 @@ export function createApiCall(
425
421
} ;
426
422
}
427
423
if ( descriptor && 'streaming' in descriptor && ! isNodeJS ( ) ) {
424
+ // TODO: with `fetch` this functionality is available in the browser...
428
425
return ( ) => {
429
426
throw new Error (
430
427
'Server streaming over the REST transport is only supported in Node.js.' ,
0 commit comments