@classytic/notifications 0.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,30 +1,58 @@
1
1
  {
2
2
  "name": "@classytic/notifications",
3
- "version": "0.0.1",
4
- "description": "Multi-channel notification system for event-driven applications. Framework-agnostic with pluggable channels.",
3
+ "version": "2.0.0",
4
+ "description": "Multi-channel notifications (Email, SMS, Push, Webhook) with rate limiting, delivery tracking, queue adapters, channel fallback, templates, retry — zero required dependencies",
5
5
  "type": "module",
6
- "main": "./src/index.js",
7
- "types": "./notifications.d.ts",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.mjs",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.mts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.mts",
13
+ "default": "./dist/index.mjs"
14
+ },
15
+ "./channels": {
16
+ "types": "./dist/channels/index.d.mts",
17
+ "default": "./dist/channels/index.mjs"
18
+ },
19
+ "./utils": {
20
+ "types": "./dist/utils/index.d.mts",
21
+ "default": "./dist/utils/index.mjs"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "README.md",
27
+ "CHANGELOG.md",
28
+ "LICENSE"
29
+ ],
8
30
  "keywords": [
9
31
  "notifications",
10
32
  "multi-channel",
11
33
  "email",
12
- "push",
13
34
  "sms",
14
- "in-app",
15
- "event-driven",
16
- "hooks",
17
- "alerts",
18
- "messaging",
19
- "dispatcher",
20
- "channels",
35
+ "push-notifications",
36
+ "webhook",
37
+ "nodemailer",
38
+ "smtp",
39
+ "ses",
40
+ "rate-limiting",
41
+ "delivery-tracking",
42
+ "queue",
43
+ "channel-fallback",
44
+ "templates",
45
+ "retry",
46
+ "idempotency",
21
47
  "pluggable",
22
48
  "framework-agnostic",
23
- "express",
24
- "fastify",
25
- "nestjs"
49
+ "typescript",
50
+ "zero-dependencies"
51
+ ],
52
+ "author": "Classytic <classytic.dev@gmail.com> (https://github.com/classytic)",
53
+ "contributors": [
54
+ "Sadman Chowdhury (https://github.com/siam923)"
26
55
  ],
27
- "author": "Classytic",
28
56
  "license": "MIT",
29
57
  "repository": {
30
58
  "type": "git",
@@ -34,49 +62,39 @@
34
62
  "url": "https://github.com/classytic/notifications/issues"
35
63
  },
36
64
  "homepage": "https://github.com/classytic/notifications#readme",
