From 2d0c55848b872d9f978efdce3af28d12d73f8276 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 13 Mar 2024 17:13:19 -0400 Subject: [PATCH] refactor: use try/catch on all apis to prevent axios errors --- src/account.ts | 106 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 30 deletions(-) diff --git a/src/account.ts b/src/account.ts index 695d30e..a1255bb 100644 --- a/src/account.ts +++ b/src/account.ts @@ -40,14 +40,19 @@ export default class AccountApi { } public async login(loginRequest: LoginRequest): Promise { - let ret = this.checkSuccessVal( - await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl }), - ); + let ret: AxiosResponse | LoginResponse | boolean = false; + try { + ret = await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl }); + } catch (e) { + return false; + } + ret = this.checkSuccessVal(ret); + if (ret) { this.jwtToken = (ret as LoginResponse).token; } - return false; + return ret as boolean; } public async logout(): Promise { @@ -56,67 +61,108 @@ export default class AccountApi { } public async register(registerRequest: RegisterRequest): Promise { - return this.checkSuccessBool( - await postApiAuthRegister(registerRequest, this.buildOptions()), - ); + let ret: AxiosResponse; + try { + ret = await postApiAuthRegister(registerRequest, { + baseURL: this.apiUrl, + }); + } catch (e) { + return false; + } + + return this.checkSuccessBool(ret); } public async verifyEmail( verifyEmailRequest: VerifyEmailRequest, ): Promise { - return this.checkSuccessBool( - await postApiAuthVerifyEmail(verifyEmailRequest, this.buildOptions()), - ); + let ret: AxiosResponse; + try { + ret = await postApiAuthVerifyEmail( + verifyEmailRequest, + this.buildOptions(), + ); + } catch (e) { + return false; + } + return this.checkSuccessBool(ret); } public async generateOtp(): Promise { - return this.checkSuccessVal( - await getApiAuthOtpGenerate(this.buildOptions()), - ); + let ret: AxiosResponse; + try { + ret = await getApiAuthOtpGenerate(this.buildOptions()); + } catch (e) { + return false; + } + return this.checkSuccessVal(ret); } public async verifyOtp(otpVerifyRequest: OTPVerifyRequest): Promise { - return this.checkSuccessBool( - await postApiAuthOtpVerify(otpVerifyRequest, this.buildOptions()), - ); + let ret: AxiosResponse; + try { + ret = await postApiAuthOtpVerify(otpVerifyRequest, this.buildOptions()); + } catch (e) { + return false; + } + return this.checkSuccessBool(ret); } public async validateOtp( otpValidateRequest: OTPValidateRequest, ): Promise { - return this.checkSuccessBool( - await postApiAuthOtpValidate(otpValidateRequest, this.buildOptions()), - ); + let ret: AxiosResponse; + try { + ret = await postApiAuthOtpValidate( + otpValidateRequest, + this.buildOptions(), + ); + } catch (e) { + return false; + } + return this.checkSuccessBool(ret); } public async disableOtp( otpDisableRequest: OTPDisableRequest, ): Promise { - return this.checkSuccessBool( - await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()), - ); + let ret: AxiosResponse; + try { + ret = await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()); + } catch (e) { + return false; + } + return this.checkSuccessBool(ret); } public async requestPasswordReset( passwordResetRequest: PasswordResetRequest, ): Promise { - return this.checkSuccessBool( - await postApiAuthPasswordResetRequest( + let ret: AxiosResponse; + try { + ret = await postApiAuthPasswordResetRequest( passwordResetRequest, this.buildOptions(), - ), - ); + ); + } catch (e) { + return false; + } + return this.checkSuccessBool(ret); } public async confirmPasswordReset( passwordResetVerifyRequest: PasswordResetVerifyRequest, ): Promise { - return this.checkSuccessBool( - await postApiAuthPasswordResetConfirm( + let ret: AxiosResponse; + try { + ret = await postApiAuthPasswordResetConfirm( passwordResetVerifyRequest, this.buildOptions(), - ), - ); + ); + } catch (e) { + return false; + } + return this.checkSuccessBool(ret); } public async ping(): Promise {