A unified TypeScript/Node.js library for sending multi-channel notifications (WhatsApp, Email, SMS) through a single interface using the Provider Pattern.
- Unified API: Single
notify()method for all channels - Provider Pattern: Easily extensible with custom providers
- Type-Safe: Full TypeScript support with comprehensive types
- Zero Dependencies: No production dependencies
- Validation: Built-in recipient and message validation
- Broadcast: Send to multiple channels simultaneously
npm install omnichannel-sdkimport {
OmniChannel,
EmailProvider,
SMSProvider,
WhatsAppProvider,
} from 'omnichannel-sdk';
// Create instance
const omni = new OmniChannel();
// Register providers
omni.registerProvider(new EmailProvider());
omni.registerProvider(new SMSProvider());
omni.registerProvider(new WhatsAppProvider());
// Send notifications
await omni.notify('email', 'user@example.com', {
subject: 'Welcome!',
body: 'Thanks for joining us.',
});
await omni.notify('sms', '+1234567890', {
body: 'Your code is 123456',
});
await omni.notify('whatsapp', '+1234567890', {
body: 'Hello from WhatsApp!',
});Main class for sending notifications.
const omni = new OmniChannel(config?: OmniChannelConfig);Config Options:
defaultChannel?: Channel- Default channel when not specifieddebug?: boolean- Enable debug loggingproviders?: Map<Channel, NotificationProvider>- Pre-registered providers
Register a notification provider.
omni.registerProvider(new EmailProvider());Send a notification through a specific channel.
const result = await omni.notify('email', 'user@example.com', {
subject: 'Hello',
body: 'Message content',
});Send a message to multiple channels simultaneously.
const results = await omni.broadcast(
['sms', 'whatsapp'],
'+1234567890',
{ body: 'Multi-channel message' }
);Validate a recipient for a specific channel.
const isValid = omni.validateRecipient('email', 'user@example.com');Check if a provider is registered for a channel.
List all registered channels.
type Channel = 'whatsapp' | 'email' | 'sms';
interface Message {
subject?: string; // Only for email
body: string;
templateId?: string;
metadata?: Record<string, unknown>;
}
interface SendResult {
success: boolean;
messageId?: string;
channel: Channel;
recipient: string;
timestamp: Date;
error?: string;
}The SDK provides custom error classes:
ValidationError: Invalid input dataProviderNotFoundError: Channel not registeredSendError: Send operation failed
import { ValidationError, ProviderNotFoundError } from 'omnichannel-sdk';
try {
await omni.notify('email', 'invalid-email', { body: 'Test' });
} catch (error) {
if (error instanceof ValidationError) {
console.log('Invalid input:', error.message);
}
}Extend BaseProvider to create custom providers:
import { BaseProvider, Message, SendResult, Channel } from 'omnichannel-sdk';
export class TwilioSMSProvider extends BaseProvider {
readonly channel: Channel = 'sms';
constructor(
private accountSid: string,
private authToken: string
) {
super();
}
validate(recipient: string): boolean {
// Custom validation logic
return /^\+[1-9]\d{9,14}$/.test(recipient);
}
async send(recipient: string, message: Message): Promise<SendResult> {
if (!this.validate(recipient)) {
throw new ValidationError('Invalid phone number');
}
// Implement Twilio API call here
// const response = await twilioClient.messages.create({...});
return this.createSuccessResult(recipient);
}
}# Install dependencies
npm install
# Build
npm run build
# Run tests
npm test
# Type check
npm run typecheck
# Run example
npm run exampleMIT
Author: @Nicoo01x