Skip to content

Commit

Permalink
feat: Add AnyAuthClient type (#1843)
Browse files Browse the repository at this point in the history
* feat: Add `AuthClients` type

* refactor: readability

* docs: docs
  • Loading branch information
d-goog authored Aug 14, 2024
1 parent 8c82467 commit 3ae120d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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 {AnyAuthClient} 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: AnyAuthClient | 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: AnyAuthClient,
quotaProjectIdOverride?: string
): Promise<ADCResponse> {
const projectId = await this.getProjectIdOptional();
Expand Down
11 changes: 11 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,15 @@ export {
export {PassThroughClient} from './auth/passthrough';
export {DefaultTransporter} from './transporters';

type ALL_EXPORTS = (typeof import('./'))[keyof typeof import('./')];

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

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

0 comments on commit 3ae120d

Please sign in to comment.