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 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
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};
Loading