diff --git a/package.json b/package.json index b5b8b2c..fcd9561 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 58a2b5b..42b29a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,8 +15,7 @@ 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`. @@ -24,6 +23,10 @@ export interface GCEImagesConfig extends GoogleAuthOptions { apiEndpoint?: string; } +export type GCEImagesConfig = Omit & { + authClient?: GoogleAuth; +}; + export interface GetOptions { deprecated?: boolean; osNames?: string[]; @@ -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`,