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

Commit 440de51

Browse files
d-googgcf-owl-bot[bot]sofisl
authored
feat: Normalize GoogleAuth<T> from JSONClient to AuthClient (#1940)
* feat: Normalize to `GoogleAuth<AuthClient>` See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: sofisl <55454395+sofisl@users.noreply.github.com>
1 parent c63f608 commit 440de51

3 files changed

Lines changed: 67 additions & 25 deletions

File tree

src/auth/googleauth.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,8 @@ import {CredentialBody, ImpersonatedJWTInput, JWTInput} from './credentials';
2626
import {IdTokenClient} from './idtokenclient';
2727
import {GCPEnv, getEnv} from './envDetect';
2828
import {JWT, JWTOptions} from './jwtclient';
29-
import {OAuth2ClientOptions} from './oauth2client';
30-
import {
31-
UserRefreshClient,
32-
UserRefreshClientOptions,
33-
USER_REFRESH_ACCOUNT_TYPE,
34-
} from './refreshclient';
35-
import {
36-
Impersonated,
37-
ImpersonatedOptions,
38-
IMPERSONATED_ACCOUNT_TYPE,
39-
} from './impersonated';
29+
import {UserRefreshClient, USER_REFRESH_ACCOUNT_TYPE} from './refreshclient';
30+
import {Impersonated, IMPERSONATED_ACCOUNT_TYPE} from './impersonated';
4031
import {
4132
ExternalAccountClient,
4233
ExternalAccountClientOptions,
@@ -52,7 +43,7 @@ import {
5243
ExternalAccountAuthorizedUserClientOptions,
5344
} from './externalAccountAuthorizedUserClient';
5445
import {originalOrCamelOptions} from '../util';
55-
import {AnyAuthClient} from '..';
46+
import {AnyAuthClient, AnyAuthClientConstructor} from '..';
5647

5748
/**
5849
* Defines all types of explicit clients that are determined via ADC JSON
@@ -82,7 +73,7 @@ export interface ADCResponse {
8273
projectId: string | null;
8374
}
8475

85-
export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
76+
export interface GoogleAuthOptions<T extends AuthClient = AnyAuthClient> {
8677
/**
8778
* An API key to use, optional. Cannot be used with {@link GoogleAuthOptions.credentials `credentials`}.
8879
*/
@@ -114,13 +105,12 @@ export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
114105
credentials?: JWTInput | ExternalAccountClientOptions;
115106

116107
/**
117-
* Options object passed to the constructor of the client
108+
* `AuthClientOptions` object passed to the constructor of the client
118109
*/
119-
clientOptions?:
120-
| JWTOptions
121-
| OAuth2ClientOptions
122-
| UserRefreshClientOptions
123-
| ImpersonatedOptions;
110+
clientOptions?: Extract<
111+
ConstructorParameters<AnyAuthClientConstructor>[0],
112+
AuthClientOptions
113+
>;
124114

125115
/**
126116
* Required scopes for the desired API request
@@ -160,7 +150,7 @@ export const GoogleAuthExceptionMessages = {
160150
'https://cloud.google.com/compute/docs/metadata/predefined-metadata-keys',
161151
} as const;
162152

163-
export class GoogleAuth<T extends AuthClient = JSONClient> {
153+
export class GoogleAuth<T extends AuthClient = AuthClient> {
164154
/**
165155
* Caches a value indicating whether the auth layer is running on Google
166156
* Compute Engine.

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,16 @@ export {PassThroughClient} from './auth/passthrough';
9191
type ALL_EXPORTS = (typeof import('./'))[keyof typeof import('./')];
9292

9393
/**
94-
* A union type for all {@link AuthClient `AuthClient`}s.
94+
* A union type for all {@link AuthClient `AuthClient`} constructors.
9595
*/
96-
export type AnyAuthClient = InstanceType<
96+
export type AnyAuthClientConstructor =
9797
// Extract All `AuthClient`s from exports
98-
Extract<ALL_EXPORTS, typeof AuthClient>
99-
>;
98+
Extract<ALL_EXPORTS, typeof AuthClient>;
99+
100+
/**
101+
* A union type for all {@link AuthClient `AuthClient`}s.
102+
*/
103+
export type AnyAuthClient = InstanceType<AnyAuthClientConstructor>;
100104

101105
const auth = new GoogleAuth();
102106
export {auth, GoogleAuth};

test/test.googleauth.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import {
4141
ExternalAccountClientOptions,
4242
Impersonated,
4343
IdentityPoolClient,
44+
PassThroughClient,
45+
AnyAuthClient,
4446
} from '../src';
4547
import {CredentialBody} from '../src/auth/credentials';
4648
import * as envDetect from '../src/auth/envDetect';
@@ -62,7 +64,7 @@ import {stringify} from 'querystring';
6264
import {GoogleAuthExceptionMessages} from '../src/auth/googleauth';
6365
import {IMPERSONATED_ACCOUNT_TYPE} from '../src/auth/impersonated';
6466
import {USER_REFRESH_ACCOUNT_TYPE} from '../src/auth/refreshclient';
65-
import {Gaxios, GaxiosError} from 'gaxios';
67+
import {Gaxios, GaxiosError, GaxiosPromise, GaxiosResponse} from 'gaxios';
6668

6769
nock.disableNetConnect();
6870

@@ -279,6 +281,52 @@ describe('googleauth', () => {
279281
return sandbox.stub(process, 'env').value(envVars);
280282
}
281283

284+
describe('types', () => {
285+
it('should be type-compatible with itself by default', () => {
286+
class CustomAuthClient extends AuthClient {
287+
request<T>(): GaxiosPromise<T> {
288+
throw new Error('Method not implemented.');
289+
}
290+
getRequestHeaders(): Promise<Headers> {
291+
throw new Error('Method not implemented.');
292+
}
293+
getAccessToken(): Promise<{
294+
token?: string | null;
295+
res?: GaxiosResponse | null;
296+
}> {
297+
throw new Error('Method not implemented.');
298+
}
299+
}
300+
301+
// default > default
302+
const defaultToDefault: GoogleAuth = new GoogleAuth();
303+
304+
// Explicit > default
305+
const explicitToDefault: GoogleAuth =
306+
new GoogleAuth<PassThroughClient>();
307+
308+
// custom > default
309+
const customToDefault: GoogleAuth = new GoogleAuth<CustomAuthClient>();
310+
311+
// AnyAuthClient > default
312+
const anyToDefault: GoogleAuth = new GoogleAuth<AnyAuthClient>();
313+
314+
// default > AnyAuthClient
315+
const defaultToAny: GoogleAuth<AnyAuthClient> = new GoogleAuth();
316+
317+
// AuthClient > AnyAuthClient
318+
const baseClientToAny: GoogleAuth<AnyAuthClient> =
319+
new GoogleAuth<AuthClient>();
320+
321+
assert(defaultToDefault);
322+
assert(explicitToDefault);
323+
assert(customToDefault);
324+
assert(anyToDefault);
325+
assert(defaultToAny);
326+
assert(baseClientToAny);
327+
});
328+
});
329+
282330
it('should accept and use an `AuthClient`', async () => {
283331
const customRequestHeaders = new Headers({
284332
'my-unique': 'header',

0 commit comments

Comments
 (0)