@terreno/comms 57.3.0 → 57.6.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 +2 -2
- package/dist/adapters/console.js +48 -109
- package/dist/adapters/expoPush.js +159 -345
- package/dist/adapters/sendgrid.js +94 -164
- package/dist/adapters/twilioSms.js +96 -151
- package/dist/adapters/twilioVerify.js +70 -132
- package/dist/commsApp.js +27 -27
- package/dist/commsQuery.d.ts +1 -0
- package/dist/commsQuery.js +34 -34
- package/dist/commsRetry.d.ts +0 -4
- package/dist/commsRetry.js +24 -33
- package/dist/commsService.js +674 -1091
- package/dist/models/commsMessage.js +64 -172
- package/dist/models/pushToken.js +5 -5
- package/dist/routes/commsDashboard.d.ts +0 -2
- package/dist/routes/commsDashboard.js +175 -328
- package/dist/routes/pushTokens.js +122 -217
- package/dist/templates.js +27 -41
- package/dist/types.d.ts +1 -0
- package/dist/webhooks/sendgridWebhooks.d.ts +0 -6
- package/dist/webhooks/sendgridWebhooks.js +111 -229
- package/dist/webhooks/twilioWebhooks.d.ts +0 -7
- package/dist/webhooks/twilioWebhooks.js +61 -120
- package/package.json +16 -16
- package/src/commsQuery.ts +1 -0
- package/src/commsRetry.ts +4 -4
- package/src/commsService.test.ts +60 -0
- package/src/commsService.ts +7 -0
- package/src/routes/commsDashboard.ts +0 -3
- package/src/types.ts +1 -0
- package/src/webhooks/sendgridWebhooks.ts +1 -1
- package/src/webhooks/twilioWebhooks.ts +1 -1
- package/tsconfig.json +6 -20
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Josh Gachnang",
|
|
3
3
|
"bugs": {
|
|
4
|
-
"url": "https://github.com/
|
|
4
|
+
"url": "https://github.com/TerrenoLabs/terreno/issues"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@terreno/api": "57.
|
|
7
|
+
"@terreno/api": "57.6.0",
|
|
8
8
|
"libphonenumber-js": "^1.13.8",
|
|
9
9
|
"luxon": "^3.7.2"
|
|
10
10
|
},
|
|
11
11
|
"description": "Pluggable mail, SMS, push, and verification providers — part of Terreno, the batteries-included TypeScript framework for universal apps.",
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@biomejs/biome": "^2.3.6",
|
|
14
|
-
"@terreno/test": "57.
|
|
14
|
+
"@terreno/test": "57.6.0",
|
|
15
15
|
"@types/bun": "^1.4.0",
|
|
16
16
|
"@types/express": "^5.0.6",
|
|
17
17
|
"@types/luxon": "^3.7.1",
|
|
@@ -22,27 +22,27 @@
|
|
|
22
22
|
},
|
|
23
23
|
"exports": {
|
|
24
24
|
".": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
25
|
+
"default": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts"
|
|
27
27
|
},
|
|
28
28
|
"./adapters/expoPush": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"default": "./dist/adapters/expoPush.js",
|
|
30
|
+
"types": "./dist/adapters/expoPush.d.ts"
|
|
31
31
|
},
|
|
32
32
|
"./adapters/sendgrid": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
33
|
+
"default": "./dist/adapters/sendgrid.js",
|
|
34
|
+
"types": "./dist/adapters/sendgrid.d.ts"
|
|
35
35
|
},
|
|
36
36
|
"./adapters/twilioSms": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
37
|
+
"default": "./dist/adapters/twilioSms.js",
|
|
38
|
+
"types": "./dist/adapters/twilioSms.d.ts"
|
|
39
39
|
},
|
|
40
40
|
"./adapters/twilioVerify": {
|
|
41
|
-
"
|
|
42
|
-
"
|
|
41
|
+
"default": "./dist/adapters/twilioVerify.js",
|
|
42
|
+
"types": "./dist/adapters/twilioVerify.d.ts"
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
|
-
"homepage": "https://github.com/
|
|
45
|
+
"homepage": "https://github.com/TerrenoLabs/terreno#readme",
|
|
46
46
|
"keywords": [
|
|
47
47
|
"communications",
|
|
48
48
|
"email",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
},
|
|
76
76
|
"repository": {
|
|
77
77
|
"type": "git",
|
|
78
|
-
"url": "git+ssh://git@github.com/
|
|
78
|
+
"url": "git+ssh://git@github.com/TerrenoLabs/terreno.git"
|
|
79
79
|
},
|
|
80
80
|
"scripts": {
|
|
81
81
|
"compile": "bun tsc",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
90
90
|
},
|
|
91
91
|
"types": "dist/index.d.ts",
|
|
92
|
-
"version": "57.
|
|
92
|
+
"version": "57.6.0"
|
|
93
93
|
}
|
package/src/commsQuery.ts
CHANGED
|
@@ -77,6 +77,7 @@ const parseIsoDate = (value: string, field: "startDate" | "endDate"): DateTime =
|
|
|
77
77
|
return parsed;
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
+
/** @internal */
|
|
80
81
|
export const defaultStatsRange = (): {endDate: DateTime; startDate: DateTime} => {
|
|
81
82
|
const endDate = DateTime.utc();
|
|
82
83
|
return {endDate, startDate: endDate.minus({days: DEFAULT_STATS_DAYS})};
|
package/src/commsRetry.ts
CHANGED
|
@@ -4,9 +4,9 @@ import {DateTime} from "luxon";
|
|
|
4
4
|
import type {CommsMessageDocument} from "./modelTypes";
|
|
5
5
|
import type {CommsChannel} from "./types";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const COMMS_RETRY_NOT_RETRYABLE = "comms-retry-not-retryable";
|
|
8
|
+
const COMMS_RETRY_PAYLOAD_EXPIRED = "comms-retry-payload-expired";
|
|
9
|
+
const COMMS_RETRY_CHANNEL_UNCONFIGURED = "comms-retry-channel-unconfigured";
|
|
10
10
|
|
|
11
11
|
export interface CommsRetryBlock {
|
|
12
12
|
code: string;
|
|
@@ -15,7 +15,7 @@ export interface CommsRetryBlock {
|
|
|
15
15
|
|
|
16
16
|
const RETRYABLE_STATUSES = new Set(["bounced", "failed"]);
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
const isPayloadPresent = (message: CommsMessageDocument): boolean => {
|
|
19
19
|
if (message.payload === undefined || message.payload === null) {
|
|
20
20
|
return false;
|
|
21
21
|
}
|
package/src/commsService.test.ts
CHANGED
|
@@ -712,6 +712,66 @@ describe("CommsService", () => {
|
|
|
712
712
|
assert.equal(result.loggedMessageId, String(row._id));
|
|
713
713
|
});
|
|
714
714
|
|
|
715
|
+
it("passes the send option userId to mail and SMS beforeSend hooks", async (): Promise<void> => {
|
|
716
|
+
const seenUserIds: Array<string | undefined> = [];
|
|
717
|
+
const mailUserId = new mongoose.Types.ObjectId();
|
|
718
|
+
const smsUserId = new mongoose.Types.ObjectId();
|
|
719
|
+
const service = new CommsService({
|
|
720
|
+
beforeSend: async (context): Promise<undefined> => {
|
|
721
|
+
seenUserIds.push(context.userId);
|
|
722
|
+
return undefined;
|
|
723
|
+
},
|
|
724
|
+
mail: {
|
|
725
|
+
id: "user-aware-mail",
|
|
726
|
+
sendMail: async (): Promise<SendResult> => ({accepted: true}),
|
|
727
|
+
},
|
|
728
|
+
sms: {
|
|
729
|
+
id: "user-aware-sms",
|
|
730
|
+
sendSms: async (): Promise<SendResult> => ({accepted: true}),
|
|
731
|
+
},
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
await service.sendMail({subject: "Welcome", to: "person@example.com"}, {userId: mailUserId});
|
|
735
|
+
await service.sendSms({body: "Hello", to: "+15555550100"}, {userId: smsUserId});
|
|
736
|
+
|
|
737
|
+
assert.deepEqual(seenUserIds, [String(mailUserId), String(smsUserId)]);
|
|
738
|
+
const mailLog = await CommsMessage.findExactlyOne({channel: "mail"});
|
|
739
|
+
const smsLog = await CommsMessage.findExactlyOne({channel: "sms"});
|
|
740
|
+
assert.equal(String(mailLog.userId), String(mailUserId));
|
|
741
|
+
assert.equal(String(smsLog.userId), String(smsUserId));
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
it("preserves the original userId when retrying mail", async (): Promise<void> => {
|
|
745
|
+
const userId = new mongoose.Types.ObjectId();
|
|
746
|
+
const seenUserIds: Array<string | undefined> = [];
|
|
747
|
+
const service = new CommsService({
|
|
748
|
+
beforeSend: async (context): Promise<undefined> => {
|
|
749
|
+
seenUserIds.push(context.userId);
|
|
750
|
+
return undefined;
|
|
751
|
+
},
|
|
752
|
+
mail: {
|
|
753
|
+
id: "retry-user-aware-mail",
|
|
754
|
+
sendMail: async (): Promise<SendResult> => ({accepted: true}),
|
|
755
|
+
},
|
|
756
|
+
});
|
|
757
|
+
const original = await CommsMessage.create({
|
|
758
|
+
channel: "mail",
|
|
759
|
+
payload: {subject: "Retry me", to: "person@example.com"},
|
|
760
|
+
payloadExpiresAt: DateTime.utc().plus({days: 1}).toJSDate(),
|
|
761
|
+
provider: "retry-user-aware-mail",
|
|
762
|
+
status: "failed",
|
|
763
|
+
subject: "Retry me",
|
|
764
|
+
to: "person@example.com",
|
|
765
|
+
userId,
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
await service.retryMessage({messageId: String(original._id)});
|
|
769
|
+
|
|
770
|
+
assert.deepEqual(seenUserIds, [String(userId)]);
|
|
771
|
+
const retried = await CommsMessage.findExactlyOne({retriedFromId: original._id});
|
|
772
|
+
assert.equal(String(retried.userId), String(userId));
|
|
773
|
+
});
|
|
774
|
+
|
|
715
775
|
it("attaches loggedMessageId when beforeSend cancels a push send", async (): Promise<void> => {
|
|
716
776
|
const userId = new mongoose.Types.ObjectId();
|
|
717
777
|
await PushToken.upsert(
|
package/src/commsService.ts
CHANGED
|
@@ -632,6 +632,7 @@ export class CommsService {
|
|
|
632
632
|
isRetry: sendOptions?.isRetry === true,
|
|
633
633
|
message: this.applyMailDefaults(message),
|
|
634
634
|
provider: provider.id,
|
|
635
|
+
userId: sendOptions?.userId ? String(sendOptions.userId) : undefined,
|
|
635
636
|
};
|
|
636
637
|
const before = await this.applyBeforeSend(context, hookErrors);
|
|
637
638
|
const activeMessage = this.applyMailDefaults(before.message as MailMessage);
|
|
@@ -651,6 +652,7 @@ export class CommsService {
|
|
|
651
652
|
subject: activeMessage.subject,
|
|
652
653
|
templateId: activeMessage.templateId,
|
|
653
654
|
to: activeMessage.to,
|
|
655
|
+
userId: context.userId,
|
|
654
656
|
});
|
|
655
657
|
return this.withLoggedId(result, logged);
|
|
656
658
|
}
|
|
@@ -667,6 +669,7 @@ export class CommsService {
|
|
|
667
669
|
subject: activeMessage.subject,
|
|
668
670
|
templateId: activeMessage.templateId,
|
|
669
671
|
to: activeMessage.to,
|
|
672
|
+
userId: context.userId,
|
|
670
673
|
},
|
|
671
674
|
provider: provider.id,
|
|
672
675
|
retry: (): Promise<SendResult> => this.sendMailOnce(provider, activeMessage),
|
|
@@ -682,6 +685,7 @@ export class CommsService {
|
|
|
682
685
|
isRetry: sendOptions?.isRetry === true,
|
|
683
686
|
message,
|
|
684
687
|
provider: provider.id,
|
|
688
|
+
userId: sendOptions?.userId ? String(sendOptions.userId) : undefined,
|
|
685
689
|
};
|
|
686
690
|
const before = await this.applyBeforeSend(context, hookErrors);
|
|
687
691
|
const activeMessage = before.message as SmsMessage;
|
|
@@ -699,6 +703,7 @@ export class CommsService {
|
|
|
699
703
|
retriedFromId: sendOptions?.retriedFromId,
|
|
700
704
|
status: "cancelled",
|
|
701
705
|
to: activeMessage.to,
|
|
706
|
+
userId: context.userId,
|
|
702
707
|
});
|
|
703
708
|
return this.withLoggedId(result, logged);
|
|
704
709
|
}
|
|
@@ -713,6 +718,7 @@ export class CommsService {
|
|
|
713
718
|
metadata: this.mergeSendMetadata(first, sendOptions),
|
|
714
719
|
retriedFromId: sendOptions?.retriedFromId,
|
|
715
720
|
to: activeMessage.to,
|
|
721
|
+
userId: context.userId,
|
|
716
722
|
},
|
|
717
723
|
provider: provider.id,
|
|
718
724
|
retry: (): Promise<SendResult> => this.sendSmsOnce(provider, activeMessage),
|
|
@@ -998,6 +1004,7 @@ export class CommsService {
|
|
|
998
1004
|
: undefined,
|
|
999
1005
|
isRetry: true,
|
|
1000
1006
|
retriedFromId: String(original._id),
|
|
1007
|
+
userId: original.userId,
|
|
1001
1008
|
};
|
|
1002
1009
|
const payload = original.payload as Record<string, unknown>;
|
|
1003
1010
|
let sendResult: SendResult | SendResult[] | undefined;
|
package/src/types.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -1,32 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"allowJs": true,
|
|
4
|
-
"allowSyntheticDefaultImports": true,
|
|
5
|
-
"baseUrl": "./",
|
|
6
3
|
"declaration": true,
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"ignoreDeprecations": "6.0",
|
|
11
|
-
"lib": ["es2017", "dom", "es2017.object"],
|
|
12
|
-
"module": "commonjs",
|
|
13
|
-
"moduleResolution": "node",
|
|
14
|
-
"noEmit": false,
|
|
4
|
+
"isolatedModules": true,
|
|
5
|
+
"lib": ["es2023"],
|
|
6
|
+
"module": "nodenext",
|
|
15
7
|
"noImplicitAny": false,
|
|
16
8
|
"noImplicitReturns": true,
|
|
17
|
-
"noImplicitThis": true,
|
|
18
|
-
"noUnusedLocals": false,
|
|
19
|
-
"noUnusedParameters": false,
|
|
20
9
|
"outDir": "./dist",
|
|
21
|
-
"preserveConstEnums": true,
|
|
22
|
-
"removeComments": false,
|
|
23
10
|
"rootDir": "./src",
|
|
24
11
|
"skipLibCheck": true,
|
|
25
12
|
"strict": true,
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"target": "es5"
|
|
13
|
+
"target": "es2023",
|
|
14
|
+
"types": ["node"]
|
|
29
15
|
},
|
|
30
16
|
"exclude": ["node_modules", "src/**/*.test.ts", "src/tests/**"],
|
|
31
|
-
"include": ["src
|
|
17
|
+
"include": ["src"]
|
|
32
18
|
}
|