@gravito/flare 1.0.0-alpha.6 → 1.0.0-beta.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.
- package/dist/index.cjs +174 -71
- package/dist/index.d.cts +383 -0
- package/dist/index.d.ts +383 -0
- package/dist/{index.mjs → index.js} +148 -47
- package/package.json +6 -5
package/dist/index.cjs
CHANGED
|
@@ -1,48 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
6
|
-
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
8
|
-
if (entry)
|
|
9
|
-
return entry;
|
|
10
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
13
|
-
get: () => from[key],
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
}));
|
|
16
|
-
__moduleCache.set(from, entry);
|
|
17
|
-
return entry;
|
|
18
|
-
};
|
|
19
6
|
var __export = (target, all) => {
|
|
20
7
|
for (var name in all)
|
|
21
|
-
__defProp(target, name, {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
27
17
|
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
19
|
|
|
29
20
|
// src/index.ts
|
|
30
|
-
var
|
|
31
|
-
__export(
|
|
32
|
-
|
|
33
|
-
SlackChannel: () => SlackChannel,
|
|
34
|
-
OrbitFlare: () => OrbitFlare,
|
|
35
|
-
NotificationManager: () => NotificationManager,
|
|
36
|
-
Notification: () => Notification,
|
|
37
|
-
MailChannel: () => MailChannel,
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BroadcastChannel: () => BroadcastChannel,
|
|
38
24
|
DatabaseChannel: () => DatabaseChannel,
|
|
39
|
-
|
|
25
|
+
MailChannel: () => MailChannel,
|
|
26
|
+
Notification: () => Notification,
|
|
27
|
+
NotificationManager: () => NotificationManager,
|
|
28
|
+
OrbitFlare: () => OrbitFlare,
|
|
29
|
+
SlackChannel: () => SlackChannel,
|
|
30
|
+
SmsChannel: () => SmsChannel
|
|
40
31
|
});
|
|
41
|
-
module.exports = __toCommonJS(
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
33
|
|
|
43
34
|
// src/channels/BroadcastChannel.ts
|
|
44
|
-
|
|
45
|
-
broadcastService;
|
|
35
|
+
var BroadcastChannel = class {
|
|
46
36
|
constructor(broadcastService) {
|
|
47
37
|
this.broadcastService = broadcastService;
|
|
48
38
|
}
|
|
@@ -54,12 +44,16 @@ class BroadcastChannel {
|
|
|
54
44
|
const notifiableId = notifiable.getNotifiableId();
|
|
55
45
|
const notifiableType = notifiable.getNotifiableType?.() || "user";
|
|
56
46
|
const channel = `private-${notifiableType}.${notifiableId}`;
|
|
57
|
-
await this.broadcastService.broadcast(
|
|
47
|
+
await this.broadcastService.broadcast(
|
|
48
|
+
channel,
|
|
49
|
+
broadcastNotification.type,
|
|
50
|
+
broadcastNotification.data
|
|
51
|
+
);
|
|
58
52
|
}
|
|
59
|
-
}
|
|
53
|
+
};
|
|
54
|
+
|
|
60
55
|
// src/channels/DatabaseChannel.ts
|
|
61
|
-
|
|
62
|
-
dbService;
|
|
56
|
+
var DatabaseChannel = class {
|
|
63
57
|
constructor(dbService) {
|
|
64
58
|
this.dbService = dbService;
|
|
65
59
|
}
|
|
@@ -75,10 +69,10 @@ class DatabaseChannel {
|
|
|
75
69
|
data: dbNotification.data
|
|
76
70
|
});
|
|
77
71
|
}
|
|
78
|
-
}
|
|
72
|
+
};
|
|
73
|
+
|
|
79
74
|
// src/channels/MailChannel.ts
|
|
80
|
-
|
|
81
|
-
mailService;
|
|
75
|
+
var MailChannel = class {
|
|
82
76
|
constructor(mailService) {
|
|
83
77
|
this.mailService = mailService;
|
|
84
78
|
}
|
|
@@ -89,10 +83,10 @@ class MailChannel {
|
|
|
89
83
|
const message = notification.toMail(notifiable);
|
|
90
84
|
await this.mailService.send(message);
|
|
91
85
|
}
|
|
92
|
-
}
|
|
86
|
+
};
|
|
87
|
+
|
|
93
88
|
// src/channels/SlackChannel.ts
|
|
94
|
-
|
|
95
|
-
config;
|
|
89
|
+
var SlackChannel = class {
|
|
96
90
|
constructor(config) {
|
|
97
91
|
this.config = config;
|
|
98
92
|
}
|
|
@@ -118,10 +112,10 @@ class SlackChannel {
|
|
|
118
112
|
throw new Error(`Failed to send Slack notification: ${response.statusText}`);
|
|
119
113
|
}
|
|
120
114
|
}
|
|
121
|
-
}
|
|
115
|
+
};
|
|
116
|
+
|
|
122
117
|
// src/channels/SmsChannel.ts
|
|
123
|
-
|
|
124
|
-
config;
|
|
118
|
+
var SmsChannel = class {
|
|
125
119
|
constructor(config) {
|
|
126
120
|
this.config = config;
|
|
127
121
|
}
|
|
@@ -141,53 +135,89 @@ class SmsChannel {
|
|
|
141
135
|
throw new Error(`Unsupported SMS provider: ${this.config.provider}`);
|
|
142
136
|
}
|
|
143
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Send SMS via Twilio.
|
|
140
|
+
*/
|
|
144
141
|
async sendViaTwilio(message) {
|
|
145
142
|
if (!this.config.apiKey || !this.config.apiSecret) {
|
|
146
143
|
throw new Error("Twilio API key and secret are required");
|
|
147
144
|
}
|
|
148
145
|
const accountSid = this.config.apiKey;
|
|
149
146
|
const authToken = this.config.apiSecret;
|
|
150
|
-
const response = await fetch(
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
147
|
+
const response = await fetch(
|
|
148
|
+
`https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json`,
|
|
149
|
+
{
|
|
150
|
+
method: "POST",
|
|
151
|
+
headers: {
|
|
152
|
+
Authorization: `Basic ${btoa(`${accountSid}:${authToken}`)}`,
|
|
153
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
154
|
+
},
|
|
155
|
+
body: new URLSearchParams({
|
|
156
|
+
From: this.config.from || "",
|
|
157
|
+
To: message.to,
|
|
158
|
+
Body: message.message
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
162
|
if (!response.ok) {
|
|
163
163
|
const error = await response.text();
|
|
164
164
|
throw new Error(`Failed to send SMS via Twilio: ${error}`);
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Send SMS via AWS SNS.
|
|
169
|
+
*/
|
|
167
170
|
async sendViaAwsSns(_message) {
|
|
168
171
|
throw new Error("AWS SNS SMS provider not yet implemented. Please install @aws-sdk/client-sns");
|
|
169
172
|
}
|
|
170
|
-
}
|
|
173
|
+
};
|
|
174
|
+
|
|
171
175
|
// src/Notification.ts
|
|
172
|
-
|
|
176
|
+
var Notification = class {
|
|
177
|
+
/**
|
|
178
|
+
* Get mail message (optional).
|
|
179
|
+
* Implement this if the notification will be sent via the mail channel.
|
|
180
|
+
*/
|
|
173
181
|
toMail(_notifiable) {
|
|
174
182
|
throw new Error("toMail method not implemented");
|
|
175
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Get database notification (optional).
|
|
186
|
+
* Implement this if the notification will be stored via the database channel.
|
|
187
|
+
*/
|
|
176
188
|
toDatabase(_notifiable) {
|
|
177
189
|
throw new Error("toDatabase method not implemented");
|
|
178
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Get broadcast notification (optional).
|
|
193
|
+
* Implement this if the notification will be sent via the broadcast channel.
|
|
194
|
+
*/
|
|
179
195
|
toBroadcast(_notifiable) {
|
|
180
196
|
throw new Error("toBroadcast method not implemented");
|
|
181
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Get Slack message (optional).
|
|
200
|
+
* Implement this if the notification will be sent via the Slack channel.
|
|
201
|
+
*/
|
|
182
202
|
toSlack(_notifiable) {
|
|
183
203
|
throw new Error("toSlack method not implemented");
|
|
184
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Get SMS message (optional).
|
|
207
|
+
* Implement this if the notification will be sent via the SMS channel.
|
|
208
|
+
*/
|
|
185
209
|
toSms(_notifiable) {
|
|
186
210
|
throw new Error("toSms method not implemented");
|
|
187
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* Check whether this notification should be queued.
|
|
214
|
+
*/
|
|
188
215
|
shouldQueue() {
|
|
189
216
|
return "queue" in this || "connection" in this || "delay" in this;
|
|
190
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Get queue configuration.
|
|
220
|
+
*/
|
|
191
221
|
getQueueConfig() {
|
|
192
222
|
if (this.shouldQueue()) {
|
|
193
223
|
const queueable = this;
|
|
@@ -199,21 +229,50 @@ class Notification {
|
|
|
199
229
|
}
|
|
200
230
|
return {};
|
|
201
231
|
}
|
|
202
|
-
}
|
|
232
|
+
};
|
|
233
|
+
|
|
203
234
|
// src/NotificationManager.ts
|
|
204
|
-
|
|
205
|
-
core;
|
|
206
|
-
channels = new Map;
|
|
207
|
-
queueManager;
|
|
235
|
+
var NotificationManager = class {
|
|
208
236
|
constructor(core) {
|
|
209
237
|
this.core = core;
|
|
210
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Channel registry.
|
|
241
|
+
*/
|
|
242
|
+
channels = /* @__PURE__ */ new Map();
|
|
243
|
+
/**
|
|
244
|
+
* Queue manager (optional, injected by `orbit-queue`).
|
|
245
|
+
*/
|
|
246
|
+
queueManager;
|
|
247
|
+
/**
|
|
248
|
+
* Register a notification channel.
|
|
249
|
+
*
|
|
250
|
+
* @param name - The name of the channel.
|
|
251
|
+
* @param channel - The channel instance.
|
|
252
|
+
*/
|
|
211
253
|
channel(name, channel) {
|
|
212
254
|
this.channels.set(name, channel);
|
|
213
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Register the queue manager (called by `orbit-queue`).
|
|
258
|
+
*
|
|
259
|
+
* @param manager - The queue manager implementation.
|
|
260
|
+
*/
|
|
214
261
|
setQueueManager(manager) {
|
|
215
262
|
this.queueManager = manager;
|
|
216
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Send a notification.
|
|
266
|
+
*
|
|
267
|
+
* @param notifiable - The recipient of the notification.
|
|
268
|
+
* @param notification - The notification instance.
|
|
269
|
+
* @returns A promise that resolves when the notification is sent or queued.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* await notificationManager.send(user, new InvoicePaid(invoice))
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
217
276
|
async send(notifiable, notification) {
|
|
218
277
|
const channels = notification.via(notifiable);
|
|
219
278
|
if (notification.shouldQueue() && this.queueManager) {
|
|
@@ -229,11 +288,23 @@ class NotificationManager {
|
|
|
229
288
|
await this.sendNow(notifiable, notification, channels);
|
|
230
289
|
}
|
|
231
290
|
};
|
|
232
|
-
await this.queueManager.push(
|
|
291
|
+
await this.queueManager.push(
|
|
292
|
+
queueJob,
|
|
293
|
+
queueConfig.queue,
|
|
294
|
+
queueConfig.connection,
|
|
295
|
+
queueConfig.delay
|
|
296
|
+
);
|
|
233
297
|
return;
|
|
234
298
|
}
|
|
235
299
|
await this.sendNow(notifiable, notification, channels);
|
|
236
300
|
}
|
|
301
|
+
/**
|
|
302
|
+
* Send immediately (without queue).
|
|
303
|
+
*
|
|
304
|
+
* @param notifiable - The recipient.
|
|
305
|
+
* @param notification - The notification.
|
|
306
|
+
* @param channels - The list of channels to send via.
|
|
307
|
+
*/
|
|
237
308
|
async sendNow(notifiable, notification, channels) {
|
|
238
309
|
for (const channelName of channels) {
|
|
239
310
|
const channel = this.channels.get(channelName);
|
|
@@ -244,10 +315,19 @@ class NotificationManager {
|
|
|
244
315
|
try {
|
|
245
316
|
await channel.send(notification, notifiable);
|
|
246
317
|
} catch (error) {
|
|
247
|
-
this.core.logger.error(
|
|
318
|
+
this.core.logger.error(
|
|
319
|
+
`[NotificationManager] Failed to send notification via '${channelName}':`,
|
|
320
|
+
error
|
|
321
|
+
);
|
|
248
322
|
}
|
|
249
323
|
}
|
|
250
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Serialize notification (for queuing).
|
|
327
|
+
*
|
|
328
|
+
* @param notification - The notification to serialize.
|
|
329
|
+
* @returns A plain object representation of the notification.
|
|
330
|
+
*/
|
|
251
331
|
serializeNotification(notification) {
|
|
252
332
|
const data = {};
|
|
253
333
|
for (const [key, value] of Object.entries(notification)) {
|
|
@@ -257,9 +337,10 @@ class NotificationManager {
|
|
|
257
337
|
}
|
|
258
338
|
return data;
|
|
259
339
|
}
|
|
260
|
-
}
|
|
340
|
+
};
|
|
341
|
+
|
|
261
342
|
// src/OrbitFlare.ts
|
|
262
|
-
|
|
343
|
+
var OrbitFlare = class _OrbitFlare {
|
|
263
344
|
options;
|
|
264
345
|
constructor(options = {}) {
|
|
265
346
|
this.options = {
|
|
@@ -271,9 +352,20 @@ class OrbitFlare {
|
|
|
271
352
|
...options
|
|
272
353
|
};
|
|
273
354
|
}
|
|
355
|
+
/**
|
|
356
|
+
* Configure OrbitFlare.
|
|
357
|
+
*
|
|
358
|
+
* @param options - The OrbitFlare configuration options.
|
|
359
|
+
* @returns A new OrbitFlare instance.
|
|
360
|
+
*/
|
|
274
361
|
static configure(options = {}) {
|
|
275
|
-
return new
|
|
362
|
+
return new _OrbitFlare(options);
|
|
276
363
|
}
|
|
364
|
+
/**
|
|
365
|
+
* Install OrbitFlare into PlanetCore.
|
|
366
|
+
*
|
|
367
|
+
* @param core - The PlanetCore instance.
|
|
368
|
+
*/
|
|
277
369
|
async install(core) {
|
|
278
370
|
const manager = new NotificationManager(core);
|
|
279
371
|
if (this.options.enableMail) {
|
|
@@ -327,4 +419,15 @@ class OrbitFlare {
|
|
|
327
419
|
}
|
|
328
420
|
core.logger.info("[OrbitFlare] Installed");
|
|
329
421
|
}
|
|
330
|
-
}
|
|
422
|
+
};
|
|
423
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
424
|
+
0 && (module.exports = {
|
|
425
|
+
BroadcastChannel,
|
|
426
|
+
DatabaseChannel,
|
|
427
|
+
MailChannel,
|
|
428
|
+
Notification,
|
|
429
|
+
NotificationManager,
|
|
430
|
+
OrbitFlare,
|
|
431
|
+
SlackChannel,
|
|
432
|
+
SmsChannel
|
|
433
|
+
});
|