Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Normalize GoogleAuth<T> from JSONClient to AuthClient #1940

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,8 @@ import {CredentialBody, ImpersonatedJWTInput, JWTInput} from './credentials';
import {IdTokenClient} from './idtokenclient';
import {GCPEnv, getEnv} from './envDetect';
import {JWT, JWTOptions} from './jwtclient';
import {OAuth2ClientOptions} from './oauth2client';
import {
UserRefreshClient,
UserRefreshClientOptions,
USER_REFRESH_ACCOUNT_TYPE,
} from './refreshclient';
import {
Impersonated,
ImpersonatedOptions,
IMPERSONATED_ACCOUNT_TYPE,
} from './impersonated';
import {UserRefreshClient, USER_REFRESH_ACCOUNT_TYPE} from './refreshclient';
import {Impersonated, IMPERSONATED_ACCOUNT_TYPE} from './impersonated';
import {
ExternalAccountClient,
ExternalAccountClientOptions,
Expand All @@ -52,7 +43,7 @@ import {
ExternalAccountAuthorizedUserClientOptions,
} from './externalAccountAuthorizedUserClient';
import {originalOrCamelOptions} from '../util';
import {AnyAuthClient} from '..';
import {AnyAuthClient, AnyAuthClientConstructor} from '..';

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

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

/**
* Options object passed to the constructor of the client
* `AuthClientOptions` object passed to the constructor of the client
*/
clientOptions?:
| JWTOptions
| OAuth2ClientOptions
| UserRefreshClientOptions
| ImpersonatedOptions;
clientOptions?: Extract<
ConstructorParameters<AnyAuthClientConstructor>[0],
AuthClientOptions
>;

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

export class GoogleAuth<T extends AuthClient = JSONClient> {
export class GoogleAuth<T extends AuthClient = AuthClient> {
/**
* Caches a value indicating whether the auth layer is running on Google
* Compute Engine.
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ export {PassThroughClient} from './auth/passthrough';
type ALL_EXPORTS = (typeof import('./'))[keyof typeof import('./')];

/**
* A union type for all {@link AuthClient `AuthClient`}s.
* A union type for all {@link AuthClient `AuthClient`} constructors.
*/
export type AnyAuthClient = InstanceType<
export type AnyAuthClientConstructor =
// Extract All `AuthClient`s from exports
Extract<ALL_EXPORTS, typeof AuthClient>
>;
Extract<ALL_EXPORTS, typeof AuthClient>;

/**
* A union type for all {@link AuthClient `AuthClient`}s.
*/
export type AnyAuthClient = InstanceType<AnyAuthClientConstructor>;

const auth = new GoogleAuth();
export {auth, GoogleAuth};
50 changes: 49 additions & 1 deletion test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
ExternalAccountClientOptions,
Impersonated,
IdentityPoolClient,
PassThroughClient,
AnyAuthClient,
} from '../src';
import {CredentialBody} from '../src/auth/credentials';
import * as envDetect from '../src/auth/envDetect';
Expand All @@ -62,7 +64,7 @@ import {stringify} from 'querystring';
import {GoogleAuthExceptionMessages} from '../src/auth/googleauth';
import {IMPERSONATED_ACCOUNT_TYPE} from '../src/auth/impersonated';
import {USER_REFRESH_ACCOUNT_TYPE} from '../src/auth/refreshclient';
import {Gaxios, GaxiosError} from 'gaxios';
import {Gaxios, GaxiosError, GaxiosPromise, GaxiosResponse} from 'gaxios';

nock.disableNetConnect();

Expand Down Expand Up @@ -283,6 +285,52 @@ describe('googleauth', () => {
return sandbox.stub(process, 'env').value(envVars);
}

describe('types', () => {
it('should be type-compatible with itself by default', () => {
class CustomAuthClient extends AuthClient {
request<T>(): GaxiosPromise<T> {
throw new Error('Method not implemented.');
}
getRequestHeaders(): Promise<Headers> {
throw new Error('Method not implemented.');
}
getAccessToken(): Promise<{
token?: string | null;
res?: GaxiosResponse | null;
}> {
throw new Error('Method not implemented.');
}
}

// default > default
const defaultToDefault: GoogleAuth = new GoogleAuth();

// Explicit > default
const explicitToDefault: GoogleAuth =
new GoogleAuth<PassThroughClient>();

// custom > default
const customToDefault: GoogleAuth = new GoogleAuth<CustomAuthClient>();

// AnyAuthClient > default
const anyToDefault: GoogleAuth = new GoogleAuth<AnyAuthClient>();

// default > AnyAuthClient
const defaultToAny: GoogleAuth<AnyAuthClient> = new GoogleAuth();

// AuthClient > AnyAuthClient
const baseClientToAny: GoogleAuth<AnyAuthClient> =
new GoogleAuth<AuthClient>();

assert(defaultToDefault);
assert(explicitToDefault);
assert(customToDefault);
assert(anyToDefault);
assert(defaultToAny);
assert(baseClientToAny);
});
});

it('should accept and use an `AuthClient`', async () => {
const customRequestHeaders = new Headers({
'my-unique': 'header',
Expand Down