@454creative/easy-email
Version:
A framework-agnostic email service library for Node.js with observability and monitoring
101 lines • 3.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailConfigBuilder = exports.DEFAULT_SES_CONFIG = exports.DEFAULT_SENDGRID_CONFIG = exports.DEFAULT_SMTP_CONFIG = exports.DEFAULT_TIMEOUT = exports.DEFAULT_RETRY_DELAY = exports.DEFAULT_RETRY_ATTEMPTS = void 0;
const email_options_interface_1 = require("../interfaces/email-options.interface");
exports.DEFAULT_RETRY_ATTEMPTS = 3;
exports.DEFAULT_RETRY_DELAY = 1000; // 1 second
exports.DEFAULT_TIMEOUT = 10000; // 10 seconds
exports.DEFAULT_SMTP_CONFIG = {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: '',
pass: '',
},
tls: {
rejectUnauthorized: true,
},
};
exports.DEFAULT_SENDGRID_CONFIG = {
apiKey: '',
};
/**
* Default AWS SES Configuration
*/
exports.DEFAULT_SES_CONFIG = {
region: 'us-west-2',
apiVersion: '2010-12-01',
maxRetries: 3,
httpOptions: {
timeout: 10000,
connectTimeout: 5000,
},
};
class EmailConfigBuilder {
constructor(providerType = email_options_interface_1.EmailProviderType.SMTP) {
this.retryAttempts = exports.DEFAULT_RETRY_ATTEMPTS;
this.retryDelay = exports.DEFAULT_RETRY_DELAY;
this.timeout = exports.DEFAULT_TIMEOUT;
let defaultConfig;
switch (providerType) {
case email_options_interface_1.EmailProviderType.SMTP:
defaultConfig = Object.assign({}, exports.DEFAULT_SMTP_CONFIG);
break;
case email_options_interface_1.EmailProviderType.SENDGRID:
defaultConfig = Object.assign({}, exports.DEFAULT_SENDGRID_CONFIG);
break;
case email_options_interface_1.EmailProviderType.SES:
// AWS SES Configuration
defaultConfig = Object.assign({}, exports.DEFAULT_SES_CONFIG);
break;
default:
defaultConfig = Object.assign({}, exports.DEFAULT_SMTP_CONFIG);
}
this.config = {
type: providerType,
config: defaultConfig,
};
}
withSmtpConfig(config) {
if (this.config.type !== email_options_interface_1.EmailProviderType.SMTP) {
throw new Error('Cannot set SMTP config for non-SMTP provider');
}
this.config.config = Object.assign(Object.assign({}, this.config.config), config);
return this;
}
withSendGridConfig(config) {
if (this.config.type !== email_options_interface_1.EmailProviderType.SENDGRID) {
throw new Error('Cannot set SendGrid config for non-SendGrid provider');
}
this.config.config = Object.assign(Object.assign({}, this.config.config), config);
return this;
}
/**
* Configure AWS SES settings
*/
withSesConfig(config) {
if (this.config.type !== email_options_interface_1.EmailProviderType.SES) {
throw new Error('Cannot set SES config for non-SES provider');
}
this.config.config = Object.assign(Object.assign({}, this.config.config), config);
return this;
}
withRetryAttempts(attempts) {
this.retryAttempts = attempts;
return this;
}
withRetryDelay(delay) {
this.retryDelay = delay;
return this;
}
withTimeout(timeout) {
this.timeout = timeout;
return this;
}
build() {
return Object.assign(Object.assign({}, this.config), { retryAttempts: this.retryAttempts, retryDelay: this.retryDelay, timeout: this.timeout });
}
}
exports.EmailConfigBuilder = EmailConfigBuilder;
//# sourceMappingURL=default-config.js.map