refactor: use try/catch on all apis to prevent axios errors
This commit is contained in:
parent
9a9283cc8a
commit
2d0c55848b
106
src/account.ts
106
src/account.ts
|
@ -40,14 +40,19 @@ export default class AccountApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async login(loginRequest: LoginRequest): Promise<boolean> {
|
public async login(loginRequest: LoginRequest): Promise<boolean> {
|
||||||
let ret = this.checkSuccessVal<LoginResponse>(
|
let ret: AxiosResponse<LoginResponse> | LoginResponse | boolean = false;
|
||||||
await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl }),
|
try {
|
||||||
);
|
ret = await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl });
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ret = this.checkSuccessVal<LoginResponse>(ret);
|
||||||
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
this.jwtToken = (ret as LoginResponse).token;
|
this.jwtToken = (ret as LoginResponse).token;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return ret as boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async logout(): Promise<boolean> {
|
public async logout(): Promise<boolean> {
|
||||||
|
@ -56,67 +61,108 @@ export default class AccountApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async register(registerRequest: RegisterRequest): Promise<boolean> {
|
public async register(registerRequest: RegisterRequest): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthRegister(registerRequest, this.buildOptions()),
|
try {
|
||||||
);
|
ret = await postApiAuthRegister(registerRequest, {
|
||||||
|
baseURL: this.apiUrl,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async verifyEmail(
|
public async verifyEmail(
|
||||||
verifyEmailRequest: VerifyEmailRequest,
|
verifyEmailRequest: VerifyEmailRequest,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthVerifyEmail(verifyEmailRequest, this.buildOptions()),
|
try {
|
||||||
);
|
ret = await postApiAuthVerifyEmail(
|
||||||
|
verifyEmailRequest,
|
||||||
|
this.buildOptions(),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generateOtp(): Promise<boolean | OTPGenerateResponse> {
|
public async generateOtp(): Promise<boolean | OTPGenerateResponse> {
|
||||||
return this.checkSuccessVal<OTPGenerateResponse>(
|
let ret: AxiosResponse<OTPGenerateResponse>;
|
||||||
await getApiAuthOtpGenerate(this.buildOptions()),
|
try {
|
||||||
);
|
ret = await getApiAuthOtpGenerate(this.buildOptions());
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessVal<OTPGenerateResponse>(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async verifyOtp(otpVerifyRequest: OTPVerifyRequest): Promise<boolean> {
|
public async verifyOtp(otpVerifyRequest: OTPVerifyRequest): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthOtpVerify(otpVerifyRequest, this.buildOptions()),
|
try {
|
||||||
);
|
ret = await postApiAuthOtpVerify(otpVerifyRequest, this.buildOptions());
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async validateOtp(
|
public async validateOtp(
|
||||||
otpValidateRequest: OTPValidateRequest,
|
otpValidateRequest: OTPValidateRequest,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthOtpValidate(otpValidateRequest, this.buildOptions()),
|
try {
|
||||||
);
|
ret = await postApiAuthOtpValidate(
|
||||||
|
otpValidateRequest,
|
||||||
|
this.buildOptions(),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async disableOtp(
|
public async disableOtp(
|
||||||
otpDisableRequest: OTPDisableRequest,
|
otpDisableRequest: OTPDisableRequest,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions()),
|
try {
|
||||||
);
|
ret = await postApiAuthOtpDisable(otpDisableRequest, this.buildOptions());
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async requestPasswordReset(
|
public async requestPasswordReset(
|
||||||
passwordResetRequest: PasswordResetRequest,
|
passwordResetRequest: PasswordResetRequest,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthPasswordResetRequest(
|
try {
|
||||||
|
ret = await postApiAuthPasswordResetRequest(
|
||||||
passwordResetRequest,
|
passwordResetRequest,
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
),
|
);
|
||||||
);
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async confirmPasswordReset(
|
public async confirmPasswordReset(
|
||||||
passwordResetVerifyRequest: PasswordResetVerifyRequest,
|
passwordResetVerifyRequest: PasswordResetVerifyRequest,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return this.checkSuccessBool(
|
let ret: AxiosResponse<void>;
|
||||||
await postApiAuthPasswordResetConfirm(
|
try {
|
||||||
|
ret = await postApiAuthPasswordResetConfirm(
|
||||||
passwordResetVerifyRequest,
|
passwordResetVerifyRequest,
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
),
|
);
|
||||||
);
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ping(): Promise<boolean> {
|
public async ping(): Promise<boolean> {
|
||||||
|
|
Loading…
Reference in New Issue