@astralibx/email-rule-engine 1.2.2 → 3.0.1

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.
Files changed (109) hide show
  1. package/README.md +93 -46
  2. package/dist/index.d.mts +607 -0
  3. package/dist/index.d.ts +595 -20
  4. package/dist/index.js +1731 -65
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +1699 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +26 -8
  9. package/dist/controllers/index.d.ts +0 -5
  10. package/dist/controllers/index.d.ts.map +0 -1
  11. package/dist/controllers/index.js +0 -12
  12. package/dist/controllers/index.js.map +0 -1
  13. package/dist/controllers/rule.controller.d.ts +0 -13
  14. package/dist/controllers/rule.controller.d.ts.map +0 -1
  15. package/dist/controllers/rule.controller.js +0 -142
  16. package/dist/controllers/rule.controller.js.map +0 -1
  17. package/dist/controllers/runner.controller.d.ts +0 -8
  18. package/dist/controllers/runner.controller.d.ts.map +0 -1
  19. package/dist/controllers/runner.controller.js +0 -22
  20. package/dist/controllers/runner.controller.js.map +0 -1
  21. package/dist/controllers/settings.controller.d.ts +0 -7
  22. package/dist/controllers/settings.controller.d.ts.map +0 -1
  23. package/dist/controllers/settings.controller.js +0 -56
  24. package/dist/controllers/settings.controller.js.map +0 -1
  25. package/dist/controllers/template.controller.d.ts +0 -15
  26. package/dist/controllers/template.controller.d.ts.map +0 -1
  27. package/dist/controllers/template.controller.js +0 -169
  28. package/dist/controllers/template.controller.js.map +0 -1
  29. package/dist/index.d.ts.map +0 -1
  30. package/dist/routes/index.d.ts +0 -16
  31. package/dist/routes/index.d.ts.map +0 -1
  32. package/dist/routes/index.js +0 -47
  33. package/dist/routes/index.js.map +0 -1
  34. package/dist/schemas/index.d.ts +0 -6
  35. package/dist/schemas/index.d.ts.map +0 -1
  36. package/dist/schemas/index.js +0 -14
  37. package/dist/schemas/index.js.map +0 -1
  38. package/dist/schemas/rule-send.schema.d.ts +0 -25
  39. package/dist/schemas/rule-send.schema.d.ts.map +0 -1
  40. package/dist/schemas/rule-send.schema.js +0 -41
  41. package/dist/schemas/rule-send.schema.js.map +0 -1
  42. package/dist/schemas/rule.schema.d.ts +0 -22
  43. package/dist/schemas/rule.schema.d.ts.map +0 -1
  44. package/dist/schemas/rule.schema.js +0 -81
  45. package/dist/schemas/rule.schema.js.map +0 -1
  46. package/dist/schemas/run-log.schema.d.ts +0 -39
  47. package/dist/schemas/run-log.schema.d.ts.map +0 -1
  48. package/dist/schemas/run-log.schema.js +0 -47
  49. package/dist/schemas/run-log.schema.js.map +0 -1
  50. package/dist/schemas/template.schema.d.ts +0 -24
  51. package/dist/schemas/template.schema.d.ts.map +0 -1
  52. package/dist/schemas/template.schema.js +0 -65
  53. package/dist/schemas/template.schema.js.map +0 -1
  54. package/dist/schemas/throttle-config.schema.d.ts +0 -19
  55. package/dist/schemas/throttle-config.schema.d.ts.map +0 -1
  56. package/dist/schemas/throttle-config.schema.js +0 -32
  57. package/dist/schemas/throttle-config.schema.js.map +0 -1
  58. package/dist/services/index.d.ts +0 -5
  59. package/dist/services/index.d.ts.map +0 -1
  60. package/dist/services/index.js +0 -12
  61. package/dist/services/index.js.map +0 -1
  62. package/dist/services/rule-runner.service.d.ts +0 -31
  63. package/dist/services/rule-runner.service.d.ts.map +0 -1
  64. package/dist/services/rule-runner.service.js +0 -279
  65. package/dist/services/rule-runner.service.js.map +0 -1
  66. package/dist/services/rule.service.d.ts +0 -27
  67. package/dist/services/rule.service.d.ts.map +0 -1
  68. package/dist/services/rule.service.js +0 -127
  69. package/dist/services/rule.service.js.map +0 -1
  70. package/dist/services/template-render.service.d.ts +0 -23
  71. package/dist/services/template-render.service.d.ts.map +0 -1
  72. package/dist/services/template-render.service.js +0 -178
  73. package/dist/services/template-render.service.js.map +0 -1
  74. package/dist/services/template.service.d.ts +0 -42
  75. package/dist/services/template.service.d.ts.map +0 -1
  76. package/dist/services/template.service.js +0 -128
  77. package/dist/services/template.service.js.map +0 -1
  78. package/dist/types/config.types.d.ts +0 -85
  79. package/dist/types/config.types.d.ts.map +0 -1
  80. package/dist/types/config.types.js +0 -3
  81. package/dist/types/config.types.js.map +0 -1
  82. package/dist/types/enums.d.ts +0 -43
  83. package/dist/types/enums.d.ts.map +0 -1
  84. package/dist/types/enums.js +0 -40
  85. package/dist/types/enums.js.map +0 -1
  86. package/dist/types/index.d.ts +0 -6
  87. package/dist/types/index.d.ts.map +0 -1
  88. package/dist/types/index.js +0 -11
  89. package/dist/types/index.js.map +0 -1
  90. package/dist/types/rule.types.d.ts +0 -91
  91. package/dist/types/rule.types.d.ts.map +0 -1
  92. package/dist/types/rule.types.js +0 -3
  93. package/dist/types/rule.types.js.map +0 -1
  94. package/dist/types/template.types.d.ts +0 -43
  95. package/dist/types/template.types.d.ts.map +0 -1
  96. package/dist/types/template.types.js +0 -3
  97. package/dist/types/template.types.js.map +0 -1
  98. package/dist/types/throttle.types.d.ts +0 -15
  99. package/dist/types/throttle.types.d.ts.map +0 -1
  100. package/dist/types/throttle.types.js +0 -3
  101. package/dist/types/throttle.types.js.map +0 -1
  102. package/dist/utils/express-helpers.d.ts +0 -4
  103. package/dist/utils/express-helpers.d.ts.map +0 -1
  104. package/dist/utils/express-helpers.js +0 -15
  105. package/dist/utils/express-helpers.js.map +0 -1
  106. package/dist/utils/redis-lock.d.ts +0 -13
  107. package/dist/utils/redis-lock.d.ts.map +0 -1
  108. package/dist/utils/redis-lock.js +0 -36
  109. package/dist/utils/redis-lock.js.map +0 -1
