notification-services
Version:
Use email, sms and custom notification services for node.js application easily
200 lines (199 loc) • 7.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fcmSendMulticast = exports.fcmSendToCondition = exports.fcmSendToTopic = exports.fcmSendToToken = exports.fcmSend = exports.fcmConfig = exports.FCMv1 = 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");
const fcm_1 = require("./fcm");
Object.defineProperty(exports, "FCMv1", { enumerable: true, get: function () { return fcm_1.FCMv1; } });
Object.defineProperty(exports, "fcmConfig", { enumerable: true, get: function () { return fcm_1.fcmConfig; } });
Object.defineProperty(exports, "fcmSend", { enumerable: true, get: function () { return fcm_1.fcmSend; } });
Object.defineProperty(exports, "fcmSendToToken", { enumerable: true, get: function () { return fcm_1.fcmSendToToken; } });
Object.defineProperty(exports, "fcmSendToTopic", { enumerable: true, get: function () { return fcm_1.fcmSendToTopic; } });
Object.defineProperty(exports, "fcmSendToCondition", { enumerable: true, get: function () { return fcm_1.fcmSendToCondition; } });
Object.defineProperty(exports, "fcmSendMulticast", { enumerable: true, get: function () { return fcm_1.fcmSendMulticast; } });
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;
/**
* @deprecated
* This function uses the legacy FCM HTTP API (not FCM v1).
* It sends a custom notification to devices via Firebase Cloud Messaging (FCM).
* Please migrate to FCM v1 API for new projects.
* @use
*
* @param options - The notification options:
* - serverKey: The FCM server key (legacy, not OAuth2).
* - title: The notification title.
* - text: The notification body text.
* - fcm_tokens: Array of FCM device tokens to send the notification to.
* @param callback - Callback function with (response, error).
*/
const sendCustomNotification = (options, callback) => {
(0, custom_notification_1.customNotification)(options, callback);
};
exports.sendCustomNotification = sendCustomNotification;