@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.d.cts
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { PlanetCore, GravitoOrbit } from 'gravito-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Notification system type definitions.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Notification channel interface.
|
|
9
|
+
*/
|
|
10
|
+
interface NotificationChannel {
|
|
11
|
+
/**
|
|
12
|
+
* Send a notification.
|
|
13
|
+
* @param notification - Notification instance
|
|
14
|
+
* @param notifiable - Recipient
|
|
15
|
+
*/
|
|
16
|
+
send(notification: Notification, notifiable: Notifiable): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Notifiable (notification recipient) interface.
|
|
20
|
+
*/
|
|
21
|
+
interface Notifiable {
|
|
22
|
+
/**
|
|
23
|
+
* Recipient identifier (usually an ID).
|
|
24
|
+
*/
|
|
25
|
+
getNotifiableId(): string | number;
|
|
26
|
+
/**
|
|
27
|
+
* Recipient type (optional, for polymorphic relations).
|
|
28
|
+
*/
|
|
29
|
+
getNotifiableType?(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Preferred channels (optional).
|
|
32
|
+
*/
|
|
33
|
+
preferredNotificationChannels?(): string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Mail message payload.
|
|
37
|
+
*/
|
|
38
|
+
interface MailMessage {
|
|
39
|
+
subject: string;
|
|
40
|
+
view?: string;
|
|
41
|
+
data?: Record<string, unknown>;
|
|
42
|
+
html?: string;
|
|
43
|
+
text?: string;
|
|
44
|
+
from?: string;
|
|
45
|
+
to?: string | string[];
|
|
46
|
+
cc?: string | string[];
|
|
47
|
+
bcc?: string | string[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Database notification payload.
|
|
51
|
+
*/
|
|
52
|
+
interface DatabaseNotification {
|
|
53
|
+
type: string;
|
|
54
|
+
data: Record<string, unknown>;
|
|
55
|
+
readAt?: Date | null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Broadcast notification payload.
|
|
59
|
+
*/
|
|
60
|
+
interface BroadcastNotification {
|
|
61
|
+
type: string;
|
|
62
|
+
data: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Slack message payload.
|
|
66
|
+
*/
|
|
67
|
+
interface SlackMessage {
|
|
68
|
+
text: string;
|
|
69
|
+
channel?: string;
|
|
70
|
+
username?: string;
|
|
71
|
+
iconEmoji?: string;
|
|
72
|
+
attachments?: Array<{
|
|
73
|
+
color?: string;
|
|
74
|
+
title?: string;
|
|
75
|
+
text?: string;
|
|
76
|
+
fields?: Array<{
|
|
77
|
+
title: string;
|
|
78
|
+
value: string;
|
|
79
|
+
short?: boolean;
|
|
80
|
+
}>;
|
|
81
|
+
}>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* SMS message payload.
|
|
85
|
+
*/
|
|
86
|
+
interface SmsMessage {
|
|
87
|
+
to: string;
|
|
88
|
+
message: string;
|
|
89
|
+
from?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Marker interface for notifications that should be queued.
|
|
94
|
+
*/
|
|
95
|
+
interface ShouldQueue {
|
|
96
|
+
/**
|
|
97
|
+
* Queue name (optional).
|
|
98
|
+
*/
|
|
99
|
+
queue?: string | undefined;
|
|
100
|
+
connection?: string | undefined;
|
|
101
|
+
delay?: number | undefined;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Base Notification class.
|
|
105
|
+
*
|
|
106
|
+
* All notifications should extend this class.
|
|
107
|
+
* Notifications can be delivered via multiple channels (mail, database, broadcast, Slack, SMS, etc.).
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* class InvoicePaid extends Notification {
|
|
112
|
+
* constructor(private invoice: Invoice) {
|
|
113
|
+
* super()
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* via(user: User): string[] {
|
|
117
|
+
* return ['mail', 'database']
|
|
118
|
+
* }
|
|
119
|
+
*
|
|
120
|
+
* toMail(user: User): MailMessage {
|
|
121
|
+
* return new MailMessage()
|
|
122
|
+
* .subject('Invoice Paid')
|
|
123
|
+
* .view('emails.invoice-paid', { invoice: this.invoice })
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* toDatabase(user: User): DatabaseNotification {
|
|
127
|
+
* return {
|
|
128
|
+
* type: 'invoice-paid',
|
|
129
|
+
* data: { invoice_id: this.invoice.id }
|
|
130
|
+
* }
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
declare abstract class Notification {
|
|
136
|
+
/**
|
|
137
|
+
* Specify which channels should be used for delivery.
|
|
138
|
+
* @param notifiable - Recipient
|
|
139
|
+
* @returns Channel names
|
|
140
|
+
*/
|
|
141
|
+
abstract via(notifiable: Notifiable): string[];
|
|
142
|
+
/**
|
|
143
|
+
* Get mail message (optional).
|
|
144
|
+
* Implement this if the notification will be sent via the mail channel.
|
|
145
|
+
*/
|
|
146
|
+
toMail?(_notifiable: Notifiable): MailMessage;
|
|
147
|
+
/**
|
|
148
|
+
* Get database notification (optional).
|
|
149
|
+
* Implement this if the notification will be stored via the database channel.
|
|
150
|
+
*/
|
|
151
|
+
toDatabase?(_notifiable: Notifiable): DatabaseNotification;
|
|
152
|
+
/**
|
|
153
|
+
* Get broadcast notification (optional).
|
|
154
|
+
* Implement this if the notification will be sent via the broadcast channel.
|
|
155
|
+
*/
|
|
156
|
+
toBroadcast?(_notifiable: Notifiable): BroadcastNotification;
|
|
157
|
+
/**
|
|
158
|
+
* Get Slack message (optional).
|
|
159
|
+
* Implement this if the notification will be sent via the Slack channel.
|
|
160
|
+
*/
|
|
161
|
+
toSlack?(_notifiable: Notifiable): SlackMessage;
|
|
162
|
+
/**
|
|
163
|
+
* Get SMS message (optional).
|
|
164
|
+
* Implement this if the notification will be sent via the SMS channel.
|
|
165
|
+
*/
|
|
166
|
+
toSms?(_notifiable: Notifiable): SmsMessage;
|
|
167
|
+
/**
|
|
168
|
+
* Check whether this notification should be queued.
|
|
169
|
+
*/
|
|
170
|
+
shouldQueue(): boolean;
|
|
171
|
+
/**
|
|
172
|
+
* Get queue configuration.
|
|
173
|
+
*/
|
|
174
|
+
getQueueConfig(): {
|
|
175
|
+
queue?: string | undefined;
|
|
176
|
+
connection?: string | undefined;
|
|
177
|
+
delay?: number | undefined;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Broadcast channel.
|
|
183
|
+
*
|
|
184
|
+
* Sends notifications via a broadcast service.
|
|
185
|
+
*/
|
|
186
|
+
declare class BroadcastChannel implements NotificationChannel {
|
|
187
|
+
private broadcastService;
|
|
188
|
+
constructor(broadcastService: {
|
|
189
|
+
broadcast(channel: string, event: string, data: Record<string, unknown>): Promise<void>;
|
|
190
|
+
});
|
|
191
|
+
send(notification: Notification, notifiable: Notifiable): Promise<void>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Database channel.
|
|
196
|
+
*
|
|
197
|
+
* Persists notifications to a database.
|
|
198
|
+
*/
|
|
199
|
+
declare class DatabaseChannel implements NotificationChannel {
|
|
200
|
+
private dbService;
|
|
201
|
+
constructor(dbService: {
|
|
202
|
+
insertNotification(data: {
|
|
203
|
+
notifiableId: string | number;
|
|
204
|
+
notifiableType: string;
|
|
205
|
+
type: string;
|
|
206
|
+
data: Record<string, unknown>;
|
|
207
|
+
}): Promise<void>;
|
|
208
|
+
});
|
|
209
|
+
send(notification: Notification, notifiable: Notifiable): Promise<void>;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Mail channel.
|
|
214
|
+
*
|
|
215
|
+
* Sends notifications via the mail service.
|
|
216
|
+
*/
|
|
217
|
+
declare class MailChannel implements NotificationChannel {
|
|
218
|
+
private mailService;
|
|
219
|
+
constructor(mailService: {
|
|
220
|
+
send(message: MailMessage): Promise<void>;
|
|
221
|
+
});
|
|
222
|
+
send(notification: Notification, notifiable: Notifiable): Promise<void>;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Slack channel configuration.
|
|
227
|
+
*/
|
|
228
|
+
interface SlackChannelConfig {
|
|
229
|
+
webhookUrl: string;
|
|
230
|
+
defaultChannel?: string;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Slack channel.
|
|
234
|
+
*
|
|
235
|
+
* Sends notifications via a Slack webhook.
|
|
236
|
+
*/
|
|
237
|
+
declare class SlackChannel implements NotificationChannel {
|
|
238
|
+
private config;
|
|
239
|
+
constructor(config: SlackChannelConfig);
|
|
240
|
+
send(notification: Notification, notifiable: Notifiable): Promise<void>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* SMS channel configuration.
|
|
245
|
+
*/
|
|
246
|
+
interface SmsChannelConfig {
|
|
247
|
+
provider: string;
|
|
248
|
+
apiKey?: string;
|
|
249
|
+
apiSecret?: string;
|
|
250
|
+
from?: string;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* SMS channel.
|
|
254
|
+
*
|
|
255
|
+
* Sends notifications via an SMS provider.
|
|
256
|
+
* Only a basic interface is provided; real implementations should be extended per provider.
|
|
257
|
+
*/
|
|
258
|
+
declare class SmsChannel implements NotificationChannel {
|
|
259
|
+
private config;
|
|
260
|
+
constructor(config: SmsChannelConfig);
|
|
261
|
+
send(notification: Notification, notifiable: Notifiable): Promise<void>;
|
|
262
|
+
/**
|
|
263
|
+
* Send SMS via Twilio.
|
|
264
|
+
*/
|
|
265
|
+
private sendViaTwilio;
|
|
266
|
+
/**
|
|
267
|
+
* Send SMS via AWS SNS.
|
|
268
|
+
*/
|
|
269
|
+
private sendViaAwsSns;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Notification manager.
|
|
274
|
+
*
|
|
275
|
+
* Responsible for managing notification channels and delivering notifications.
|
|
276
|
+
*/
|
|
277
|
+
declare class NotificationManager {
|
|
278
|
+
private core;
|
|
279
|
+
/**
|
|
280
|
+
* Channel registry.
|
|
281
|
+
*/
|
|
282
|
+
private channels;
|
|
283
|
+
/**
|
|
284
|
+
* Queue manager (optional, injected by `orbit-queue`).
|
|
285
|
+
*/
|
|
286
|
+
private queueManager?;
|
|
287
|
+
constructor(core: PlanetCore);
|
|
288
|
+
/**
|
|
289
|
+
* Register a notification channel.
|
|
290
|
+
*
|
|
291
|
+
* @param name - The name of the channel.
|
|
292
|
+
* @param channel - The channel instance.
|
|
293
|
+
*/
|
|
294
|
+
channel(name: string, channel: NotificationChannel): void;
|
|
295
|
+
/**
|
|
296
|
+
* Register the queue manager (called by `orbit-queue`).
|
|
297
|
+
*
|
|
298
|
+
* @param manager - The queue manager implementation.
|
|
299
|
+
*/
|
|
300
|
+
setQueueManager(manager: NotificationManager['queueManager']): void;
|
|
301
|
+
/**
|
|
302
|
+
* Send a notification.
|
|
303
|
+
*
|
|
304
|
+
* @param notifiable - The recipient of the notification.
|
|
305
|
+
* @param notification - The notification instance.
|
|
306
|
+
* @returns A promise that resolves when the notification is sent or queued.
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* await notificationManager.send(user, new InvoicePaid(invoice))
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
send(notifiable: Notifiable, notification: Notification): Promise<void>;
|
|
314
|
+
/**
|
|
315
|
+
* Send immediately (without queue).
|
|
316
|
+
*
|
|
317
|
+
* @param notifiable - The recipient.
|
|
318
|
+
* @param notification - The notification.
|
|
319
|
+
* @param channels - The list of channels to send via.
|
|
320
|
+
*/
|
|
321
|
+
private sendNow;
|
|
322
|
+
/**
|
|
323
|
+
* Serialize notification (for queuing).
|
|
324
|
+
*
|
|
325
|
+
* @param notification - The notification to serialize.
|
|
326
|
+
* @returns A plain object representation of the notification.
|
|
327
|
+
*/
|
|
328
|
+
private serializeNotification;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* OrbitFlare options.
|
|
333
|
+
*/
|
|
334
|
+
interface OrbitFlareOptions {
|
|
335
|
+
/**
|
|
336
|
+
* Enable mail channel.
|
|
337
|
+
*/
|
|
338
|
+
enableMail?: boolean;
|
|
339
|
+
/**
|
|
340
|
+
* Enable database channel.
|
|
341
|
+
*/
|
|
342
|
+
enableDatabase?: boolean;
|
|
343
|
+
/**
|
|
344
|
+
* Enable broadcast channel.
|
|
345
|
+
*/
|
|
346
|
+
enableBroadcast?: boolean;
|
|
347
|
+
/**
|
|
348
|
+
* Enable Slack channel.
|
|
349
|
+
*/
|
|
350
|
+
enableSlack?: boolean;
|
|
351
|
+
/**
|
|
352
|
+
* Enable SMS channel.
|
|
353
|
+
*/
|
|
354
|
+
enableSms?: boolean;
|
|
355
|
+
/**
|
|
356
|
+
* Custom channel configuration.
|
|
357
|
+
*/
|
|
358
|
+
channels?: Record<string, unknown>;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Notifications Orbit
|
|
362
|
+
*
|
|
363
|
+
* Provides notifications with multiple channels (mail, database, broadcast, Slack, SMS).
|
|
364
|
+
*/
|
|
365
|
+
declare class OrbitFlare implements GravitoOrbit {
|
|
366
|
+
private options;
|
|
367
|
+
constructor(options?: OrbitFlareOptions);
|
|
368
|
+
/**
|
|
369
|
+
* Configure OrbitFlare.
|
|
370
|
+
*
|
|
371
|
+
* @param options - The OrbitFlare configuration options.
|
|
372
|
+
* @returns A new OrbitFlare instance.
|
|
373
|
+
*/
|
|
374
|
+
static configure(options?: OrbitFlareOptions): OrbitFlare;
|
|
375
|
+
/**
|
|
376
|
+
* Install OrbitFlare into PlanetCore.
|
|
377
|
+
*
|
|
378
|
+
* @param core - The PlanetCore instance.
|
|
379
|
+
*/
|
|
380
|
+
install(core: PlanetCore): Promise<void>;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export { BroadcastChannel, type BroadcastNotification, DatabaseChannel, type DatabaseNotification, MailChannel, type MailMessage, type Notifiable, Notification, type NotificationChannel, NotificationManager, OrbitFlare, type OrbitFlareOptions, type ShouldQueue, SlackChannel, type SlackChannelConfig, type SlackMessage, SmsChannel, type SmsChannelConfig, type SmsMessage };
|