Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
fix(typescript): address bug with GoogleAuth types (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Apr 13, 2022
1 parent 27032ae commit c78f532
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"license": "MIT",
"dependencies": {
"arrify": "^2.0.0",
"google-auth-library": "^7.0.0"
"google-auth-library": "^7.14.1"
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.10",
Expand Down
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
import arrify = require('arrify');
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';

export interface GCEImagesConfig extends GoogleAuthOptions {
authClient?: GoogleAuth;
interface AuthOptions extends GoogleAuthOptions {
/**
* The host name used to access the compute API.
* Defaults to `compute.googleapis.com`.
*/
apiEndpoint?: string;
}

export type GCEImagesConfig = Omit<AuthOptions, 'authClient'> & {
authClient?: GoogleAuth;
};

export interface GetOptions {
deprecated?: boolean;
osNames?: string[];
Expand Down Expand Up @@ -94,15 +97,23 @@ export interface OSUrls {
windows: string;
}

function isGoogleAuthOptions(
config: GoogleAuthOptions | GCEImagesConfig
): config is GoogleAuthOptions {
return !config.authClient;
}

export class GCEImages {
private _auth: GoogleAuth;
private apiEndpoint: string;
OS_URLS: OSUrls;
OS_TO_URL: {[index: string]: string};
constructor(config: GCEImagesConfig = {}) {
constructor(config: GCEImagesConfig | AuthOptions = {}) {
this.apiEndpoint = config.apiEndpoint || 'compute.googleapis.com';
config.scopes = ['https://www.googleapis.com/auth/compute'];
this._auth = config.authClient || new GoogleAuth(config);
this._auth = isGoogleAuthOptions(config)
? new GoogleAuth(config)
: config.authClient!;
const projectsPath = `https://${this.apiEndpoint}/compute/v1/projects`;
this.OS_URLS = {
centos: `${projectsPath}/centos-cloud/global/images`,
Expand Down

0 comments on commit c78f532

Please sign in to comment.