Skip to content

Commit

Permalink
fix: Rename auth to authClient & Use Generics for AuthClient (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead authored Feb 17, 2022
1 parent 6ff40e5 commit 8373000
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
6 changes: 3 additions & 3 deletions samples/downscopedclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ async function main() {
const cabClient = new DownscopedClient(client, cab);

// OAuth 2.0 Client
const oauth2Client = new OAuth2Client();
const authClient = new OAuth2Client();
// Define a refreshHandler that will be used to refresh the downscoped token
// when it expires.
oauth2Client.refreshHandler = async () => {
authClient.refreshHandler = async () => {
const refreshedAccessToken = await cabClient.getAccessToken();
return {
access_token: refreshedAccessToken.token,
Expand All @@ -77,7 +77,7 @@ async function main() {

const storageOptions = {
projectId,
authClient: new GoogleAuth({auth: oauth2Client}),
authClient: new GoogleAuth({authClient}),
};

const storage = new Storage(storageOptions);
Expand Down
13 changes: 6 additions & 7 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export interface ADCResponse {
projectId: string | null;
}

export interface GoogleAuthOptions {
export interface GoogleAuthOptions<T extends AuthClient = JSONClient> {
/**
* An `AuthClient` to use
*/
auth?: AuthClient;
authClient?: T;
/**
* Path to a .json, .pem, or .p12 key file
*/
Expand Down Expand Up @@ -115,7 +115,7 @@ export interface GoogleAuthOptions {
export const CLOUD_SDK_CLIENT_ID =
'764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com';

export class GoogleAuth {
export class GoogleAuth<T extends AuthClient = JSONClient> {
transporter?: Transporter;

/**
Expand All @@ -139,8 +139,7 @@ export class GoogleAuth {
// To save the contents of the JSON credential file
jsonContent: JWTInput | ExternalAccountClientOptions | null = null;

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

/**
* Scopes populated by the client library by default. We differentiate between
Expand All @@ -156,11 +155,11 @@ export class GoogleAuth {
*/
static DefaultTransporter = DefaultTransporter;

constructor(opts?: GoogleAuthOptions) {
constructor(opts?: GoogleAuthOptions<T>) {
opts = opts || {};

this._cachedProjectId = opts.projectId || null;
this.cachedCredential = opts.auth || null;
this.cachedCredential = opts.authClient || null;
this.keyFilename = opts.keyFilename || opts.keyFile;
this.scopes = opts.scopes;
this.jsonContent = opts.credentials || null;
Expand Down
4 changes: 1 addition & 3 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,7 @@ describe('googleauth', () => {

const authClient = new MyAuthClient();

const auth = new GoogleAuth({
auth: authClient,
});
const auth = new GoogleAuth({authClient});

assert.equal(auth.cachedCredential, authClient);
assert.equal(await auth.getClient(), authClient);
Expand Down

0 comments on commit 8373000

Please sign in to comment.