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
This commit is contained in:
parent
b7a9484785
commit
55fc51ad0a
|
@ -33,6 +33,16 @@ import {
|
||||||
} from "./account/generated/index.js";
|
} from "./account/generated/index.js";
|
||||||
import { AxiosError, AxiosResponse } from "axios";
|
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 {
|
export class AccountApi {
|
||||||
private apiUrl: string;
|
private apiUrl: string;
|
||||||
private _jwtToken?: string;
|
private _jwtToken?: string;
|
||||||
|
@ -47,6 +57,7 @@ export class AccountApi {
|
||||||
set jwtToken(value: string) {
|
set jwtToken(value: string) {
|
||||||
this._jwtToken = value;
|
this._jwtToken = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get jwtToken(): string {
|
get jwtToken(): string {
|
||||||
return <string>this._jwtToken;
|
return <string>this._jwtToken;
|
||||||
}
|
}
|
||||||
|
@ -55,12 +66,17 @@ export class AccountApi {
|
||||||
return new AccountApi(apiUrl);
|
return new AccountApi(apiUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async login(loginRequest: LoginRequest): Promise<boolean> {
|
public async login(
|
||||||
|
loginRequest: LoginRequest,
|
||||||
|
): Promise<boolean | AccountError> {
|
||||||
let ret: AxiosResponse<LoginResponse> | LoginResponse | boolean = false;
|
let ret: AxiosResponse<LoginResponse> | LoginResponse | boolean = false;
|
||||||
try {
|
try {
|
||||||
ret = await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl });
|
ret = await postApiAuthLogin(loginRequest, { baseURL: this.apiUrl });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return new AccountError(
|
||||||
|
(e as AxiosError).response?.data as string,
|
||||||
|
(e as AxiosError).response?.status as number,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
ret = this.checkSuccessVal<LoginResponse>(ret);
|
ret = this.checkSuccessVal<LoginResponse>(ret);
|
||||||
|
|
||||||
|
@ -81,7 +97,10 @@ export class AccountApi {
|
||||||
baseURL: this.apiUrl,
|
baseURL: this.apiUrl,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
|
@ -97,17 +116,25 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generateOtp(): Promise<boolean | OTPGenerateResponse | Error> {
|
public async generateOtp(): Promise<
|
||||||
|
boolean | OTPGenerateResponse | AccountError
|
||||||
|
> {
|
||||||
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 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<OTPGenerateResponse>(ret);
|
return this.checkSuccessVal<OTPGenerateResponse>(ret);
|
||||||
}
|
}
|
||||||
|
@ -122,14 +149,17 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async validateOtp(
|
public async validateOtp(
|
||||||
otpValidateRequest: OTPValidateRequest,
|
otpValidateRequest: OTPValidateRequest,
|
||||||
): Promise<boolean | Error> {
|
): Promise<boolean | AccountError> {
|
||||||
let ret: AxiosResponse<void>;
|
let ret: AxiosResponse<void>;
|
||||||
try {
|
try {
|
||||||
ret = await postApiAccountOtpValidate(
|
ret = await postApiAccountOtpValidate(
|
||||||
|
@ -137,26 +167,32 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async disableOtp(
|
public async disableOtp(
|
||||||
otpDisableRequest: OTPDisableRequest,
|
otpDisableRequest: OTPDisableRequest,
|
||||||
): Promise<boolean | Error> {
|
): Promise<boolean | AccountError> {
|
||||||
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 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);
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async requestPasswordReset(
|
public async requestPasswordReset(
|
||||||
passwordResetRequest: PasswordResetRequest,
|
passwordResetRequest: PasswordResetRequest,
|
||||||
): Promise<boolean | Error> {
|
): Promise<boolean | AccountError> {
|
||||||
let ret: AxiosResponse<void>;
|
let ret: AxiosResponse<void>;
|
||||||
try {
|
try {
|
||||||
ret = await postApiAccountPasswordResetRequest(
|
ret = await postApiAccountPasswordResetRequest(
|
||||||
|
@ -164,14 +200,17 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async confirmPasswordReset(
|
public async confirmPasswordReset(
|
||||||
passwordResetVerifyRequest: PasswordResetVerifyRequest,
|
passwordResetVerifyRequest: PasswordResetVerifyRequest,
|
||||||
): Promise<boolean | Error> {
|
): Promise<boolean | AccountError> {
|
||||||
let ret: AxiosResponse<void>;
|
let ret: AxiosResponse<void>;
|
||||||
try {
|
try {
|
||||||
ret = await postApiAccountPasswordResetConfirm(
|
ret = await postApiAccountPasswordResetConfirm(
|
||||||
|
@ -179,7 +218,10 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
}
|
}
|
||||||
|
@ -212,11 +254,11 @@ export class AccountApi {
|
||||||
return this.checkSuccessVal(ret);
|
return this.checkSuccessVal(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async logout(): Promise<boolean | Error> {
|
public async logout(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
await postApiAuthLogout(this.buildOptions());
|
await postApiAuthLogout(this.buildOptions());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return new Error((e as AxiosError).response?.data as string);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._jwtToken = undefined;
|
this._jwtToken = undefined;
|
||||||
|
@ -238,7 +280,7 @@ export class AccountApi {
|
||||||
public async updateEmail(
|
public async updateEmail(
|
||||||
email: string,
|
email: string,
|
||||||
password: string,
|
password: string,
|
||||||
): Promise<boolean | Error> {
|
): Promise<boolean | AccountError> {
|
||||||
let ret: AxiosResponse<void>;
|
let ret: AxiosResponse<void>;
|
||||||
try {
|
try {
|
||||||
ret = await postApiAccountUpdateEmail(
|
ret = await postApiAccountUpdateEmail(
|
||||||
|
@ -246,7 +288,10 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
|
@ -255,7 +300,7 @@ export class AccountApi {
|
||||||
public async updatePassword(
|
public async updatePassword(
|
||||||
currentPasswprd: string,
|
currentPasswprd: string,
|
||||||
newPassword: string,
|
newPassword: string,
|
||||||
): Promise<boolean | Error> {
|
): Promise<boolean | AccountError> {
|
||||||
let ret: AxiosResponse<void>;
|
let ret: AxiosResponse<void>;
|
||||||
try {
|
try {
|
||||||
ret = await postApiAccountUpdatePassword(
|
ret = await postApiAccountUpdatePassword(
|
||||||
|
@ -263,7 +308,10 @@ export class AccountApi {
|
||||||
this.buildOptions(),
|
this.buildOptions(),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} 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);
|
return this.checkSuccessBool(ret);
|
||||||
|
|
Loading…
Reference in New Issue