@astralibx/email-rule-engine 1.2.1 → 3.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/README.md +93 -46
- package/dist/index.d.mts +607 -0
- package/dist/index.d.ts +595 -20
- package/dist/index.js +1731 -65
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1699 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +27 -8
- package/dist/controllers/index.d.ts +0 -5
- package/dist/controllers/index.d.ts.map +0 -1
- package/dist/controllers/index.js +0 -12
- package/dist/controllers/index.js.map +0 -1
- package/dist/controllers/rule.controller.d.ts +0 -13
- package/dist/controllers/rule.controller.d.ts.map +0 -1
- package/dist/controllers/rule.controller.js +0 -142
- package/dist/controllers/rule.controller.js.map +0 -1
- package/dist/controllers/runner.controller.d.ts +0 -8
- package/dist/controllers/runner.controller.d.ts.map +0 -1
- package/dist/controllers/runner.controller.js +0 -22
- package/dist/controllers/runner.controller.js.map +0 -1
- package/dist/controllers/settings.controller.d.ts +0 -7
- package/dist/controllers/settings.controller.d.ts.map +0 -1
- package/dist/controllers/settings.controller.js +0 -56
- package/dist/controllers/settings.controller.js.map +0 -1
- package/dist/controllers/template.controller.d.ts +0 -15
- package/dist/controllers/template.controller.d.ts.map +0 -1
- package/dist/controllers/template.controller.js +0 -169
- package/dist/controllers/template.controller.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/routes/index.d.ts +0 -16
- package/dist/routes/index.d.ts.map +0 -1
- package/dist/routes/index.js +0 -47
- package/dist/routes/index.js.map +0 -1
- package/dist/schemas/index.d.ts +0 -6
- package/dist/schemas/index.d.ts.map +0 -1
- package/dist/schemas/index.js +0 -14
- package/dist/schemas/index.js.map +0 -1
- package/dist/schemas/rule-send.schema.d.ts +0 -25
- package/dist/schemas/rule-send.schema.d.ts.map +0 -1
- package/dist/schemas/rule-send.schema.js +0 -41
- package/dist/schemas/rule-send.schema.js.map +0 -1
- package/dist/schemas/rule.schema.d.ts +0 -22
- package/dist/schemas/rule.schema.d.ts.map +0 -1
- package/dist/schemas/rule.schema.js +0 -81
- package/dist/schemas/rule.schema.js.map +0 -1
- package/dist/schemas/run-log.schema.d.ts +0 -39
- package/dist/schemas/run-log.schema.d.ts.map +0 -1
- package/dist/schemas/run-log.schema.js +0 -47
- package/dist/schemas/run-log.schema.js.map +0 -1
- package/dist/schemas/template.schema.d.ts +0 -24
- package/dist/schemas/template.schema.d.ts.map +0 -1
- package/dist/schemas/template.schema.js +0 -65
- package/dist/schemas/template.schema.js.map +0 -1
- package/dist/schemas/throttle-config.schema.d.ts +0 -19
- package/dist/schemas/throttle-config.schema.d.ts.map +0 -1
- package/dist/schemas/throttle-config.schema.js +0 -32
- package/dist/schemas/throttle-config.schema.js.map +0 -1
- package/dist/services/index.d.ts +0 -5
- package/dist/services/index.d.ts.map +0 -1
- package/dist/services/index.js +0 -12
- package/dist/services/index.js.map +0 -1
- package/dist/services/rule-runner.service.d.ts +0 -31
- package/dist/services/rule-runner.service.d.ts.map +0 -1
- package/dist/services/rule-runner.service.js +0 -279
- package/dist/services/rule-runner.service.js.map +0 -1
- package/dist/services/rule.service.d.ts +0 -27
- package/dist/services/rule.service.d.ts.map +0 -1
- package/dist/services/rule.service.js +0 -127
- package/dist/services/rule.service.js.map +0 -1
- package/dist/services/template-render.service.d.ts +0 -23
- package/dist/services/template-render.service.d.ts.map +0 -1
- package/dist/services/template-render.service.js +0 -178
- package/dist/services/template-render.service.js.map +0 -1
- package/dist/services/template.service.d.ts +0 -42
- package/dist/services/template.service.d.ts.map +0 -1
- package/dist/services/template.service.js +0 -128
- package/dist/services/template.service.js.map +0 -1
- package/dist/types/config.types.d.ts +0 -85
- package/dist/types/config.types.d.ts.map +0 -1
- package/dist/types/config.types.js +0 -3
- package/dist/types/config.types.js.map +0 -1
- package/dist/types/enums.d.ts +0 -43
- package/dist/types/enums.d.ts.map +0 -1
- package/dist/types/enums.js +0 -40
- package/dist/types/enums.js.map +0 -1
- package/dist/types/index.d.ts +0 -6
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -11
- package/dist/types/index.js.map +0 -1
- package/dist/types/rule.types.d.ts +0 -91
- package/dist/types/rule.types.d.ts.map +0 -1
- package/dist/types/rule.types.js +0 -3
- package/dist/types/rule.types.js.map +0 -1
- package/dist/types/template.types.d.ts +0 -43
- package/dist/types/template.types.d.ts.map +0 -1
- package/dist/types/template.types.js +0 -3
- package/dist/types/template.types.js.map +0 -1
- package/dist/types/throttle.types.d.ts +0 -15
- package/dist/types/throttle.types.d.ts.map +0 -1
- package/dist/types/throttle.types.js +0 -3
- package/dist/types/throttle.types.js.map +0 -1
- package/dist/utils/express-helpers.d.ts +0 -4
- package/dist/utils/express-helpers.d.ts.map +0 -1
- package/dist/utils/express-helpers.js +0 -15
- package/dist/utils/express-helpers.js.map +0 -1
- package/dist/utils/redis-lock.d.ts +0 -13
- package/dist/utils/redis-lock.d.ts.map +0 -1
- package/dist/utils/redis-lock.js +0 -36
- 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
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@astralibx/email-rule-engine)
|
|
4
|
+
[](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
|
|
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:
|
|
20
|
-
redis: { connection:
|
|
44
|
+
db: { connection: dbConnection },
|
|
45
|
+
redis: { connection: redis },
|
|
21
46
|
adapters: {
|
|
22
|
-
queryUsers: async (target, limit) =>
|
|
23
|
-
|
|
24
|
-
|
|
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) => {
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
| Feature | Description | Docs |
|
|
36
|
-
|---------|-------------|------|
|
|
37
|
-
| Adapters | Connect to your data, delivery, and accounts | [adapters.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/adapters.md) |
|
|
38
|
-
| Templates | MJML + Handlebars responsive email templates | [templates.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/templates.md) |
|
|
39
|
-
| Rules | Condition-based targeting and automation | [rules.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/rules.md) |
|
|
40
|
-
| Throttling | Per-user daily/weekly rate limits | [throttling.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/throttling.md) |
|
|
41
|
-
| Send Window | Time-based execution control | [send-window.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/send-window.md) |
|
|
42
|
-
| Delivery Delays | Natural send spread with jitter | [delivery-delays.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/delivery-delays.md) |
|
|
43
|
-
| Progress Hooks | Live execution tracking callbacks | [progress-hooks.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/progress-hooks.md) |
|
|
44
|
-
| SMTP Pooling | Efficient email delivery patterns | [smtp-pooling.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/smtp-pooling.md) |
|
|
45
|
-
| Account Rotation | Multi-account sending with quotas | [account-rotation.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/account-rotation.md) |
|
|
46
|
-
| Email Validation | MX checks and domain validation | [email-validation.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/email-validation.md) |
|
|
47
|
-
| Execution Flow | How the runner processes rules | [execution-flow.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/execution-flow.md) |
|
|
48
|
-
| API Routes | REST endpoint reference | [api-routes.md](https://github.com/AstralibX/astralibx/blob/main/packages/email-rule-engine/docs/api-routes.md) |
|
|
49
|
-
| Config Reference | Full configuration options | [config-reference.md](https://github.com/AstralibX/astralibx/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
|
-
|
|
62
|
-
npm install express mongoose ioredis handlebars mjml html-to-text
|
|
76
|
+
app.listen(3000);
|
|
63
77
|
```
|
|
64
78
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|