UNPKG

unemail

Version:

A modern TypeScript email library with zero dependencies, supporting multiple providers including AWS SES, Resend, MailCrab, and HTTP APIs

109 lines (105 loc) 3.73 kB
import { EmailAddress, EmailOptions, ErrorOptions, Result } from 'unemail/types'; import { Buffer } from 'node:buffer'; import * as http from 'node:http'; import { URL } from 'node:url'; /** * Internal utilities for email operations */ /** * Validate email address format * @param email Email address to validate * @returns Boolean indicating if the email is valid */ declare function validateEmail(email: string): boolean; /** * Format email address as "Name <email@example.com>" * @param address Email address object * @returns Formatted email string */ declare function formatEmailAddress(address: EmailAddress): string; /** * Format email addresses list * @param addresses Single address or array of addresses * @returns Comma-separated string of formatted addresses */ declare function formatEmailAddresses(addresses: EmailAddress | EmailAddress[]): string; /** * Generate boundary string for multipart emails * @returns Random boundary string */ declare function generateBoundary(): string; /** * Check if a port is available * @param host Host to check * @param port Port to check * @returns Promise resolving to boolean indicating if port is available */ declare function isPortAvailable(host: string, port: number): Promise<boolean>; /** * Validate email options * @param options Email options to validate * @returns Array of validation errors (empty if valid) */ declare function validateEmailOptions<T extends EmailOptions>(options: T): string[]; /** * Build a MIME message from email options * @param options Email options * @returns MIME message as string */ declare function buildMimeMessage<T extends EmailOptions>(options: T): string; /** * Creates a formatted error message * * @param component The component where the error occurred * @param message Error message * @param opts Additional error options * @returns Error object */ declare function createError(component: string, message: string, opts?: ErrorOptions): Error; /** * Creates an error for missing required options * * @param component The component where the error occurred * @param name Name of the missing option(s) * @returns Error object */ declare function createRequiredError(component: string, name: string | string[]): Error; /** * Generates a random message ID for emails * * @returns A unique message ID */ declare function generateMessageId(): string; /** * Makes an HTTP request without external dependencies * * @param url The URL to make the request to * @param options Request options * @param data Optional data to send with the request * @returns Promise with the response data */ declare function makeRequest(url: string | URL, options?: http.RequestOptions, data?: string | Buffer): Promise<Result<any>>; /** * Encodes email content for SMTP API * * @param content The string to encode * @returns Base64 encoded string */ declare function encodeBase64(content: string | Buffer): string; /** * Helper function to wrap any value in a promise * * @param value Any value or promise * @returns Promise resolving to the value */ declare function wrapPromise<T>(value: T | Promise<T>): Promise<T>; /** * Helper function to retry a function with exponential backoff * * @param fn Function to retry * @param retries Number of retries * @param delay Initial delay in ms * @returns Promise with the function result */ declare function retry<T>(fn: () => Promise<Result<T>>, retries?: number, delay?: number): Promise<Result<T>>; export { buildMimeMessage, createError, createRequiredError, encodeBase64, formatEmailAddress, formatEmailAddresses, generateBoundary, generateMessageId, isPortAvailable, makeRequest, retry, validateEmail, validateEmailOptions, wrapPromise };