From 0800802b99d179acb4111081b3ddf56831d68dbc Mon Sep 17 00:00:00 2001 From: Brian Graves Date: Thu, 25 Sep 2025 12:01:35 -0700 Subject: [PATCH] Narrow the return type of oauthFetchAccessToken (#694) * Added ActionToken class to narrow the return type of `OAuthActionV2.oauthFetchAccessToken()` --- lib/actions/google/drive/google_drive.d.ts | 5 +---- lib/actions/google/drive/google_drive.js | 2 +- lib/hub/action_token.d.ts | 5 +++++ lib/hub/action_token.js | 10 ++++++++++ lib/hub/index.d.ts | 1 + lib/hub/index.js | 1 + lib/hub/oauth_action_v2.d.ts | 3 ++- src/actions/google/drive/google_drive.ts | 2 +- src/hub/action_token.ts | 3 +++ src/hub/index.ts | 1 + src/hub/oauth_action_v2.ts | 4 +++- 11 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 lib/hub/action_token.d.ts create mode 100644 lib/hub/action_token.js create mode 100644 src/hub/action_token.ts diff --git a/lib/actions/google/drive/google_drive.d.ts b/lib/actions/google/drive/google_drive.d.ts index 1c516ec6..268013f7 100644 --- a/lib/actions/google/drive/google_drive.d.ts +++ b/lib/actions/google/drive/google_drive.d.ts @@ -26,10 +26,7 @@ export declare class GoogleDriveAction extends Hub.OAuthActionV2 { oauthHandleRedirect(urlParams: { [key: string]: string; }, redirectUri: string): Promise; - oauthFetchAccessToken(request: Hub.ActionRequest): Promise<{ - tokens: Credentials; - redirect: any; - }>; + oauthFetchAccessToken(request: Hub.ActionRequest): Promise; oauthCheck(request: Hub.ActionRequest): Promise; oauth2Client(redirectUri: string | undefined): OAuth2Client; sendData(filename: string, request: Hub.ActionRequest, drive: Drive): Promise>; diff --git a/lib/actions/google/drive/google_drive.js b/lib/actions/google/drive/google_drive.js index b682f84c..3c470d25 100644 --- a/lib/actions/google/drive/google_drive.js +++ b/lib/actions/google/drive/google_drive.js @@ -260,7 +260,7 @@ class GoogleDriveAction extends Hub.OAuthActionV2 { }); const state = JSON.parse(plaintext); const tokens = await this.getAccessTokenCredentialsFromCode(state.redirecturi, state.code); - return { tokens, redirect: state.redirecturi }; + return new Hub.ActionToken(tokens, state.redirecturi); } else { throw new Error("Request is missing state parameter."); diff --git a/lib/hub/action_token.d.ts b/lib/hub/action_token.d.ts new file mode 100644 index 00000000..39594996 --- /dev/null +++ b/lib/hub/action_token.d.ts @@ -0,0 +1,5 @@ +export declare class ActionToken { + tokens: any; + redirect: any; + constructor(tokens: any, redirect: any); +} diff --git a/lib/hub/action_token.js b/lib/hub/action_token.js new file mode 100644 index 00000000..e7bafda6 --- /dev/null +++ b/lib/hub/action_token.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.ActionToken = void 0; +class ActionToken { + constructor(tokens, redirect) { + this.tokens = tokens; + this.redirect = redirect; + } +} +exports.ActionToken = ActionToken; diff --git a/lib/hub/index.d.ts b/lib/hub/index.d.ts index e1c23ed3..2d491ed2 100644 --- a/lib/hub/index.d.ts +++ b/lib/hub/index.d.ts @@ -6,6 +6,7 @@ export * from "./action"; export * from "./oauth_action"; export * from "./oauth_action_v2"; export * from "./delegate_oauth_action"; +export * from "./action_token"; export * from "./sources"; export * from "./utils"; import { LookmlModelExploreField as FieldBase } from "../api_types/lookml_model_explore_field"; diff --git a/lib/hub/index.js b/lib/hub/index.js index ff8fa157..5226ffd8 100644 --- a/lib/hub/index.js +++ b/lib/hub/index.js @@ -24,6 +24,7 @@ __exportStar(require("./action"), exports); __exportStar(require("./oauth_action"), exports); __exportStar(require("./oauth_action_v2"), exports); __exportStar(require("./delegate_oauth_action"), exports); +__exportStar(require("./action_token"), exports); __exportStar(require("./sources"), exports); __exportStar(require("./utils"), exports); const aes_transit_crypto_1 = require("../crypto/aes_transit_crypto"); diff --git a/lib/hub/oauth_action_v2.d.ts b/lib/hub/oauth_action_v2.d.ts index 1b75023b..104b612b 100644 --- a/lib/hub/oauth_action_v2.d.ts +++ b/lib/hub/oauth_action_v2.d.ts @@ -1,12 +1,13 @@ import { Action, RouteBuilder } from "./action"; import { ActionRequest } from "./action_request"; +import { ActionToken } from "./action_token"; export declare abstract class OAuthActionV2 extends Action { abstract oauthCheck(request: ActionRequest): Promise; abstract oauthUrl(redirectUri: string, encryptedState: string): Promise; abstract oauthHandleRedirect(urlParams: { [key: string]: string; }, redirectUri: string): Promise; - abstract oauthFetchAccessToken(request: ActionRequest): Promise; + abstract oauthFetchAccessToken(request: ActionRequest): Promise; asJson(router: RouteBuilder, request: ActionRequest): any; } export declare function isOauthActionV2(action: Action): boolean; diff --git a/src/actions/google/drive/google_drive.ts b/src/actions/google/drive/google_drive.ts index 75e7e0af..f94f814f 100644 --- a/src/actions/google/drive/google_drive.ts +++ b/src/actions/google/drive/google_drive.ts @@ -302,7 +302,7 @@ export class GoogleDriveAction extends Hub.OAuthActionV2 { const state = JSON.parse(plaintext) const tokens = await this.getAccessTokenCredentialsFromCode(state.redirecturi, state.code) - return {tokens, redirect: state.redirecturi} + return new Hub.ActionToken(tokens, state.redirecturi) } else { throw new Error("Request is missing state parameter.") } diff --git a/src/hub/action_token.ts b/src/hub/action_token.ts new file mode 100644 index 00000000..3feac524 --- /dev/null +++ b/src/hub/action_token.ts @@ -0,0 +1,3 @@ +export class ActionToken { + constructor(public tokens: any, public redirect: any) {} +} diff --git a/src/hub/index.ts b/src/hub/index.ts index 023478d2..d917ea9f 100644 --- a/src/hub/index.ts +++ b/src/hub/index.ts @@ -6,6 +6,7 @@ export * from "./action" export * from "./oauth_action" export * from "./oauth_action_v2" export * from "./delegate_oauth_action" +export * from "./action_token" export * from "./sources" export * from "./utils" diff --git a/src/hub/oauth_action_v2.ts b/src/hub/oauth_action_v2.ts index 1d91058d..d194aaca 100644 --- a/src/hub/oauth_action_v2.ts +++ b/src/hub/oauth_action_v2.ts @@ -1,11 +1,13 @@ import {Action, RouteBuilder} from "./action" import {ActionRequest} from "./action_request" +import {ActionToken} from "./action_token" + export abstract class OAuthActionV2 extends Action { abstract oauthCheck(request: ActionRequest): Promise abstract oauthUrl(redirectUri: string, encryptedState: string): Promise abstract oauthHandleRedirect(urlParams: { [key: string]: string }, redirectUri: string): Promise - abstract oauthFetchAccessToken(request: ActionRequest): Promise + abstract oauthFetchAccessToken(request: ActionRequest): Promise asJson(router: RouteBuilder, request: ActionRequest): any { const json = super.asJson(router, request)