@memberjunction/communication-engine 5.0.0 → 5.2.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 +198 -0
- package/package.json +6 -6
package/README.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# @memberjunction/communication-engine
|
|
2
|
+
|
|
3
|
+
Server-side communication engine for MemberJunction. This package extends `CommunicationEngineBase` from `@memberjunction/communication-types` to provide the concrete implementation for sending messages, creating drafts, and managing communication runs through registered providers.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD
|
|
9
|
+
subgraph engine["@memberjunction/communication-engine"]
|
|
10
|
+
CE["CommunicationEngine\n(Singleton)"]
|
|
11
|
+
PMS["ProcessedMessageServer"]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
subgraph base["@memberjunction/communication-types"]
|
|
15
|
+
CEB["CommunicationEngineBase"]
|
|
16
|
+
BCP["BaseCommunicationProvider"]
|
|
17
|
+
MSG["Message"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
subgraph providers["Registered Providers"]
|
|
21
|
+
SG["SendGrid"]
|
|
22
|
+
GM["Gmail"]
|
|
23
|
+
TW["Twilio"]
|
|
24
|
+
MSP["MS Graph"]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
subgraph templates["@memberjunction/templates"]
|
|
28
|
+
TE["TemplateEngineServer"]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
CEB --> CE
|
|
32
|
+
CE -->|GetProvider| BCP
|
|
33
|
+
CE -->|processes| MSG
|
|
34
|
+
MSG --> PMS
|
|
35
|
+
PMS -->|renders via| TE
|
|
36
|
+
BCP --> SG
|
|
37
|
+
BCP --> GM
|
|
38
|
+
BCP --> TW
|
|
39
|
+
BCP --> MSP
|
|
40
|
+
|
|
41
|
+
style engine fill:#7c5295,stroke:#563a6b,color:#fff
|
|
42
|
+
style base fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
43
|
+
style providers fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
44
|
+
style templates fill:#b8762f,stroke:#8a5722,color:#fff
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install @memberjunction/communication-engine
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Key Classes
|
|
54
|
+
|
|
55
|
+
### CommunicationEngine
|
|
56
|
+
|
|
57
|
+
Singleton engine that orchestrates message sending across all registered providers. Handles provider lookup via the MJGlobal class factory, message processing (template rendering), communication run lifecycle, and logging.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { CommunicationEngine } from '@memberjunction/communication-engine';
|
|
61
|
+
import { Message, MessageRecipient } from '@memberjunction/communication-types';
|
|
62
|
+
|
|
63
|
+
const engine = CommunicationEngine.Instance;
|
|
64
|
+
await engine.Config(false, contextUser);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Sending a Single Message
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
const message = new Message();
|
|
71
|
+
message.From = 'sender@example.com';
|
|
72
|
+
message.To = 'recipient@example.com';
|
|
73
|
+
message.Subject = 'Welcome';
|
|
74
|
+
message.HTMLBody = '<h1>Hello</h1>';
|
|
75
|
+
|
|
76
|
+
const result = await engine.SendSingleMessage(
|
|
77
|
+
'SendGrid', // provider name
|
|
78
|
+
'Email', // provider message type name
|
|
79
|
+
message,
|
|
80
|
+
undefined, // optional CommunicationRunEntity
|
|
81
|
+
false // previewOnly
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (result.Success) {
|
|
85
|
+
console.log('Message sent');
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Sending to Multiple Recipients
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const recipients: MessageRecipient[] = [
|
|
93
|
+
{ To: 'alice@example.com', FullName: 'Alice', ContextData: { role: 'admin' } },
|
|
94
|
+
{ To: 'bob@example.com', FullName: 'Bob', ContextData: { role: 'user' } }
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
const message = new Message();
|
|
98
|
+
message.From = 'noreply@example.com';
|
|
99
|
+
message.BodyTemplate = templateEntity; // uses template for personalization
|
|
100
|
+
message.Subject = 'Update';
|
|
101
|
+
|
|
102
|
+
const results = await engine.SendMessages(
|
|
103
|
+
'SendGrid',
|
|
104
|
+
'Email',
|
|
105
|
+
message,
|
|
106
|
+
recipients,
|
|
107
|
+
false // previewOnly
|
|
108
|
+
);
|
|
109
|
+
// results is MessageResult[] - one per recipient
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Creating a Draft
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
const message = new Message();
|
|
116
|
+
message.From = 'user@example.com';
|
|
117
|
+
message.To = 'recipient@example.com';
|
|
118
|
+
message.Subject = 'Draft Email';
|
|
119
|
+
message.HTMLBody = '<p>Content here</p>';
|
|
120
|
+
|
|
121
|
+
const result = await engine.CreateDraft(
|
|
122
|
+
message,
|
|
123
|
+
'Microsoft Graph', // only providers with SupportsDrafts
|
|
124
|
+
contextUser
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
if (result.Success) {
|
|
128
|
+
console.log(`Draft ID: ${result.DraftID}`);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Per-Request Credentials
|
|
133
|
+
|
|
134
|
+
All send methods accept an optional `credentials` parameter for per-request credential overrides:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { SendGridCredentials } from '@memberjunction/communication-sendgrid';
|
|
138
|
+
|
|
139
|
+
const result = await engine.SendSingleMessage(
|
|
140
|
+
'SendGrid',
|
|
141
|
+
'Email',
|
|
142
|
+
message,
|
|
143
|
+
undefined,
|
|
144
|
+
false,
|
|
145
|
+
{ apiKey: 'SG.customer-specific-key' } // per-request credentials
|
|
146
|
+
);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### ProcessedMessageServer
|
|
150
|
+
|
|
151
|
+
Server-side implementation of `ProcessedMessage` that renders templates using `TemplateEngineServer`. Automatically processes body, HTML body, and subject templates with the provided context data.
|
|
152
|
+
|
|
153
|
+
```mermaid
|
|
154
|
+
sequenceDiagram
|
|
155
|
+
participant App as Application
|
|
156
|
+
participant CE as CommunicationEngine
|
|
157
|
+
participant PMS as ProcessedMessageServer
|
|
158
|
+
participant TE as TemplateEngineServer
|
|
159
|
+
participant P as Provider
|
|
160
|
+
|
|
161
|
+
App->>CE: SendSingleMessage(providerName, messageType, message)
|
|
162
|
+
CE->>CE: GetProvider(providerName)
|
|
163
|
+
CE->>PMS: new ProcessedMessageServer(message)
|
|
164
|
+
CE->>PMS: Process()
|
|
165
|
+
PMS->>TE: RenderTemplate(bodyTemplate, contextData)
|
|
166
|
+
TE-->>PMS: rendered content
|
|
167
|
+
PMS-->>CE: ProcessResult
|
|
168
|
+
CE->>P: SendSingleMessage(processedMessage, credentials)
|
|
169
|
+
P-->>CE: MessageResult
|
|
170
|
+
CE-->>App: MessageResult
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## API Reference
|
|
174
|
+
|
|
175
|
+
| Method | Description |
|
|
176
|
+
|--------|-------------|
|
|
177
|
+
| `Config(forceRefresh, contextUser, provider)` | Initialize the engine and load metadata |
|
|
178
|
+
| `GetProvider(providerName)` | Retrieve a provider instance from the class factory |
|
|
179
|
+
| `SendSingleMessage(provider, type, message, run?, preview?, credentials?)` | Send one message |
|
|
180
|
+
| `SendMessages(provider, type, message, recipients, preview?, credentials?)` | Send to multiple recipients |
|
|
181
|
+
| `CreateDraft(message, providerName, contextUser?, credentials?)` | Create a draft message |
|
|
182
|
+
|
|
183
|
+
## Dependencies
|
|
184
|
+
|
|
185
|
+
| Package | Purpose |
|
|
186
|
+
|---------|---------|
|
|
187
|
+
| `@memberjunction/communication-types` | Base engine, provider, and message types |
|
|
188
|
+
| `@memberjunction/core` | UserInfo, logging, metadata access |
|
|
189
|
+
| `@memberjunction/core-entities` | CommunicationRunEntity and related entities |
|
|
190
|
+
| `@memberjunction/global` | MJGlobal class factory for provider instantiation |
|
|
191
|
+
| `@memberjunction/templates` | Server-side template rendering engine |
|
|
192
|
+
|
|
193
|
+
## Development
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
npm run build # Compile TypeScript
|
|
197
|
+
npm start # Watch mode
|
|
198
|
+
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/communication-engine",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.2.0",
|
|
5
5
|
"description": "MemberJunction: Core Communication Framework Library",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"typescript": "^5.9.3"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@memberjunction/global": "5.
|
|
24
|
-
"@memberjunction/core": "5.
|
|
25
|
-
"@memberjunction/templates": "5.
|
|
26
|
-
"@memberjunction/core-entities": "5.
|
|
27
|
-
"@memberjunction/communication-types": "5.
|
|
23
|
+
"@memberjunction/global": "5.2.0",
|
|
24
|
+
"@memberjunction/core": "5.2.0",
|
|
25
|
+
"@memberjunction/templates": "5.2.0",
|
|
26
|
+
"@memberjunction/core-entities": "5.2.0",
|
|
27
|
+
"@memberjunction/communication-types": "5.2.0",
|
|
28
28
|
"rxjs": "^7.8.2"
|
|
29
29
|
},
|
|
30
30
|
"repository": {
|