Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit d2648f0

Browse files
authored
Revert "feat: Improve AuthClient Compatibility (#1641)" (#1648)
* Revert "feat: Improve `AuthClient` Compatibility (#1641)" This reverts commit 4edd33d. * chore: keep `cheerio`
1 parent cf51a5d commit d2648f0

6 files changed

Lines changed: 58 additions & 29 deletions

File tree

gax/src/clientInterface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import {GrpcClientOptions, ClientStubOptions} from './grpc';
2020
import * as gax from './gax';
21-
import {AuthClient, GoogleAuthOptions} from 'google-auth-library';
21+
import {GoogleAuthOptions} from 'google-auth-library';
2222
import {
2323
BundleDescriptor,
2424
LongrunningDescriptor,
@@ -30,7 +30,7 @@ import * as operationProtos from '../protos/operations';
3030

3131
export interface ClientOptions
3232
extends GrpcClientOptions,
33-
GoogleAuthOptions<AuthClient>,
33+
GoogleAuthOptions,
3434
ClientStubOptions {
3535
libName?: string;
3636
libVersion?: string;

gax/src/fallback.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ import * as protobuf from 'protobufjs';
2020
import * as gax from './gax';
2121
import * as routingHeader from './routingHeader';
2222
import {Status} from './status';
23-
import {GoogleAuth, AuthClient, AnyAuthClient} from 'google-auth-library';
23+
import {
24+
GoogleAuth,
25+
OAuth2Client,
26+
Compute,
27+
JWT,
28+
UserRefreshClient,
29+
GoogleAuthOptions,
30+
BaseExternalAccountClient,
31+
} from 'google-auth-library';
2432
import {OperationsClientBuilder} from './operationsClient';
2533
import type {GrpcClientOptions, ClientStubOptions} from './grpc';
2634
import {GaxCall, GRPCCall} from './apitypes';
@@ -37,7 +45,6 @@ import * as IamProtos from '../protos/iam_service';
3745
import * as LocationProtos from '../protos/locations';
3846
import * as operationsProtos from '../protos/operations';
3947

40-
export {AnyAuthClient as AuthClient};
4148
export {FallbackServiceError};
4249
export {PathTemplate} from './pathTemplate';
4350
export {routingHeader};
@@ -78,8 +85,15 @@ export interface ServiceMethods {
7885
[name: string]: protobuf.Method;
7986
}
8087

88+
export type AuthClient =
89+
| OAuth2Client
90+
| Compute
91+
| JWT
92+
| UserRefreshClient
93+
| BaseExternalAccountClient;
94+
8195
export class GrpcClient {
82-
auth?: AuthClient | GoogleAuth<AuthClient>;
96+
auth?: OAuth2Client | GoogleAuth;
8397
authClient?: AuthClient;
8498
fallback: boolean;
8599
grpcVersion: string;
@@ -99,33 +113,33 @@ export class GrpcClient {
99113
* gRPC-fallback version of GrpcClient
100114
* Implements GrpcClient API for a browser using grpc-fallback protocol (sends serialized protobuf to HTTP/1 $rpc endpoint).
101115
*
102-
* @param {Object=} options.auth - An instance of AuthClient to use in browser, or an instance of GoogleAuth from google-auth-library
116+
* @param {Object=} options.auth - An instance of OAuth2Client to use in browser, or an instance of GoogleAuth from google-auth-library
103117
* to use in Node.js. Required for browser, optional for Node.js.
104118
* @constructor
105119
*/
106120

107121
constructor(
108-
options: (GrpcClientOptions | {auth: AuthClient}) & {
122+
options: (GrpcClientOptions | {auth: OAuth2Client}) & {
109123
/**
110124
* Fallback mode to use instead of gRPC.
111125
* A string is accepted for compatibility, all non-empty string values enable the HTTP REST fallback.
112126
*/
113127
fallback?: boolean | string;
114128
} = {}
115129
) {
116-
if (options.auth) {
117-
this.auth = options.auth;
118-
} else if ('authClient' in options) {
119-
this.auth = options.authClient;
120-
} else if (!isNodeJS()) {
121-
throw new Error(
122-
JSON.stringify(options) +
123-
'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Provide a `GoogleAuth` or `AuthClient` instance from `google-auth-library`.'
124-
);
130+
if (!isNodeJS()) {
131+
if (!options.auth) {
132+
throw new Error(
133+
JSON.stringify(options) +
134+
'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.'
135+
);
136+
}
137+
this.auth = options.auth as OAuth2Client;
125138
} else {
126-
this.auth = new GoogleAuth(options as GrpcClientOptions);
139+
this.auth =
140+
(options.auth as GoogleAuth) ||
141+
new GoogleAuth(options as GoogleAuthOptions);
127142
}
128-
129143
this.fallback = options.fallback ? true : false;
130144
this.grpcVersion = require('../../package.json').version;
131145
this.httpRules = (options as GrpcClientOptions).httpRules;
@@ -250,7 +264,7 @@ export class GrpcClient {
250264

251265
/**
252266
* gRPC-fallback version of createStub
253-
* Creates a gRPC-fallback stub with authentication headers built from supplied AuthClient instance
267+
* Creates a gRPC-fallback stub with authentication headers built from supplied OAuth2Client instance
254268
*
255269
* @param {function} CreateStub - The constructor function of the stub.
256270
* @param {Object} service - A protobufjs Service object (as returned by lookupService)

gax/src/grpc.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import * as grpcProtoLoader from '@grpc/proto-loader';
1818
import {execFile} from 'child_process';
1919
import * as fs from 'fs';
20-
import {GoogleAuth, GoogleAuthOptions, AuthClient} from 'google-auth-library';
20+
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
2121
import * as grpc from '@grpc/grpc-js';
2222
import * as os from 'os';
2323
import {join} from 'path';
@@ -44,8 +44,8 @@ const COMMON_PROTO_FILES: string[] = commonProtoFiles.map(file =>
4444
file.replace(/[/\\]/g, path.sep)
4545
);
4646

47-
export interface GrpcClientOptions extends GoogleAuthOptions<AuthClient> {
48-
auth?: GoogleAuth<AuthClient>;
47+
export interface GrpcClientOptions extends GoogleAuthOptions {
48+
auth?: GoogleAuth;
4949
grpc?: GrpcModule;
5050
protoJson?: protobuf.Root;
5151
httpRules?: Array<google.api.IHttpRule>;
@@ -113,7 +113,7 @@ export class ClientStub extends grpc.Client {
113113
}
114114

115115
export class GrpcClient {
116-
auth: GoogleAuth<AuthClient>;
116+
auth: GoogleAuth;
117117
grpc: GrpcModule;
118118
grpcVersion: string;
119119
fallback: boolean | 'rest' | 'proto';

gax/src/iamService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as gax from './gax';
2020
import type {GrpcClient, ClientStubOptions} from './grpc';
2121
import type {GrpcClient as FallbackGrpcClient} from './fallback';
2222
import {createApiCall} from './createApiCall';
23-
import {GoogleAuth, AuthClient} from 'google-auth-library';
23+
import {GoogleAuth, OAuth2Client} from 'google-auth-library';
2424
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
2525
import * as routingHeader from './routingHeader';
2626
import * as gapicConfig from './iam_policy_service_client_config.json';
@@ -40,7 +40,7 @@ export class IamClient {
4040
private _defaults: {[method: string]: gax.CallSettings};
4141
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4242
private _protos: any;
43-
auth?: GoogleAuth<AuthClient> | AuthClient;
43+
auth?: GoogleAuth | OAuth2Client;
4444
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
4545
innerApiCalls: {[name: string]: Function} = {};
4646
iamPolicyStub?: Promise<{[name: string]: Function}>;

gax/src/operationsClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {GoogleAuth, AuthClient} from 'google-auth-library';
17+
import type {GoogleAuth, OAuth2Client} from 'google-auth-library';
1818
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
1919
import type {ClientOptions, Callback} from './clientInterface';
2020

@@ -62,7 +62,7 @@ export const ALL_SCOPES: string[] = [];
6262
* @class
6363
*/
6464
export class OperationsClient {
65-
auth?: GoogleAuth<AuthClient> | AuthClient;
65+
auth?: GoogleAuth | OAuth2Client;
6666
innerApiCalls: {[name: string]: Function};
6767
descriptor: {[method: string]: PageDescriptor};
6868
operationsStub: Promise<{[method: string]: Function}>;

gax/test/unit/regapic.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,27 @@ import * as stream from 'stream';
2424
import echoProtoJson = require('../fixtures/echo.json');
2525
import {GrpcClient} from '../../src/fallback';
2626
import * as transcoding from '../../src/transcoding';
27-
import {PassThroughClient} from 'google-auth-library';
27+
import {OAuth2Client} from 'google-auth-library';
28+
import {GrpcClientOptions} from '../../src';
2829
import {StreamArrayParser} from '../../src/streamArrayParser';
2930

31+
const authClient = {
32+
async getRequestHeaders() {
33+
return {Authorization: 'Bearer SOME_TOKEN'};
34+
},
35+
};
36+
37+
const authStub = {
38+
async getClient() {
39+
return authClient;
40+
},
41+
};
42+
3043
const opts = {
31-
authClient: new PassThroughClient(),
44+
auth: authStub,
3245
fallback: 'rest', // enabling REGAPIC
46+
} as unknown as (GrpcClientOptions | {auth: OAuth2Client}) & {
47+
fallback?: boolean | 'rest' | 'proto';
3348
};
3449

3550
describe('REGAPIC', () => {

0 commit comments

Comments
 (0)