37
- "scripts": {
38
- "test": "node --test tests/*.test.js",
39
- "test:watch": "node --test --watch tests/*.test.js",
40
- "prepublishOnly": "npm test && npm pack --dry-run",
41
- "version:patch": "npm version patch",
42
- "version:minor": "npm version minor",
43
- "version:major": "npm version major",
44
- "publish:npm": "npm publish --access public",
45
- "release:patch": "npm run version:patch && npm run publish:npm",
46
- "release:minor": "npm run version:minor && npm run publish:npm",
47
- "release:major": "npm run version:major && npm run publish:npm"
65
+ "peerDependencies": {
66
+ "nodemailer": ">=6"
67
+ },
68
+ "peerDependenciesMeta": {
69
+ "nodemailer": {
70
+ "optional": true
71
+ }
48
72
  },
49
73
  "engines": {
50
- "node": ">=18.0.0"
74
+ "node": ">=18"
51
75
  },
52
- "exports": {
53
- ".": {
54
- "types": "./notifications.d.ts",
55
- "import": "./src/index.js"
56
- },
57
- "./core/channel": {
58
- "import": "./src/core/channel.js"
59
- },
60
- "./core/dispatcher": {
61
- "import": "./src/core/dispatcher.js"
62
- },
63
- "./core/factory": {
64
- "import": "./src/core/factory.js"
65
- },
66
- "./utils/merge-hooks": {
67
- "import": "./src/utils/merge-hooks.js"
68
- }
76
+ "scripts": {
77
+ "build": "tsdown",
78
+ "dev": "tsdown --watch",
79
+ "test": "vitest run",
80
+ "test:watch": "vitest",
81
+ "test:coverage": "vitest run --coverage",
82
+ "typecheck": "tsc --noEmit",
83
+ "prepublishOnly": "npm run build && npm run typecheck",
84
+ "publish:dry": "npm publish --dry-run --access public",
85
+ "publish:npm": "npm publish --access public",
86
+ "release": "npm run build && npm run typecheck && npm publish --access public",
87
+ "release:patch": "npm version patch && npm run release",
88
+ "release:minor": "npm version minor && npm run release",
89
+ "release:major": "npm version major && npm run release"
69
90
  },
70
- "files": [
71
- "src/**/*.js",
72
- "!src/**/*.test.js",
73
- "!src/**/*.spec.js",
74
- "!src/**/__tests__/**",
75
- "notifications.d.ts",
76
- "README.md",
77
- "LICENSE"
78
- ],
79
- "peerDependencies": {},
80
- "dependencies": {}
91
+ "devDependencies": {
92
+ "@types/node": "^22.0.0",
93
+ "@types/nodemailer": "^6.4.17",
94
+ "@vitest/coverage-v8": "3.2.4",
95
+ "nodemailer": "^6.10.1",
96
+ "tsdown": "0.21.4",
97
+ "typescript": "^5.7.0",
98
+ "vitest": "^3.0.0"
99
+ }
81
100
  }
82
-
@@ -1,269 +0,0 @@
1
- /**
2
- * Type definitions for @classytic/notifications
3
- * Multi-channel notification system for event-driven applications
4
- */
5
-
6
- // ============ CORE TYPES ============
7
-
8
- /**
9
- * Notification recipient information
10
- */
11
- export interface NotificationRecipient {
12
- /** Recipient ID */
13
- id?: string | any;
14
- /** Recipient email address */
15
- email?: string;
16
- /** Recipient phone number */
17
- phone?: string;
18
- /** Recipient name */
19
- name?: string;
20
- /** Additional recipient data */
21
- [key: string]: any;
22
- }
23
-
24
- /**
25
- * Notification data passed to channels
26
- */
27
- export interface NotificationData {
28
- /** Subject/title of notification */
29
- subject?: string;
30
- /** Message body */
31
- message?: string;
32
- /** Template ID or name */
33
- template?: string;
34
- /** Template variables */
35
- templateData?: Record<string, any>;
36
- /** Additional custom data */
37
- [key: string]: any;
38
- }
39
-
40
- /**
41
- * Notification object sent to channels
42
- */
43
- export interface Notification {
44
- /** Event name that triggered the notification */
45
- event: string;
46
- /** Recipient information */
47
- recipient: NotificationRecipient;
48
- /** Notification data */
49
- data: NotificationData;
50
- }
51
-
52
- /**
53
- * Notification send result
54
- */
55
- export interface NotificationResult {
56
- /** Send status */
57
- status: 'sent' | 'skipped' | 'failed';
58
- /** Channel name */
59
- channel?: string;
60
- /** Error message if failed */
61
- error?: string;
62
- /** Additional result data */
63
- [key: string]: any;
64
- }
65
-
66
- /**
67
- * Channel capabilities
68
- */
69
- export interface ChannelCapabilities {
70
- /** Supports batch sending */
71
- supportsBatch: boolean;
72
- /** Supports scheduled sending */
73
- supportsScheduling: boolean;
74
- /** Supports file attachments */
75
- supportsAttachments: boolean;
76
- }
77
-
78
- // ============ NOTIFICATION CHANNEL ============
79
-
80
- /**
81
- * Notification channel configuration
82
- */
83
- export interface NotificationChannelConfig {
84
- /** Channel name */
85
- name?: string;
86
- /** Enable/disable channel */
87
- enabled?: boolean;
88
- /** Events this channel should handle (empty = all events) */
89
- events?: string[];
90
- /** Additional configuration */
91
- [key: string]: any;
92
- }
93
-
94
- /**
95
- * Notification Channel Base Class
96
- * Abstract base class for all notification channels
97
- */
98
- export class NotificationChannel {
99
- /** Channel configuration */
100
- config: NotificationChannelConfig;
101
- /** Whether channel is enabled */
102
- enabled: boolean;
103
- /** Channel name */
104
- name: string;
105
-
106
- /**
107
- * Create notification channel
108
- * @param config Channel configuration
109
- */
110
- constructor(config?: NotificationChannelConfig);
111
-
112
- /**
113
- * Send notification (must be implemented by subclass)
114
- * @param notification Notification to send
115
- * @returns Send result
116
- */
117
- send(notification: Notification): Promise<NotificationResult>;
118
-
119
- /**
120
- * Get events this channel handles
121
- * @returns Event names (empty array = handle all events)
122
- */
123
- getSupportedEvents(): string[];
124
-
125
- /**
126
- * Check if this channel should handle this event
127
- * @param event Event name
128
- * @returns True if channel should handle event
129
- */
130
- shouldHandle(event: string): boolean;
131
-
132
- /**
133
- * Get channel capabilities
134
- * @returns Channel capabilities
135
- */
136
- getCapabilities(): ChannelCapabilities;
137
- }
138
-
139
- // ============ DISPATCHER ============
140
-
141
- /**
142
- * Recipient resolver function
143
- * Extracts recipient information from event data
144
- */
145
- export type RecipientResolver = (eventData: any) => Promise<NotificationRecipient | null> | NotificationRecipient | null;
146
-
147
- /**
148
- * Data extractor function
149
- * Extracts notification data from event data
150
- */
151
- export type DataExtractor = (eventData: any) => NotificationData;
152
-
153
- /**
154
- * Dispatch result
155
- */
156
- export interface DispatchResult {
157
- /** Number of channels that successfully sent */
158
- sent: number;
159
- /** Number of channels that failed */
160
- failed?: number;
161
- /** Total channels attempted */
162
- total?: number;
163
- /** List of channel names */
164
- channels?: string[];
165
- /** Whether sending was skipped */
166
- skipped?: boolean;
167
- /** Reason for skip */
168
- reason?: string;
169
- /** Error message */
170
- error?: string;
171
- }
172
-
173
- /**
174
- * Notification dispatcher function
175
- */
176
- export type Dispatcher = (
177
- event: string,
178
- eventData: any,
179
- recipientResolver: RecipientResolver,
180
- dataExtractor: DataExtractor
181
- ) => Promise<DispatchResult>;
182
-
183
- /**
184
- * Create notification dispatcher
185
- * Routes notifications to appropriate channels
186
- * @param channels Registered notification channels
187
- * @returns Dispatcher function
188
- */
189
- export function createDispatcher(channels?: NotificationChannel[]): Dispatcher;
190
-
191
- // ============ FACTORY ============
192
-
193
- /**
194
- * Notification handler configuration
195
- */
196
- export interface NotificationHandlerConfig {
197
- /** Event name this handler responds to */
198
- event: string;
199
- /** Notification channels to use */
200
- channels?: NotificationChannel[];
201
- /** Function to extract recipient from event data */
202
- getRecipient: RecipientResolver;
203
- /** Function to extract template data from event data */
204
- getTemplateData: DataExtractor;
205
- /** Enable/disable this notification handler */
206
- enabled?: boolean;
207
- }
208
-
209
- /**
210
- * Notification handler function
211
- */
212
- export type NotificationHandler = (eventData: any) => Promise<void>;
213
-
214
- /**
215
- * Create notification handler for an event
216
- * @param config Handler configuration
217
- * @returns Async handler function
218
- */
219
- export function createNotificationHandler(config: NotificationHandlerConfig): NotificationHandler;
220
-
221
- /**
222
- * Map of event names to handler arrays
223
- */
224
- export type NotificationHandlers = Record<string, NotificationHandler[]>;
225
-
226
- /**
227
- * Create multiple notification handlers
228
- * @param configs Array of handler configurations
229
- * @param channels Registered notification channels
230
- * @returns Map of event → handler arrays
231
- */
232
- export function createNotificationHandlers(
233
- configs: NotificationHandlerConfig[],
234
- channels?: NotificationChannel[]
235
- ): NotificationHandlers;
236
-
237
- // ============ UTILITIES ============
238
-
239
- /**
240
- * Hook configuration object
241
- * Maps event names to handler functions or arrays of handlers
242
- */
243
- export type HookConfig = Record<string, NotificationHandler | NotificationHandler[]>;
244
-
245
- /**
246
- * Merge multiple hook configurations
247
- * Each event can have multiple handlers from different sources
248
- * @param hookConfigs Hook configuration objects to merge
249
- * @returns Merged hooks with all handlers combined
250
- *
251
- * @example
252
- * const hooks = mergeHooks(
253
- * { 'user.created': [handler1] },
254
- * { 'user.created': [handler2], 'user.deleted': [handler3] }
255
- * );
256
- * // Result: { 'user.created': [handler1, handler2], 'user.deleted': [handler3] }
257
- */
258
- export function mergeHooks(...hookConfigs: HookConfig[]): Record<string, NotificationHandler[]>;
259
-
260
- // ============ DEFAULT EXPORT ============
261
-
262
- declare const _default: {
263
- NotificationChannel: typeof NotificationChannel;
264
- createDispatcher: typeof createDispatcher;
265
- createNotificationHandlers: typeof createNotificationHandlers;
266
- mergeHooks: typeof mergeHooks;
267
- };
268
-
269
- export default _default;
@@ -1,71 +0,0 @@
1
- /**
2
- * Notification Channel Base Class
3
- * @classytic/notifications
4
- *
5
- * Abstract base class for all notification channels
6
- * Inspired by: AWS SNS, Twilio Notify, Firebase Cloud Messaging
7
- */
8
-
9
- export class NotificationChannel {
10
- constructor(config = {}) {
11
- this.config = config;
12
- this.enabled = config.enabled !== false;
13
- this.name = config.name || this.constructor.name;
14
- }
15
-
16
- /**
17
- * Send notification
18
- * Must be implemented by subclass
19
- *
20
- * @param {Object} notification
21
- * @param {string} notification.event - Event name
22
- * @param {Object} notification.recipient - Recipient info
23
- * @param {Object} notification.data - Notification data
24
- * @returns {Promise<Object>} { status: 'sent' | 'skipped' | 'failed', ... }
25
- */
26
- async send(notification) {
27
- throw new Error(`${this.name}.send() must be implemented`);
28
- }
29
-
30
- /**
31
- * Get events this channel handles
32
- * Return empty array to handle all events
33
- *
34
- * @returns {Array<string>} Event names
35
- */
36
- getSupportedEvents() {
37
- return this.config.events || [];
38
- }
39
-
40
- /**
41
- * Check if this channel should handle this event
42
- *
43
- * @param {string} event - Event name
44
- * @returns {boolean}
45
- */
46
- shouldHandle(event) {
47
- if (!this.enabled) return false;
48
-
49
- const supported = this.getSupportedEvents();
50
-
51
- // Empty array = handle all events
52
- if (supported.length === 0) return true;
53
-
54
- return supported.includes(event);
55
- }
56
-
57
- /**
58
- * Get channel capabilities
59
- * @returns {Object} Capabilities
60
- */
61
- getCapabilities() {
62
- return {
63
- supportsBatch: false,
64
- supportsScheduling: false,
65
- supportsAttachments: false,
66
- };
67
- }
68
- }
69
-
70
- export default NotificationChannel;
71
-
@@ -1,73 +0,0 @@
1
- /**
2
- * Notification Dispatcher
3
- * @classytic/notifications
4
- *
5
- * Routes notifications to appropriate channels
6
- * Pattern: Mediator + Chain of Responsibility
7
- */
8
-
9
- /**
10
- * Create notification dispatcher
11
- *
12
- * @param {Array<NotificationChannel>} channels - Registered channels
13
- * @returns {Function} Async dispatcher function
14
- */
15
- export function createDispatcher(channels = []) {
16
- if (!Array.isArray(channels)) {
17
- throw new Error('Channels must be an array');
18
- }
19
-
20
- return async function dispatch(event, eventData, recipientResolver, dataExtractor) {
21
- if (!channels.length) return { sent: 0, channels: [] };
22
-
23
- try {
24
- // 1. Resolve recipient
25
- const recipient = await recipientResolver(eventData);
26
- if (!recipient) {
27
- return { sent: 0, skipped: true, reason: 'no_recipient' };
28
- }
29
-
30
- // 2. Extract notification data
31
- const data = dataExtractor(eventData);
32
-
33
- // 3. Filter channels that should handle this event
34
- const activeChannels = channels.filter(channel => channel.shouldHandle(event));
35
-
36
- if (!activeChannels.length) {
37
- return { sent: 0, skipped: true, reason: 'no_active_channels' };
38
- }
39
-
40
- // 4. Send to all active channels (parallel)
41
- const results = await Promise.allSettled(
42
- activeChannels.map(channel =>
43
- channel.send({
44
- event,
45
- recipient,
46
- data,
47
- }).catch(error => ({
48
- status: 'failed',
49
- channel: channel.name,
50
- error: error.message,
51
- }))
52
- )
53
- );
54
-
55
- // 5. Collect results
56
- const sent = results.filter(r => r.status === 'fulfilled' && r.value?.status === 'sent').length;
57
- const failed = results.filter(r => r.status === 'rejected' || r.value?.status === 'failed').length;
58
-
59
- return {
60
- sent,
61
- failed,
62
- total: activeChannels.length,
63
- channels: activeChannels.map(c => c.name),
64
- };
65
- } catch (error) {
66
- console.error(`[Dispatcher] Failed for ${event}:`, error.message);
67
- return { sent: 0, failed: 1, error: error.message };
68
- }
69
- };
70
- }
71
-
72
- export default createDispatcher;
73
-
@@ -1,92 +0,0 @@
1
- /**
2
- * Notification Factory
3
- * @classytic/notifications
4
- *
5
- * Creates notification handlers from configurations
6
- * Pattern: Factory + Builder
7
- */
8
-
9
- import { createDispatcher } from './dispatcher.js';
10
-
11
- /**
12
- * Create notification handler for an event
13
- *
14
- * @param {Object} config
15
- * @param {string} config.event - Event name
16
- * @param {Array<NotificationChannel>} config.channels - Notification channels
17
- * @param {Function} config.getRecipient - Extract recipient from event data
18
- * @param {Function} config.getTemplateData - Extract data for templates
19
- * @param {boolean} config.enabled - Enable/disable this notification
20
- * @returns {Function} Async handler function
21
- */
22
- export function createNotificationHandler(config) {
23
- const {
24
- event,
25
- channels = [],
26
- getRecipient,
27
- getTemplateData,
28
- enabled = true,
29
- } = config;
30
-
31
- if (!event) {
32
- throw new Error('Event name is required');
33
- }
34
-
35
- if (!getRecipient || typeof getRecipient !== 'function') {
36
- throw new Error('getRecipient function is required');
37
- }
38
-
39
- if (!getTemplateData || typeof getTemplateData !== 'function') {
40
- throw new Error('getTemplateData function is required');
41
- }
42
-
43
- const dispatcher = createDispatcher(channels);
44
-
45
- return async (eventData) => {
46
- if (!enabled) return;
47
-
48
- try {
49
- await dispatcher(event, eventData, getRecipient, getTemplateData);
50
- } catch (error) {
51
- // Fire-and-forget: log but don't throw
52
- console.error(`[Notification] ${event} failed:`, error.message);
53
- }
54
- };
55
- }
56
-
57
- /**
58
- * Create multiple notification handlers
59
- *
60
- * @param {Array} configs - Notification configurations
61
- * @param {Array<NotificationChannel>} channels - Registered channels
62
- * @returns {Object} Map of event → [handlers]
63
- */
64
- export function createNotificationHandlers(configs, channels = []) {
65
- if (!Array.isArray(configs)) {
66
- throw new Error('Configs must be an array');
67
- }
68
-
69
- const handlers = {};
70
-
71
- configs.forEach(config => {
72
- if (!config.event) {
73
- throw new Error('Each config must have an event property');
74
- }
75
-
76
- if (!handlers[config.event]) {
77
- handlers[config.event] = [];
78
- }
79
-
80
- const handler = createNotificationHandler({
81
- ...config,
82
- channels,
83
- });
84
-
85
- handlers[config.event].push(handler);
86
- });
87
-
88
- return handlers;
89
- }
90
-
91
- export default { createNotificationHandler, createNotificationHandlers };
92
-
package/src/index.js DELETED
@@ -1,36 +0,0 @@
1
- /**
2
- * @classytic/notifications
3
- * Multi-Channel Notification System
4
- *
5
- * Framework-agnostic notification system with pluggable channels
6
- * Works with any event system (hooks, EventEmitter, custom)
7
- *
8
- * @version 1.0.0
9
- * @author Classytic
10
- * @license MIT
11
- */
12
-
13
- // ============ CORE ============
14
- export { NotificationChannel } from './core/channel.js';
15
- export { createDispatcher } from './core/dispatcher.js';
16
- export {
17
- createNotificationHandler,
18
- createNotificationHandlers
19
- } from './core/factory.js';
20
-
21
- // ============ UTILITIES ============
22
- export { mergeHooks } from './utils/merge-hooks.js';
23
-
24
- // ============ DEFAULT EXPORT ============
25
- import { NotificationChannel as _NotificationChannel } from './core/channel.js';
26
- import { createDispatcher as _createDispatcher } from './core/dispatcher.js';
27
- import { createNotificationHandlers as _createNotificationHandlers } from './core/factory.js';
28
- import { mergeHooks as _mergeHooks } from './utils/merge-hooks.js';
29
-
30
- export default {
31
- NotificationChannel: _NotificationChannel,
32
- createDispatcher: _createDispatcher,
33
- createNotificationHandlers: _createNotificationHandlers,
34
- mergeHooks: _mergeHooks,
35
- };
36
-