94 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * SMTP Client Factory
 | |
|  * Factory function for client creation and dependency injection
 | |
|  */
 | |
| 
 | |
| import { SmtpClient } from './smtp-client.ts';
 | |
| import { ConnectionManager } from './connection-manager.ts';
 | |
| import { CommandHandler } from './command-handler.ts';
 | |
| import { AuthHandler } from './auth-handler.ts';
 | |
| import { TlsHandler } from './tls-handler.ts';
 | |
| import { SmtpErrorHandler } from './error-handler.ts';
 | |
| import type { ISmtpClientOptions } from './interfaces.ts';
 | |
| import { validateClientOptions } from './utils/validation.ts';
 | |
| import { DEFAULTS } from './constants.ts';
 | |
| 
 | |
| /**
 | |
|  * Create a complete SMTP client with all components
 | |
|  */
 | |
| export function createSmtpClient(options: ISmtpClientOptions): SmtpClient {
 | |
|   // Validate options
 | |
|   const errors = validateClientOptions(options);
 | |
|   if (errors.length > 0) {
 | |
|     throw new Error(`Invalid client options: ${errors.join(', ')}`);
 | |
|   }
 | |
|   
 | |
|   // Apply defaults
 | |
|   const clientOptions: ISmtpClientOptions = {
 | |
|     connectionTimeout: DEFAULTS.CONNECTION_TIMEOUT,
 | |
|     socketTimeout: DEFAULTS.SOCKET_TIMEOUT,
 | |
|     maxConnections: DEFAULTS.MAX_CONNECTIONS,
 | |
|     maxMessages: DEFAULTS.MAX_MESSAGES,
 | |
|     pool: false,
 | |
|     secure: false,
 | |
|     debug: false,
 | |
|     ...options
 | |
|   };
 | |
|   
 | |
|   // Create handlers
 | |
|   const errorHandler = new SmtpErrorHandler(clientOptions);
 | |
|   const connectionManager = new ConnectionManager(clientOptions);
 | |
|   const commandHandler = new CommandHandler(clientOptions);
 | |
|   const authHandler = new AuthHandler(clientOptions, commandHandler);
 | |
|   const tlsHandler = new TlsHandler(clientOptions, commandHandler);
 | |
|   
 | |
|   // Create and return SMTP client
 | |
|   return new SmtpClient({
 | |
|     options: clientOptions,
 | |
|     connectionManager,
 | |
|     commandHandler,
 | |
|     authHandler,
 | |
|     tlsHandler,
 | |
|     errorHandler
 | |
|   });
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Create SMTP client with connection pooling enabled
 | |
|  */
 | |
| export function createPooledSmtpClient(options: ISmtpClientOptions): SmtpClient {
 | |
|   return createSmtpClient({
 | |
|     ...options,
 | |
|     pool: true,
 | |
|     maxConnections: options.maxConnections || DEFAULTS.MAX_CONNECTIONS,
 | |
|     maxMessages: options.maxMessages || DEFAULTS.MAX_MESSAGES
 | |
|   });
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Create SMTP client for high-volume sending
 | |
|  */
 | |
| export function createBulkSmtpClient(options: ISmtpClientOptions): SmtpClient {
 | |
|   return createSmtpClient({
 | |
|     ...options,
 | |
|     pool: true,
 | |
|     maxConnections: Math.max(options.maxConnections || 10, 10),
 | |
|     maxMessages: Math.max(options.maxMessages || 1000, 1000),
 | |
|     connectionTimeout: options.connectionTimeout || 30000,
 | |
|     socketTimeout: options.socketTimeout || 120000
 | |
|   });
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Create SMTP client for transactional emails
 | |
|  */
 | |
| export function createTransactionalSmtpClient(options: ISmtpClientOptions): SmtpClient {
 | |
|   return createSmtpClient({
 | |
|     ...options,
 | |
|     pool: false, // Use fresh connections for transactional emails
 | |
|     maxConnections: 1,
 | |
|     maxMessages: 1,
 | |
|     connectionTimeout: options.connectionTimeout || 10000,
 | |
|     socketTimeout: options.socketTimeout || 30000
 | |
|   });
 | |
| } |