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: Add AnyAuthClient type #1843

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
ExternalAccountAuthorizedUserClientOptions,
} from './externalAccountAuthorizedUserClient';
import {originalOrCamelOptions} from '../util';
import {AuthClients} from '..';

/**
* Defines all types of explicit clients that are determined via ADC JSON
Expand Down Expand Up @@ -162,7 +163,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
useJWTAccessWithScope?: boolean;
defaultServicePath?: string;

// Note: this properly is only public to satisify unit tests.
// Note: this properly is only public to satisfy unit tests.
// https://github.com/Microsoft/TypeScript/issues/5228
get isGCE() {
return this.checkIsGCE;
Expand All @@ -174,7 +175,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
// To save the contents of the JSON credential file
jsonContent: JWTInput | ExternalAccountClientOptions | null = null;

cachedCredential: JSONClient | Impersonated | Compute | T | null = null;
cachedCredential: AuthClients | T | null = null;

/**
* Scopes populated by the client library by default. We differentiate between
Expand Down Expand Up @@ -457,7 +458,7 @@ export class GoogleAuth<T extends AuthClient = JSONClient> {
}

private async prepareAndCacheADC(
credential: JSONClient | Impersonated | Compute | T,
credential: AuthClients,
quotaProjectIdOverride?: string
): Promise<ADCResponse> {
const projectId = await this.getProjectIdOptional();
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {GoogleAuth} from './auth/googleauth';
export * as gcpMetadata from 'gcp-metadata';
export * as gaxios from 'gaxios';

import {AuthClient} from './auth/authclient';
export {AuthClient, DEFAULT_UNIVERSE} from './auth/authclient';
export {Compute, ComputeOptions} from './auth/computeclient';
export {
Expand Down Expand Up @@ -88,5 +89,14 @@ export {
export {PassThroughClient} from './auth/passthrough';
export {DefaultTransporter} from './transporters';

/**
* A union type for all AuthClients.
*
* @experimental
*/
export type AuthClients = InstanceType<
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too sure about the name; AuthClients could imply AuthClient[]. Would something like AnyAuthClient be clearer?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean about the naming. AnyAuthClient works for me, or UnionAuthClients? this is a tough naming situation.

My other feedback is that there is a lot going on in the InstanceType and the Extract - is it possible to split this up into a couple of lines and to comment what's going on so it's easier to read? It's hard to parse just by looking at it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated

Extract<(typeof import('./'))[keyof typeof import('./')], typeof AuthClient>
>;

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