UNPKG

notification-services

Version:

use email, sms and custom notification services for node.js application easily

160 lines (159 loc) 5.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendCustomNotification = exports.SMSSend = exports.sendMail = exports.mailConfig = void 0; const custom_notification_1 = require("./custom-notification"); const json_1 = require("./email/json"); const sendmail_1 = require("./email/sendmail"); const ses_1 = require("./email/ses"); const smtp_1 = require("./email/smtp"); const stream_1 = require("./email/stream"); const fast2sms_1 = require("./sms/fast2sms"); const gupshup_1 = require("./sms/gupshup"); const messagebird_1 = require("./sms/messagebird"); const twilio_1 = require("./sms/twilio"); let serviceType = ''; // email configuration const mailConfig = (type, options, callback) => { const { path, newline, awsAccessId, awsAccessKey, apiVersion, region, host, user, pass } = options; if (type) { type = type.toLowerCase(); } serviceType = type; switch (serviceType) { case 'json': { (0, json_1.JSONConfig)(); callback({ message: 'Connection Setup!' }, null); break; } case 'sendmail': { if (!path || !path.length || !newline || !newline.length) { callback(null, { message: 'Please provide path and newline.' }); } else { (0, sendmail_1.sendmailConfig)(path, newline); callback({ message: 'Connection Setup!' }, null); } break; } case 'ses': { if (!awsAccessId || !awsAccessId.length || !awsAccessKey || !awsAccessKey.length || !apiVersion || !apiVersion.length || !region || !region.length) { callback(null, { message: 'Please provide all the required configurations.' }); } else { (0, ses_1.SESConfig)(awsAccessId, awsAccessKey, apiVersion, region); callback({ message: 'Connection Setup!' }, null); } break; } case 'stream': { (0, stream_1.streamConfig)(); callback({ message: 'Connection Setup!' }, null); break; } case 'smtp': { if (!host || !host.length || !user || !user.length || !pass || !pass.length) { callback(null, { message: 'Please provide all the required configurations.' }); } else { (0, smtp_1.SMTPConfig)(host, user, pass, callback); } break; } default: { if (!host || !host.length || !user || !user.length || !pass || !pass.length) { callback(null, { message: 'Please provide all the required configurations.' }); } else { (0, smtp_1.SMTPConfig)(host, user, pass, callback); } break; } } }; exports.mailConfig = mailConfig; // send mail with defined transport object const sendMail = (transporter, mailOptions, template, callback) => { switch (serviceType) { case 'json': { (0, json_1.JSONSend)(mailOptions, callback); break; } case 'sendmail': { (0, sendmail_1.sendmailSend)(mailOptions, callback); break; } case 'ses': { (0, ses_1.SESSend)(mailOptions, callback); break; } case 'stream': { (0, stream_1.streamSend)(mailOptions, callback); break; } case 'smtp': { (0, smtp_1.SMTPSend)(transporter, mailOptions, template, callback); break; } default: { (0, smtp_1.SMTPSend)(transporter, mailOptions, template, callback); break; } } }; exports.sendMail = sendMail; // send sms const SMSSend = (type, options, callback) => { if (type) { type = type.toLowerCase(); } switch (type) { case 'fast2sms': { (0, fast2sms_1.fast2smsSend)(options, callback); break; } case 'gupshup': { if (!options.userId || !options.password || !options.mask || !options.message || !options.to) { throw new Error("Config object is not correct."); } (0, gupshup_1.gupshupSend)({ userId: options.userId, password: options.password, mask: options.mask, message: options.message, to: options.to }, callback); break; } case 'messagebird': { (0, messagebird_1.messagebirdSend)(options, callback); break; } case 'twilio': { (0, twilio_1.twilioSend)(options, callback); break; } default: { if (!options.userId || !options.password || !options.mask || !options.message || !options.to) { throw new Error("Config object is not correct."); } (0, gupshup_1.gupshupSend)({ userId: options.userId, password: options.password, mask: options.mask, message: options.message, to: options.to }, callback); break; } } }; exports.SMSSend = SMSSend; // custom notifications const sendCustomNotification = (options, callback) => { (0, custom_notification_1.customNotification)(options, callback); }; exports.sendCustomNotification = sendCustomNotification;