From 19a57dbf6e1156183ea789573d95d87919c9b446 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Wed, 8 Sep 2021 05:49:24 +0500 Subject: [PATCH] chore: code refactor renamed variables/functions --- src/SASjs.ts | 4 ++-- src/auth/AuthManager.ts | 16 ++++++++-------- src/auth/openWebPage.ts | 7 +++++-- ...fyingPopUpLoginSAS9.ts => verifySas9Login.ts} | 2 +- ...opUpLoginSASVIYA.ts => verifySasViyaLogin.ts} | 6 ++++-- src/types/Login.ts | 2 +- 6 files changed, 21 insertions(+), 16 deletions(-) rename src/auth/{verifyingPopUpLoginSAS9.ts => verifySas9Login.ts} (87%) rename src/auth/{verifyingPopUpLoginSASVIYA.ts => verifySasViyaLogin.ts} (83%) diff --git a/src/SASjs.ts b/src/SASjs.ts index 9323fa5..a631014 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -25,7 +25,7 @@ import { Sas9JobExecutor } from './job-execution' import { ErrorResponse } from './types/errors' -import { LoginOptions, LoginReturn } from './types/Login' +import { LoginOptions, LoginResult } from './types/Login' const defaultConfig: SASjsConfig = { serverUrl: '', @@ -538,7 +538,7 @@ export default class SASjs { username?: string, password?: string, options: LoginOptions = {} - ): Promise { + ): Promise { if (this.sasjsConfig.loginMechanism === LoginMechanism.Default) { if (!username || !password) { throw new Error( diff --git a/src/auth/AuthManager.ts b/src/auth/AuthManager.ts index 441a455..7ab854d 100644 --- a/src/auth/AuthManager.ts +++ b/src/auth/AuthManager.ts @@ -1,10 +1,10 @@ import { ServerType } from '@sasjs/utils/types' import { RequestClient } from '../request/RequestClient' -import { LoginOptions, LoginReturn } from '../types/Login' +import { LoginOptions, LoginResult } from '../types/Login' import { serialize } from '../utils' import { openWebPage } from './openWebPage' -import { verifyingPopUpLoginSAS9 } from './verifyingPopUpLoginSAS9' -import { verifyingPopUpLoginSASVIYA } from './verifyingPopUpLoginSASVIYA' +import { verifySas9Login } from './verifySas9Login' +import { verifySasViyaLogin } from './verifySasViyaLogin' export class AuthManager { public userName = '' @@ -30,7 +30,7 @@ export class AuthManager { */ public async redirectedLogIn({ onLoggedOut - }: LoginOptions): Promise { + }: LoginOptions): Promise { const loginPopup = await openWebPage( this.loginPreventRedirectUrl, 'SASLogon', @@ -47,8 +47,8 @@ export class AuthManager { const { isLoggedIn } = this.serverType === ServerType.SasViya - ? await verifyingPopUpLoginSASVIYA(loginPopup) - : await verifyingPopUpLoginSAS9(loginPopup) + ? await verifySasViyaLogin(loginPopup) + : await verifySas9Login(loginPopup) loginPopup.close() @@ -78,7 +78,7 @@ export class AuthManager { * @param password - a string representing the password. * @returns - a boolean `isLoggedin` and a string `username` */ - public async logIn(username: string, password: string): Promise { + public async logIn(username: string, password: string): Promise { const loginParams = { _service: 'default', username, @@ -209,7 +209,7 @@ export class AuthManager { //For VIYA we will send request on API endpoint. Which is faster then pinging SASJobExecution. //For SAS9 we will send request on SASStoredProcess const url = - this.serverType === 'SASVIYA' + this.serverType === ServerType.SasViya ? `${this.serverUrl}/identities/users/@currentUser` : `${this.serverUrl}/SASStoredProcess` diff --git a/src/auth/openWebPage.ts b/src/auth/openWebPage.ts index d8cd0b0..d3b69f2 100644 --- a/src/auth/openWebPage.ts +++ b/src/auth/openWebPage.ts @@ -1,16 +1,19 @@ import { openLoginPrompt } from '../utils/loginPrompt' -interface windowFeatures { +interface WindowFeatures { width: number height: number } +const defaultWindowFeatures: WindowFeatures = { width: 500, height: 600 } + export async function openWebPage( url: string, windowName: string = '', - { width, height }: windowFeatures, + WindowFeatures: WindowFeatures = defaultWindowFeatures, onLoggedOut?: () => Promise ): Promise { + const { width, height } = WindowFeatures const left = screen.width / 2 - width / 2 const top = screen.height / 2 - height / 2 diff --git a/src/auth/verifyingPopUpLoginSAS9.ts b/src/auth/verifySas9Login.ts similarity index 87% rename from src/auth/verifyingPopUpLoginSAS9.ts rename to src/auth/verifySas9Login.ts index 8eae30d..2907d67 100644 --- a/src/auth/verifyingPopUpLoginSAS9.ts +++ b/src/auth/verifySas9Login.ts @@ -1,6 +1,6 @@ import { delay } from '../utils' -export async function verifyingPopUpLoginSAS9(loginPopup: Window) { +export async function verifySas9Login(loginPopup: Window) { let isLoggedIn = false let startTime = new Date() let elapsedSeconds = 0 diff --git a/src/auth/verifyingPopUpLoginSASVIYA.ts b/src/auth/verifySasViyaLogin.ts similarity index 83% rename from src/auth/verifyingPopUpLoginSASVIYA.ts rename to src/auth/verifySasViyaLogin.ts index bff5ad4..1c9105e 100644 --- a/src/auth/verifyingPopUpLoginSASVIYA.ts +++ b/src/auth/verifySasViyaLogin.ts @@ -1,6 +1,6 @@ import { delay } from '../utils' -export async function verifyingPopUpLoginSASVIYA(loginPopup: Window) { +export async function verifySasViyaLogin(loginPopup: Window) { let isLoggedIn = false let startTime = new Date() let elapsedSeconds = 0 @@ -18,7 +18,9 @@ export async function verifyingPopUpLoginSASVIYA(loginPopup: Window) { if (loginPopup.closed) break isAuthorized = !loginPopup.window.location.href.includes('SASLogon') || - loginPopup.window.document.body.innerText.includes('You have signed in.') + loginPopup.window.document.body?.innerText?.includes( + 'You have signed in.' + ) elapsedSeconds = (new Date().valueOf() - startTime.valueOf()) / 1000 } while (!isAuthorized && elapsedSeconds < 5 * 60) diff --git a/src/types/Login.ts b/src/types/Login.ts index 60d21cb..6239456 100644 --- a/src/types/Login.ts +++ b/src/types/Login.ts @@ -2,7 +2,7 @@ export interface LoginOptions { onLoggedOut?: () => Promise } -export interface LoginReturn { +export interface LoginResult { isLoggedIn: boolean userName: string }