@terreno/comms 56.0.0-beta.2

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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/biome.jsonc +17 -0
  3. package/bunfig.toml +5 -0
  4. package/dist/adapters/console.d.ts +30 -0
  5. package/dist/adapters/console.js +122 -0
  6. package/dist/commsApp.d.ts +14 -0
  7. package/dist/commsApp.js +37 -0
  8. package/dist/commsService.d.ts +20 -0
  9. package/dist/commsService.js +386 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.js +19 -0
  12. package/dist/modelTypes.d.ts +53 -0
  13. package/dist/modelTypes.js +2 -0
  14. package/dist/models/commsMessage.d.ts +2 -0
  15. package/dist/models/commsMessage.js +119 -0
  16. package/dist/models/pushToken.d.ts +2 -0
  17. package/dist/models/pushToken.js +56 -0
  18. package/dist/routes/commsExplorer.d.ts +7 -0
  19. package/dist/routes/commsExplorer.js +147 -0
  20. package/dist/routes/pushTokens.d.ts +7 -0
  21. package/dist/routes/pushTokens.js +292 -0
  22. package/dist/templates.d.ts +10 -0
  23. package/dist/templates.js +32 -0
  24. package/dist/types.d.ts +78 -0
  25. package/dist/types.js +2 -0
  26. package/package.json +55 -0
  27. package/src/adapters/console.test.ts +227 -0
  28. package/src/adapters/console.ts +87 -0
  29. package/src/commsApp.test.ts +448 -0
  30. package/src/commsApp.ts +48 -0
  31. package/src/commsService.test.ts +570 -0
  32. package/src/commsService.ts +293 -0
  33. package/src/index.ts +45 -0
  34. package/src/modelTypes.ts +78 -0
  35. package/src/models/commsMessage.test.ts +53 -0
  36. package/src/models/commsMessage.ts +97 -0
  37. package/src/models/pushToken.test.ts +45 -0
  38. package/src/models/pushToken.ts +69 -0
  39. package/src/routes/commsExplorer.ts +114 -0
  40. package/src/routes/pushTokens.ts +265 -0
  41. package/src/templates.test.ts +37 -0
  42. package/src/templates.ts +31 -0
  43. package/src/tests/bunSetup.ts +14 -0
  44. package/src/types.ts +93 -0
  45. package/tsconfig.json +32 -0
