notification-service-sdk
Version:
A Node.js notification service SDK supporting email and SMS providers
55 lines • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmailProvider = void 0;
const nodemailer_1 = require("nodemailer");
const interfaces_1 = require("../interfaces");
class EmailProvider {
constructor(config) {
var _a, _b;
if (!config.host || !config.port || !((_a = config.auth) === null || _a === void 0 ? void 0 : _a.user) || !((_b = config.auth) === null || _b === void 0 ? void 0 : _b.pass)) {
throw new interfaces_1.NotificationError('Invalid email configuration', 'INVALID_CONFIG');
}
this.config = config;
this.transporter = (0, nodemailer_1.createTransport)({
host: config.host,
port: config.port,
secure: config.secure || false,
auth: {
user: config.auth.user,
pass: config.auth.pass,
},
});
}
async send(message) {
try {
if (!message.to) {
throw new interfaces_1.NotificationError('Recipient is required', 'MISSING_RECIPIENT');
}
if (!message.subject) {
throw new interfaces_1.NotificationError('Subject is required', 'MISSING_SUBJECT');
}
if (!message.text && !message.html) {
throw new interfaces_1.NotificationError('Message content is required', 'MISSING_CONTENT');
}
const info = await this.transporter.sendMail({
from: message.from || this.config.auth.user,
to: message.to,
subject: message.subject,
text: message.text,
html: message.html,
});
return {
success: true,
messageId: info.messageId,
};
}
catch (error) {
return {
success: false,
error: error instanceof Error ? error : new Error(String(error)),
};
}
}
}
exports.EmailProvider = EmailProvider;
//# sourceMappingURL=email.js.map