15
15
import { EventEmitter } from 'events' ;
16
16
import { Gaxios , GaxiosOptions , GaxiosPromise , GaxiosResponse } from 'gaxios' ;
17
17
18
- import { DefaultTransporter , Transporter } from '../transporters' ;
19
18
import { Credentials } from './credentials' ;
20
19
import { OriginalAndCamel , originalOrCamelOptions } from '../util' ;
21
20
21
+ import { PRODUCT_NAME , USER_AGENT } from '../shared.cjs' ;
22
+
22
23
/**
23
24
* Base auth configurations (e.g. from JWT or `.json` files) with conventional
24
25
* camelCased options.
@@ -81,13 +82,17 @@ export interface AuthClientOptions
81
82
credentials ?: Credentials ;
82
83
83
84
/**
84
- * A `Gaxios` or `Transporter` instance to use for `AuthClient` requests.
85
+ * The {@link Gaxios `Gaxios`} instance used for making requests.
86
+ *
87
+ * @see {@link AuthClientOptions.useAuthRequestParameters }
85
88
*/
86
- transporter ?: Gaxios | Transporter ;
89
+ transporter ?: Gaxios ;
87
90
88
91
/**
89
92
* Provides default options to the transporter, such as {@link GaxiosOptions.agent `agent`} or
90
93
* {@link GaxiosOptions.retryConfig `retryConfig`}.
94
+ *
95
+ * This option is ignored if {@link AuthClientOptions.transporter `gaxios`} has been provided
91
96
*/
92
97
transporterOptions ?: GaxiosOptions ;
93
98
@@ -103,6 +108,19 @@ export interface AuthClientOptions
103
108
* on the expiry_date.
104
109
*/
105
110
forceRefreshOnFailure ?: boolean ;
111
+
112
+ /**
113
+ * Enables/disables the adding of the AuthClient's default interceptor.
114
+ *
115
+ * @see {@link AuthClientOptions.transporter }
116
+ *
117
+ * @remarks
118
+ *
119
+ * Disabling is useful for debugging and experimentation.
120
+ *
121
+ * @default true
122
+ */
123
+ useAuthRequestParameters ?: boolean ;
106
124
}
107
125
108
126
/**
@@ -183,7 +201,10 @@ export abstract class AuthClient
183
201
* See {@link https://cloud.google.com/docs/quota Working with quotas}
184
202
*/
185
203
quotaProjectId ?: string ;
186
- transporter : Transporter ;
204
+ /**
205
+ * The {@link Gaxios `Gaxios`} instance used for making requests.
206
+ */
207
+ transporter : Gaxios ;
187
208
credentials : Credentials = { } ;
188
209
eagerRefreshThresholdMillis = DEFAULT_EAGER_REFRESH_THRESHOLD_MILLIS ;
189
210
forceRefreshOnFailure = false ;
@@ -202,10 +223,12 @@ export abstract class AuthClient
202
223
this . universeDomain = options . get ( 'universe_domain' ) ?? DEFAULT_UNIVERSE ;
203
224
204
225
// Shared client options
205
- this . transporter = opts . transporter ?? new DefaultTransporter ( ) ;
226
+ this . transporter = opts . transporter ?? new Gaxios ( opts . transporterOptions ) ;
206
227
207
- if ( opts . transporterOptions ) {
208
- this . transporter . defaults = opts . transporterOptions ;
228
+ if ( options . get ( 'useAuthRequestParameters' ) !== false ) {
229
+ this . transporter . interceptors . request . add (
230
+ AuthClient . DEFAULT_REQUEST_INTERCEPTOR
231
+ ) ;
209
232
}
210
233
211
234
if ( opts . eagerRefreshThresholdMillis ) {
@@ -216,29 +239,11 @@ export abstract class AuthClient
216
239
}
217
240
218
241
/**
219
- * Return the { @link Gaxios `Gaxios`} instance from the { @link AuthClient.transporter} .
242
+ * The public request API in which credentials may be added to the request .
220
243
*
221
- * @expiremental
222
- */
223
- get gaxios ( ) : Gaxios | null {
224
- if ( this . transporter instanceof Gaxios ) {
225
- return this . transporter ;
226
- } else if ( this . transporter instanceof DefaultTransporter ) {
227
- return this . transporter . instance ;
228
- } else if (
229
- 'instance' in this . transporter &&
230
- this . transporter . instance instanceof Gaxios
231
- ) {
232
- return this . transporter . instance ;
233
- }
234
-
235
- return null ;
236
- }
237
-
238
- /**
239
- * Provides an alternative Gaxios request implementation with auth credentials
244
+ * @param options options for `gaxios`
240
245
*/
241
- abstract request < T > ( opts : GaxiosOptions ) : GaxiosPromise < T > ;
246
+ abstract request < T > ( options : GaxiosOptions ) : GaxiosPromise < T > ;
242
247
243
248
/**
244
249
* The main authentication interface. It takes an optional url which when
@@ -288,6 +293,31 @@ export abstract class AuthClient
288
293
return headers ;
289
294
}
290
295
296
+ static readonly DEFAULT_REQUEST_INTERCEPTOR : Parameters <
297
+ Gaxios [ 'interceptors' ] [ 'request' ] [ 'add' ]
298
+ > [ 0 ] = {
299
+ resolved : async config => {
300
+ const headers = config . headers || { } ;
301
+
302
+ // Set `x-goog-api-client`, if not already set
303
+ if ( ! headers [ 'x-goog-api-client' ] ) {
304
+ const nodeVersion = process . version . replace ( / ^ v / , '' ) ;
305
+ headers [ 'x-goog-api-client' ] = `gl-node/${ nodeVersion } ` ;
306
+ }
307
+
308
+ // Set `User-Agent`
309
+ if ( ! headers [ 'User-Agent' ] ) {
310
+ headers [ 'User-Agent' ] = USER_AGENT ;
311
+ } else if ( ! headers [ 'User-Agent' ] . includes ( `${ PRODUCT_NAME } /` ) ) {
312
+ headers [ 'User-Agent' ] = `${ headers [ 'User-Agent' ] } ${ USER_AGENT } ` ;
313
+ }
314
+
315
+ config . headers = headers ;
316
+
317
+ return config ;
318
+ } ,
319
+ } ;
320
+
291
321
/**
292
322
* Retry config for Auth-related requests.
293
323
*
@@ -315,3 +345,10 @@ export interface GetAccessTokenResponse {
315
345
token ?: string | null ;
316
346
res ?: GaxiosResponse | null ;
317
347
}
348
+
349
+ /**
350
+ * @deprecated - use the Promise API instead
351
+ */
352
+ export interface BodyResponseCallback < T > {
353
+ ( err : Error | null , res ?: GaxiosResponse < T > | null ) : void ;
354
+ }
0 commit comments