@akkikhan/saas-framework-notifications
Version:
Multi-channel notification SDK for SaaS Framework applications
367 lines • 11.7 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaaSNotifications = void 0;
const axios_1 = __importDefault(require("axios"));
/**
* SaaS Framework Notifications SDK
* Multi-channel notification system with templates, preferences, and delivery tracking
*/
class SaaSNotifications {
constructor(config) {
this.config = config;
this.client = axios_1.default.create({
baseURL: config.baseUrl,
headers: {
'X-API-Key': config.apiKey,
'Content-Type': 'application/json',
...(config.tenantId && { 'X-Tenant-ID': config.tenantId })
},
});
}
// ============ DIRECT NOTIFICATIONS ============
/**
* Send email notification
*/
async sendEmail(notification) {
try {
const response = await this.client.post('/notifications/email', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send email:', error);
throw new Error('Failed to send email notification');
}
}
/**
* Send SMS notification
*/
async sendSMS(notification) {
try {
const response = await this.client.post('/notifications/sms', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send SMS:', error);
throw new Error('Failed to send SMS notification');
}
}
/**
* Send push notification
*/
async sendPush(notification) {
try {
const response = await this.client.post('/notifications/push', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send push notification:', error);
throw new Error('Failed to send push notification');
}
}
/**
* Send webhook notification
*/
async sendWebhook(notification) {
try {
const response = await this.client.post('/notifications/webhook', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send webhook:', error);
throw new Error('Failed to send webhook notification');
}
}
/**
* Send multi-channel notification
*/
async sendMultiChannel(notification) {
try {
const response = await this.client.post('/notifications/multi-channel', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send multi-channel notification:', error);
throw new Error('Failed to send multi-channel notification');
}
}
// ============ TEMPLATE MANAGEMENT ============
/**
* Create notification template
*/
async createTemplate(template) {
try {
const response = await this.client.post('/notifications/templates', {
...template,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to create template:', error);
throw new Error('Failed to create notification template');
}
}
/**
* Update notification template
*/
async updateTemplate(templateId, updates) {
try {
const response = await this.client.put(`/notifications/templates/${templateId}`, updates);
return response.data;
}
catch (error) {
console.error('Failed to update template:', error);
throw new Error('Failed to update notification template');
}
}
/**
* Delete notification template
*/
async deleteTemplate(templateId) {
try {
await this.client.delete(`/notifications/templates/${templateId}`);
}
catch (error) {
console.error('Failed to delete template:', error);
throw new Error('Failed to delete notification template');
}
}
/**
* Get all templates
*/
async getTemplates() {
try {
const response = await this.client.get('/notifications/templates');
return response.data;
}
catch (error) {
console.error('Failed to get templates:', error);
throw new Error('Failed to get notification templates');
}
}
/**
* Get template by ID
*/
async getTemplate(templateId) {
try {
const response = await this.client.get(`/notifications/templates/${templateId}`);
return response.data;
}
catch (error) {
console.error('Failed to get template:', error);
throw new Error('Failed to get notification template');
}
}
/**
* Send notification using template
*/
async sendFromTemplate(request) {
try {
const response = await this.client.post('/notifications/send-template', {
...request,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send template notification:', error);
throw new Error('Failed to send template notification');
}
}
// ============ BULK NOTIFICATIONS ============
/**
* Send bulk notifications
*/
async sendBulk(notification) {
try {
const response = await this.client.post('/notifications/bulk', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to send bulk notification:', error);
throw new Error('Failed to send bulk notification');
}
}
/**
* Get bulk notification status
*/
async getBulkStatus(batchId) {
try {
const response = await this.client.get(`/notifications/bulk/${batchId}/status`);
return response.data;
}
catch (error) {
console.error('Failed to get bulk status:', error);
throw new Error('Failed to get bulk notification status');
}
}
// ============ DELIVERY TRACKING ============
/**
* Get notification status
*/
async getNotificationStatus(notificationId) {
try {
const response = await this.client.get(`/notifications/${notificationId}/status`);
return response.data;
}
catch (error) {
console.error('Failed to get notification status:', error);
throw new Error('Failed to get notification status');
}
}
/**
* Get delivery statistics
*/
async getDeliveryStats(options) {
try {
const response = await this.client.get('/notifications/stats', {
params: options
});
return response.data;
}
catch (error) {
console.error('Failed to get delivery stats:', error);
throw new Error('Failed to get delivery statistics');
}
}
// ============ USER PREFERENCES ============
/**
* Set user notification preferences
*/
async setUserPreferences(userId, preferences) {
try {
await this.client.put(`/notifications/preferences/${userId}`, {
...preferences,
tenantId: this.config.tenantId
});
}
catch (error) {
console.error('Failed to set preferences:', error);
throw new Error('Failed to set user preferences');
}
}
/**
* Get user notification preferences
*/
async getUserPreferences(userId) {
try {
const response = await this.client.get(`/notifications/preferences/${userId}`);
return response.data;
}
catch (error) {
console.error('Failed to get preferences:', error);
throw new Error('Failed to get user preferences');
}
}
/**
* Check if user can receive notification
*/
async canSendToUser(userId, channel) {
try {
const response = await this.client.get(`/notifications/preferences/${userId}/can-send`, {
params: { channel }
});
return response.data.canSend;
}
catch (error) {
console.error('Failed to check send permission:', error);
return false;
}
}
// ============ SCHEDULED NOTIFICATIONS ============
/**
* Schedule notification for later delivery
*/
async scheduleNotification(notification) {
try {
const response = await this.client.post('/notifications/schedule', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to schedule notification:', error);
throw new Error('Failed to schedule notification');
}
}
/**
* Cancel scheduled notification
*/
async cancelScheduledNotification(scheduleId) {
try {
await this.client.delete(`/notifications/schedule/${scheduleId}`);
}
catch (error) {
console.error('Failed to cancel scheduled notification:', error);
throw new Error('Failed to cancel scheduled notification');
}
}
/**
* Get scheduled notifications
*/
async getScheduledNotifications() {
try {
const response = await this.client.get('/notifications/schedule');
return response.data;
}
catch (error) {
console.error('Failed to get scheduled notifications:', error);
throw new Error('Failed to get scheduled notifications');
}
}
// ============ HELPER METHODS ============
/**
* Validate email template
*/
async validateTemplate(template) {
try {
const response = await this.client.post('/notifications/templates/validate', template);
return response.data;
}
catch (error) {
console.error('Failed to validate template:', error);
throw new Error('Failed to validate template');
}
}
/**
* Test notification delivery
*/
async testNotification(notification) {
try {
const response = await this.client.post('/notifications/test', {
...notification,
tenantId: this.config.tenantId
});
return response.data;
}
catch (error) {
console.error('Failed to test notification:', error);
throw new Error('Failed to test notification');
}
}
}
exports.SaaSNotifications = SaaSNotifications;
exports.default = SaaSNotifications;
//# sourceMappingURL=index.js.map