From 55fc51ad0af283447b7ec69de8b97a6e9f68656c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 20 Mar 2024 15:41:22 -0400 Subject: [PATCH] refactor: update more methods to return an error optionally, and switch to a custom Error child class so we can get the status code and message --- src/account.ts | 90 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 21 deletions(-) diff --git a/src/account.ts b/src/account.ts index 912ea24..2477f73 100644 --- a/src/account.ts +++ b/src/account.ts @@ -33,6 +33,16 @@ import { } from "./account/generated/index.js"; import { AxiosError, AxiosResponse } from "axios"; +export class AccountError extends Error { + public statusCode: number; + + constructor(message: string, statusCode: number) { + super(message); + this.name = "AccountError"; + this.statusCode = statusCode; + } +} + export class AccountApi { private apiUrl: string; private _jwtToken?: string; @@ -47,6 +57,7 @@ export class AccountApi { set jwtToken(value: string) { this._jwtToken = value; } + get jwtToken(): string { return this._jwtToken; } @@ -55,12 +66,17 @@ export class AccountApi { return new AccountApi(apiUrl); } - public async login(loginRequest: LoginRequest): Promise { + public async login( + loginRequest: LoginRequest, + ): Promise { let ret: AxiosResponse | LoginResponse | boolean = false; try { ret = await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl }); } catch (e) { - return false; + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } ret = this.checkSuccessVal(ret); @@ -81,7 +97,10 @@ export class AccountApi { baseURL: this.apiUrl, }); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); @@ -97,17 +116,25 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); } - public async generateOtp(): Promise { + public async generateOtp(): Promise< + boolean | OTPGenerateResponse | AccountError + > { let ret: AxiosResponse; try { ret = await getApiAuthOtpGenerate(this.buildOptions()); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessVal(ret); } @@ -122,14 +149,17 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); } public async validateOtp( otpValidateRequest: OTPValidateRequest, - ): Promise { + ): Promise { let ret: AxiosResponse; try { ret = await postApiAccountOtpValidate( @@ -137,26 +167,32 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); } public async disableOtp( otpDisableRequest: OTPDisableRequest, - ): Promise { + ): Promise { let ret: AxiosResponse; try { ret = await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); } public async requestPasswordReset( passwordResetRequest: PasswordResetRequest, - ): Promise { + ): Promise { let ret: AxiosResponse; try { ret = await postApiAccountPasswordResetRequest( @@ -164,14 +200,17 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); } public async confirmPasswordReset( passwordResetVerifyRequest: PasswordResetVerifyRequest, - ): Promise { + ): Promise { let ret: AxiosResponse; try { ret = await postApiAccountPasswordResetConfirm( @@ -179,7 +218,10 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); } @@ -212,11 +254,11 @@ export class AccountApi { return this.checkSuccessVal(ret); } - public async logout(): Promise { + public async logout(): Promise { try { await postApiAuthLogout(this.buildOptions()); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return false; } this._jwtToken = undefined; @@ -238,7 +280,7 @@ export class AccountApi { public async updateEmail( email: string, password: string, - ): Promise { + ): Promise { let ret: AxiosResponse; try { ret = await postApiAccountUpdateEmail( @@ -246,7 +288,10 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret); @@ -255,7 +300,7 @@ export class AccountApi { public async updatePassword( currentPasswprd: string, newPassword: string, - ): Promise { + ): Promise { let ret: AxiosResponse; try { ret = await postApiAccountUpdatePassword( @@ -263,7 +308,10 @@ export class AccountApi { this.buildOptions(), ); } catch (e) { - return new Error((e as AxiosError).response?.data as string); + return new AccountError( + (e as AxiosError).response?.data as string, + (e as AxiosError).response?.status as number, + ); } return this.checkSuccessBool(ret);