@guren/server 2.9.0 → 2.10.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/DocsViewerServiceProvider-qFCaeiUv.js +77 -0
- package/dist/Hash-CzrwKeDp.js +133 -0
- package/dist/HttpException-BkS3euNG.js +161 -0
- package/dist/Job-BVT813DO.js +441 -0
- package/dist/McpServiceProvider-BoAN-UcD.js +367 -0
- package/dist/MessageSigner-Bd25ZJAV.js +93 -0
- package/dist/Random-DbIJ22KQ.js +191 -0
- package/dist/address-D0Iq-XIH.js +20 -0
- package/dist/auth/index.js +2 -122
- package/dist/auth-C7sVmYgc.js +1954 -0
- package/dist/authorization/index.js +2 -38
- package/dist/authorization-Dbcuc5ZK.js +610 -0
- package/dist/broadcasting/index.js +2 -26
- package/dist/broadcasting-Dw-FhyjN.js +1055 -0
- package/dist/cache/index.js +2 -17
- package/dist/cache/types.d.ts +4 -4
- package/dist/cache-BYnZf2FR.js +812 -0
- package/dist/client-Dd7vLcqp.js +27 -0
- package/dist/console/Output.d.ts +18 -18
- package/dist/discovery/AutoDiscovery.d.ts +8 -0
- package/dist/encryption/index.js +5 -96
- package/dist/encryption-fOWQaKqX.js +250 -0
- package/dist/endpoint-D-20PiHY.js +46 -0
- package/dist/endpoint-D6GIqOpe.js +31 -0
- package/dist/errors-quJOkkCn.js +81 -0
- package/dist/events/index.js +2 -28
- package/dist/events-aIH_bWRx.js +410 -0
- package/dist/health/index.js +2 -24
- package/dist/health-VWdWXRwh.js +345 -0
- package/dist/hot-disposables-WsTX2ySN.js +281 -0
- package/dist/hot-reload/testing.d.ts +1 -1
- package/dist/http/Application.d.ts +0 -1
- package/dist/i18n/index.js +2 -30
- package/dist/i18n-mcWi7LCd.js +617 -0
- package/dist/index.js +7496 -7281
- package/dist/lambda/index.js +114 -71
- package/dist/logging/index.js +2 -24
- package/dist/logging-CPaRMuPP.js +518 -0
- package/dist/loopback-guard-D0Kc7-xc.js +224 -0
- package/dist/mail/index.js +2 -27
- package/dist/mail-DfEfSQEr.js +699 -0
- package/dist/mcp/index.js +2 -10
- package/dist/notifications/index.js +2 -33
- package/dist/notifications-BGqmUNZ8.js +759 -0
- package/dist/queue/index.js +3 -42
- package/dist/queue-CXrRK85G.js +948 -0
- package/dist/redis/index.js +672 -642
- package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
- package/dist/runtime/index.js +474 -604
- package/dist/scheduling/index.js +2 -29
- package/dist/scheduling-CPiEdtQd.js +839 -0
- package/dist/storage/index.js +2 -15
- package/dist/storage-tWHMv2H_.js +841 -0
- package/dist/support/expiry.js +69 -12
- package/dist/trim-slashes-ZJ_VsY5s.js +25 -0
- package/dist/vite/index.js +118 -181
- package/dist/vite/plugin.d.ts +1 -1
- package/dist/vite-dev-server-CpCUqmiC.js +131 -0
- package/package.json +6 -5
- package/dist/DocsViewerServiceProvider-RUYDFBMW.js +0 -75
- package/dist/McpServiceProvider-SL7DUZ4G.js +0 -8
- package/dist/chunk-2SC566CD.js +0 -376
- package/dist/chunk-2V3YID4H.js +0 -20
- package/dist/chunk-3KV5X2FL.js +0 -31
- package/dist/chunk-63SKW5PS.js +0 -457
- package/dist/chunk-6J6B5KOB.js +0 -425
- package/dist/chunk-A5JJ7XTH.js +0 -24
- package/dist/chunk-AZOYI43J.js +0 -18
- package/dist/chunk-B5E5VBDI.js +0 -115
- package/dist/chunk-DAQKYKLH.js +0 -182
- package/dist/chunk-DRNPVBAA.js +0 -1002
- package/dist/chunk-EI5GYMAT.js +0 -853
- package/dist/chunk-H3B23O6T.js +0 -271
- package/dist/chunk-HPET6XY5.js +0 -853
- package/dist/chunk-HR2KZ3WL.js +0 -616
- package/dist/chunk-IOTWFHZU.js +0 -558
- package/dist/chunk-K6CUBJX6.js +0 -642
- package/dist/chunk-KTNJG7R7.js +0 -911
- package/dist/chunk-LAJQCSSF.js +0 -355
- package/dist/chunk-LHR6OHI7.js +0 -189
- package/dist/chunk-N7PLD7MB.js +0 -729
- package/dist/chunk-O5XIXGYM.js +0 -274
- package/dist/chunk-Q2P6YLRR.js +0 -24
- package/dist/chunk-QQKTH5KX.js +0 -114
- package/dist/chunk-SFQP6O52.js +0 -126
- package/dist/chunk-SQR7277M.js +0 -911
- package/dist/chunk-T2FFUU6J.js +0 -484
- package/dist/chunk-TVV5YNEC.js +0 -1842
- package/dist/chunk-UY3AZSYL.js +0 -14
- package/dist/chunk-V4DNC2ER.js +0 -114
- package/dist/chunk-YSOJKAAU.js +0 -427
- package/dist/errors-PUNHAFL2.js +0 -34
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
import { o as registerJob, t as Job } from "./Job-BVT813DO.js";
|
|
2
|
+
import { t as parseMailAddress } from "./address-D0Iq-XIH.js";
|
|
3
|
+
//#region src/notifications/Notification.ts
|
|
4
|
+
/**
|
|
5
|
+
* Base notification class.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* class OrderShipped extends Notification {
|
|
10
|
+
* static shouldQueue = true
|
|
11
|
+
* static queue = 'notifications'
|
|
12
|
+
*
|
|
13
|
+
* constructor(private order: Order) {
|
|
14
|
+
* super()
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* via(notifiable: Notifiable): string[] {
|
|
18
|
+
* return ['mail', 'database']
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* toMail(notifiable: Notifiable): NotificationMailMessage {
|
|
22
|
+
* return {
|
|
23
|
+
* subject: `Order #${this.order.id} shipped`,
|
|
24
|
+
* html: '<p>Your order is on its way!</p>',
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* toDatabase(notifiable: Notifiable): Record<string, unknown> {
|
|
29
|
+
* return {
|
|
30
|
+
* orderId: this.order.id,
|
|
31
|
+
* message: 'Your order has shipped',
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
var Notification = class {
|
|
38
|
+
/**
|
|
39
|
+
* Whether this notification should be queued.
|
|
40
|
+
*/
|
|
41
|
+
static shouldQueue = false;
|
|
42
|
+
/**
|
|
43
|
+
* The queue name for this notification.
|
|
44
|
+
*/
|
|
45
|
+
static queue;
|
|
46
|
+
/**
|
|
47
|
+
* Delay in milliseconds before sending.
|
|
48
|
+
*/
|
|
49
|
+
static delay;
|
|
50
|
+
/**
|
|
51
|
+
* Unique notification ID.
|
|
52
|
+
*/
|
|
53
|
+
id;
|
|
54
|
+
/**
|
|
55
|
+
* When the notification was created.
|
|
56
|
+
*/
|
|
57
|
+
createdAt;
|
|
58
|
+
constructor() {
|
|
59
|
+
this.id = this.generateId();
|
|
60
|
+
this.createdAt = /* @__PURE__ */ new Date();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the notification type (default: class name).
|
|
64
|
+
*/
|
|
65
|
+
get type() {
|
|
66
|
+
return this.constructor.name;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Check if the notification should be sent to the given notifiable.
|
|
70
|
+
* Override to add custom logic.
|
|
71
|
+
*/
|
|
72
|
+
shouldSend(_notifiable) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Generate a unique notification ID.
|
|
77
|
+
*/
|
|
78
|
+
generateId() {
|
|
79
|
+
return `notif_${Date.now().toString(36)}${Math.random().toString(36).substring(2, 10)}`;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get static queue configuration.
|
|
83
|
+
*/
|
|
84
|
+
static getQueueConfig() {
|
|
85
|
+
return {
|
|
86
|
+
shouldQueue: this.shouldQueue,
|
|
87
|
+
queue: this.queue,
|
|
88
|
+
delay: this.delay
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/notifications/registry.ts
|
|
94
|
+
const notificationRegistry = /* @__PURE__ */ new Map();
|
|
95
|
+
/**
|
|
96
|
+
* Resolve the registry key for a class without constructing it.
|
|
97
|
+
*
|
|
98
|
+
* Reads the `type` getter off the prototype so a class overriding it registers
|
|
99
|
+
* under the same key the dispatched payload carries.
|
|
100
|
+
*/
|
|
101
|
+
function resolveRegistryKey(notificationClass) {
|
|
102
|
+
try {
|
|
103
|
+
const { type } = Object.create(notificationClass.prototype);
|
|
104
|
+
if (typeof type === "string" && type.length > 0) return type;
|
|
105
|
+
} catch {}
|
|
106
|
+
return notificationClass.name;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Register a notification class so queued instances can be rebuilt.
|
|
110
|
+
*
|
|
111
|
+
* Notifications are registered automatically when they are queued, which
|
|
112
|
+
* covers delivery by a worker running in the same process. A worker running
|
|
113
|
+
* in a separate process must register them explicitly at boot.
|
|
114
|
+
*
|
|
115
|
+
* Type names must be unique within an application: as with the job registry,
|
|
116
|
+
* registering a second class under an existing type replaces the first.
|
|
117
|
+
*
|
|
118
|
+
* @param notificationClass - The notification class to register
|
|
119
|
+
* @param type - Registry key, defaulting to the class's own `type` value
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* registerNotification(OrderShipped)
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
function registerNotification(notificationClass, type) {
|
|
127
|
+
notificationRegistry.set(type ?? resolveRegistryKey(notificationClass), notificationClass);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get a registered notification class by type.
|
|
131
|
+
*/
|
|
132
|
+
function getNotification(type) {
|
|
133
|
+
return notificationRegistry.get(type);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Clear all registered notifications (for testing).
|
|
137
|
+
*/
|
|
138
|
+
function clearNotificationRegistry() {
|
|
139
|
+
notificationRegistry.clear();
|
|
140
|
+
}
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/notifications/notifiable-type.ts
|
|
143
|
+
/**
|
|
144
|
+
* Resolve the stable type name of a notifiable.
|
|
145
|
+
*
|
|
146
|
+
* Channels that record which entity a notification belongs to should use this
|
|
147
|
+
* rather than reading `constructor.name` directly: a notifiable rebuilt from a
|
|
148
|
+
* queue payload carries its original name in `notifiableType`, and its
|
|
149
|
+
* constructor is a plain object.
|
|
150
|
+
*/
|
|
151
|
+
function resolveNotifiableType(notifiable) {
|
|
152
|
+
return notifiable.notifiableType ?? notifiable.constructor?.name ?? "Unknown";
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/notifications/NotificationManager.ts
|
|
156
|
+
/**
|
|
157
|
+
* Notification manager for sending notifications through multiple channels.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const notifications = new NotificationManager()
|
|
162
|
+
*
|
|
163
|
+
* notifications
|
|
164
|
+
* .registerChannel('mail', new MailChannel(mailManager))
|
|
165
|
+
* .registerChannel('database', new DatabaseChannel())
|
|
166
|
+
* .registerChannel('slack', new SlackChannel(webhookUrl))
|
|
167
|
+
*
|
|
168
|
+
* // Send notification
|
|
169
|
+
* await notifications.send(user, new OrderShipped(order))
|
|
170
|
+
*
|
|
171
|
+
* // Send to multiple users
|
|
172
|
+
* await notifications.sendToMany(admins, new NewUserRegistered(user))
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
var NotificationManager = class {
|
|
176
|
+
channels = /* @__PURE__ */ new Map();
|
|
177
|
+
channelFactories = /* @__PURE__ */ new Map();
|
|
178
|
+
resolvedChannels = /* @__PURE__ */ new Map();
|
|
179
|
+
constructor(options = {}) {
|
|
180
|
+
if (options.channels) for (const [name, channel] of Object.entries(options.channels)) this.registerChannel(name, channel);
|
|
181
|
+
if (options.channelFactories) for (const [name, factory] of Object.entries(options.channelFactories)) this.registerChannelFactory(name, factory);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Register a notification channel.
|
|
185
|
+
*/
|
|
186
|
+
registerChannel(name, channel) {
|
|
187
|
+
this.channels.set(name, channel);
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Register a notification channel factory.
|
|
192
|
+
*/
|
|
193
|
+
registerChannelFactory(name, factory) {
|
|
194
|
+
this.channelFactories.set(name, factory);
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get a notification channel by name.
|
|
199
|
+
*/
|
|
200
|
+
channel(name) {
|
|
201
|
+
const resolved = this.resolvedChannels.get(name);
|
|
202
|
+
if (resolved) return resolved;
|
|
203
|
+
const channel = this.channels.get(name);
|
|
204
|
+
if (channel) return channel;
|
|
205
|
+
const factory = this.channelFactories.get(name);
|
|
206
|
+
if (factory) {
|
|
207
|
+
const created = factory({});
|
|
208
|
+
this.resolvedChannels.set(name, created);
|
|
209
|
+
return created;
|
|
210
|
+
}
|
|
211
|
+
throw new Error(`Notification channel "${name}" not found`);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Check if a channel is registered.
|
|
215
|
+
*/
|
|
216
|
+
hasChannel(name) {
|
|
217
|
+
return this.channels.has(name) || this.channelFactories.has(name) || this.resolvedChannels.has(name);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Get all registered channel names.
|
|
221
|
+
*/
|
|
222
|
+
getChannelNames() {
|
|
223
|
+
const names = /* @__PURE__ */ new Set([
|
|
224
|
+
...this.channels.keys(),
|
|
225
|
+
...this.channelFactories.keys(),
|
|
226
|
+
...this.resolvedChannels.keys()
|
|
227
|
+
]);
|
|
228
|
+
return Array.from(names);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Send a notification to a notifiable entity.
|
|
232
|
+
* Respects queue configuration on the notification class.
|
|
233
|
+
*/
|
|
234
|
+
async send(notifiable, notification) {
|
|
235
|
+
const queueConfig = notification.constructor.getQueueConfig?.() ?? { shouldQueue: false };
|
|
236
|
+
if (queueConfig.shouldQueue) await this.queue(notifiable, notification, queueConfig);
|
|
237
|
+
else await this.sendNow(notifiable, notification);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Send a notification immediately (bypasses queue).
|
|
241
|
+
*/
|
|
242
|
+
async sendNow(notifiable, notification) {
|
|
243
|
+
if (!await notification.shouldSend(notifiable)) return;
|
|
244
|
+
const viaChannels = notification.via(notifiable);
|
|
245
|
+
await Promise.all(viaChannels.map(async (channelName) => {
|
|
246
|
+
try {
|
|
247
|
+
await this.channel(channelName).send(notifiable, notification);
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.error(`Failed to send notification via ${channelName}:`, error);
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}));
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Queue a notification for later delivery.
|
|
256
|
+
*/
|
|
257
|
+
async queue(notifiable, notification, queueConfig) {
|
|
258
|
+
const job = this.registerQueueJob();
|
|
259
|
+
registerNotification(notification.constructor, notification.type);
|
|
260
|
+
const payload = {
|
|
261
|
+
notifiableData: this.serializeNotifiable(notifiable, notification.via(notifiable)),
|
|
262
|
+
notificationData: this.serializeNotification(notification),
|
|
263
|
+
notificationType: notification.type
|
|
264
|
+
};
|
|
265
|
+
if (queueConfig.delay) await job.dispatchAfter(queueConfig.delay, payload, { queue: queueConfig.queue });
|
|
266
|
+
else await job.dispatch(payload, { queue: queueConfig.queue });
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Send notification to multiple notifiables.
|
|
270
|
+
*/
|
|
271
|
+
async sendToMany(notifiables, notification) {
|
|
272
|
+
await Promise.all(notifiables.map((notifiable) => this.send(notifiable, notification)));
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Send notification immediately to multiple notifiables.
|
|
276
|
+
*/
|
|
277
|
+
async sendNowToMany(notifiables, notification) {
|
|
278
|
+
await Promise.all(notifiables.map((notifiable) => this.sendNow(notifiable, notification)));
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Serialize notifiable for queue storage.
|
|
282
|
+
*
|
|
283
|
+
* Routing is resolved here rather than in the worker: `routeNotificationFor`
|
|
284
|
+
* is arbitrary user code — often a closure on an object literal — and cannot
|
|
285
|
+
* be reconstructed from a payload.
|
|
286
|
+
*
|
|
287
|
+
* @param channels - Channels to resolve routes for
|
|
288
|
+
*/
|
|
289
|
+
serializeNotifiable(notifiable, channels = []) {
|
|
290
|
+
const routes = {};
|
|
291
|
+
for (const channel of channels) routes[channel] = notifiable.routeNotificationFor(channel);
|
|
292
|
+
return {
|
|
293
|
+
type: resolveNotifiableType(notifiable),
|
|
294
|
+
routes,
|
|
295
|
+
data: { ...notifiable }
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Serialize notification for queue storage.
|
|
300
|
+
*
|
|
301
|
+
* Only own enumerable properties are captured. Behaviour is restored by
|
|
302
|
+
* rebuilding the registered class in the job handler, not by serializing it.
|
|
303
|
+
*/
|
|
304
|
+
serializeNotification(notification) {
|
|
305
|
+
return {
|
|
306
|
+
id: notification.id,
|
|
307
|
+
createdAt: notification.createdAt.toISOString(),
|
|
308
|
+
data: { ...notification }
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Bind this manager to the queued-notification job and register it.
|
|
313
|
+
*
|
|
314
|
+
* Call this during boot in a process that runs a worker but may never send
|
|
315
|
+
* a notification itself: without it, the worker cannot resolve the job.
|
|
316
|
+
*/
|
|
317
|
+
registerQueueJob() {
|
|
318
|
+
SendNotificationJob.notificationManager = this;
|
|
319
|
+
registerJob(SendNotificationJob);
|
|
320
|
+
return SendNotificationJob;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Job for sending queued notifications.
|
|
325
|
+
*/
|
|
326
|
+
var SendNotificationJob = class extends Job {
|
|
327
|
+
static jobName = "SendNotificationJob";
|
|
328
|
+
static queue = "notifications";
|
|
329
|
+
static maxAttempts = 3;
|
|
330
|
+
static notificationManager = null;
|
|
331
|
+
async handle(payload) {
|
|
332
|
+
const manager = this.constructor.notificationManager;
|
|
333
|
+
if (!manager) throw new Error("NotificationManager not set on SendNotificationJob");
|
|
334
|
+
const notifiable = this.rebuildNotifiable(payload.notifiableData);
|
|
335
|
+
const notification = this.rebuildNotification(payload.notificationType, payload.notificationData);
|
|
336
|
+
await manager.sendNow(notifiable, notification);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Rebuild the notifiable from its serialized data.
|
|
340
|
+
*/
|
|
341
|
+
rebuildNotifiable(serialized) {
|
|
342
|
+
const { data, routes } = serialized;
|
|
343
|
+
return {
|
|
344
|
+
...data,
|
|
345
|
+
notifiableType: serialized.type,
|
|
346
|
+
routeNotificationFor(channel) {
|
|
347
|
+
if (routes && channel in routes) return routes[channel] ?? null;
|
|
348
|
+
const value = data[`${channel}Route`];
|
|
349
|
+
if (typeof value === "string") return value;
|
|
350
|
+
if (channel === "mail") {
|
|
351
|
+
const email = data["email"];
|
|
352
|
+
return typeof email === "string" ? email : null;
|
|
353
|
+
}
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Rebuild a real notification instance from its serialized data.
|
|
360
|
+
*
|
|
361
|
+
* The class is looked up in the notification registry and instantiated via
|
|
362
|
+
* its prototype, so prototype methods (`via`, `toMail`, `toDatabase`,
|
|
363
|
+
* `toSlack`, `shouldSend`) are restored without running the constructor.
|
|
364
|
+
*/
|
|
365
|
+
rebuildNotification(type, serialized) {
|
|
366
|
+
const NotificationClass = getNotification(type);
|
|
367
|
+
if (!NotificationClass) throw new Error(`Notification type "${type}" is not registered. Call registerNotification(${type}) before running the queue worker.`);
|
|
368
|
+
return Object.assign(Object.create(NotificationClass.prototype), serialized.data, {
|
|
369
|
+
id: serialized.id,
|
|
370
|
+
createdAt: new Date(serialized.createdAt)
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
let globalNotificationManager = null;
|
|
375
|
+
/**
|
|
376
|
+
* Set the global notification manager.
|
|
377
|
+
*/
|
|
378
|
+
function setNotificationManager(manager) {
|
|
379
|
+
globalNotificationManager = manager;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Get the global notification manager.
|
|
383
|
+
*/
|
|
384
|
+
function getNotificationManager() {
|
|
385
|
+
if (!globalNotificationManager) throw new Error("NotificationManager not initialized. Call setNotificationManager() first.");
|
|
386
|
+
return globalNotificationManager;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Create a notification manager.
|
|
390
|
+
*/
|
|
391
|
+
function createNotificationManager(options) {
|
|
392
|
+
return new NotificationManager(options);
|
|
393
|
+
}
|
|
394
|
+
//#endregion
|
|
395
|
+
//#region src/notifications/channels/MailChannel.ts
|
|
396
|
+
function parseAddress(input) {
|
|
397
|
+
if (!input) return;
|
|
398
|
+
return parseMailAddress(input);
|
|
399
|
+
}
|
|
400
|
+
function parseAddressList(input) {
|
|
401
|
+
if (!input) return;
|
|
402
|
+
const parsed = (Array.isArray(input) ? input : [input]).map((item) => parseAddress(item)).filter((item) => Boolean(item));
|
|
403
|
+
return parsed.length > 0 ? parsed : void 0;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Mail notification channel.
|
|
407
|
+
*
|
|
408
|
+
* Sends notifications via email using the MailManager.
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* const mailChannel = new MailChannel(mailManager)
|
|
413
|
+
* notifications.registerChannel('mail', mailChannel)
|
|
414
|
+
*
|
|
415
|
+
* // In notification class:
|
|
416
|
+
* toMail(notifiable: Notifiable): NotificationMailMessage {
|
|
417
|
+
* return {
|
|
418
|
+
* subject: 'Order Shipped',
|
|
419
|
+
* html: '<p>Your order has shipped!</p>',
|
|
420
|
+
* }
|
|
421
|
+
* }
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
var MailChannel = class {
|
|
425
|
+
mailManager;
|
|
426
|
+
options;
|
|
427
|
+
name = "mail";
|
|
428
|
+
constructor(mailManager, options = {}) {
|
|
429
|
+
this.mailManager = mailManager;
|
|
430
|
+
this.options = options;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Send the notification via mail.
|
|
434
|
+
*/
|
|
435
|
+
async send(notifiable, notification) {
|
|
436
|
+
const message = notification.toMail?.(notifiable);
|
|
437
|
+
if (!message) return;
|
|
438
|
+
const to = notifiable.routeNotificationFor("mail");
|
|
439
|
+
if (!to) return;
|
|
440
|
+
const transport = this.mailManager.transport(this.options.transport);
|
|
441
|
+
const toAddresses = parseAddressList(to);
|
|
442
|
+
if (!toAddresses || toAddresses.length === 0) return;
|
|
443
|
+
await transport.send({
|
|
444
|
+
to: toAddresses,
|
|
445
|
+
from: parseAddress(message.from ?? this.options.from),
|
|
446
|
+
replyTo: parseAddress(message.replyTo),
|
|
447
|
+
cc: parseAddressList(message.cc),
|
|
448
|
+
bcc: parseAddressList(message.bcc),
|
|
449
|
+
subject: message.subject,
|
|
450
|
+
html: message.html,
|
|
451
|
+
text: message.text,
|
|
452
|
+
attachments: message.attachments?.map((a) => ({
|
|
453
|
+
filename: a.filename,
|
|
454
|
+
content: a.content,
|
|
455
|
+
path: a.path,
|
|
456
|
+
contentType: a.contentType
|
|
457
|
+
}))
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
//#endregion
|
|
462
|
+
//#region src/notifications/channels/DatabaseChannel.ts
|
|
463
|
+
/**
|
|
464
|
+
* Database notification channel.
|
|
465
|
+
*
|
|
466
|
+
* Stores notifications in the database for in-app notifications.
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* ```typescript
|
|
470
|
+
* const dbChannel = new DatabaseChannel({
|
|
471
|
+
* store: async (notifiable, notification) => {
|
|
472
|
+
* await db.insert(notifications).values(notification)
|
|
473
|
+
* },
|
|
474
|
+
* })
|
|
475
|
+
* notifications.registerChannel('database', dbChannel)
|
|
476
|
+
*
|
|
477
|
+
* // In notification class:
|
|
478
|
+
* toDatabase(notifiable: Notifiable): Record<string, unknown> {
|
|
479
|
+
* return {
|
|
480
|
+
* orderId: this.order.id,
|
|
481
|
+
* message: 'Your order has shipped',
|
|
482
|
+
* }
|
|
483
|
+
* }
|
|
484
|
+
* ```
|
|
485
|
+
*/
|
|
486
|
+
var DatabaseChannel = class {
|
|
487
|
+
options;
|
|
488
|
+
name = "database";
|
|
489
|
+
stored = [];
|
|
490
|
+
constructor(options = {}) {
|
|
491
|
+
this.options = options;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Send the notification to the database.
|
|
495
|
+
*/
|
|
496
|
+
async send(notifiable, notification) {
|
|
497
|
+
const data = notification.toDatabase?.(notifiable);
|
|
498
|
+
if (!data) return;
|
|
499
|
+
const record = {
|
|
500
|
+
id: notification.id,
|
|
501
|
+
type: notification.type,
|
|
502
|
+
notifiableId: this.getNotifiableId(notifiable),
|
|
503
|
+
notifiableType: this.getNotifiableType(notifiable),
|
|
504
|
+
data,
|
|
505
|
+
readAt: null,
|
|
506
|
+
createdAt: notification.createdAt
|
|
507
|
+
};
|
|
508
|
+
if (this.options.store) await this.options.store(notifiable, record);
|
|
509
|
+
else {
|
|
510
|
+
this.stored.push(record);
|
|
511
|
+
if (notifiable.notifications) notifiable.notifications.push(record);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Get the notifiable ID.
|
|
516
|
+
*/
|
|
517
|
+
getNotifiableId(notifiable) {
|
|
518
|
+
const withId = notifiable;
|
|
519
|
+
if (withId.id !== void 0) return withId.id;
|
|
520
|
+
return String(notifiable);
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Get the notifiable type.
|
|
524
|
+
*/
|
|
525
|
+
getNotifiableType(notifiable) {
|
|
526
|
+
return resolveNotifiableType(notifiable);
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Get all stored notifications (for testing).
|
|
530
|
+
*/
|
|
531
|
+
getStored() {
|
|
532
|
+
return [...this.stored];
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Get stored notifications for a notifiable.
|
|
536
|
+
*/
|
|
537
|
+
getStoredFor(notifiable) {
|
|
538
|
+
const id = this.getNotifiableId(notifiable);
|
|
539
|
+
const type = this.getNotifiableType(notifiable);
|
|
540
|
+
return this.stored.filter((n) => n.notifiableId === id && n.notifiableType === type);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Clear stored notifications (for testing).
|
|
544
|
+
*/
|
|
545
|
+
clear() {
|
|
546
|
+
this.stored = [];
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region src/notifications/channels/SlackChannel.ts
|
|
551
|
+
/**
|
|
552
|
+
* Slack notification channel.
|
|
553
|
+
*
|
|
554
|
+
* Sends notifications to Slack via incoming webhooks.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```typescript
|
|
558
|
+
* const slackChannel = new SlackChannel('https://hooks.slack.com/services/...')
|
|
559
|
+
* notifications.registerChannel('slack', slackChannel)
|
|
560
|
+
*
|
|
561
|
+
* // In notification class:
|
|
562
|
+
* toSlack(notifiable: Notifiable): SlackMessage {
|
|
563
|
+
* return {
|
|
564
|
+
* text: `Order #${this.order.id} has been shipped!`,
|
|
565
|
+
* blocks: [
|
|
566
|
+
* {
|
|
567
|
+
* type: 'section',
|
|
568
|
+
* text: {
|
|
569
|
+
* type: 'mrkdwn',
|
|
570
|
+
* text: `*Order Shipped*\nOrder #${this.order.id} is on its way!`,
|
|
571
|
+
* },
|
|
572
|
+
* },
|
|
573
|
+
* ],
|
|
574
|
+
* }
|
|
575
|
+
* }
|
|
576
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
var SlackChannel = class {
|
|
579
|
+
webhookUrl;
|
|
580
|
+
options;
|
|
581
|
+
name = "slack";
|
|
582
|
+
constructor(webhookUrl, options = {}) {
|
|
583
|
+
this.webhookUrl = webhookUrl;
|
|
584
|
+
this.options = options;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Send the notification to Slack.
|
|
588
|
+
*/
|
|
589
|
+
async send(notifiable, notification) {
|
|
590
|
+
const message = notification.toSlack?.(notifiable);
|
|
591
|
+
if (!message) return;
|
|
592
|
+
const webhookUrl = notifiable.routeNotificationFor("slack") ?? this.webhookUrl;
|
|
593
|
+
if (!webhookUrl) return;
|
|
594
|
+
const payload = this.buildPayload(message);
|
|
595
|
+
const response = await fetch(webhookUrl, {
|
|
596
|
+
method: "POST",
|
|
597
|
+
headers: { "Content-Type": "application/json" },
|
|
598
|
+
body: JSON.stringify(payload)
|
|
599
|
+
});
|
|
600
|
+
if (!response.ok) {
|
|
601
|
+
const text = await response.text();
|
|
602
|
+
throw new Error(`Slack webhook failed: ${response.status} ${text}`);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Build the Slack webhook payload.
|
|
607
|
+
*/
|
|
608
|
+
buildPayload(message) {
|
|
609
|
+
const payload = {};
|
|
610
|
+
if (message.text) payload.text = message.text;
|
|
611
|
+
if (message.blocks && message.blocks.length > 0) payload.blocks = message.blocks;
|
|
612
|
+
if (message.attachments && message.attachments.length > 0) payload.attachments = message.attachments;
|
|
613
|
+
if (message.channel) payload.channel = message.channel;
|
|
614
|
+
if (message.username ?? this.options.username) payload.username = message.username ?? this.options.username;
|
|
615
|
+
if (message.icon_emoji ?? this.options.iconEmoji) payload.icon_emoji = message.icon_emoji ?? this.options.iconEmoji;
|
|
616
|
+
if (message.icon_url ?? this.options.iconUrl) payload.icon_url = message.icon_url ?? this.options.iconUrl;
|
|
617
|
+
return payload;
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
//#endregion
|
|
621
|
+
//#region src/notifications/channels/MemoryChannel.ts
|
|
622
|
+
/**
|
|
623
|
+
* Memory notification channel for testing.
|
|
624
|
+
*
|
|
625
|
+
* Stores all sent notifications in memory for assertions.
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* ```typescript
|
|
629
|
+
* const memoryChannel = new MemoryChannel()
|
|
630
|
+
* notifications.registerChannel('memory', memoryChannel)
|
|
631
|
+
*
|
|
632
|
+
* await notifications.send(user, new OrderShipped(order))
|
|
633
|
+
*
|
|
634
|
+
* memoryChannel.assertSentTo(user, 'OrderShipped')
|
|
635
|
+
* memoryChannel.assertCount(1)
|
|
636
|
+
* ```
|
|
637
|
+
*/
|
|
638
|
+
var MemoryChannel = class {
|
|
639
|
+
name = "memory";
|
|
640
|
+
/**
|
|
641
|
+
* All sent notifications.
|
|
642
|
+
*/
|
|
643
|
+
sent = [];
|
|
644
|
+
/**
|
|
645
|
+
* Send (store) the notification.
|
|
646
|
+
*/
|
|
647
|
+
async send(notifiable, notification) {
|
|
648
|
+
this.sent.push({
|
|
649
|
+
notifiable,
|
|
650
|
+
notification,
|
|
651
|
+
channels: notification.via(notifiable),
|
|
652
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Assert that a notification was sent to a notifiable.
|
|
657
|
+
*/
|
|
658
|
+
assertSentTo(notifiable, notificationType) {
|
|
659
|
+
if (!this.sent.some((record) => {
|
|
660
|
+
const matchesNotifiable = record.notifiable === notifiable;
|
|
661
|
+
const matchesType = notificationType ? record.notification.type === notificationType : true;
|
|
662
|
+
return matchesNotifiable && matchesType;
|
|
663
|
+
})) {
|
|
664
|
+
const typeMsg = notificationType ? ` of type "${notificationType}"` : "";
|
|
665
|
+
throw new Error(`Expected notification${typeMsg} to be sent to notifiable`);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Assert that a notification was not sent to a notifiable.
|
|
670
|
+
*/
|
|
671
|
+
assertNotSentTo(notifiable, notificationType) {
|
|
672
|
+
if (this.sent.some((record) => {
|
|
673
|
+
const matchesNotifiable = record.notifiable === notifiable;
|
|
674
|
+
const matchesType = notificationType ? record.notification.type === notificationType : true;
|
|
675
|
+
return matchesNotifiable && matchesType;
|
|
676
|
+
})) {
|
|
677
|
+
const typeMsg = notificationType ? ` of type "${notificationType}"` : "";
|
|
678
|
+
throw new Error(`Expected notification${typeMsg} not to be sent to notifiable`);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Assert the total count of sent notifications.
|
|
683
|
+
*/
|
|
684
|
+
assertCount(count) {
|
|
685
|
+
if (this.sent.length !== count) throw new Error(`Expected ${count} notifications to be sent, but ${this.sent.length} were sent`);
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Assert that a notification type was sent.
|
|
689
|
+
*/
|
|
690
|
+
assertSent(notificationType) {
|
|
691
|
+
if (!this.sent.some((record) => record.notification.type === notificationType)) throw new Error(`Expected notification of type "${notificationType}" to be sent`);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Assert that a notification type was not sent.
|
|
695
|
+
*/
|
|
696
|
+
assertNotSent(notificationType) {
|
|
697
|
+
if (this.sent.some((record) => record.notification.type === notificationType)) throw new Error(`Expected notification of type "${notificationType}" not to be sent`);
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Assert that no notifications were sent.
|
|
701
|
+
*/
|
|
702
|
+
assertNothingSent() {
|
|
703
|
+
if (this.sent.length > 0) throw new Error(`Expected no notifications to be sent, but ${this.sent.length} were sent`);
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Get notifications sent to a specific notifiable.
|
|
707
|
+
*/
|
|
708
|
+
getSentTo(notifiable) {
|
|
709
|
+
return this.sent.filter((record) => record.notifiable === notifiable);
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Get notifications of a specific type.
|
|
713
|
+
*/
|
|
714
|
+
getSentOfType(notificationType) {
|
|
715
|
+
return this.sent.filter((record) => record.notification.type === notificationType);
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Check if a notification was sent to a notifiable.
|
|
719
|
+
*/
|
|
720
|
+
hasSentTo(notifiable, notificationType) {
|
|
721
|
+
return this.sent.some((record) => {
|
|
722
|
+
const matchesNotifiable = record.notifiable === notifiable;
|
|
723
|
+
const matchesType = notificationType ? record.notification.type === notificationType : true;
|
|
724
|
+
return matchesNotifiable && matchesType;
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Check if a notification type was sent.
|
|
729
|
+
*/
|
|
730
|
+
hasSent(notificationType) {
|
|
731
|
+
return this.sent.some((record) => record.notification.type === notificationType);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Get the count of sent notifications.
|
|
735
|
+
*/
|
|
736
|
+
count() {
|
|
737
|
+
return this.sent.length;
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Get the last sent notification.
|
|
741
|
+
*/
|
|
742
|
+
last() {
|
|
743
|
+
return this.sent[this.sent.length - 1];
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Get the first sent notification.
|
|
747
|
+
*/
|
|
748
|
+
first() {
|
|
749
|
+
return this.sent[0];
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Clear all sent notifications.
|
|
753
|
+
*/
|
|
754
|
+
clear() {
|
|
755
|
+
this.sent = [];
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
//#endregion
|
|
759
|
+
export { NotificationManager as a, setNotificationManager as c, getNotification as d, registerNotification as f, MailChannel as i, resolveNotifiableType as l, SlackChannel as n, createNotificationManager as o, Notification as p, DatabaseChannel as r, getNotificationManager as s, MemoryChannel as t, clearNotificationRegistry as u };
|