@@ -0,0 +1,227 @@
1
+ import {describe, it, spyOn} from "bun:test";
2
+ import {logger} from "@terreno/api";
3
+ import {assert} from "chai";
4
+
5
+ import type {
6
+ CheckVerificationOptions,
7
+ CommsChannel,
8
+ CommsMessageStatus,
9
+ CommsOptions,
10
+ DeliveryEvent,
11
+ MailMessage,
12
+ MailProvider,
13
+ PushMessage,
14
+ PushProvider,
15
+ SendPushToUserMessage,
16
+ SendResult,
17
+ SmsMessage,
18
+ SmsProvider,
19
+ StartVerificationOptions,
20
+ VerificationProvider,
21
+ VerificationResult,
22
+ } from "../index";
23
+ import {
24
+ ConsoleMailProvider,
25
+ ConsolePushProvider,
26
+ ConsoleSmsProvider,
27
+ ConsoleVerificationProvider,
28
+ } from "../index";
29
+
30
+ describe("console communications providers", () => {
31
+ it("exports the complete provider contract surface", (): void => {
32
+ const mailMessage: MailMessage = {subject: "Subject", to: "person@example.com"};
33
+ const smsMessage: SmsMessage = {body: "Body", to: "+15555550100"};
34
+ const pushMessage: PushMessage = {body: "Body", title: "Title", tokens: ["token"]};
35
+ const pushToUserMessage: SendPushToUserMessage = {
36
+ body: "Body",
37
+ title: "Title",
38
+ userId: "user-id",
39
+ };
40
+ const start: StartVerificationOptions = {channel: "sms", to: "+15555550100"};
41
+ const check: CheckVerificationOptions = {code: "123456", to: "+15555550100"};
42
+ const result: SendResult = {accepted: true};
43
+ const verificationResult: VerificationResult = {valid: true};
44
+ const delivery: DeliveryEvent = {
45
+ channel: "mail",
46
+ providerMessageId: "provider-id",
47
+ status: "delivered",
48
+ };
49
+ const channel: CommsChannel = "mail";
50
+ const status: CommsMessageStatus = "sent";
51
+ const options: CommsOptions = {defaultFrom: "sender@example.com"};
52
+
53
+ assert.deepEqual(
54
+ {
55
+ channel,
56
+ check,
57
+ delivery,
58
+ mailMessage,
59
+ options,
60
+ pushMessage,
61
+ pushToUserMessage,
62
+ result,
63
+ smsMessage,
64
+ start,
65
+ status,
66
+ verificationResult,
67
+ },
68
+ {
69
+ channel: "mail",
70
+ check: {code: "123456", to: "+15555550100"},
71
+ delivery: {
72
+ channel: "mail",
73
+ providerMessageId: "provider-id",
74
+ status: "delivered",
75
+ },
76
+ mailMessage: {subject: "Subject", to: "person@example.com"},
77
+ options: {defaultFrom: "sender@example.com"},
78
+ pushMessage: {body: "Body", title: "Title", tokens: ["token"]},
79
+ pushToUserMessage: {body: "Body", title: "Title", userId: "user-id"},
80
+ result: {accepted: true},
81
+ smsMessage: {body: "Body", to: "+15555550100"},
82
+ start: {channel: "sms", to: "+15555550100"},
83
+ status: "sent",
84
+ verificationResult: {valid: true},
85
+ }
86
+ );
87
+ });
88
+
89
+ it("accepts every channel operation and emits channel-specific logs", async (): Promise<void> => {
90
+ const messages: string[] = [];
91
+ const log = (message: string): void => {
92
+ messages.push(message);
93
+ };
94
+ const mail: MailProvider = new ConsoleMailProvider({log});
95
+ const sms: SmsProvider = new ConsoleSmsProvider({log});
96
+ const push: PushProvider = new ConsolePushProvider({log});
97
+ const verification: VerificationProvider = new ConsoleVerificationProvider({log});
98
+
99
+ const mailResult = await mail.sendMail({
100
+ subject: "Welcome",
101
+ text: "Hello",
102
+ to: "person@example.com",
103
+ });
104
+ const smsResult = await sms.sendSms({body: "Hello", to: "+15555550100"});
105
+ const pushResults = await push.sendPush({
106
+ body: "Hello",
107
+ title: "Welcome",
108
+ tokens: ["ExponentPushToken[first]", "ExponentPushToken[second]"],
109
+ });
110
+ const startResult = await verification.startVerification({
111
+ channel: "sms",
112
+ to: "+15555550100",
113
+ });
114
+ const checkResult = await verification.checkVerification({
115
+ code: "123456",
116
+ to: "+15555550100",
117
+ });
118
+
119
+ assert.isTrue(mailResult.accepted);
120
+ assert.isTrue(smsResult.accepted);
121
+ assert.lengthOf(pushResults, 2);
122
+ assert.isTrue(pushResults.every((result) => result.accepted));
123
+ assert.isTrue(startResult.accepted);
124
+ assert.isTrue(checkResult.valid);
125
+ assert.deepEqual(messages, [
126
+ "[comms:mail] recipients=1 subjectLength=7",
127
+ "[comms:sms] bodyLength=5",
128
+ "[comms:push] tokens=2 titleLength=7",
129
+ "[comms:verification:start] channel=sms",
130
+ "[comms:verification:check]",
131
+ ]);
132
+ });
133
+
134
+ it("supports email verification without logging the email address", async (): Promise<void> => {
135
+ const messages: string[] = [];
136
+ const verification = new ConsoleVerificationProvider({
137
+ log: (message: string): void => {
138
+ messages.push(message);
139
+ },
140
+ });
141
+
142
+ const startResult = await verification.startVerification({
143
+ channel: "email",
144
+ to: "person@example.com",
145
+ });
146
+ const checkResult = await verification.checkVerification({
147
+ code: "123456",
148
+ to: "person@example.com",
149
+ });
150
+
151
+ assert.isTrue(startResult.accepted);
152
+ assert.isTrue(checkResult.valid);
153
+ assert.deepEqual(messages, [
154
+ "[comms:verification:start] channel=email",
155
+ "[comms:verification:check]",
156
+ ]);
157
+ assert.notInclude(messages.join(" "), "person@example.com");
158
+ assert.notInclude(messages.join(" "), "123456");
159
+ });
160
+
161
+ it("does not include content or recipient identifiers in logs", async (): Promise<void> => {
162
+ const messages: string[] = [];
163
+ const log = (message: string): void => {
164
+ messages.push(message);
165
+ };
166
+
167
+ await new ConsoleMailProvider({log}).sendMail({
168
+ subject: "Private reset token 654321",
169
+ text: "Private mail body",
170
+ to: "private@example.com",
171
+ });
172
+ await new ConsoleSmsProvider({log}).sendSms({
173
+ body: "Your private code is 654321",
174
+ to: "+15555550100",
175
+ });
176
+ await new ConsoleVerificationProvider({log}).checkVerification({
177
+ code: "654321",
178
+ to: "+15555550100",
179
+ });
180
+ await new ConsolePushProvider({log}).sendPush({
181
+ body: "Private push body",
182
+ title: "Private push title",
183
+ tokens: ["ExponentPushToken[private-token]"],
184
+ });
185
+
186
+ assert.notInclude(messages.join(" "), "Your private code");
187
+ assert.notInclude(messages.join(" "), "Private reset token");
188
+ assert.notInclude(messages.join(" "), "private@example.com");
189
+ assert.notInclude(messages.join(" "), "+15555550100");
190
+ assert.notInclude(messages.join(" "), "Private push body");
191
+ assert.notInclude(messages.join(" "), "Private push title");
192
+ assert.notInclude(messages.join(" "), "ExponentPushToken[private-token]");
193
+ assert.notInclude(messages.join(" "), "654321");
194
+ });
195
+
196
+ it("uses logger.info when no log override is provided", async (): Promise<void> => {
197
+ const messages: string[] = [];
198
+ const info = spyOn(logger, "info").mockImplementation((message: string): void => {
199
+ messages.push(message);
200
+ });
201
+
202
+ try {
203
+ await new ConsoleMailProvider().sendMail({subject: "Welcome", to: "person@example.com"});
204
+ await new ConsoleSmsProvider().sendSms({body: "Hello", to: "+15555550100"});
205
+ await new ConsolePushProvider().sendPush({
206
+ body: "Hello",
207
+ title: "Welcome",
208
+ tokens: ["ExponentPushToken[test]"],
209
+ });
210
+ await new ConsoleVerificationProvider().startVerification({
211
+ channel: "sms",
212
+ to: "+15555550100",
213
+ });
214
+
215
+ assert.deepEqual(messages, [
216
+ "[comms:mail] recipients=1 subjectLength=7",
217
+ "[comms:sms] bodyLength=5",
218
+ "[comms:push] tokens=1 titleLength=7",
219
+ "[comms:verification:start] channel=sms",
220
+ ]);
221
+ assert.notInclude(messages.join(" "), "person@example.com");
222
+ assert.notInclude(messages.join(" "), "+15555550100");
223
+ } finally {
224
+ info.mockRestore();
225
+ }
226
+ });
227
+ });
@@ -0,0 +1,87 @@
1
+ import {logger} from "@terreno/api";
2
+
3
+ import type {
4
+ CheckVerificationOptions,
5
+ MailMessage,
6
+ MailProvider,
7
+ PushMessage,
8
+ PushProvider,
9
+ SendResult,
10
+ SmsMessage,
11
+ SmsProvider,
12
+ StartVerificationOptions,
13
+ VerificationProvider,
14
+ VerificationResult,
15
+ } from "../types";
16
+
17
+ interface ConsoleProviderOptions {
18
+ log?: (message: string) => void;
19
+ }
20
+
21
+ const defaultLog = (message: string): void => {
22
+ logger.info(message);
23
+ };
24
+
25
+ const acceptedResult = (): SendResult => ({accepted: true});
26
+
27
+ export class ConsoleMailProvider implements MailProvider {
28
+ readonly id = "console";
29
+ private readonly log: (message: string) => void;
30
+
31
+ constructor(options?: ConsoleProviderOptions) {
32
+ this.log = options?.log ?? defaultLog;
33
+ }
34
+
35
+ async sendMail(message: MailMessage): Promise<SendResult> {
36
+ const recipientCount = Array.isArray(message.to) ? message.to.length : 1;
37
+ this.log(`[comms:mail] recipients=${recipientCount} subjectLength=${message.subject.length}`);
38
+ return acceptedResult();
39
+ }
40
+ }
41
+
42
+ export class ConsoleSmsProvider implements SmsProvider {
43
+ readonly id = "console";
44
+ private readonly log: (message: string) => void;
45
+
46
+ constructor(options?: ConsoleProviderOptions) {
47
+ this.log = options?.log ?? defaultLog;
48
+ }
49
+
50
+ async sendSms(message: SmsMessage): Promise<SendResult> {
51
+ this.log(`[comms:sms] bodyLength=${message.body.length}`);
52
+ return acceptedResult();
53
+ }
54
+ }
55
+
56
+ export class ConsolePushProvider implements PushProvider {
57
+ readonly id = "console";
58
+ private readonly log: (message: string) => void;
59
+
60
+ constructor(options?: ConsoleProviderOptions) {
61
+ this.log = options?.log ?? defaultLog;
62
+ }
63
+
64
+ async sendPush(message: PushMessage): Promise<SendResult[]> {
65
+ this.log(`[comms:push] tokens=${message.tokens.length} titleLength=${message.title.length}`);
66
+ return message.tokens.map(acceptedResult);
67
+ }
68
+ }
69
+
70
+ export class ConsoleVerificationProvider implements VerificationProvider {
71
+ readonly id = "console";
72
+ private readonly log: (message: string) => void;
73
+
74
+ constructor(options?: ConsoleProviderOptions) {
75
+ this.log = options?.log ?? defaultLog;
76
+ }
77
+
78
+ async checkVerification(_options: CheckVerificationOptions): Promise<VerificationResult> {
79
+ this.log("[comms:verification:check]");
80
+ return {valid: true};
81
+ }
82
+
83
+ async startVerification(options: StartVerificationOptions): Promise<SendResult> {
84
+ this.log(`[comms:verification:start] channel=${options.channel}`);
85
+ return acceptedResult();
86
+ }
87
+ }