package/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # @astralibx/email-rule-engine
2
2
 
3
- Rule-based email automation engine with MJML + Handlebars templates, per-user throttling, and Redis distributed locking.
3
+ [![npm version](https://img.shields.io/npm/v/@astralibx/email-rule-engine.svg)](https://www.npmjs.com/package/@astralibx/email-rule-engine)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ Rule-based email automation engine for Node.js. Define targeting rules with conditions, design responsive emails with MJML + Handlebars templates, and let the engine handle per-user throttling, send deduplication, distributed locking, and delivery scheduling. Ships with Express REST routes for admin UIs and a programmatic API for direct service access.
4
7
 
5
8
  ## Install
6
9
 
@@ -8,68 +11,112 @@ Rule-based email automation engine with MJML + Handlebars templates, per-user th
8
11
  npm install @astralibx/email-rule-engine
9
12
  ```
10
13
 
14
+ **Peer dependencies:**
15
+
16
+ | Peer Dependency | Version |
17
+ |-----------------|---------|
18
+ | `express` | `^4.18.0 \|\| ^5.0.0` |
19
+ | `mongoose` | `^7.0.0 \|\| ^8.0.0` |
20
+ | `ioredis` | `^5.0.0` |
21
+ | `handlebars` | `^4.7.0` |
22
+ | `mjml` | `^4.0.0` |
23
+ | `html-to-text` | `^9.0.0` |
24
+
25
+ ```bash
26
+ npm install express mongoose ioredis handlebars mjml html-to-text
27
+ ```
28
+
11
29
  ## Quick Start
12
30
 
13
31
  ```typescript
14
- import { createEmailRuleEngine } from '@astralibx/email-rule-engine';
32
+ import express from 'express';
15
33
  import mongoose from 'mongoose';
16
34
  import Redis from 'ioredis';
35
+ import { createEmailRuleEngine } from '@astralibx/email-rule-engine';
36
+
37
+ const app = express();
38
+ app.use(express.json());
39
+
40
+ const dbConnection = mongoose.createConnection('mongodb://localhost/myapp');
41
+ const redis = new Redis();
17
42
 
18
43
  const engine = createEmailRuleEngine({
19
- db: { connection: mongoose.createConnection('mongodb://localhost/myapp') },
20
- redis: { connection: new Redis() },
44
+ db: { connection: dbConnection },
45
+ redis: { connection: redis },
21
46
  adapters: {
22
- queryUsers: async (target, limit) => db.collection('users').find({ role: target.role }).limit(limit).toArray(),
23
- resolveData: (user) => ({ user: { name: user.name, email: user.email }, platform: { name: 'MyApp' } }),
24
- sendEmail: async (params) => transporter.sendMail({ to: params.identifierId, subject: params.subject, html: params.htmlBody }),
47
+ queryUsers: async (target, limit) => {
48
+ return User.find({ role: target.role, platform: target.platform }).limit(limit).lean();
49
+ },
50
+ resolveData: (user) => ({
51
+ user: { name: user.name, email: user.email },
52
+ platform: { name: 'MyApp', domain: 'myapp.com' },
53
+ }),
54
+ sendEmail: async (params) => {
55
+ await transporter.sendMail({
56
+ to: params.identifierId,
57
+ subject: params.subject,
58
+ html: params.htmlBody,
59
+ text: params.textBody,
60
+ });
61
+ },
25
62
  selectAgent: async () => ({ accountId: 'default' }),
26
- findIdentifier: async (email) => { const c = await Contacts.findOne({ email }); return c ? { id: c._id.toString(), contactId: c._id.toString() } : null; },
63
+ findIdentifier: async (email) => {
64
+ const contact = await Contact.findOne({ email });
65
+ return contact ? { id: contact._id.toString(), contactId: contact._id.toString() } : null;
66
+ },
27
67
  },
28
68
  });
29
69
 
70
+ // Mount REST routes
30
71
  app.use('/api/email-rules', engine.routes);
31
- ```
32
72
 
33
- ## Features & Documentation
34
-
35
- | Feature | Description | Docs |
36
- |---------|-------------|------|
37
- | Adapters | Connect to your data, delivery, and accounts | [adapters.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/adapters.md) |
38
- | Templates | MJML + Handlebars responsive email templates | [templates.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/templates.md) |
39
- | Rules | Condition-based targeting and automation | [rules.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/rules.md) |
40
- | Throttling | Per-user daily/weekly rate limits | [throttling.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/throttling.md) |
41
- | Send Window | Time-based execution control | [send-window.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/send-window.md) |
42
- | Delivery Delays | Natural send spread with jitter | [delivery-delays.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/delivery-delays.md) |
43
- | Progress Hooks | Live execution tracking callbacks | [progress-hooks.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/progress-hooks.md) |
44
- | SMTP Pooling | Efficient email delivery patterns | [smtp-pooling.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/smtp-pooling.md) |
45
- | Account Rotation | Multi-account sending with quotas | [account-rotation.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/account-rotation.md) |
46
- | Email Validation | MX checks and domain validation | [email-validation.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/email-validation.md) |
47
- | Execution Flow | How the runner processes rules | [execution-flow.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/execution-flow.md) |
48
- | API Routes | REST endpoint reference | [api-routes.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/api-routes.md) |
49
- | Config Reference | Full configuration options | [config-reference.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email-rule-engine/docs/config-reference.md) |
50
-
51
- ## Prerequisites
52
-
53
- | Dependency | Version | Purpose |
54
- |------------|---------|---------|
55
- | Node.js | >= 18 | Runtime |
56
- | MongoDB | >= 5.0 | Data storage (5 collections auto-created) |
57
- | Redis | >= 6.0 | Distributed locking |
58
-
59
- ### Peer Dependencies
73
+ // Trigger a run programmatically (e.g., from a cron job)
74
+ // await engine.runner.runAllRules('cron');
60
75
 
61
- ```bash
62
- npm install express mongoose ioredis handlebars mjml html-to-text
76
+ app.listen(3000);
63
77
  ```
64
78
 
65
- | Package | Version | Purpose |
66
- |---------|---------|---------|
67
- | `express` | ^4.18 or ^5.0 | HTTP routes |
68
- | `mongoose` | ^7.0 or ^8.0 | MongoDB ODM |
69
- | `ioredis` | ^5.0 | Redis client |
70
- | `handlebars` | ^4.7 | Template variables |
71
- | `mjml` | ^4.0 | Responsive email HTML |
72
- | `html-to-text` | ^9.0 | Plain text fallback |
79
+ The factory returns an `EmailRuleEngine` instance with:
80
+
81
+ - `routes` -- Express Router with all CRUD and runner endpoints
82
+ - `runner` -- `RuleRunnerService` for triggering runs programmatically
83
+ - `templateService` -- `TemplateService` for CRUD and preview
84
+ - `ruleService` -- `RuleService` for CRUD and dry runs
85
+ - `models` -- Direct Mongoose model access (`EmailTemplate`, `EmailRule`, `EmailRuleSend`, `EmailRuleRunLog`, `EmailThrottleConfig`)
86
+
87
+ ## Configuration
88
+
89
+ The `createEmailRuleEngine(config)` factory accepts an `EmailRuleEngineConfig` object validated at startup with Zod.
90
+
91
+ | Section | Required | Description |
92
+ |---------|----------|-------------|
93
+ | `db` | Yes | Mongoose connection and optional collection prefix |
94
+ | `redis` | Yes | ioredis connection for distributed locking |
95
+ | `adapters` | Yes | 5 required + 1 optional function bridging your app to the engine |
96
+ | `platforms` | No | Valid platform values for schema validation |
97
+ | `audiences` | No | Valid audience/role values for schema validation |
98
+ | `categories` | No | Valid template categories for schema validation |
99
+ | `options` | No | Lock TTL, max per run, send window, delays |
100
+ | `hooks` | No | Callbacks at key execution points |
101
+ | `logger` | No | Logger with `info`, `warn`, `error` methods |
102
+
103
+ See [docs/configuration.md](docs/configuration.md) for the full reference with examples.
104
+
105
+ ## Documentation
106
+
107
+ | Document | Description |
108
+ |----------|-------------|
109
+ | [Configuration](docs/configuration.md) | Full config reference -- db, redis, adapters, platforms, options, hooks, logger |
110
+ | [Adapters](docs/adapters.md) | All 6 adapters with type signatures and example implementations |
111
+ | [Templates](docs/templates.md) | Creating templates, MJML + Handlebars syntax, built-in helpers |
112
+ | [Rules](docs/rules.md) | Targeting conditions, operators, sendOnce/resend, dry runs |
113
+ | [Throttling](docs/throttling.md) | Per-user limits, global caps, bypass rules, tracking |
114
+ | [API Routes](docs/api-routes.md) | All REST endpoints with curl examples |
115
+ | [Programmatic API](docs/programmatic-api.md) | Using services directly -- runner, templateService, ruleService |
116
+ | [Execution Flow](docs/execution-flow.md) | Step-by-step runner flow and error behavior |
117
+ | [Error Handling](docs/error-handling.md) | All error classes with codes and when thrown |
118
+ | [Constants](docs/constants.md) | All exported constants and derived types |
119
+ | [Migration v1 to v2](docs/migration-v1-to-v2.md) | Breaking changes from v1 |
73
120
 
74
121
  ## License
75
122