UNPKG

@upyo/core

Version:

Simple email sending library for Node.js, Deno, Bun, and edge functions

91 lines 3.06 kB
//#region src/address.d.ts /** * A type alias for email address strings that must contain an @ symbol. * @since 0.2.0 */ type EmailAddress = `${string}@${string}`; /** * A pair of name (which is optional) and email address. */ interface Address { /** * The name of the person or entity associated with the email address. */ readonly name?: string; /** * The email address itself. */ readonly address: EmailAddress; } /** * Formats an address object into a string representation. This function is * an inverse of the {@link parseAddress} function. * * @example Formatting an address with a name * ```ts * import { type Address, formatAddress } from "@upyo/core/address"; * const address: Address = { name: "John Doe", address: "john@example.com" }; * console.log(formatAddress(address)); // "John Doe <john@example.com>" * ``` * * @example Formatting an address without a name * ```ts * import { type Address, formatAddress } from "@upyo/core/address"; * const address: Address = { address: "jane@examle.com" }; * console.log(formatAddress(address)); // "jane@example.com" * ``` * * @param address The address object to format. * @return A string representation of the address. */ declare function formatAddress(address: Address): string; /** * Parses a string representation of an email address into an {@link Address} * object. This function is an inverse of the {@link formatAddress} function. * * @example Parsing an address with a name * ```ts * import { parseAddress } from "@upyo/core/address"; * const address = parseAddress("John Doe <john@example.com>"); * console.log(address); // { name: "John Doe", address: "john@example.com" } * ``` * * @example Parsing an address without a name * ```ts * import { parseAddress } from "@upyo/core/address"; * const address = parseAddress("jane@example.com"); * console.log(address); // { address: "jane@example.com" } * ``` * * @example Trying to parse an invalid address * ```ts * import { parseAddress } from "@upyo/core/address"; * const address = parseAddress("invalid-email"); * console.log(address); // undefined * ``` * * @param address The string representation of the address to parse. * @returns An {@link Address} object if the parsing is successful, * or `undefined` if the input is invalid. */ declare function parseAddress(address: string): Address | undefined; /** * Type guard function that checks if a given value is a valid email address. * * @example * ```ts * import { isEmailAddress } from "@upyo/core/address"; * * const userInput = "user@example.com"; * if (isEmailAddress(userInput)) { * // TypeScript now knows userInput is EmailAddress type * console.log(userInput); // Type: `${string}@${string}` * } * ``` * * @param email The value to check * @returns `true` if the value is a valid email address, `false` otherwise */ declare function isEmailAddress(email: unknown): email is EmailAddress; //#endregion export { Address, EmailAddress, formatAddress, isEmailAddress, parseAddress };