Skip to content

Commit

Permalink
feat!: expose GoogleAuth constructor on AuthPlus class (#154)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: pulls in breaking API changes in google-auth-library. getProjectId() and getProjectId() have been modified to make the impact of these changes less noticeable on the legacy googleapis module (getClient() is idempotent, but getProjectId() will use the last configuration).
  • Loading branch information
bcoe committed Jul 24, 2019
1 parent d77c70e commit 7d7a961
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -73,7 +73,7 @@
"dependencies": {
"extend": "^3.0.2",
"gaxios": "^2.0.1",
"google-auth-library": "^4.2.5",
"google-auth-library": "^5.1.0",
"qs": "^6.7.0",
"url-template": "^2.0.8",
"uuid": "^3.3.2"
Expand Down
43 changes: 42 additions & 1 deletion src/authplus.ts
Expand Up @@ -11,7 +11,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Compute, GoogleAuth, JWT, OAuth2Client} from 'google-auth-library';
import {
Compute,
GoogleAuth,
GoogleAuthOptions,
JWT,
OAuth2Client,
ProjectIdCallback,
UserRefreshClient,
} from 'google-auth-library';

export class AuthPlus extends GoogleAuth {
// tslint:disable-next-line: variable-name
Expand All @@ -20,4 +28,37 @@ export class AuthPlus extends GoogleAuth {
Compute = Compute;
// tslint:disable-next-line: variable-name
OAuth2 = OAuth2Client;
// tslint:disable-next-line: variable-name
GoogleAuth = GoogleAuth;

private _cachedAuth?: GoogleAuth;

/**
* Override getClient(), memoizing an instance of auth for
* subsequent calls to getProjectId().
*/
async getClient(
options?: GoogleAuthOptions
): Promise<Compute | JWT | UserRefreshClient> {
this._cachedAuth = new GoogleAuth(options);
return this._cachedAuth.getClient();
}

/**
* Override getProjectId(), using the most recently configured
* auth instance when fetching projectId.
*/
getProjectId(): Promise<string>;
getProjectId(callback: ProjectIdCallback): void;
getProjectId(callback?: ProjectIdCallback): Promise<string | null> | void {
if (callback) {
return this._cachedAuth
? this._cachedAuth.getProjectId(callback)
: super.getProjectId(callback);
} else {
return this._cachedAuth
? this._cachedAuth.getProjectId()
: super.getProjectId();
}
}
}

0 comments on commit 7d7a961

Please sign in to comment.