refactor: Refactor email verification and payment functions with error handling and validation

- Improve email verification functionality
- Refactor checkEmailVerified to return typed object with verified and email properties
- Add error handling to checkEmailVerified for failed response and invalid data structure or values
- Improve error handling and validation in alchemyPrePay function
- Change alchemyPrePay function to open web page instead of URL
This commit is contained in:
zhl 2023-03-24 14:58:10 +08:00
parent 91c7b9b940
commit bf4875bf19
2 changed files with 32 additions and 7 deletions

View File

@ -8,16 +8,28 @@ export class EmailVerifySvr {
* Checks if the user's email is verified
* @returns an object containing the verification status and the user's email
*/
public async checkEmailVerified() {
public async checkEmailVerified(): Promise<{
verified: number;
email: string | null;
}> {
let res = await isEmailVerified();
let verified = 0;
let email = "";
if (!res.errcode) {
verified = res.data.verified;
email = res.data?.email;
if (res.errcode) {
throw new Error("Failed to fetch email verification status"); //Throw error if call to the checking function fails
}
const { data } = res;
if (!data) {
throw new Error("Invalid response from the server"); //Throw error if the expected data structure is missing
}
const { verified, email } = data;
if (!email || typeof verified !== "number") {
throw new Error("Invalid values returned from server"); //Throw error if the data doesn't have the expected types or values
}
return { verified, email };
}
/**
* Begins the process of verifying the user's email
* @param email - the email to be verified

View File

@ -10,6 +10,19 @@ export class PaySvr {
* @returns The result of the alchemyPrePay function.
*/
public async alchemyPrePay(data: IPayData) {
return alchemyPrePay(data);
let res = await alchemyPrePay(data);
if (res.errcode) {
throw new Error(res.errmsg);
}
if (!res.data) {
throw new Error("No data returned");
}
const url = res.data.url;
if (!url) {
throw new Error("No url returned");
}
jsb.showWebPage(url);
// jsb.openURL(url);
return true;
}
}