@facteurjs/core 1.0.0-beta.4 → 2.0.0-beta.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 +27 -0
- package/dist/api/handlers/{notifications.js → notifications.mjs} +39 -16
- package/dist/api/handlers/preferences.mjs +73 -0
- package/dist/api/handlers/utils.mjs +23 -0
- package/dist/api/types.d.mts +52 -0
- package/dist/api/types.mjs +1 -0
- package/dist/api.d.mts +22 -0
- package/dist/api.mjs +25 -0
- package/dist/channels/aws-sns/{channel.d.ts → channel.d.mts} +3 -3
- package/dist/channels/aws-sns/{channel.js → channel.mjs} +2 -3
- package/dist/channels/aws-sns/types.mjs +1 -0
- package/dist/channels/aws-sns.d.mts +3 -0
- package/dist/channels/aws-sns.mjs +4 -0
- package/dist/channels/discord/{channel.d.ts → channel.d.mts} +5 -5
- package/dist/channels/discord/{channel.js → channel.mjs} +1 -1
- package/dist/channels/discord/{message.d.ts → message.d.mts} +2 -2
- package/dist/channels/discord/{message.js → message.mjs} +1 -1
- package/dist/channels/discord/{types.d.ts → types.d.mts} +1 -1
- package/dist/channels/discord/types.mjs +1 -0
- package/dist/channels/discord.d.mts +3 -0
- package/dist/channels/discord.mjs +4 -0
- package/dist/channels/expo/{channel.d.ts → channel.d.mts} +5 -3
- package/dist/channels/expo/channel.mjs +69 -0
- package/dist/channels/expo/types.mjs +1 -0
- package/dist/channels/expo.d.mts +3 -0
- package/dist/channels/expo.mjs +4 -0
- package/dist/channels/fcm/{channel.d.ts → channel.d.mts} +5 -5
- package/dist/channels/fcm/channel.mjs +68 -0
- package/dist/channels/fcm/types.mjs +1 -0
- package/dist/channels/fcm.d.mts +3 -0
- package/dist/channels/fcm.mjs +4 -0
- package/dist/channels/slack/{channel.d.ts → channel.d.mts} +5 -5
- package/dist/channels/slack/{channel.js → channel.mjs} +2 -2
- package/dist/channels/slack/{message.d.ts → message.d.mts} +1 -1
- package/dist/channels/slack/{message.js → message.mjs} +1 -1
- package/dist/channels/slack/{types.d.ts → types.d.mts} +1 -1
- package/dist/channels/slack/types.mjs +1 -0
- package/dist/channels/slack.d.mts +3 -0
- package/dist/channels/slack.mjs +4 -0
- package/dist/channels/socketio/{channel.d.ts → channel.d.mts} +3 -3
- package/dist/channels/socketio/{channel.js → channel.mjs} +2 -3
- package/dist/channels/socketio/types.mjs +1 -0
- package/dist/channels/socketio.d.mts +3 -0
- package/dist/channels/socketio.mjs +4 -0
- package/dist/channels/transmit/{channel.d.ts → channel.d.mts} +3 -3
- package/dist/channels/transmit/{channel.js → channel.mjs} +2 -3
- package/dist/channels/transmit/types.mjs +1 -0
- package/dist/channels/transmit.d.mts +3 -0
- package/dist/channels/transmit.mjs +4 -0
- package/dist/channels/twilio/{channel.d.ts → channel.d.mts} +3 -3
- package/dist/channels/twilio/{channel.js → channel.mjs} +3 -6
- package/dist/channels/twilio/types.mjs +1 -0
- package/dist/channels/twilio.d.mts +4 -0
- package/dist/channels/twilio.mjs +4 -0
- package/dist/channels/webhook/{exceptions.d.ts → exceptions.d.mts} +1 -1
- package/dist/channels/webhook/{provider.d.ts → provider.d.mts} +3 -3
- package/dist/channels/webhook/{provider.js → provider.mjs} +6 -8
- package/dist/channels/webhook/types.mjs +1 -0
- package/dist/channels/webhook.d.mts +4 -0
- package/dist/channels/webhook.mjs +5 -0
- package/dist/channels/webpush/{channel.d.ts → channel.d.mts} +3 -3
- package/dist/channels/webpush/{channel.js → channel.mjs} +2 -3
- package/dist/channels/webpush/types.mjs +1 -0
- package/dist/channels/webpush.d.mts +3 -0
- package/dist/channels/webpush.mjs +4 -0
- package/dist/database/adapters/{knex.d.ts → knex.d.mts} +1 -1
- package/dist/database/adapters/{knex.js → knex.mjs} +19 -15
- package/dist/database/adapters/{kysely.d.ts → kysely.d.mts} +1 -1
- package/dist/database/adapters/{kysely.js → kysely.mjs} +2 -4
- package/dist/database/{channel.d.ts → channel.d.mts} +3 -3
- package/dist/database/{channel.js → channel.mjs} +4 -6
- package/dist/database/{database.d.ts → database.d.mts} +3 -3
- package/dist/database/{database.js → database.mjs} +15 -12
- package/dist/database/{message.d.ts → message.d.mts} +2 -2
- package/dist/database/{types.d.ts → types.d.mts} +3 -3
- package/dist/database/types.mjs +1 -0
- package/dist/database.d.mts +3 -0
- package/dist/database.mjs +4 -0
- package/dist/errors/{duplicate_notification_exception.js → duplicate_notification_exception.mjs} +1 -2
- package/dist/errors/{http_error.d.ts → http_error.d.mts} +0 -3
- package/dist/errors/{index.d.ts → index.d.mts} +1 -1
- package/dist/errors/{index.js → index.mjs} +2 -3
- package/dist/events/{events.d.ts → events.d.mts} +2 -2
- package/dist/facteur.d.mts +45 -0
- package/dist/facteur.mjs +167 -0
- package/dist/{fake.d.ts → fake.d.mts} +3 -3
- package/dist/{fake.js → fake.mjs} +2 -3
- package/dist/{index.d.ts → index.d.mts} +3 -3
- package/dist/{index.js → index.mjs} +2 -2
- package/dist/notifications/batching_sender.mjs +148 -0
- package/dist/notifications/{channel_resolver.js → channel_resolver.mjs} +40 -31
- package/dist/notifications/notification_builder.mjs +77 -0
- package/dist/notifications/{notification_discoverer.d.ts → notification_discoverer.d.mts} +1 -1
- package/dist/notifications/{notification_discoverer.js → notification_discoverer.mjs} +9 -14
- package/dist/notifications/notification_sender.mjs +356 -0
- package/dist/notifications/orchestration_sender.mjs +92 -0
- package/dist/{options.d.ts → options.d.mts} +7 -6
- package/dist/{options.js → options.mjs} +5 -4
- package/dist/types/builder.d.mts +122 -0
- package/dist/types/channel.d.mts +56 -0
- package/dist/types/{events.d.ts → events.d.mts} +1 -1
- package/dist/types/{extend.d.ts → extend.d.mts} +2 -2
- package/dist/types/{notifications.d.ts → notifications.d.mts} +3 -3
- package/dist/types/{options.d.ts → options.d.mts} +94 -11
- package/dist/types/{preferences.d.ts → preferences.d.mts} +1 -1
- package/dist/types.d.mts +9 -0
- package/dist/types.mjs +4 -0
- package/dist/utils/chunk.mjs +28 -0
- package/package.json +69 -54
- package/dist/api/handlers/preferences.js +0 -43
- package/dist/api/index.d.ts +0 -16
- package/dist/api/index.js +0 -21
- package/dist/api/types.d.ts +0 -22
- package/dist/api/types.js +0 -0
- package/dist/channels/aws-sns/index.d.ts +0 -3
- package/dist/channels/aws-sns/index.js +0 -4
- package/dist/channels/aws-sns/types.js +0 -0
- package/dist/channels/discord/index.d.ts +0 -3
- package/dist/channels/discord/index.js +0 -4
- package/dist/channels/discord/types.js +0 -0
- package/dist/channels/expo/channel.js +0 -38
- package/dist/channels/expo/index.d.ts +0 -3
- package/dist/channels/expo/index.js +0 -4
- package/dist/channels/expo/types.js +0 -0
- package/dist/channels/fcm/channel.js +0 -44
- package/dist/channels/fcm/index.d.ts +0 -3
- package/dist/channels/fcm/index.js +0 -4
- package/dist/channels/fcm/types.js +0 -0
- package/dist/channels/slack/index.d.ts +0 -3
- package/dist/channels/slack/index.js +0 -4
- package/dist/channels/slack/types.js +0 -0
- package/dist/channels/socketio/index.d.ts +0 -3
- package/dist/channels/socketio/index.js +0 -4
- package/dist/channels/socketio/types.js +0 -0
- package/dist/channels/transmit/index.d.ts +0 -3
- package/dist/channels/transmit/index.js +0 -4
- package/dist/channels/transmit/types.js +0 -0
- package/dist/channels/twilio/index.d.ts +0 -4
- package/dist/channels/twilio/index.js +0 -4
- package/dist/channels/twilio/types.js +0 -0
- package/dist/channels/webhook/index.d.ts +0 -4
- package/dist/channels/webhook/index.js +0 -5
- package/dist/channels/webhook/types.js +0 -0
- package/dist/channels/webpush/index.d.ts +0 -3
- package/dist/channels/webpush/index.js +0 -4
- package/dist/channels/webpush/types.js +0 -0
- package/dist/database/index.d.ts +0 -3
- package/dist/database/index.js +0 -4
- package/dist/database/types.js +0 -0
- package/dist/facteur.d.ts +0 -37
- package/dist/facteur.js +0 -100
- package/dist/notifications/notification_sender.js +0 -211
- package/dist/types/channel.d.ts +0 -18
- package/dist/types/index.d.ts +0 -8
- package/dist/types/index.js +0 -4
- /package/dist/channels/aws-sns/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/aws-sns/{message.js → message.mjs} +0 -0
- /package/dist/channels/aws-sns/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/expo/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/expo/{message.js → message.mjs} +0 -0
- /package/dist/channels/expo/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/fcm/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/fcm/{message.js → message.mjs} +0 -0
- /package/dist/channels/fcm/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/socketio/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/socketio/{message.js → message.mjs} +0 -0
- /package/dist/channels/socketio/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/transmit/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/transmit/{message.js → message.mjs} +0 -0
- /package/dist/channels/transmit/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/twilio/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/twilio/{message.js → message.mjs} +0 -0
- /package/dist/channels/twilio/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/webhook/{exceptions.js → exceptions.mjs} +0 -0
- /package/dist/channels/webhook/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/webhook/{message.js → message.mjs} +0 -0
- /package/dist/channels/webhook/{types.d.ts → types.d.mts} +0 -0
- /package/dist/channels/webpush/{message.d.ts → message.d.mts} +0 -0
- /package/dist/channels/webpush/{message.js → message.mjs} +0 -0
- /package/dist/channels/webpush/{types.d.ts → types.d.mts} +0 -0
- /package/dist/database/{message.js → message.mjs} +0 -0
- /package/dist/{debug.js → debug.mjs} +0 -0
- /package/dist/errors/{duplicate_notification_exception.d.ts → duplicate_notification_exception.d.mts} +0 -0
- /package/dist/errors/{http_error.js → http_error.mjs} +0 -0
- /package/dist/events/{events.js → events.mjs} +0 -0
- /package/dist/{helpers.js → helpers.mjs} +0 -0
- /package/dist/types/{channel.js → channel.mjs} +0 -0
- /package/dist/types/{notifications.js → notifications.mjs} +0 -0
- /package/dist/types/{queue.d.ts → queue.d.mts} +0 -0
|
File without changes
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { WebhookMessage } from "./message.js";
|
|
2
|
-
import { WebhookChannel, webhookChannel } from "./provider.js";
|
|
3
|
-
import { WebhookRequestException, errors } from "./exceptions.js";
|
|
4
|
-
export { WebhookChannel, WebhookMessage, WebhookRequestException, errors, webhookChannel };
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { WebhookMessage } from "./message.js";
|
|
2
|
-
import { WebhookRequestException, errors } from "./exceptions.js";
|
|
3
|
-
import { WebhookChannel, webhookChannel } from "./provider.js";
|
|
4
|
-
|
|
5
|
-
export { WebhookChannel, WebhookMessage, WebhookRequestException, errors, webhookChannel };
|
|
File without changes
|
|
File without changes
|
package/dist/database/index.d.ts
DELETED
package/dist/database/index.js
DELETED
package/dist/database/types.js
DELETED
|
File without changes
|
package/dist/facteur.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { DatabaseAdapter } from "./database/types.js";
|
|
2
|
-
import { Channel } from "./types/channel.js";
|
|
3
|
-
import { FacteurConfiguration, SendOptions } from "./types/options.js";
|
|
4
|
-
import { Notification, NotificationClass, NotificationSendResult } from "./types/notifications.js";
|
|
5
|
-
import { FacteurFake } from "./fake.js";
|
|
6
|
-
import { FacteurDatabase } from "./database/database.js";
|
|
7
|
-
|
|
8
|
-
//#region src/facteur.d.ts
|
|
9
|
-
declare function createFacteur<T extends Record<string, Channel>>(config: FacteurConfiguration<T>): Facteur<T, null>;
|
|
10
|
-
/**
|
|
11
|
-
* Main manager class for Facteur library.
|
|
12
|
-
*/
|
|
13
|
-
declare class Facteur<KnownChannels extends Record<string, Channel>, DBAdapter extends DatabaseAdapter | null = null> {
|
|
14
|
-
#private;
|
|
15
|
-
constructor(config: FacteurConfiguration<KnownChannels, DBAdapter>);
|
|
16
|
-
get db(): DBAdapter extends DatabaseAdapter ? FacteurDatabase : never;
|
|
17
|
-
get discoverer(): {
|
|
18
|
-
discoverNotifications: () => Promise<(new (...args: any[]) => Notification)[]>;
|
|
19
|
-
getNotifications: () => Promise<(new (...args: any[]) => Notification)[]>;
|
|
20
|
-
getNotificationTags: () => Promise<string[]>;
|
|
21
|
-
clearCache: () => void;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Fake the notification sending process for testing purposes
|
|
25
|
-
*/
|
|
26
|
-
fake(): FacteurFake;
|
|
27
|
-
/**
|
|
28
|
-
* Restore the original notification sending process after faking it
|
|
29
|
-
*/
|
|
30
|
-
restore(): void;
|
|
31
|
-
/**
|
|
32
|
-
* Send a notification
|
|
33
|
-
*/
|
|
34
|
-
send<TNotificationClass extends NotificationClass<any, any>>(options: SendOptions<TNotificationClass>): Promise<NotificationSendResult>;
|
|
35
|
-
}
|
|
36
|
-
//#endregion
|
|
37
|
-
export { Facteur, createFacteur };
|
package/dist/facteur.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { FacteurFake } from "./fake.js";
|
|
2
|
-
import { FacteurOptions } from "./options.js";
|
|
3
|
-
import { FacteurDatabase } from "./database/database.js";
|
|
4
|
-
import { ChannelResolver } from "./notifications/channel_resolver.js";
|
|
5
|
-
import { NotificationSender } from "./notifications/notification_sender.js";
|
|
6
|
-
import "./types/index.js";
|
|
7
|
-
import { NotificationDiscoverer } from "./notifications/notification_discoverer.js";
|
|
8
|
-
|
|
9
|
-
//#region src/facteur.ts
|
|
10
|
-
function createFacteur(config) {
|
|
11
|
-
return new Facteur(config);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Main manager class for Facteur library.
|
|
15
|
-
*/
|
|
16
|
-
var Facteur = class {
|
|
17
|
-
#sender;
|
|
18
|
-
#fake = null;
|
|
19
|
-
#db = null;
|
|
20
|
-
#discoverer;
|
|
21
|
-
#channelResolver;
|
|
22
|
-
#options;
|
|
23
|
-
constructor(config) {
|
|
24
|
-
this.#options = new FacteurOptions(config);
|
|
25
|
-
this.#discoverer = new NotificationDiscoverer({
|
|
26
|
-
searchDirectory: config.discoverer.searchDirectory,
|
|
27
|
-
fileSuffix: config.discoverer.fileSuffix
|
|
28
|
-
});
|
|
29
|
-
if (this.#options.databaseAdapter) {
|
|
30
|
-
const options = this.#options;
|
|
31
|
-
this.#db = new FacteurDatabase(options, this.#discoverer);
|
|
32
|
-
this.#channelResolver = new ChannelResolver(this.#db);
|
|
33
|
-
} else this.#channelResolver = new ChannelResolver();
|
|
34
|
-
this.#sender = new NotificationSender(this.#options.channels, this.#channelResolver, this.#options.emitter);
|
|
35
|
-
}
|
|
36
|
-
get db() {
|
|
37
|
-
if (!this.#options.databaseAdapter) throw new Error("No database adapter configured");
|
|
38
|
-
return this.#db;
|
|
39
|
-
}
|
|
40
|
-
get discoverer() {
|
|
41
|
-
return {
|
|
42
|
-
discoverNotifications: () => this.#discoverer.discoverNotifications(),
|
|
43
|
-
getNotifications: () => this.#discoverer.getNotifications(),
|
|
44
|
-
getNotificationTags: () => this.#discoverer.getAllNotificationTags(),
|
|
45
|
-
clearCache: () => this.#discoverer.clearCache()
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Fake the notification sending process for testing purposes
|
|
50
|
-
*/
|
|
51
|
-
fake() {
|
|
52
|
-
this.#fake = new FacteurFake();
|
|
53
|
-
return this.#fake;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Restore the original notification sending process after faking it
|
|
57
|
-
*/
|
|
58
|
-
restore() {
|
|
59
|
-
this.#fake = null;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Send a notification
|
|
63
|
-
*/
|
|
64
|
-
async send(options) {
|
|
65
|
-
if ("via" in options && !("to" in options)) return this.#sendToSingle(options);
|
|
66
|
-
const optionsWithTo = options;
|
|
67
|
-
const recipients = Array.isArray(optionsWithTo.to) ? optionsWithTo.to : [optionsWithTo.to];
|
|
68
|
-
const results = await Promise.all(recipients.map((recipient) => this.#sendToSingle({
|
|
69
|
-
...optionsWithTo,
|
|
70
|
-
to: recipient
|
|
71
|
-
})));
|
|
72
|
-
return {
|
|
73
|
-
success: results.reduce((sum, result) => sum + result.success, 0),
|
|
74
|
-
failed: results.reduce((sum, result) => sum + result.failed, 0),
|
|
75
|
-
results: results.flatMap((result) => result.results)
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Send notification to a single recipient
|
|
80
|
-
*/
|
|
81
|
-
async #sendToSingle(options) {
|
|
82
|
-
const notification = await this.#options.notificationResolver(options.notification, {
|
|
83
|
-
to: "to" in options ? options.to : void 0,
|
|
84
|
-
params: options.params,
|
|
85
|
-
tenantId: options.tenantId
|
|
86
|
-
});
|
|
87
|
-
await notification.beforeSend();
|
|
88
|
-
const shouldSend = await notification.shouldSend();
|
|
89
|
-
if (shouldSend === false) return {
|
|
90
|
-
success: 0,
|
|
91
|
-
failed: 0,
|
|
92
|
-
results: []
|
|
93
|
-
};
|
|
94
|
-
if (this.#fake) return this.#fake.recordSent(options);
|
|
95
|
-
return this.#sender.send(options, notification);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
//#endregion
|
|
100
|
-
export { Facteur, createFacteur };
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { errors } from "../errors/index.js";
|
|
2
|
-
import debug_default from "../debug.js";
|
|
3
|
-
import { facteurEvents } from "../events/events.js";
|
|
4
|
-
import "./channel_resolver.js";
|
|
5
|
-
import { capitalizeFirstLetter } from "../helpers.js";
|
|
6
|
-
|
|
7
|
-
//#region src/notifications/notification_sender.ts
|
|
8
|
-
/**
|
|
9
|
-
* Responsible for sending notifications and messages
|
|
10
|
-
*/
|
|
11
|
-
var NotificationSender = class {
|
|
12
|
-
constructor(channels, channelResolver, emitter) {
|
|
13
|
-
this.channels = channels;
|
|
14
|
-
this.channelResolver = channelResolver;
|
|
15
|
-
this.emitter = emitter;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Emit notification sending event
|
|
19
|
-
*/
|
|
20
|
-
#emitNotificationSending(notification, resolvedChannels) {
|
|
21
|
-
const event = facteurEvents.notificationSending({
|
|
22
|
-
notification,
|
|
23
|
-
resolvedChannels
|
|
24
|
-
});
|
|
25
|
-
this.emitter.emit(event.name, event.data);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Emit notification sent event
|
|
29
|
-
*/
|
|
30
|
-
#emitNotificationSent(notification, results) {
|
|
31
|
-
const event = facteurEvents.notificationSent({
|
|
32
|
-
notification,
|
|
33
|
-
results
|
|
34
|
-
});
|
|
35
|
-
this.emitter.emit(event.name, event.data);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Emit notification failed event
|
|
39
|
-
*/
|
|
40
|
-
#emitNotificationFailed(notification, errors$1) {
|
|
41
|
-
const event = facteurEvents.notificationFailed({
|
|
42
|
-
notification,
|
|
43
|
-
errors: errors$1
|
|
44
|
-
});
|
|
45
|
-
this.emitter.emit(event.name, event.data);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Emit message sending event
|
|
49
|
-
*/
|
|
50
|
-
#emitMessageSending(notification, channelName, message) {
|
|
51
|
-
const event = facteurEvents.messageSending({
|
|
52
|
-
notification,
|
|
53
|
-
channelName,
|
|
54
|
-
message
|
|
55
|
-
});
|
|
56
|
-
this.emitter.emit(event.name, event.data);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Emit message sent event
|
|
60
|
-
*/
|
|
61
|
-
#emitMessageSent(notification, channelName, message) {
|
|
62
|
-
const event = facteurEvents.messageSent({
|
|
63
|
-
notification,
|
|
64
|
-
channelName,
|
|
65
|
-
message
|
|
66
|
-
});
|
|
67
|
-
this.emitter.emit(event.name, event.data);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Emit message failed event
|
|
71
|
-
*/
|
|
72
|
-
#emitMessageFailed(notification, channelName, message, error) {
|
|
73
|
-
const event = facteurEvents.messageFailed({
|
|
74
|
-
notification,
|
|
75
|
-
channelName,
|
|
76
|
-
message,
|
|
77
|
-
error
|
|
78
|
-
});
|
|
79
|
-
this.emitter.emit(event.name, event.data);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Get a channel by its name
|
|
83
|
-
*/
|
|
84
|
-
#getChannel(channelName) {
|
|
85
|
-
const channel = this.channels[channelName];
|
|
86
|
-
if (!channel) throw new Error(`Channel '${channelName}' is not registered`);
|
|
87
|
-
return channel;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Send a single message through a specific channel
|
|
91
|
-
*/
|
|
92
|
-
async #sendMessage(options) {
|
|
93
|
-
const { channelName, options: sendOptions, channelConfig } = options;
|
|
94
|
-
/**
|
|
95
|
-
* First build the message content using the notification's
|
|
96
|
-
* `as<ChannelName>Message` method
|
|
97
|
-
*/
|
|
98
|
-
const channel = this.#getChannel(channelName);
|
|
99
|
-
const capitalizedChannelName = capitalizeFirstLetter(channelName);
|
|
100
|
-
const channelMethodName = `as${capitalizedChannelName}Message`;
|
|
101
|
-
const messageBuilder = options.notification[channelMethodName];
|
|
102
|
-
if (typeof messageBuilder !== "function") throw new errors.E_MISSING_MESSAGE_METHOD([capitalizedChannelName]);
|
|
103
|
-
const messageContent = messageBuilder.call(options.notification, {
|
|
104
|
-
to: sendOptions.to,
|
|
105
|
-
params: sendOptions.params,
|
|
106
|
-
tenantId: sendOptions.tenantId
|
|
107
|
-
});
|
|
108
|
-
if (!messageContent) return null;
|
|
109
|
-
/**
|
|
110
|
-
* Send the message and emit appropriate events
|
|
111
|
-
*/
|
|
112
|
-
debug_default(`Sending message via ${channelName}: %O`, messageContent);
|
|
113
|
-
this.#emitMessageSending(options.notification, channelName, messageContent);
|
|
114
|
-
try {
|
|
115
|
-
await channel.send({
|
|
116
|
-
tenantId: sendOptions.tenantId,
|
|
117
|
-
message: messageContent,
|
|
118
|
-
targets: channelConfig.target,
|
|
119
|
-
to: sendOptions.to
|
|
120
|
-
});
|
|
121
|
-
debug_default(`Message sent via ${channelName}`);
|
|
122
|
-
this.#emitMessageSent(options.notification, channelName, messageContent);
|
|
123
|
-
return {
|
|
124
|
-
channel: channelName,
|
|
125
|
-
status: "success"
|
|
126
|
-
};
|
|
127
|
-
} catch (error) {
|
|
128
|
-
this.#emitMessageFailed(options.notification, channelName, messageContent, error);
|
|
129
|
-
throw error;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Process results from sending messages and emit appropriate events
|
|
134
|
-
*/
|
|
135
|
-
async #processResults(options) {
|
|
136
|
-
const { results, throwOnError, notification } = options;
|
|
137
|
-
const channelResults = results.filter((result) => result !== null);
|
|
138
|
-
const successes = channelResults.filter((r) => r.status === "success");
|
|
139
|
-
const failures = channelResults.filter((r) => r.status === "failed");
|
|
140
|
-
/**
|
|
141
|
-
* Everything succeeded
|
|
142
|
-
*/
|
|
143
|
-
if (!failures.length) {
|
|
144
|
-
this.#emitNotificationSent(notification, channelResults);
|
|
145
|
-
await notification.afterSend();
|
|
146
|
-
return {
|
|
147
|
-
failed: failures.length,
|
|
148
|
-
success: successes.length,
|
|
149
|
-
results: channelResults
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Some channels failed
|
|
154
|
-
*/
|
|
155
|
-
const failureReasons = failures.map((r) => r.error);
|
|
156
|
-
this.#emitNotificationFailed(notification, failureReasons);
|
|
157
|
-
await notification.afterSend();
|
|
158
|
-
if (throwOnError !== false) throw new errors.E_SEND_NOTIFICATION_FAILED(failureReasons);
|
|
159
|
-
return {
|
|
160
|
-
failed: failures.length,
|
|
161
|
-
success: successes.length,
|
|
162
|
-
results: channelResults
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Send a notification through all resolved channels
|
|
167
|
-
*/
|
|
168
|
-
async send(options, notification) {
|
|
169
|
-
const { via, params, tenantId } = options;
|
|
170
|
-
const to = "to" in options ? options.to : void 0;
|
|
171
|
-
const resolvedChannels = await this.channelResolver.resolveChannels({
|
|
172
|
-
to,
|
|
173
|
-
params,
|
|
174
|
-
tenantId,
|
|
175
|
-
notification: options.notification,
|
|
176
|
-
...via ? { via } : {}
|
|
177
|
-
});
|
|
178
|
-
debug_default(`Resolved channels: %O`, resolvedChannels);
|
|
179
|
-
this.#emitNotificationSending(notification, resolvedChannels);
|
|
180
|
-
/**
|
|
181
|
-
* Send messages for each resolved channel
|
|
182
|
-
*/
|
|
183
|
-
const promises = Object.entries(resolvedChannels).map(async ([name, config]) => {
|
|
184
|
-
if (!config.shouldSend || !config.target) return null;
|
|
185
|
-
return await this.#sendMessage({
|
|
186
|
-
notification,
|
|
187
|
-
channelConfig: config,
|
|
188
|
-
channelName: name,
|
|
189
|
-
options
|
|
190
|
-
}).catch((error) => {
|
|
191
|
-
debug_default(`Failed to send notification via ${name}: %O`, error);
|
|
192
|
-
return {
|
|
193
|
-
channel: name,
|
|
194
|
-
status: "failed",
|
|
195
|
-
error
|
|
196
|
-
};
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
/**
|
|
200
|
-
* Process results and emit appropriate events
|
|
201
|
-
*/
|
|
202
|
-
return await this.#processResults({
|
|
203
|
-
notification,
|
|
204
|
-
results: await Promise.all(promises),
|
|
205
|
-
throwOnError: options.throwOnError !== false
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
//#endregion
|
|
211
|
-
export { NotificationSender };
|
package/dist/types/channel.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Identifier } from "../database/types.js";
|
|
2
|
-
import { Awaitable } from "@julr/utils/types";
|
|
3
|
-
|
|
4
|
-
//#region src/types/channel.d.ts
|
|
5
|
-
type ChannelSendParams<Message, Targets> = {
|
|
6
|
-
to?: any;
|
|
7
|
-
message: Message;
|
|
8
|
-
targets?: Targets;
|
|
9
|
-
tenantId?: Identifier | undefined;
|
|
10
|
-
};
|
|
11
|
-
declare const kTargetSymbol: unique symbol;
|
|
12
|
-
interface Channel<_Options = any, Message = any, Response = any, Targets = any> {
|
|
13
|
-
[kTargetSymbol]: Targets;
|
|
14
|
-
name: string;
|
|
15
|
-
send: (options: ChannelSendParams<Message, Targets>) => Awaitable<Response>;
|
|
16
|
-
}
|
|
17
|
-
//#endregion
|
|
18
|
-
export { Channel, ChannelSendParams, kTargetSymbol };
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Channel, ChannelSendParams, kTargetSymbol } from "./channel.js";
|
|
2
|
-
import { Emitter, FacteurEvents } from "./events.js";
|
|
3
|
-
import { QueueAdapter, QueueItemOptions } from "./queue.js";
|
|
4
|
-
import { DefaultPreferences, ResolvedDefaultPreferences } from "./preferences.js";
|
|
5
|
-
import { ChannelName, DatabaseContent, InferChannelsFromConfig, NotificationChannels } from "./extend.js";
|
|
6
|
-
import { Arrayable, ChannelSpecificConfig, CommonSendOptions, DeliverByOptions, ExtractChannelTargets, ExtractNotifiable, ExtractParams, FacteurConfiguration, MessageCtx, NotificationOptions, NotificationParams, NotificationResolver, ProviderTarget, SendOptions } from "./options.js";
|
|
7
|
-
import { ChannelSendResult, Notifiable, NotifiableTargets, Notification, NotificationClass, NotificationSendResult } from "./notifications.js";
|
|
8
|
-
export { Arrayable, Channel, ChannelName, ChannelSendParams, ChannelSendResult, ChannelSpecificConfig, CommonSendOptions, DatabaseContent, DefaultPreferences, DeliverByOptions, Emitter, ExtractChannelTargets, ExtractNotifiable, ExtractParams, FacteurConfiguration, FacteurEvents, InferChannelsFromConfig, MessageCtx, Notifiable, NotifiableTargets, Notification, NotificationChannels, NotificationClass, NotificationOptions, NotificationParams, NotificationResolver, NotificationSendResult, ProviderTarget, QueueAdapter, QueueItemOptions, ResolvedDefaultPreferences, SendOptions, kTargetSymbol };
|
package/dist/types/index.js
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|