Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions packages/scripts/dist/freshaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,28 @@ export class FreshAddress {
return true;
}
callProxy() {
var _a;
var _a, _b;
if (!this.options || !this.shouldRun)
return;
window.FreshAddressStatus = "validating";
ENGrid.disableSubmit("Validating Email Address...");
// Before calling the API, do a basic check to see if the email is in a valid format.
// This is to prevent unnecessary API calls.
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(this.emailField.value)) {
this.logger.log("Invalid Email Format from Basic Check");
this.writeToFields("Invalid", "Invalid Email Format");
ENGrid.setError(this.emailWrapper, "This email address is not valid.");
(_a = this.emailField) === null || _a === void 0 ? void 0 : _a.focus();
ENGrid.enableSubmit();
return;
}
fetch(this.options.proxyUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email: (_a = this.emailField) === null || _a === void 0 ? void 0 : _a.value }),
body: JSON.stringify({ email: (_b = this.emailField) === null || _b === void 0 ? void 0 : _b.value }),
signal: AbortSignal.timeout(5000),
})
.then((response) => {
Expand All @@ -234,14 +245,20 @@ export class FreshAddress {
this.validateProxyResponse(data);
})
.catch((error) => {
// 422 (Unprocessable Content) - This means the email is in an invalid format.
if (error.message.includes("422")) {
this.logger.log("Invalid Email Format");
this.writeToFields("Invalid", "Invalid Email Format");
ENGrid.setError(this.emailWrapper, "This email address is not valid.");
return;
}
if (error.name === "AbortError") {
this.logger.log("Proxy API request timed out");
this.writeToFields("Request Timeout", "The request took too long.");
return;
}
else {
this.logger.log("Proxy API Error", error);
this.writeToFields("Service Error", error.toString());
}
this.logger.log("Proxy API Error", error);
this.writeToFields("Service Error", error.toString());
})
.finally(() => {
window.FreshAddressStatus = "idle";
Expand Down Expand Up @@ -270,6 +287,7 @@ export class FreshAddress {
ENGrid.setError(this.emailWrapper, "This email address is not valid.");
(_a = this.emailField) === null || _a === void 0 ? void 0 : _a.focus();
if (res.email_corrections && res.email_corrections.length > 0) {
this.emailField.value = res.email_corrections[0];
ENGrid.setError(this.emailWrapper, `This email address is not valid. Did you mean ${res.email_corrections[0]}?`);
}
}
Expand Down
31 changes: 28 additions & 3 deletions packages/scripts/src/freshaddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ export class FreshAddress {
window.FreshAddressStatus = "validating";
ENGrid.disableSubmit("Validating Email Address...");

// Before calling the API, do a basic check to see if the email is in a valid format.
// This is to prevent unnecessary API calls.
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(this.emailField!.value)) {
this.logger.log("Invalid Email Format from Basic Check");
this.writeToFields("Invalid", "Invalid Email Format");
ENGrid.setError(this.emailWrapper, "This email address is not valid.");
this.emailField?.focus();
ENGrid.enableSubmit();
return;
}

fetch(this.options!.proxyUrl!, {
method: "POST",
headers: {
Expand All @@ -267,13 +279,25 @@ export class FreshAddress {
this.validateProxyResponse(data);
})
.catch((error) => {
// 422 (Unprocessable Content) - This means the email is in an invalid format.
if (error.message.includes("422")) {
this.logger.log("Invalid Email Format");
this.writeToFields("Invalid", "Invalid Email Format");
ENGrid.setError(
this.emailWrapper,
"This email address is not valid."
);
return;
}

if (error.name === "AbortError") {
this.logger.log("Proxy API request timed out");
this.writeToFields("Request Timeout", "The request took too long.");
} else {
this.logger.log("Proxy API Error", error);
this.writeToFields("Service Error", error.toString());
return;
}

this.logger.log("Proxy API Error", error);
this.writeToFields("Service Error", error.toString());
})
.finally(() => {
window.FreshAddressStatus = "idle";
Expand Down Expand Up @@ -305,6 +329,7 @@ export class FreshAddress {
ENGrid.setError(this.emailWrapper, "This email address is not valid.");
this.emailField?.focus();
if (res.email_corrections && res.email_corrections.length > 0) {
this.emailField!.value = res.email_corrections[0];
ENGrid.setError(
this.emailWrapper,
`This email address is not valid. Did you mean ${res.email_corrections[0]}?`
Expand Down
Loading