A modular, type-safe notification service for Bun applications, supporting Discord, Telegram, and Email (SMTP) channels, with Drizzle ORM database tracking and a flexible template system.
@vasugrover/bun-notifications provides a robust, extensible notification infrastructure for Bun-based backends. It enables seamless integration with multiple notification channels, ensures reliable delivery tracking via PostgreSQL, and offers a template-driven approach for dynamic message content.
- Multi-Provider Support: Out-of-the-box integration with Discord (webhook), Telegram (Bot API), and Email (SMTP via nodemailer).
- Database-Backed Tracking: All notifications are tracked in PostgreSQL using Drizzle ORM, supporting auditability and status monitoring.
- Template System: Variable substitution for dynamic, context-aware messages.
- Extensible Architecture: Add new providers by implementing the
NotificationProviderinterface. - Environment-Based Configuration: Providers auto-configure from environment variables for secure, flexible deployment.
- Bun-Native: Designed and tested exclusively for Bun environments.
- Type Safety: All APIs and types are strictly typed for reliability and maintainability.
Install via Bun:
bun add @vasugrover/bun-notificationsimport { NotificationService } from '@vasugrover/bun-notifications';
import { db } from './db'; // Your Drizzle ORM instance
const env = process.env;
const logger = console; // Or your preferred logger
const service = new NotificationService(db, env, logger);
service.initializeDefaultProviders();Send a notification to a specific provider:
await service.sendNotification('discord', 'System Alert', 'A new event occurred.', 'alert');Send a broadcast to multiple providers:
await service.sendBroadcast(['discord', 'telegram'], 'Update', 'System maintenance at 2 AM.', 'info');import { templates } from '@vasugrover/bun-notifications';
const rendered = templates.welcome.render({ name: 'Alice', message: 'Welcome to the platform!' });
await service.sendNotification('email', rendered.title, rendered.message, 'info', { email: 'alice@example.com' });Implement a custom provider by extending the NotificationProvider interface:
import { NotificationProvider, Notification } from '@vasugrover/bun-notifications';
class MyProvider implements NotificationProvider {
async send(notification: Notification): Promise<boolean> {
// Custom logic
return true;
}
}
service.registerProvider('my', new MyProvider());Set the following environment variables as needed for your providers:
DISCORD_WEBHOOK_URLTELEGRAM_BOT_TOKENTELEGRAM_CHAT_IDSMTP_HOSTSMTP_PORTSMTP_USERSMTP_PASSSMTP_FROM
For email notifications, always provide metadata.email in the notification call.
The package uses a notifications table with the following fields:
id,title,message,type,provider,status,sentAt,error,metadata,createdAt,updatedAt
Types:
Notification(select)NewNotification(insert)
Templates support variable substitution using the {{var}} syntax. Predefined templates include:
templates.welcometemplates.alerttemplates.info
You can create custom templates by implementing the TemplateRenderer interface.
NotificationStatus: 'pending' | 'sent' | 'failed'Notification: Full notification objectNewNotification: For insertsNotificationProvider:{ send(notification: Notification): Promise<boolean> }
- Ensure all required environment variables are set for each provider.
- Check the provider's
send()return value and the database record if a notification is not sent. - Verify template variable names and data if templates do not render as expected.
- Review type definitions in
src/types.tsandsrc/schema.tsfor type errors.
Contributions are welcome. Please review the contribution guidelines before submitting changes. Ensure that:
- All changes are backward compatible
- TypeScript types are updated as needed
- Documentation is kept current
- All checks pass using Bun (
bun run typecheck,bun run lint,bun run build)
This project is licensed under the MIT License. See the LICENSE file for details.
- GitHub: https://github.com/itsvasugrover/bun-notifications
- Issues: https://github.com/itsvasugrover/bun-notifications/issues
For questions or support, please open an issue on the GitHub repository.