refactor: update some of the methods to return an error in the try catch/vs a false

This commit is contained in:
Derrick Hammer 2024-03-20 12:58:31 -04:00
parent 7a3248b21b
commit b7a9484785
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 22 additions and 18 deletions

View File

@ -72,14 +72,16 @@ export class AccountApi {
return false; return false;
} }
public async register(registerRequest: RegisterRequest): Promise<boolean> { public async register(
registerRequest: RegisterRequest,
): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAuthRegister(registerRequest, { ret = await postApiAuthRegister(registerRequest, {
baseURL: this.apiUrl, baseURL: this.apiUrl,
}); });
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
@ -87,7 +89,7 @@ export class AccountApi {
public async verifyEmail( public async verifyEmail(
verifyEmailRequest: VerifyEmailRequest, verifyEmailRequest: VerifyEmailRequest,
): Promise<boolean> { ): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAccountVerifyEmail( ret = await postApiAccountVerifyEmail(
@ -95,22 +97,24 @@ export class AccountApi {
this.buildOptions(), this.buildOptions(),
); );
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
} }
public async generateOtp(): Promise<boolean | OTPGenerateResponse> { public async generateOtp(): Promise<boolean | OTPGenerateResponse | Error> {
let ret: AxiosResponse<OTPGenerateResponse>; let ret: AxiosResponse<OTPGenerateResponse>;
try { try {
ret = await getApiAuthOtpGenerate(this.buildOptions()); ret = await getApiAuthOtpGenerate(this.buildOptions());
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessVal<OTPGenerateResponse>(ret); return this.checkSuccessVal<OTPGenerateResponse>(ret);
} }
public async verifyOtp(otpVerifyRequest: OTPVerifyRequest): Promise<boolean> { public async verifyOtp(
otpVerifyRequest: OTPVerifyRequest,
): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAccountOtpVerify( ret = await postApiAccountOtpVerify(
@ -118,14 +122,14 @@ export class AccountApi {
this.buildOptions(), this.buildOptions(),
); );
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
} }
public async validateOtp( public async validateOtp(
otpValidateRequest: OTPValidateRequest, otpValidateRequest: OTPValidateRequest,
): Promise<boolean> { ): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAccountOtpValidate( ret = await postApiAccountOtpValidate(
@ -133,26 +137,26 @@ export class AccountApi {
this.buildOptions(), this.buildOptions(),
); );
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
} }
public async disableOtp( public async disableOtp(
otpDisableRequest: OTPDisableRequest, otpDisableRequest: OTPDisableRequest,
): Promise<boolean> { ): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()); ret = await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions());
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
} }
public async requestPasswordReset( public async requestPasswordReset(
passwordResetRequest: PasswordResetRequest, passwordResetRequest: PasswordResetRequest,
): Promise<boolean> { ): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAccountPasswordResetRequest( ret = await postApiAccountPasswordResetRequest(
@ -160,14 +164,14 @@ export class AccountApi {
this.buildOptions(), this.buildOptions(),
); );
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
} }
public async confirmPasswordReset( public async confirmPasswordReset(
passwordResetVerifyRequest: PasswordResetVerifyRequest, passwordResetVerifyRequest: PasswordResetVerifyRequest,
): Promise<boolean> { ): Promise<boolean | Error> {
let ret: AxiosResponse<void>; let ret: AxiosResponse<void>;
try { try {
ret = await postApiAccountPasswordResetConfirm( ret = await postApiAccountPasswordResetConfirm(
@ -175,7 +179,7 @@ export class AccountApi {
this.buildOptions(), this.buildOptions(),
); );
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
return this.checkSuccessBool(ret); return this.checkSuccessBool(ret);
} }
@ -208,11 +212,11 @@ export class AccountApi {
return this.checkSuccessVal(ret); return this.checkSuccessVal(ret);
} }
public async logout(): Promise<boolean> { public async logout(): Promise<boolean | Error> {
try { try {
await postApiAuthLogout(this.buildOptions()); await postApiAuthLogout(this.buildOptions());
} catch (e) { } catch (e) {
return false; return new Error((e as AxiosError).response?.data as string);
} }
this._jwtToken = undefined; this._jwtToken = undefined;