UNPKG

@454creative/easy-email

Version:

A framework-agnostic email service library for Node.js with observability and monitoring

110 lines 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigValidator = void 0; const email_options_interface_1 = require("../interfaces/email-options.interface"); const errors_1 = require("../errors"); class ConfigValidator { static validateSmtpConfig(config) { if (!config.host) { throw new errors_1.ConfigurationError('SMTP host is required'); } if (!config.port) { throw new errors_1.ConfigurationError('SMTP port is required'); } if (config.auth) { if (!config.auth.user) { throw new errors_1.ConfigurationError('SMTP username is required when auth is provided'); } if (!config.auth.pass) { throw new errors_1.ConfigurationError('SMTP password is required when auth is provided'); } } } static validateSendGridConfig(config) { if (!config.apiKey) { throw new errors_1.ConfigurationError('SendGrid API key is required'); } // Check for basic API key format (SendGrid keys typically start with "SG.") if (typeof config.apiKey === 'string' && !config.apiKey.startsWith('SG.')) { throw new errors_1.ConfigurationError('SendGrid API key appears to be invalid. Valid SendGrid API keys typically start with "SG."'); } // Check for minimum length (SendGrid keys are typically 69 characters) if (typeof config.apiKey === 'string' && config.apiKey.length < 20) { throw new errors_1.ConfigurationError('SendGrid API key appears to be too short. Please verify your API key.'); } } static validateSesConfig(config) { if (!config.region) { throw new errors_1.ConfigurationError('SES region is required'); } // Validate that at least one credential method is provided const hasCredentials = config.credentials || (config.accessKeyId && config.secretAccessKey) || process.env.AWS_ACCESS_KEY_ID; if (!hasCredentials) { throw new errors_1.ConfigurationError('SES credentials are required (accessKeyId/secretAccessKey, credentials object, or AWS environment variables)'); } // Validate region format (basic check) if (!/^[a-z0-9-]+$/.test(config.region)) { throw new errors_1.ConfigurationError('Invalid SES region format'); } // Validate optional parameters if (config.maxRetries !== undefined && config.maxRetries < 0) { throw new errors_1.ConfigurationError('SES maxRetries must be a non-negative number'); } if (config.httpOptions) { if (config.httpOptions.timeout !== undefined && config.httpOptions.timeout < 0) { throw new errors_1.ConfigurationError('SES httpOptions timeout must be a non-negative number'); } if (config.httpOptions.connectTimeout !== undefined && config.httpOptions.connectTimeout < 0) { throw new errors_1.ConfigurationError('SES httpOptions connectTimeout must be a non-negative number'); } } } static validateProviderConfig(config) { if (!config.type) { throw new errors_1.ConfigurationError('Provider type is required'); } switch (config.type) { case email_options_interface_1.EmailProviderType.SMTP: this.validateSmtpConfig(config.config); break; case email_options_interface_1.EmailProviderType.SENDGRID: this.validateSendGridConfig(config.config); break; case email_options_interface_1.EmailProviderType.SES: this.validateSesConfig(config.config); break; default: throw new errors_1.ConfigurationError(`Unsupported provider type: ${config.type}`); } } static validateEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { throw new errors_1.ValidationError(`Invalid email format: ${email}`); } } static validatePort(port) { if (port < 1 || port > 65535) { throw new errors_1.ConfigurationError(`Invalid port number: ${port}`); } } static validateTimeout(timeout) { if (timeout < 0) { throw new errors_1.ConfigurationError('Timeout must be a positive number'); } } static validateRetryAttempts(attempts) { if (attempts < 0) { throw new errors_1.ConfigurationError('Retry attempts must be a positive number'); } } static validateRetryDelay(delay) { if (delay < 0) { throw new errors_1.ConfigurationError('Retry delay must be a positive number'); } } } exports.ConfigValidator = ConfigValidator; //# sourceMappingURL=validator.js.map