@bymax-one/nest-notification 1.0.2 → 1.0.4
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/CHANGELOG.md +53 -1
- package/README.md +1 -1
- package/dist/server/index.cjs +154 -147
- package/dist/server/index.mjs +154 -147
- package/package.json +2 -14
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,56 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.0.4] - 2026-08-06
|
|
11
|
+
|
|
12
|
+
**Published-artifact change, not a behavioural one.** `dist/` differs from `1.0.3` — this
|
|
13
|
+
bundler preserves comments and the source gained mutation-suppression notes — but no runtime
|
|
14
|
+
path changed. Measured by building both revisions and diffing the output.
|
|
15
|
+
|
|
16
|
+
### Documentation
|
|
17
|
+
|
|
18
|
+
- The mutation badge said **98%**; the measured score is **100.00%**.
|
|
19
|
+
|
|
20
|
+
### Tests
|
|
21
|
+
|
|
22
|
+
- OTP expiry is `now + ttlSeconds * 1000` and only its order of magnitude was checked. Dividing
|
|
23
|
+
instead of multiplying would expire a 300-second code 0.3 milliseconds after minting it, and
|
|
24
|
+
every failure would read as a delivery problem rather than an arithmetic one.
|
|
25
|
+
- A dotted template path stops at the first non-object. Without that guard `{{ name.0 }}` renders
|
|
26
|
+
the first letter of the string, and the same slip against `{{ token.0 }}` would put the first
|
|
27
|
+
character of a secret in an email.
|
|
28
|
+
|
|
29
|
+
## [1.0.3] - 2026-08-05
|
|
30
|
+
|
|
31
|
+
### Fixed
|
|
32
|
+
|
|
33
|
+
- **`NotificationService` never received its channel services, and said the channels were
|
|
34
|
+
disabled.** Both constructor parameters carried `@Optional()` and no `@Inject`. Nest
|
|
35
|
+
reads three separate metadata keys here — `self:paramtypes` from `@Inject()`,
|
|
36
|
+
`design:paramtypes` from TypeScript, and `optional:paramtypes` from `@Optional()` — and
|
|
37
|
+
the third only says a dependency may be missing; it carries no token. The published
|
|
38
|
+
bundle is built by tsup/esbuild, which documents that it cannot emit `design:paramtypes`,
|
|
39
|
+
so neither key that names a dependency was present and both parameters resolved to
|
|
40
|
+
`undefined`.
|
|
41
|
+
|
|
42
|
+
With both channels configured, the module logged `Initialized with channels: email, otp`
|
|
43
|
+
while `getEnabledChannels()` returned `[]` and `dispatch()` threw `CHANNEL_DISABLED` —
|
|
44
|
+
an error that blamed a configuration that was correct. Both parameters now carry
|
|
45
|
+
`@Inject`, keeping `@Optional()`, which is the shape `@nestjs/jwt` uses for the same
|
|
46
|
+
situation.
|
|
47
|
+
|
|
48
|
+
### Changed
|
|
49
|
+
|
|
50
|
+
- `emitDecoratorMetadata` is `false` in `tsconfig.json`. It was `true`, which was never
|
|
51
|
+
true of the artifact — tsup printed this on every build of this package:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
You have emitDecoratorMetadata enabled but @swc/core was not installed, skipping swc plugin
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The source now compiles the way the bundle is built, so a parameter that depends on
|
|
58
|
+
reflected types fails where it is cheap to see rather than in a consumer's process.
|
|
59
|
+
|
|
10
60
|
## [1.0.2] - 2026-08-04
|
|
11
61
|
|
|
12
62
|
### Security
|
|
@@ -115,7 +165,9 @@ rejected at startup rather than failing on the first send.
|
|
|
115
165
|
- **`forRootAsync` `useClass` / `useExisting`** — only `useFactory` is wired.
|
|
116
166
|
- **Multi-provider failover and routing.**
|
|
117
167
|
|
|
118
|
-
[
|
|
168
|
+
[1.0.4]: https://github.com/bymaxone/nest-notification/compare/v1.0.3...v1.0.4
|
|
169
|
+
[1.0.3]: https://github.com/bymaxone/nest-notification/compare/v1.0.2...v1.0.3
|
|
170
|
+
[Unreleased]: https://github.com/bymaxone/nest-notification/compare/v1.0.3...HEAD
|
|
119
171
|
[1.0.2]: https://github.com/bymaxone/nest-notification/compare/v1.0.1...v1.0.2
|
|
120
172
|
[1.0.1]: https://github.com/bymaxone/nest-notification/compare/v1.0.0...v1.0.1
|
|
121
173
|
[1.0.0]: https://github.com/bymaxone/nest-notification/releases/tag/v1.0.0
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
<a href="https://www.npmjs.com/package/@bymax-one/nest-notification"><img src="https://img.shields.io/npm/dm/@bymax-one/nest-notification?style=flat-square&colorA=000000&colorB=000000" alt="npm downloads" /></a>
|
|
15
15
|
<a href="https://github.com/bymaxone/nest-notification/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/bymaxone/nest-notification/ci.yml?branch=main&style=flat-square&colorA=000000&label=CI" alt="CI status" /></a>
|
|
16
16
|
<a href="https://github.com/bymaxone/nest-notification/actions/workflows/ci.yml"><img src="https://img.shields.io/badge/coverage-100%25-brightgreen?style=flat-square&colorA=000000" alt="coverage" /></a>
|
|
17
|
-
<a href="https://github.com/bymaxone/nest-notification/blob/main/docs/mutation_testing_results.md"><img src="https://img.shields.io/badge/mutation-
|
|
17
|
+
<a href="https://github.com/bymaxone/nest-notification/blob/main/docs/mutation_testing_results.md"><img src="https://img.shields.io/badge/mutation-100.00%25-brightgreen?style=flat-square&colorA=000000" alt="mutation score" /></a>
|
|
18
18
|
<a href="https://scorecard.dev/viewer/?uri=github.com/bymaxone/nest-notification"><img src="https://api.scorecard.dev/projects/github.com/bymaxone/nest-notification/badge?style=flat-square" alt="OpenSSF Scorecard" /></a>
|
|
19
19
|
<a href="https://github.com/bymaxone/nest-notification/blob/main/LICENSE"><img src="https://img.shields.io/github/license/bymaxone/nest-notification?style=flat-square&colorA=000000&colorB=000000" alt="license" /></a>
|
|
20
20
|
<a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/TypeScript-strict-3178C6?style=flat-square&logo=typescript&logoColor=white" alt="TypeScript" /></a>
|
package/dist/server/index.cjs
CHANGED
|
@@ -604,7 +604,10 @@ exports.EmailService = class EmailService {
|
|
|
604
604
|
return;
|
|
605
605
|
}
|
|
606
606
|
const totalBytes = attachments.reduce(
|
|
607
|
-
(sum, { content }) =>
|
|
607
|
+
(sum, { content }) => (
|
|
608
|
+
// Stryker disable next-line ConditionalExpression: equivalent — `Buffer.byteLength` answers a Buffer's own length, so both arms measure the same bytes; the conditional says where the number comes from
|
|
609
|
+
sum + (typeof content === "string" ? Buffer.byteLength(content) : content.length)
|
|
610
|
+
),
|
|
608
611
|
0
|
|
609
612
|
);
|
|
610
613
|
if (totalBytes > maxBytes) {
|
|
@@ -689,152 +692,6 @@ exports.EmailService = __decorateClass([
|
|
|
689
692
|
__decorateParam(2, common.Inject(BYMAX_NOTIFICATION_TEMPLATE_RENDERER)),
|
|
690
693
|
__decorateParam(3, common.Inject(BYMAX_NOTIFICATION_LOG_REPOSITORY))
|
|
691
694
|
], exports.EmailService);
|
|
692
|
-
exports.NotificationService = class NotificationService {
|
|
693
|
-
/**
|
|
694
|
-
* @param emailService - The email service, present only when the email channel is configured.
|
|
695
|
-
* @param otpService - The OTP service, present only when the OTP channel is configured.
|
|
696
|
-
*/
|
|
697
|
-
constructor(emailService, otpService) {
|
|
698
|
-
this.emailService = emailService;
|
|
699
|
-
this.otpService = otpService;
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* Dispatches a notification to the channel named in `input`.
|
|
703
|
-
*
|
|
704
|
-
* @param input - The channel-discriminated dispatch request.
|
|
705
|
-
* @returns The channel-discriminated result.
|
|
706
|
-
* @throws NotificationException `CHANNEL_DISABLED` (channel not configured) or
|
|
707
|
-
* `EMAIL_MISSING_BODY` (email payload has neither a template nor subject+html).
|
|
708
|
-
*/
|
|
709
|
-
async dispatch(input) {
|
|
710
|
-
if (input.channel === "email") {
|
|
711
|
-
return {
|
|
712
|
-
channel: "email",
|
|
713
|
-
messageId: await this.dispatchEmail(input.tenantId, input.payload)
|
|
714
|
-
};
|
|
715
|
-
}
|
|
716
|
-
return { channel: "otp", result: await this.dispatchOtp(input.tenantId, input.payload) };
|
|
717
|
-
}
|
|
718
|
-
/**
|
|
719
|
-
* Lists the channels whose service is present and reports itself configured.
|
|
720
|
-
*
|
|
721
|
-
* @returns The enabled channels.
|
|
722
|
-
*/
|
|
723
|
-
getEnabledChannels() {
|
|
724
|
-
const channels = [];
|
|
725
|
-
if (this.emailService?.isConfigured()) {
|
|
726
|
-
channels.push("email");
|
|
727
|
-
}
|
|
728
|
-
if (this.otpService?.isConfigured()) {
|
|
729
|
-
channels.push("otp");
|
|
730
|
-
}
|
|
731
|
-
return channels;
|
|
732
|
-
}
|
|
733
|
-
/**
|
|
734
|
-
* Returns the email service or throws when the email channel is not enabled.
|
|
735
|
-
*
|
|
736
|
-
* @returns The email service.
|
|
737
|
-
* @throws NotificationException `CHANNEL_DISABLED` when email is not configured.
|
|
738
|
-
*/
|
|
739
|
-
getEmail() {
|
|
740
|
-
if (!this.emailService) {
|
|
741
|
-
throw new NotificationException("CHANNEL_DISABLED", { channel: "email" });
|
|
742
|
-
}
|
|
743
|
-
return this.emailService;
|
|
744
|
-
}
|
|
745
|
-
/**
|
|
746
|
-
* Returns the OTP service or throws when the OTP channel is not enabled.
|
|
747
|
-
*
|
|
748
|
-
* @returns The OTP service.
|
|
749
|
-
* @throws NotificationException `CHANNEL_DISABLED` when OTP is not configured.
|
|
750
|
-
*/
|
|
751
|
-
getOtp() {
|
|
752
|
-
if (!this.otpService) {
|
|
753
|
-
throw new NotificationException("CHANNEL_DISABLED", { channel: "otp" });
|
|
754
|
-
}
|
|
755
|
-
return this.otpService;
|
|
756
|
-
}
|
|
757
|
-
/** Routes an email dispatch to `sendTemplate` (template) or `send` (raw body). */
|
|
758
|
-
async dispatchEmail(tenantId, payload) {
|
|
759
|
-
const email = this.getEmail();
|
|
760
|
-
if (payload.template !== void 0) {
|
|
761
|
-
const input = this.toTemplateInput(tenantId, payload.template, payload);
|
|
762
|
-
return (await email.sendTemplate(input)).messageId;
|
|
763
|
-
}
|
|
764
|
-
if (payload.subject !== void 0 && payload.html !== void 0) {
|
|
765
|
-
const input = this.toSendInput(tenantId, payload.subject, payload.html, payload);
|
|
766
|
-
return (await email.send(input)).messageId;
|
|
767
|
-
}
|
|
768
|
-
throw new NotificationException("EMAIL_MISSING_BODY", {
|
|
769
|
-
hint: "payload requires either `template` OR `{ subject, html }`"
|
|
770
|
-
});
|
|
771
|
-
}
|
|
772
|
-
/** Routes an OTP dispatch by `action`, defaulting to `generate`. */
|
|
773
|
-
async dispatchOtp(tenantId, payload) {
|
|
774
|
-
const otp = this.getOtp();
|
|
775
|
-
const action = payload.action ?? "generate";
|
|
776
|
-
const ref = {
|
|
777
|
-
tenantId,
|
|
778
|
-
recipient: payload.recipient,
|
|
779
|
-
purpose: payload.purpose,
|
|
780
|
-
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
781
|
-
};
|
|
782
|
-
if (action === "verify") {
|
|
783
|
-
return otp.verify({ ...ref, code: payload.code ?? "" });
|
|
784
|
-
}
|
|
785
|
-
if (action === "consume") {
|
|
786
|
-
return otp.consume(ref);
|
|
787
|
-
}
|
|
788
|
-
return otp.generate(this.toGenerateInput(ref, payload));
|
|
789
|
-
}
|
|
790
|
-
/** Builds the `sendTemplate` input from a resolved template name and payload. */
|
|
791
|
-
toTemplateInput(tenantId, template, payload) {
|
|
792
|
-
return {
|
|
793
|
-
tenantId,
|
|
794
|
-
to: payload.to,
|
|
795
|
-
template,
|
|
796
|
-
data: payload.data ?? {},
|
|
797
|
-
...payload.locale !== void 0 ? { locale: payload.locale } : {},
|
|
798
|
-
...this.commonEmailFields(payload)
|
|
799
|
-
};
|
|
800
|
-
}
|
|
801
|
-
/** Builds the raw `send` input from a resolved subject/html and payload. */
|
|
802
|
-
toSendInput(tenantId, subject, html, payload) {
|
|
803
|
-
return {
|
|
804
|
-
tenantId,
|
|
805
|
-
to: payload.to,
|
|
806
|
-
subject,
|
|
807
|
-
html,
|
|
808
|
-
...payload.text !== void 0 ? { text: payload.text } : {},
|
|
809
|
-
...this.commonEmailFields(payload)
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
|
-
/** The optional envelope fields shared by both email paths. */
|
|
813
|
-
commonEmailFields(payload) {
|
|
814
|
-
return {
|
|
815
|
-
...payload.from !== void 0 ? { from: payload.from } : {},
|
|
816
|
-
...payload.fromName !== void 0 ? { fromName: payload.fromName } : {},
|
|
817
|
-
...payload.replyTo !== void 0 ? { replyTo: payload.replyTo } : {},
|
|
818
|
-
...payload.tags !== void 0 ? { tags: payload.tags } : {},
|
|
819
|
-
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
/** Builds the `generate` input from a reference and an OTP dispatch payload. */
|
|
823
|
-
toGenerateInput(ref, payload) {
|
|
824
|
-
return {
|
|
825
|
-
...ref,
|
|
826
|
-
...payload.deliverVia !== void 0 ? { deliverVia: payload.deliverVia } : {},
|
|
827
|
-
...payload.emailTemplate !== void 0 ? { emailTemplate: payload.emailTemplate } : {},
|
|
828
|
-
...payload.emailData !== void 0 ? { emailData: payload.emailData } : {},
|
|
829
|
-
...payload.locale !== void 0 ? { locale: payload.locale } : {}
|
|
830
|
-
};
|
|
831
|
-
}
|
|
832
|
-
};
|
|
833
|
-
exports.NotificationService = __decorateClass([
|
|
834
|
-
common.Injectable(),
|
|
835
|
-
__decorateParam(0, common.Optional()),
|
|
836
|
-
__decorateParam(1, common.Optional())
|
|
837
|
-
], exports.NotificationService);
|
|
838
695
|
var NUMERIC = "0123456789";
|
|
839
696
|
var ALPHA = "ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
840
697
|
var ALPHANUMERIC = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
@@ -1158,6 +1015,156 @@ exports.OtpService = __decorateClass([
|
|
|
1158
1015
|
__decorateParam(3, common.Inject(exports.EmailService))
|
|
1159
1016
|
], exports.OtpService);
|
|
1160
1017
|
|
|
1018
|
+
// src/server/services/notification.service.ts
|
|
1019
|
+
exports.NotificationService = class NotificationService {
|
|
1020
|
+
/**
|
|
1021
|
+
* @param emailService - The email service, present only when the email channel is configured.
|
|
1022
|
+
* @param otpService - The OTP service, present only when the OTP channel is configured.
|
|
1023
|
+
*/
|
|
1024
|
+
constructor(emailService, otpService) {
|
|
1025
|
+
this.emailService = emailService;
|
|
1026
|
+
this.otpService = otpService;
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Dispatches a notification to the channel named in `input`.
|
|
1030
|
+
*
|
|
1031
|
+
* @param input - The channel-discriminated dispatch request.
|
|
1032
|
+
* @returns The channel-discriminated result.
|
|
1033
|
+
* @throws NotificationException `CHANNEL_DISABLED` (channel not configured) or
|
|
1034
|
+
* `EMAIL_MISSING_BODY` (email payload has neither a template nor subject+html).
|
|
1035
|
+
*/
|
|
1036
|
+
async dispatch(input) {
|
|
1037
|
+
if (input.channel === "email") {
|
|
1038
|
+
return {
|
|
1039
|
+
channel: "email",
|
|
1040
|
+
messageId: await this.dispatchEmail(input.tenantId, input.payload)
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
return { channel: "otp", result: await this.dispatchOtp(input.tenantId, input.payload) };
|
|
1044
|
+
}
|
|
1045
|
+
/**
|
|
1046
|
+
* Lists the channels whose service is present and reports itself configured.
|
|
1047
|
+
*
|
|
1048
|
+
* @returns The enabled channels.
|
|
1049
|
+
*/
|
|
1050
|
+
getEnabledChannels() {
|
|
1051
|
+
const channels = [];
|
|
1052
|
+
if (this.emailService?.isConfigured()) {
|
|
1053
|
+
channels.push("email");
|
|
1054
|
+
}
|
|
1055
|
+
if (this.otpService?.isConfigured()) {
|
|
1056
|
+
channels.push("otp");
|
|
1057
|
+
}
|
|
1058
|
+
return channels;
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Returns the email service or throws when the email channel is not enabled.
|
|
1062
|
+
*
|
|
1063
|
+
* @returns The email service.
|
|
1064
|
+
* @throws NotificationException `CHANNEL_DISABLED` when email is not configured.
|
|
1065
|
+
*/
|
|
1066
|
+
getEmail() {
|
|
1067
|
+
if (!this.emailService) {
|
|
1068
|
+
throw new NotificationException("CHANNEL_DISABLED", { channel: "email" });
|
|
1069
|
+
}
|
|
1070
|
+
return this.emailService;
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
* Returns the OTP service or throws when the OTP channel is not enabled.
|
|
1074
|
+
*
|
|
1075
|
+
* @returns The OTP service.
|
|
1076
|
+
* @throws NotificationException `CHANNEL_DISABLED` when OTP is not configured.
|
|
1077
|
+
*/
|
|
1078
|
+
getOtp() {
|
|
1079
|
+
if (!this.otpService) {
|
|
1080
|
+
throw new NotificationException("CHANNEL_DISABLED", { channel: "otp" });
|
|
1081
|
+
}
|
|
1082
|
+
return this.otpService;
|
|
1083
|
+
}
|
|
1084
|
+
/** Routes an email dispatch to `sendTemplate` (template) or `send` (raw body). */
|
|
1085
|
+
async dispatchEmail(tenantId, payload) {
|
|
1086
|
+
const email = this.getEmail();
|
|
1087
|
+
if (payload.template !== void 0) {
|
|
1088
|
+
const input = this.toTemplateInput(tenantId, payload.template, payload);
|
|
1089
|
+
return (await email.sendTemplate(input)).messageId;
|
|
1090
|
+
}
|
|
1091
|
+
if (payload.subject !== void 0 && payload.html !== void 0) {
|
|
1092
|
+
const input = this.toSendInput(tenantId, payload.subject, payload.html, payload);
|
|
1093
|
+
return (await email.send(input)).messageId;
|
|
1094
|
+
}
|
|
1095
|
+
throw new NotificationException("EMAIL_MISSING_BODY", {
|
|
1096
|
+
hint: "payload requires either `template` OR `{ subject, html }`"
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
/** Routes an OTP dispatch by `action`, defaulting to `generate`. */
|
|
1100
|
+
async dispatchOtp(tenantId, payload) {
|
|
1101
|
+
const otp = this.getOtp();
|
|
1102
|
+
const action = payload.action ?? "generate";
|
|
1103
|
+
const ref = {
|
|
1104
|
+
tenantId,
|
|
1105
|
+
recipient: payload.recipient,
|
|
1106
|
+
purpose: payload.purpose,
|
|
1107
|
+
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
1108
|
+
};
|
|
1109
|
+
if (action === "verify") {
|
|
1110
|
+
return otp.verify({ ...ref, code: payload.code ?? "" });
|
|
1111
|
+
}
|
|
1112
|
+
if (action === "consume") {
|
|
1113
|
+
return otp.consume(ref);
|
|
1114
|
+
}
|
|
1115
|
+
return otp.generate(this.toGenerateInput(ref, payload));
|
|
1116
|
+
}
|
|
1117
|
+
/** Builds the `sendTemplate` input from a resolved template name and payload. */
|
|
1118
|
+
toTemplateInput(tenantId, template, payload) {
|
|
1119
|
+
return {
|
|
1120
|
+
tenantId,
|
|
1121
|
+
to: payload.to,
|
|
1122
|
+
template,
|
|
1123
|
+
data: payload.data ?? {},
|
|
1124
|
+
...payload.locale !== void 0 ? { locale: payload.locale } : {},
|
|
1125
|
+
...this.commonEmailFields(payload)
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
/** Builds the raw `send` input from a resolved subject/html and payload. */
|
|
1129
|
+
toSendInput(tenantId, subject, html, payload) {
|
|
1130
|
+
return {
|
|
1131
|
+
tenantId,
|
|
1132
|
+
to: payload.to,
|
|
1133
|
+
subject,
|
|
1134
|
+
html,
|
|
1135
|
+
...payload.text !== void 0 ? { text: payload.text } : {},
|
|
1136
|
+
...this.commonEmailFields(payload)
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
/** The optional envelope fields shared by both email paths. */
|
|
1140
|
+
commonEmailFields(payload) {
|
|
1141
|
+
return {
|
|
1142
|
+
...payload.from !== void 0 ? { from: payload.from } : {},
|
|
1143
|
+
...payload.fromName !== void 0 ? { fromName: payload.fromName } : {},
|
|
1144
|
+
...payload.replyTo !== void 0 ? { replyTo: payload.replyTo } : {},
|
|
1145
|
+
...payload.tags !== void 0 ? { tags: payload.tags } : {},
|
|
1146
|
+
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
/** Builds the `generate` input from a reference and an OTP dispatch payload. */
|
|
1150
|
+
toGenerateInput(ref, payload) {
|
|
1151
|
+
return {
|
|
1152
|
+
...ref,
|
|
1153
|
+
...payload.deliverVia !== void 0 ? { deliverVia: payload.deliverVia } : {},
|
|
1154
|
+
...payload.emailTemplate !== void 0 ? { emailTemplate: payload.emailTemplate } : {},
|
|
1155
|
+
...payload.emailData !== void 0 ? { emailData: payload.emailData } : {},
|
|
1156
|
+
...payload.locale !== void 0 ? { locale: payload.locale } : {}
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
};
|
|
1160
|
+
exports.NotificationService = __decorateClass([
|
|
1161
|
+
common.Injectable(),
|
|
1162
|
+
__decorateParam(0, common.Optional()),
|
|
1163
|
+
__decorateParam(0, common.Inject(exports.EmailService)),
|
|
1164
|
+
__decorateParam(1, common.Optional()),
|
|
1165
|
+
__decorateParam(1, common.Inject(exports.OtpService))
|
|
1166
|
+
], exports.NotificationService);
|
|
1167
|
+
|
|
1161
1168
|
// src/server/bymax-notification.module.ts
|
|
1162
1169
|
var RAW_NOTIFICATION_OPTIONS = /* @__PURE__ */ Symbol("RAW_NOTIFICATION_OPTIONS");
|
|
1163
1170
|
exports.BymaxNotificationModule = class BymaxNotificationModule {
|
package/dist/server/index.mjs
CHANGED
|
@@ -602,7 +602,10 @@ var EmailService = class {
|
|
|
602
602
|
return;
|
|
603
603
|
}
|
|
604
604
|
const totalBytes = attachments.reduce(
|
|
605
|
-
(sum, { content }) =>
|
|
605
|
+
(sum, { content }) => (
|
|
606
|
+
// Stryker disable next-line ConditionalExpression: equivalent — `Buffer.byteLength` answers a Buffer's own length, so both arms measure the same bytes; the conditional says where the number comes from
|
|
607
|
+
sum + (typeof content === "string" ? Buffer.byteLength(content) : content.length)
|
|
608
|
+
),
|
|
606
609
|
0
|
|
607
610
|
);
|
|
608
611
|
if (totalBytes > maxBytes) {
|
|
@@ -687,152 +690,6 @@ EmailService = __decorateClass([
|
|
|
687
690
|
__decorateParam(2, Inject(BYMAX_NOTIFICATION_TEMPLATE_RENDERER)),
|
|
688
691
|
__decorateParam(3, Inject(BYMAX_NOTIFICATION_LOG_REPOSITORY))
|
|
689
692
|
], EmailService);
|
|
690
|
-
var NotificationService = class {
|
|
691
|
-
/**
|
|
692
|
-
* @param emailService - The email service, present only when the email channel is configured.
|
|
693
|
-
* @param otpService - The OTP service, present only when the OTP channel is configured.
|
|
694
|
-
*/
|
|
695
|
-
constructor(emailService, otpService) {
|
|
696
|
-
this.emailService = emailService;
|
|
697
|
-
this.otpService = otpService;
|
|
698
|
-
}
|
|
699
|
-
/**
|
|
700
|
-
* Dispatches a notification to the channel named in `input`.
|
|
701
|
-
*
|
|
702
|
-
* @param input - The channel-discriminated dispatch request.
|
|
703
|
-
* @returns The channel-discriminated result.
|
|
704
|
-
* @throws NotificationException `CHANNEL_DISABLED` (channel not configured) or
|
|
705
|
-
* `EMAIL_MISSING_BODY` (email payload has neither a template nor subject+html).
|
|
706
|
-
*/
|
|
707
|
-
async dispatch(input) {
|
|
708
|
-
if (input.channel === "email") {
|
|
709
|
-
return {
|
|
710
|
-
channel: "email",
|
|
711
|
-
messageId: await this.dispatchEmail(input.tenantId, input.payload)
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
return { channel: "otp", result: await this.dispatchOtp(input.tenantId, input.payload) };
|
|
715
|
-
}
|
|
716
|
-
/**
|
|
717
|
-
* Lists the channels whose service is present and reports itself configured.
|
|
718
|
-
*
|
|
719
|
-
* @returns The enabled channels.
|
|
720
|
-
*/
|
|
721
|
-
getEnabledChannels() {
|
|
722
|
-
const channels = [];
|
|
723
|
-
if (this.emailService?.isConfigured()) {
|
|
724
|
-
channels.push("email");
|
|
725
|
-
}
|
|
726
|
-
if (this.otpService?.isConfigured()) {
|
|
727
|
-
channels.push("otp");
|
|
728
|
-
}
|
|
729
|
-
return channels;
|
|
730
|
-
}
|
|
731
|
-
/**
|
|
732
|
-
* Returns the email service or throws when the email channel is not enabled.
|
|
733
|
-
*
|
|
734
|
-
* @returns The email service.
|
|
735
|
-
* @throws NotificationException `CHANNEL_DISABLED` when email is not configured.
|
|
736
|
-
*/
|
|
737
|
-
getEmail() {
|
|
738
|
-
if (!this.emailService) {
|
|
739
|
-
throw new NotificationException("CHANNEL_DISABLED", { channel: "email" });
|
|
740
|
-
}
|
|
741
|
-
return this.emailService;
|
|
742
|
-
}
|
|
743
|
-
/**
|
|
744
|
-
* Returns the OTP service or throws when the OTP channel is not enabled.
|
|
745
|
-
*
|
|
746
|
-
* @returns The OTP service.
|
|
747
|
-
* @throws NotificationException `CHANNEL_DISABLED` when OTP is not configured.
|
|
748
|
-
*/
|
|
749
|
-
getOtp() {
|
|
750
|
-
if (!this.otpService) {
|
|
751
|
-
throw new NotificationException("CHANNEL_DISABLED", { channel: "otp" });
|
|
752
|
-
}
|
|
753
|
-
return this.otpService;
|
|
754
|
-
}
|
|
755
|
-
/** Routes an email dispatch to `sendTemplate` (template) or `send` (raw body). */
|
|
756
|
-
async dispatchEmail(tenantId, payload) {
|
|
757
|
-
const email = this.getEmail();
|
|
758
|
-
if (payload.template !== void 0) {
|
|
759
|
-
const input = this.toTemplateInput(tenantId, payload.template, payload);
|
|
760
|
-
return (await email.sendTemplate(input)).messageId;
|
|
761
|
-
}
|
|
762
|
-
if (payload.subject !== void 0 && payload.html !== void 0) {
|
|
763
|
-
const input = this.toSendInput(tenantId, payload.subject, payload.html, payload);
|
|
764
|
-
return (await email.send(input)).messageId;
|
|
765
|
-
}
|
|
766
|
-
throw new NotificationException("EMAIL_MISSING_BODY", {
|
|
767
|
-
hint: "payload requires either `template` OR `{ subject, html }`"
|
|
768
|
-
});
|
|
769
|
-
}
|
|
770
|
-
/** Routes an OTP dispatch by `action`, defaulting to `generate`. */
|
|
771
|
-
async dispatchOtp(tenantId, payload) {
|
|
772
|
-
const otp = this.getOtp();
|
|
773
|
-
const action = payload.action ?? "generate";
|
|
774
|
-
const ref = {
|
|
775
|
-
tenantId,
|
|
776
|
-
recipient: payload.recipient,
|
|
777
|
-
purpose: payload.purpose,
|
|
778
|
-
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
779
|
-
};
|
|
780
|
-
if (action === "verify") {
|
|
781
|
-
return otp.verify({ ...ref, code: payload.code ?? "" });
|
|
782
|
-
}
|
|
783
|
-
if (action === "consume") {
|
|
784
|
-
return otp.consume(ref);
|
|
785
|
-
}
|
|
786
|
-
return otp.generate(this.toGenerateInput(ref, payload));
|
|
787
|
-
}
|
|
788
|
-
/** Builds the `sendTemplate` input from a resolved template name and payload. */
|
|
789
|
-
toTemplateInput(tenantId, template, payload) {
|
|
790
|
-
return {
|
|
791
|
-
tenantId,
|
|
792
|
-
to: payload.to,
|
|
793
|
-
template,
|
|
794
|
-
data: payload.data ?? {},
|
|
795
|
-
...payload.locale !== void 0 ? { locale: payload.locale } : {},
|
|
796
|
-
...this.commonEmailFields(payload)
|
|
797
|
-
};
|
|
798
|
-
}
|
|
799
|
-
/** Builds the raw `send` input from a resolved subject/html and payload. */
|
|
800
|
-
toSendInput(tenantId, subject, html, payload) {
|
|
801
|
-
return {
|
|
802
|
-
tenantId,
|
|
803
|
-
to: payload.to,
|
|
804
|
-
subject,
|
|
805
|
-
html,
|
|
806
|
-
...payload.text !== void 0 ? { text: payload.text } : {},
|
|
807
|
-
...this.commonEmailFields(payload)
|
|
808
|
-
};
|
|
809
|
-
}
|
|
810
|
-
/** The optional envelope fields shared by both email paths. */
|
|
811
|
-
commonEmailFields(payload) {
|
|
812
|
-
return {
|
|
813
|
-
...payload.from !== void 0 ? { from: payload.from } : {},
|
|
814
|
-
...payload.fromName !== void 0 ? { fromName: payload.fromName } : {},
|
|
815
|
-
...payload.replyTo !== void 0 ? { replyTo: payload.replyTo } : {},
|
|
816
|
-
...payload.tags !== void 0 ? { tags: payload.tags } : {},
|
|
817
|
-
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
818
|
-
};
|
|
819
|
-
}
|
|
820
|
-
/** Builds the `generate` input from a reference and an OTP dispatch payload. */
|
|
821
|
-
toGenerateInput(ref, payload) {
|
|
822
|
-
return {
|
|
823
|
-
...ref,
|
|
824
|
-
...payload.deliverVia !== void 0 ? { deliverVia: payload.deliverVia } : {},
|
|
825
|
-
...payload.emailTemplate !== void 0 ? { emailTemplate: payload.emailTemplate } : {},
|
|
826
|
-
...payload.emailData !== void 0 ? { emailData: payload.emailData } : {},
|
|
827
|
-
...payload.locale !== void 0 ? { locale: payload.locale } : {}
|
|
828
|
-
};
|
|
829
|
-
}
|
|
830
|
-
};
|
|
831
|
-
NotificationService = __decorateClass([
|
|
832
|
-
Injectable(),
|
|
833
|
-
__decorateParam(0, Optional()),
|
|
834
|
-
__decorateParam(1, Optional())
|
|
835
|
-
], NotificationService);
|
|
836
693
|
var NUMERIC = "0123456789";
|
|
837
694
|
var ALPHA = "ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
838
695
|
var ALPHANUMERIC = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
@@ -1156,6 +1013,156 @@ OtpService = __decorateClass([
|
|
|
1156
1013
|
__decorateParam(3, Inject(EmailService))
|
|
1157
1014
|
], OtpService);
|
|
1158
1015
|
|
|
1016
|
+
// src/server/services/notification.service.ts
|
|
1017
|
+
var NotificationService = class {
|
|
1018
|
+
/**
|
|
1019
|
+
* @param emailService - The email service, present only when the email channel is configured.
|
|
1020
|
+
* @param otpService - The OTP service, present only when the OTP channel is configured.
|
|
1021
|
+
*/
|
|
1022
|
+
constructor(emailService, otpService) {
|
|
1023
|
+
this.emailService = emailService;
|
|
1024
|
+
this.otpService = otpService;
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Dispatches a notification to the channel named in `input`.
|
|
1028
|
+
*
|
|
1029
|
+
* @param input - The channel-discriminated dispatch request.
|
|
1030
|
+
* @returns The channel-discriminated result.
|
|
1031
|
+
* @throws NotificationException `CHANNEL_DISABLED` (channel not configured) or
|
|
1032
|
+
* `EMAIL_MISSING_BODY` (email payload has neither a template nor subject+html).
|
|
1033
|
+
*/
|
|
1034
|
+
async dispatch(input) {
|
|
1035
|
+
if (input.channel === "email") {
|
|
1036
|
+
return {
|
|
1037
|
+
channel: "email",
|
|
1038
|
+
messageId: await this.dispatchEmail(input.tenantId, input.payload)
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
return { channel: "otp", result: await this.dispatchOtp(input.tenantId, input.payload) };
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Lists the channels whose service is present and reports itself configured.
|
|
1045
|
+
*
|
|
1046
|
+
* @returns The enabled channels.
|
|
1047
|
+
*/
|
|
1048
|
+
getEnabledChannels() {
|
|
1049
|
+
const channels = [];
|
|
1050
|
+
if (this.emailService?.isConfigured()) {
|
|
1051
|
+
channels.push("email");
|
|
1052
|
+
}
|
|
1053
|
+
if (this.otpService?.isConfigured()) {
|
|
1054
|
+
channels.push("otp");
|
|
1055
|
+
}
|
|
1056
|
+
return channels;
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Returns the email service or throws when the email channel is not enabled.
|
|
1060
|
+
*
|
|
1061
|
+
* @returns The email service.
|
|
1062
|
+
* @throws NotificationException `CHANNEL_DISABLED` when email is not configured.
|
|
1063
|
+
*/
|
|
1064
|
+
getEmail() {
|
|
1065
|
+
if (!this.emailService) {
|
|
1066
|
+
throw new NotificationException("CHANNEL_DISABLED", { channel: "email" });
|
|
1067
|
+
}
|
|
1068
|
+
return this.emailService;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Returns the OTP service or throws when the OTP channel is not enabled.
|
|
1072
|
+
*
|
|
1073
|
+
* @returns The OTP service.
|
|
1074
|
+
* @throws NotificationException `CHANNEL_DISABLED` when OTP is not configured.
|
|
1075
|
+
*/
|
|
1076
|
+
getOtp() {
|
|
1077
|
+
if (!this.otpService) {
|
|
1078
|
+
throw new NotificationException("CHANNEL_DISABLED", { channel: "otp" });
|
|
1079
|
+
}
|
|
1080
|
+
return this.otpService;
|
|
1081
|
+
}
|
|
1082
|
+
/** Routes an email dispatch to `sendTemplate` (template) or `send` (raw body). */
|
|
1083
|
+
async dispatchEmail(tenantId, payload) {
|
|
1084
|
+
const email = this.getEmail();
|
|
1085
|
+
if (payload.template !== void 0) {
|
|
1086
|
+
const input = this.toTemplateInput(tenantId, payload.template, payload);
|
|
1087
|
+
return (await email.sendTemplate(input)).messageId;
|
|
1088
|
+
}
|
|
1089
|
+
if (payload.subject !== void 0 && payload.html !== void 0) {
|
|
1090
|
+
const input = this.toSendInput(tenantId, payload.subject, payload.html, payload);
|
|
1091
|
+
return (await email.send(input)).messageId;
|
|
1092
|
+
}
|
|
1093
|
+
throw new NotificationException("EMAIL_MISSING_BODY", {
|
|
1094
|
+
hint: "payload requires either `template` OR `{ subject, html }`"
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
/** Routes an OTP dispatch by `action`, defaulting to `generate`. */
|
|
1098
|
+
async dispatchOtp(tenantId, payload) {
|
|
1099
|
+
const otp = this.getOtp();
|
|
1100
|
+
const action = payload.action ?? "generate";
|
|
1101
|
+
const ref = {
|
|
1102
|
+
tenantId,
|
|
1103
|
+
recipient: payload.recipient,
|
|
1104
|
+
purpose: payload.purpose,
|
|
1105
|
+
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
1106
|
+
};
|
|
1107
|
+
if (action === "verify") {
|
|
1108
|
+
return otp.verify({ ...ref, code: payload.code ?? "" });
|
|
1109
|
+
}
|
|
1110
|
+
if (action === "consume") {
|
|
1111
|
+
return otp.consume(ref);
|
|
1112
|
+
}
|
|
1113
|
+
return otp.generate(this.toGenerateInput(ref, payload));
|
|
1114
|
+
}
|
|
1115
|
+
/** Builds the `sendTemplate` input from a resolved template name and payload. */
|
|
1116
|
+
toTemplateInput(tenantId, template, payload) {
|
|
1117
|
+
return {
|
|
1118
|
+
tenantId,
|
|
1119
|
+
to: payload.to,
|
|
1120
|
+
template,
|
|
1121
|
+
data: payload.data ?? {},
|
|
1122
|
+
...payload.locale !== void 0 ? { locale: payload.locale } : {},
|
|
1123
|
+
...this.commonEmailFields(payload)
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
/** Builds the raw `send` input from a resolved subject/html and payload. */
|
|
1127
|
+
toSendInput(tenantId, subject, html, payload) {
|
|
1128
|
+
return {
|
|
1129
|
+
tenantId,
|
|
1130
|
+
to: payload.to,
|
|
1131
|
+
subject,
|
|
1132
|
+
html,
|
|
1133
|
+
...payload.text !== void 0 ? { text: payload.text } : {},
|
|
1134
|
+
...this.commonEmailFields(payload)
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
/** The optional envelope fields shared by both email paths. */
|
|
1138
|
+
commonEmailFields(payload) {
|
|
1139
|
+
return {
|
|
1140
|
+
...payload.from !== void 0 ? { from: payload.from } : {},
|
|
1141
|
+
...payload.fromName !== void 0 ? { fromName: payload.fromName } : {},
|
|
1142
|
+
...payload.replyTo !== void 0 ? { replyTo: payload.replyTo } : {},
|
|
1143
|
+
...payload.tags !== void 0 ? { tags: payload.tags } : {},
|
|
1144
|
+
...payload.userId !== void 0 ? { userId: payload.userId } : {}
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
/** Builds the `generate` input from a reference and an OTP dispatch payload. */
|
|
1148
|
+
toGenerateInput(ref, payload) {
|
|
1149
|
+
return {
|
|
1150
|
+
...ref,
|
|
1151
|
+
...payload.deliverVia !== void 0 ? { deliverVia: payload.deliverVia } : {},
|
|
1152
|
+
...payload.emailTemplate !== void 0 ? { emailTemplate: payload.emailTemplate } : {},
|
|
1153
|
+
...payload.emailData !== void 0 ? { emailData: payload.emailData } : {},
|
|
1154
|
+
...payload.locale !== void 0 ? { locale: payload.locale } : {}
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
};
|
|
1158
|
+
NotificationService = __decorateClass([
|
|
1159
|
+
Injectable(),
|
|
1160
|
+
__decorateParam(0, Optional()),
|
|
1161
|
+
__decorateParam(0, Inject(EmailService)),
|
|
1162
|
+
__decorateParam(1, Optional()),
|
|
1163
|
+
__decorateParam(1, Inject(OtpService))
|
|
1164
|
+
], NotificationService);
|
|
1165
|
+
|
|
1159
1166
|
// src/server/bymax-notification.module.ts
|
|
1160
1167
|
var RAW_NOTIFICATION_OPTIONS = /* @__PURE__ */ Symbol("RAW_NOTIFICATION_OPTIONS");
|
|
1161
1168
|
var BymaxNotificationModule = class {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-notification",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Multi-channel notification library for NestJS — email, OTP, SMS, push — with pluggable providers and storage, multi-tenant by design, zero runtime dependencies.",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -237,24 +237,12 @@
|
|
|
237
237
|
"prettier --write"
|
|
238
238
|
]
|
|
239
239
|
},
|
|
240
|
-
"packageManager": "pnpm@
|
|
240
|
+
"packageManager": "pnpm@11.20.0",
|
|
241
241
|
"engines": {
|
|
242
242
|
"node": ">=24.0.0"
|
|
243
243
|
},
|
|
244
244
|
"publishConfig": {
|
|
245
245
|
"access": "public",
|
|
246
246
|
"registry": "https://registry.npmjs.org/"
|
|
247
|
-
},
|
|
248
|
-
"pnpm": {
|
|
249
|
-
"overrides": {
|
|
250
|
-
"brace-expansion@1": "^1.1.18",
|
|
251
|
-
"brace-expansion@2": "^2.1.4",
|
|
252
|
-
"brace-expansion@5": "^5.0.9",
|
|
253
|
-
"esbuild": "^0.28.1",
|
|
254
|
-
"fast-uri": "^3.1.5",
|
|
255
|
-
"js-yaml@3": "^3.15.0",
|
|
256
|
-
"js-yaml@4": "^4.3.0",
|
|
257
|
-
"qs": "^6.15.2"
|
|
258
|
-
}
|
|
259
247
|
}
|
|
260
248
|
}
|