email 0.0.1
Install from the command line:
Learn more about npm packages
$ npm install @edgefirst-dev/email@0.0.1
Install via package.json:
"@edgefirst-dev/email": "0.0.1"
About this version
The Email class offers methods to validate, parse, and manipulate email addresses. It supports operations like retrieving the username, domain, and alias, and email validation using an external API via the EmailVerifier class.
bun add @edgefirst-dev/emailImport the Email class and create an instance using a valid email string or an Email object.
import { Email } from "@edgefirst-dev/email";
let email = Email.from("[email protected]");
console.log(email.toString()); // "[email protected]"You can retrieve and modify the username, domain (hostname), and alias from an email object.
let email = Email.from("[email protected]");
console.log(email.username); // "user"
console.log(email.hostname); // "example.com"
console.log(email.alias); // "alias"
email.username = "newuser";
email.hostname = "newdomain.com";
email.alias = "newalias";
console.log(email.toString()); // "[email protected]"[!TIP] If you want to disallow users from using aliases in their email addresses, you can set the
aliasproperty toundefined.let email = Email.from("[email protected]) email.alias = undefined; console.log(email.toString()); // "[email protected]"This can be useful to prevent users from creating multiple accounts using the same email address.
The EmailVerifier class validates an email through an external API.
import { Email } from "@edgefirst-dev/email";
let email = Email.from("[email protected]");
try {
await email.verify();
console.log("Email is valid");
} catch (error) {
console.error("Invalid email:", error);
}You can override this by extending the Email class and implementing your own verification logic.
class MyEmail extends Email {
override verifier = {
async verify(email: Email): Promise<void> {
// Custom verification logic that throws if it's invalid
},
};
}
let myEmail = MyEmail.from("[email protected]");
await myEmail.verify(); // This will call your own verify functionYou can retrieve the SHA-256 hash of an email address.
console.log(email.hash); // SHA-256 hash of the emailThis can be used along with Gravatar to get the user's avatar.
let email = Email.from("[email protected]");
let avatar = `https://www.gravatar.com/avatar/${email.hash}`;The Email class provides static methods for validation and parsing:
Email.canParse("[email protected]"); // true
Email.canParse("invalid-email"); // falseThis is similar to URL.canParse in the URL class.
You can test the EmailVerifier using msw to mock API responses.
import { http } from "msw";
import { setupServer } from "msw/node";
import { Email } from "@edgefirst-dev/email";
let server = setupServer(
http.get(
"https://verifier.meetchopra.com/verify/[email protected]",
(req, res, ctx) => {
return res(ctx.json({ status: true }));
}
)
);
server.listen();
let email = Email.from("[email protected]");
await email.verify(); // Mocked valid responseSee LICENSE