Skip to content

Commit 4edd33d

Browse files
authored
feat: Improve AuthClient Compatibility (#1641)
* feat: Improve `AuthClient` Compatibility * chore: fix export * refactor: Use `AnyAuthClient` for backwards compatibility * refactor: Improve `AuthClient` compat * chore: Pin `cheerio` * refactor: further improvements * fix: type compile
1 parent d74fa40 commit 4edd33d

File tree

7 files changed

+30
-58
lines changed

7 files changed

+30
-58
lines changed

gax/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@types/sinon": "^17.0.0",
3838
"@types/uglify-js": "^3.17.0",
3939
"c8": "^9.0.0",
40+
"cheerio": "1.0.0-rc.12",
4041
"codecov": "^3.1.0",
4142
"execa": "^5.0.0",
4243
"glob": "10.4.5",

gax/src/clientInterface.ts

+2-2
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 {GoogleAuthOptions} from 'google-auth-library';
21+
import {AuthClient, 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,
33+
GoogleAuthOptions<AuthClient>,
3434
ClientStubOptions {
3535
libName?: string;
3636
libVersion?: string;

gax/src/fallback.ts

+17-31
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ import * as protobuf from 'protobufjs';
2020
import * as gax from './gax';
2121
import * as routingHeader from './routingHeader';
2222
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, AnyAuthClient} from 'google-auth-library';
3224
import {OperationsClientBuilder} from './operationsClient';
3325
import type {GrpcClientOptions, ClientStubOptions} from './grpc';
3426
import {GaxCall, GRPCCall} from './apitypes';
@@ -45,6 +37,7 @@ import * as IamProtos from '../protos/iam_service';
4537
import * as LocationProtos from '../protos/locations';
4638
import * as operationsProtos from '../protos/operations';
4739

40+
export {AnyAuthClient as AuthClient};
4841
export {FallbackServiceError};
4942
export {PathTemplate} from './pathTemplate';
5043
export {routingHeader};
@@ -85,15 +78,8 @@ export interface ServiceMethods {
8578
[name: string]: protobuf.Method;
8679
}
8780

88-
export type AuthClient =
89-
| OAuth2Client
90-
| Compute
91-
| JWT
92-
| UserRefreshClient
93-
| BaseExternalAccountClient;
94-
9581
export class GrpcClient {
96-
auth?: OAuth2Client | GoogleAuth;
82+
auth?: AuthClient | GoogleAuth<AuthClient>;
9783
authClient?: AuthClient;
9884
fallback: boolean;
9985
grpcVersion: string;
@@ -113,33 +99,33 @@ export class GrpcClient {
11399
* gRPC-fallback version of GrpcClient
114100
* Implements GrpcClient API for a browser using grpc-fallback protocol (sends serialized protobuf to HTTP/1 $rpc endpoint).
115101
*
116-
* @param {Object=} options.auth - An instance of OAuth2Client to use in browser, or an instance of GoogleAuth from google-auth-library
102+
* @param {Object=} options.auth - An instance of AuthClient to use in browser, or an instance of GoogleAuth from google-auth-library
117103
* to use in Node.js. Required for browser, optional for Node.js.
118104
* @constructor
119105
*/
120106

121107
constructor(
122-
options: (GrpcClientOptions | {auth: OAuth2Client}) & {
108+
options: (GrpcClientOptions | {auth: AuthClient}) & {
123109
/**
124110
* Fallback mode to use instead of gRPC.
125111
* A string is accepted for compatibility, all non-empty string values enable the HTTP REST fallback.
126112
*/
127113
fallback?: boolean | string;
128114
} = {}
129115
) {
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;
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+
);
138125
} else {
139-
this.auth =
140-
(options.auth as GoogleAuth) ||
141-
new GoogleAuth(options as GoogleAuthOptions);
126+
this.auth = new GoogleAuth(options as GrpcClientOptions);
142127
}
128+
143129
this.fallback = options.fallback ? true : false;
144130
this.grpcVersion = require('../../package.json').version;
145131
this.httpRules = (options as GrpcClientOptions).httpRules;
@@ -264,7 +250,7 @@ export class GrpcClient {
264250

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

gax/src/grpc.ts

+4-4
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} from 'google-auth-library';
20+
import {GoogleAuth, GoogleAuthOptions, AuthClient} 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 {
48-
auth?: GoogleAuth;
47+
export interface GrpcClientOptions extends GoogleAuthOptions<AuthClient> {
48+
auth?: GoogleAuth<AuthClient>;
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;
116+
auth: GoogleAuth<AuthClient>;
117117
grpc: GrpcModule;
118118
grpcVersion: string;
119119
fallback: boolean | 'rest' | 'proto';

gax/src/iamService.ts

+2-2
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, OAuth2Client} from 'google-auth-library';
23+
import {GoogleAuth, AuthClient} 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 | OAuth2Client;
43+
auth?: GoogleAuth<AuthClient> | AuthClient;
4444
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
4545
innerApiCalls: {[name: string]: Function} = {};
4646
iamPolicyStub?: Promise<{[name: string]: Function}>;

gax/src/operationsClient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type {GoogleAuth, OAuth2Client} from 'google-auth-library';
17+
import {GoogleAuth, AuthClient} 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 | OAuth2Client;
65+
auth?: GoogleAuth<AuthClient> | AuthClient;
6666
innerApiCalls: {[name: string]: Function};
6767
descriptor: {[method: string]: PageDescriptor};
6868
operationsStub: Promise<{[method: string]: Function}>;

gax/test/unit/regapic.ts

+2-17
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,12 @@ 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 {OAuth2Client} from 'google-auth-library';
28-
import {GrpcClientOptions} from '../../src';
27+
import {PassThroughClient} from 'google-auth-library';
2928
import {StreamArrayParser} from '../../src/streamArrayParser';
3029

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-
4330
const opts = {
44-
auth: authStub,
31+
authClient: new PassThroughClient(),
4532
fallback: 'rest', // enabling REGAPIC
46-
} as unknown as (GrpcClientOptions | {auth: OAuth2Client}) & {
47-
fallback?: boolean | 'rest' | 'proto';
4833
};
4934

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

0 commit comments

Comments
 (0)