@subhajit60/notification-engine
v1.0.0
Published
A powerful and extensible notification engine for TypeScript applications.
Downloads
20
Readme
@subhajit60/notification-engine
A powerful, extensible, and decorator-based notification engine for TypeScript applications.
Features
- Decorator Pattern: Easily add priority, timestamps, images, and custom metadata to notifications.
- Multiple Channels: Built-in support for Email, Telegram, and In-App notifications.
- Strategy Pattern: Decouple notification logic from delivery services (Nodemailer, Telegram Bots, etc.).
- Built-in Formatters: Beautifully formatted HTML emails and Telegram messages with customizable templates.
- Type-Safe: Full TypeScript support with consistent interfaces.
Installation
npm install @subhajit60/notification-engineBasic Usage
1. Initialize Strategies
Define how your notifications are actually sent by providing "sender" functions to the built-in strategies.
import {
NotificationDispatcher,
SendEmailNotification,
SendTelegramMessage
} from "@subhajit60/notification-engine";
// Example: Your custom mailer
const mailer = new MyMailer();
const myTelegramBot = new MyTelegramBot();
const dispatcher = new NotificationDispatcher({
email: new SendEmailNotification(mailer.sendHtmlMail.bind(mailer)),
telegram: new SendTelegramMessage(myTelegramBot.sendMessage.bind(myTelegramBot))
});2. Build a Notification
Use the built-in builders to create common notification types.
import { buildEmailNotification } from "@subhajit60/notification-engine";
const notification = buildEmailNotification(
'quiz', // type
'success', // variant
"Your quiz results are ready!", // message
"Quiz Success" // title
);3. Send the Notification
Dispatch the notification to a specific recipient.
await dispatcher.dispatch(notification, "[email protected]");Advanced Usage: Decorators
You can manually compose notifications using decorators for maximum flexibility.
import {
SimpleNotification,
PriorityDecorator,
TimestampDecorator,
ChannelDecorator
} from "@subhajit60/notification-engine";
let n = new SimpleNotification("alert", "warning", "System maintenance scheduled.");
n = new PriorityDecorator(n, "high");
n = new TimestampDecorator(n);
n = new ChannelDecorator(n, "email");
const payload = n.getContent();Builders
| Builder | Purpose |
|---------|---------|
| buildEmailNotification | Creates a high-priority email notification with support for images. |
| buildTelegramNotification | Creates a telegram notification with a timestamp and medium priority. |
| buildQuizNotification | Standard quiz-related notification for in-app use. |
| buildAnnouncementNotification | General announcement for in-app display. |
Strategy Interface
To implement custom delivery, simply extend INotificationStrategy:
import { INotificationStrategy, NotificationPayload } from "@subhajit60/notification-engine";
export class MyCustomStrategy extends INotificationStrategy {
async send(notification: NotificationPayload, to: string) {
// Your sending logic here
}
}License
ISC
