@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.
- package/LICENSE +21 -0
- package/biome.jsonc +17 -0
- package/bunfig.toml +5 -0
- package/dist/adapters/console.d.ts +30 -0
- package/dist/adapters/console.js +122 -0
- package/dist/commsApp.d.ts +14 -0
- package/dist/commsApp.js +37 -0
- package/dist/commsService.d.ts +20 -0
- package/dist/commsService.js +386 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +19 -0
- package/dist/modelTypes.d.ts +53 -0
- package/dist/modelTypes.js +2 -0
- package/dist/models/commsMessage.d.ts +2 -0
- package/dist/models/commsMessage.js +119 -0
- package/dist/models/pushToken.d.ts +2 -0
- package/dist/models/pushToken.js +56 -0
- package/dist/routes/commsExplorer.d.ts +7 -0
- package/dist/routes/commsExplorer.js +147 -0
- package/dist/routes/pushTokens.d.ts +7 -0
- package/dist/routes/pushTokens.js +292 -0
- package/dist/templates.d.ts +10 -0
- package/dist/templates.js +32 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +2 -0
- package/package.json +55 -0
- package/src/adapters/console.test.ts +227 -0
- package/src/adapters/console.ts +87 -0
- package/src/commsApp.test.ts +448 -0
- package/src/commsApp.ts +48 -0
- package/src/commsService.test.ts +570 -0
- package/src/commsService.ts +293 -0
- package/src/index.ts +45 -0
- package/src/modelTypes.ts +78 -0
- package/src/models/commsMessage.test.ts +53 -0
- package/src/models/commsMessage.ts +97 -0
- package/src/models/pushToken.test.ts +45 -0
- package/src/models/pushToken.ts +69 -0
- package/src/routes/commsExplorer.ts +114 -0
- package/src/routes/pushTokens.ts +265 -0
- package/src/templates.test.ts +37 -0
- package/src/templates.ts +31 -0
- package/src/tests/bunSetup.ts +14 -0
- package/src/types.ts +93 -0
- package/tsconfig.json +32 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
+
if (!m) return o;
|
|
52
|
+
var i = m.call(o), r, ar = [], e;
|
|
53
|
+
try {
|
|
54
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
+
}
|
|
56
|
+
catch (error) { e = { error: error }; }
|
|
57
|
+
finally {
|
|
58
|
+
try {
|
|
59
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
+
}
|
|
61
|
+
finally { if (e) throw e.error; }
|
|
62
|
+
}
|
|
63
|
+
return ar;
|
|
64
|
+
};
|
|
65
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
exports.addPushTokenRoutes = void 0;
|
|
67
|
+
var api_1 = require("@terreno/api");
|
|
68
|
+
var luxon_1 = require("luxon");
|
|
69
|
+
var pushToken_1 = require("../models/pushToken");
|
|
70
|
+
var platformValues = ["android", "ios", "web"];
|
|
71
|
+
var openApiOptions = function (openApi) {
|
|
72
|
+
return openApi ? { openApi: openApi } : {};
|
|
73
|
+
};
|
|
74
|
+
var getAuthenticatedUser = function (req) {
|
|
75
|
+
var user = req.user;
|
|
76
|
+
if (!(user === null || user === void 0 ? void 0 : user._id)) {
|
|
77
|
+
throw new api_1.APIError({ status: 401, title: "Authentication required" });
|
|
78
|
+
}
|
|
79
|
+
return user;
|
|
80
|
+
};
|
|
81
|
+
var isDuplicateKeyError = function (error) {
|
|
82
|
+
return (error === null || error === void 0 ? void 0 : error.code) === 11000;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Claims a device token for the caller in a single atomic statement.
|
|
86
|
+
*
|
|
87
|
+
* The filter only matches a token that is unclaimed, already the caller's, or released by its
|
|
88
|
+
* previous owner, so a concurrent request can never transfer an active token between accounts.
|
|
89
|
+
* A token held by another user fails the filter, and the unique index turns the resulting upsert
|
|
90
|
+
* insert into a duplicate-key error that surfaces as a conflict.
|
|
91
|
+
*/
|
|
92
|
+
var claimPushToken = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
93
|
+
var now, claimableFilter, result;
|
|
94
|
+
var _c;
|
|
95
|
+
var deviceId = _b.deviceId, platform = _b.platform, token = _b.token, userId = _b.userId;
|
|
96
|
+
return __generator(this, function (_d) {
|
|
97
|
+
switch (_d.label) {
|
|
98
|
+
case 0:
|
|
99
|
+
now = luxon_1.DateTime.utc().toJSDate();
|
|
100
|
+
claimableFilter = {
|
|
101
|
+
$or: [{ userId: userId }, { active: false }],
|
|
102
|
+
token: token,
|
|
103
|
+
};
|
|
104
|
+
return [4 /*yield*/, pushToken_1.PushToken.findOneAndUpdate(claimableFilter, {
|
|
105
|
+
// createdUpdatedPlugin only hooks save(), so timestamps are maintained here.
|
|
106
|
+
$set: __assign(__assign({ active: true, deleted: false }, (deviceId === undefined ? {} : { deviceId: deviceId })), { lastSeenAt: now, platform: platform, updated: now, userId: userId }),
|
|
107
|
+
$setOnInsert: { created: now },
|
|
108
|
+
}, {
|
|
109
|
+
includeResultMetadata: true,
|
|
110
|
+
returnDocument: "after",
|
|
111
|
+
runValidators: true,
|
|
112
|
+
upsert: true,
|
|
113
|
+
})];
|
|
114
|
+
case 1:
|
|
115
|
+
result = _d.sent();
|
|
116
|
+
if (!result.value) {
|
|
117
|
+
throw new api_1.APIError({ status: 500, title: "Push token claim returned no document" });
|
|
118
|
+
}
|
|
119
|
+
return [2 /*return*/, { created: ((_c = result.lastErrorObject) === null || _c === void 0 ? void 0 : _c.updatedExisting) !== true, document: result.value }];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}); };
|
|
123
|
+
var addPushTokenRoutes = function (app, options) {
|
|
124
|
+
var _a;
|
|
125
|
+
var basePath = (_a = options === null || options === void 0 ? void 0 : options.basePath) !== null && _a !== void 0 ? _a : "/comms/pushTokens";
|
|
126
|
+
var routeOpenApi = openApiOptions(options === null || options === void 0 ? void 0 : options.openApi);
|
|
127
|
+
// A dedicated create endpoint is required because modelRouter create cannot provide
|
|
128
|
+
// the idempotent token upsert contract used by device registration.
|
|
129
|
+
app.post(basePath, [
|
|
130
|
+
(0, api_1.authenticateMiddleware)(),
|
|
131
|
+
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
132
|
+
.withTags(["comms"])
|
|
133
|
+
.withSummary("Register or refresh a push token")
|
|
134
|
+
.withRequestBody({
|
|
135
|
+
deviceId: { type: "string" },
|
|
136
|
+
platform: {
|
|
137
|
+
description: "Device platform: android, ios, or web",
|
|
138
|
+
required: true,
|
|
139
|
+
type: "string",
|
|
140
|
+
},
|
|
141
|
+
token: { required: true, type: "string" },
|
|
142
|
+
})
|
|
143
|
+
.withResponse(200, { data: { type: "object" } })
|
|
144
|
+
.withResponse(201, { data: { type: "object" } })
|
|
145
|
+
.build(),
|
|
146
|
+
], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
147
|
+
var user, body, claim, result, error_1, retryError_1;
|
|
148
|
+
var _a;
|
|
149
|
+
return __generator(this, function (_b) {
|
|
150
|
+
switch (_b.label) {
|
|
151
|
+
case 0:
|
|
152
|
+
user = getAuthenticatedUser(req);
|
|
153
|
+
body = req.body;
|
|
154
|
+
if (!((_a = body.token) === null || _a === void 0 ? void 0 : _a.trim())) {
|
|
155
|
+
throw new api_1.APIError({ status: 400, title: "Push token is required" });
|
|
156
|
+
}
|
|
157
|
+
if (!body.platform || !platformValues.includes(body.platform)) {
|
|
158
|
+
throw new api_1.APIError({ status: 400, title: "Valid push platform is required" });
|
|
159
|
+
}
|
|
160
|
+
claim = {
|
|
161
|
+
deviceId: body.deviceId,
|
|
162
|
+
platform: body.platform,
|
|
163
|
+
token: body.token,
|
|
164
|
+
userId: user._id,
|
|
165
|
+
};
|
|
166
|
+
_b.label = 1;
|
|
167
|
+
case 1:
|
|
168
|
+
_b.trys.push([1, 3, , 8]);
|
|
169
|
+
return [4 /*yield*/, claimPushToken(claim)];
|
|
170
|
+
case 2:
|
|
171
|
+
result = _b.sent();
|
|
172
|
+
return [3 /*break*/, 8];
|
|
173
|
+
case 3:
|
|
174
|
+
error_1 = _b.sent();
|
|
175
|
+
if (!isDuplicateKeyError(error_1)) {
|
|
176
|
+
throw error_1;
|
|
177
|
+
}
|
|
178
|
+
_b.label = 4;
|
|
179
|
+
case 4:
|
|
180
|
+
_b.trys.push([4, 6, , 7]);
|
|
181
|
+
return [4 /*yield*/, claimPushToken(claim)];
|
|
182
|
+
case 5:
|
|
183
|
+
result = _b.sent();
|
|
184
|
+
return [3 /*break*/, 7];
|
|
185
|
+
case 6:
|
|
186
|
+
retryError_1 = _b.sent();
|
|
187
|
+
if (!isDuplicateKeyError(retryError_1)) {
|
|
188
|
+
throw retryError_1;
|
|
189
|
+
}
|
|
190
|
+
throw new api_1.APIError({ status: 409, title: "Push token is registered to another user" });
|
|
191
|
+
case 7: return [3 /*break*/, 8];
|
|
192
|
+
case 8: return [2 /*return*/, res.status(result.created ? 201 : 200).json({ data: result.document })];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}); }));
|
|
196
|
+
app.get(basePath, [
|
|
197
|
+
(0, api_1.authenticateMiddleware)(),
|
|
198
|
+
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
199
|
+
.withTags(["comms"])
|
|
200
|
+
.withSummary("List the current user's push tokens")
|
|
201
|
+
.withQueryParameter("page", { type: "number" }, { required: false })
|
|
202
|
+
.withQueryParameter("limit", { type: "number" }, { required: false })
|
|
203
|
+
.withQueryParameter("active", { type: "boolean" }, { required: false })
|
|
204
|
+
.withQueryParameter("platform", { type: "string" }, { required: false })
|
|
205
|
+
.withResponse(200, {
|
|
206
|
+
data: { items: { type: "object" }, type: "array" },
|
|
207
|
+
limit: { type: "number" },
|
|
208
|
+
more: { type: "boolean" },
|
|
209
|
+
page: { type: "number" },
|
|
210
|
+
total: { type: "number" },
|
|
211
|
+
})
|
|
212
|
+
.build(),
|
|
213
|
+
], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
214
|
+
var user, page, limit, skip, match, _a, tokens, total;
|
|
215
|
+
return __generator(this, function (_b) {
|
|
216
|
+
switch (_b.label) {
|
|
217
|
+
case 0:
|
|
218
|
+
user = getAuthenticatedUser(req);
|
|
219
|
+
page = Math.max(1, Number.parseInt(req.query.page, 10) || 1);
|
|
220
|
+
limit = Math.min(100, Math.max(1, Number.parseInt(req.query.limit, 10) || 20));
|
|
221
|
+
skip = (page - 1) * limit;
|
|
222
|
+
match = { userId: user._id };
|
|
223
|
+
if (req.query.active === "true" || req.query.active === "false") {
|
|
224
|
+
match.active = req.query.active === "true";
|
|
225
|
+
}
|
|
226
|
+
if (req.query.platform) {
|
|
227
|
+
match.platform = req.query.platform;
|
|
228
|
+
}
|
|
229
|
+
return [4 /*yield*/, Promise.all([
|
|
230
|
+
pushToken_1.PushToken.find(match).sort("-lastSeenAt").skip(skip).limit(limit),
|
|
231
|
+
pushToken_1.PushToken.countDocuments(match),
|
|
232
|
+
])];
|
|
233
|
+
case 1:
|
|
234
|
+
_a = __read.apply(void 0, [_b.sent(), 2]), tokens = _a[0], total = _a[1];
|
|
235
|
+
return [2 /*return*/, res.json({
|
|
236
|
+
data: tokens,
|
|
237
|
+
limit: limit,
|
|
238
|
+
more: skip + limit < total,
|
|
239
|
+
page: page,
|
|
240
|
+
total: total,
|
|
241
|
+
})];
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}); }));
|
|
245
|
+
app.delete("".concat(basePath, "/:id"), [
|
|
246
|
+
(0, api_1.authenticateMiddleware)(),
|
|
247
|
+
(0, api_1.createOpenApiBuilder)(routeOpenApi)
|
|
248
|
+
.withTags(["comms"])
|
|
249
|
+
.withSummary("Deactivate a push token")
|
|
250
|
+
.withPathParameter("id", { type: "string" })
|
|
251
|
+
.withResponse(204, {})
|
|
252
|
+
.build(),
|
|
253
|
+
], (0, api_1.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
|
|
254
|
+
var user, token, isOwner;
|
|
255
|
+
return __generator(this, function (_a) {
|
|
256
|
+
switch (_a.label) {
|
|
257
|
+
case 0:
|
|
258
|
+
user = getAuthenticatedUser(req);
|
|
259
|
+
return [4 /*yield*/, pushToken_1.PushToken.findExactlyOne({ _id: req.params.id })];
|
|
260
|
+
case 1:
|
|
261
|
+
token = _a.sent();
|
|
262
|
+
isOwner = token.userId.toString() === String(user._id);
|
|
263
|
+
if (!isOwner) {
|
|
264
|
+
throw new api_1.APIError({ status: 403, title: "Push token belongs to another user" });
|
|
265
|
+
}
|
|
266
|
+
token.active = false;
|
|
267
|
+
return [4 /*yield*/, token.save()];
|
|
268
|
+
case 2:
|
|
269
|
+
_a.sent();
|
|
270
|
+
return [2 /*return*/, res.status(204).send()];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}); }));
|
|
274
|
+
var routerOptions = __assign(__assign({}, routeOpenApi), {
|
|
275
|
+
// Share the "comms" tag with the custom routes so generated SDK mutations invalidate this read.
|
|
276
|
+
openApiOverwrite: { get: { tags: ["comms"] } }, permissions: {
|
|
277
|
+
create: [],
|
|
278
|
+
delete: [],
|
|
279
|
+
list: [],
|
|
280
|
+
read: [api_1.Permissions.IsOwner],
|
|
281
|
+
update: [],
|
|
282
|
+
}, queryFields: ["active", "platform"],
|
|
283
|
+
// PushToken persists userId, while OwnerQueryFilter targets a persisted ownerId field.
|
|
284
|
+
queryFilter: function (user) {
|
|
285
|
+
if (!user) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
return { userId: user.id };
|
|
289
|
+
}, sort: "-lastSeenAt" });
|
|
290
|
+
app.use(basePath, (0, api_1.modelRouter)(pushToken_1.PushToken, routerOptions));
|
|
291
|
+
};
|
|
292
|
+
exports.addPushTokenRoutes = addPushTokenRoutes;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface MessageTemplate {
|
|
2
|
+
html?: string;
|
|
3
|
+
subject: string;
|
|
4
|
+
text?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface RenderTemplateOptions {
|
|
7
|
+
data: Record<string, unknown>;
|
|
8
|
+
template: MessageTemplate;
|
|
9
|
+
}
|
|
10
|
+
export declare const renderTemplate: ({ data, template }: RenderTemplateOptions) => MessageTemplate;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.renderTemplate = void 0;
|
|
15
|
+
var TEMPLATE_VARIABLE_PATTERN = /{{\s*([A-Za-z0-9_]+)\s*}}/g;
|
|
16
|
+
var interpolate = function (value, data) {
|
|
17
|
+
return value.replace(TEMPLATE_VARIABLE_PATTERN, function (_match, key) {
|
|
18
|
+
if (Object.getOwnPropertyDescriptor(data, key) === undefined) {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
var replacement = data[key];
|
|
22
|
+
if (replacement === null || replacement === undefined) {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
return String(replacement);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
var renderTemplate = function (_a) {
|
|
29
|
+
var data = _a.data, template = _a.template;
|
|
30
|
+
return (__assign(__assign(__assign({}, (template.html === undefined ? {} : { html: interpolate(template.html, data) })), { subject: interpolate(template.subject, data) }), (template.text === undefined ? {} : { text: interpolate(template.text, data) })));
|
|
31
|
+
};
|
|
32
|
+
exports.renderTemplate = renderTemplate;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type mongoose from "mongoose";
|
|
2
|
+
export interface SendResult {
|
|
3
|
+
accepted: boolean;
|
|
4
|
+
error?: string;
|
|
5
|
+
isPermanentFailure?: boolean;
|
|
6
|
+
providerMessageId?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface MailMessage {
|
|
9
|
+
from?: string;
|
|
10
|
+
html?: string;
|
|
11
|
+
metadata?: Record<string, string>;
|
|
12
|
+
replyTo?: string;
|
|
13
|
+
subject: string;
|
|
14
|
+
text?: string;
|
|
15
|
+
to: string | string[];
|
|
16
|
+
}
|
|
17
|
+
export interface MailProvider {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
sendMail(message: MailMessage): Promise<SendResult>;
|
|
20
|
+
}
|
|
21
|
+
export interface SmsMessage {
|
|
22
|
+
body: string;
|
|
23
|
+
to: string;
|
|
24
|
+
}
|
|
25
|
+
export interface SmsProvider {
|
|
26
|
+
readonly id: string;
|
|
27
|
+
sendSms(message: SmsMessage): Promise<SendResult>;
|
|
28
|
+
}
|
|
29
|
+
export interface PushMessage {
|
|
30
|
+
badge?: number;
|
|
31
|
+
body: string;
|
|
32
|
+
data?: Record<string, unknown>;
|
|
33
|
+
sound?: string | null;
|
|
34
|
+
title: string;
|
|
35
|
+
tokens: string[];
|
|
36
|
+
}
|
|
37
|
+
export interface PushProvider {
|
|
38
|
+
readonly id: string;
|
|
39
|
+
sendPush(message: PushMessage): Promise<SendResult[]>;
|
|
40
|
+
}
|
|
41
|
+
export interface StartVerificationOptions {
|
|
42
|
+
channel: "email" | "sms";
|
|
43
|
+
to: string;
|
|
44
|
+
}
|
|
45
|
+
export interface CheckVerificationOptions {
|
|
46
|
+
code: string;
|
|
47
|
+
to: string;
|
|
48
|
+
}
|
|
49
|
+
export interface VerificationResult {
|
|
50
|
+
error?: string;
|
|
51
|
+
valid: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface VerificationProvider {
|
|
54
|
+
readonly id: string;
|
|
55
|
+
checkVerification(options: CheckVerificationOptions): Promise<VerificationResult>;
|
|
56
|
+
startVerification(options: StartVerificationOptions): Promise<SendResult>;
|
|
57
|
+
}
|
|
58
|
+
export interface DeliveryEvent {
|
|
59
|
+
channel: "mail" | "push" | "sms";
|
|
60
|
+
providerMessageId: string;
|
|
61
|
+
raw?: unknown;
|
|
62
|
+
status: "bounced" | "delivered" | "failed" | "opened";
|
|
63
|
+
}
|
|
64
|
+
export type CommsChannel = "mail" | "push" | "sms" | "verification";
|
|
65
|
+
export type CommsMessageStatus = "bounced" | "delivered" | "failed" | "sent";
|
|
66
|
+
export interface CommsOptions {
|
|
67
|
+
defaultFrom?: string;
|
|
68
|
+
logMessages?: boolean;
|
|
69
|
+
mail?: MailProvider;
|
|
70
|
+
onDeliveryEvent?: (event: DeliveryEvent) => Promise<void>;
|
|
71
|
+
push?: PushProvider;
|
|
72
|
+
redactRecipients?: boolean;
|
|
73
|
+
sms?: SmsProvider;
|
|
74
|
+
verification?: VerificationProvider;
|
|
75
|
+
}
|
|
76
|
+
export interface SendPushToUserMessage extends Omit<PushMessage, "tokens"> {
|
|
77
|
+
userId: mongoose.Types.ObjectId | string;
|
|
78
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Josh Gachnang",
|
|
3
|
+
"bugs": {
|
|
4
|
+
"url": "https://github.com/FlourishHealth/terreno/issues"
|
|
5
|
+
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@terreno/api": "56.0.0-beta.2",
|
|
8
|
+
"luxon": "^3.7.2"
|
|
9
|
+
},
|
|
10
|
+
"description": "Pluggable communications providers for Terreno backends",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@biomejs/biome": "^2.3.6",
|
|
13
|
+
"@terreno/test": "56.0.0-beta.2",
|
|
14
|
+
"@types/bun": "^1.3.8",
|
|
15
|
+
"@types/express": "^5.0.6",
|
|
16
|
+
"@types/luxon": "^3.7.1",
|
|
17
|
+
"@types/supertest": "^7.2.0",
|
|
18
|
+
"chai": "^6.2.2",
|
|
19
|
+
"supertest": "^7.2.2",
|
|
20
|
+
"typescript": "~6.0.3"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/FlourishHealth/terreno#readme",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"communications",
|
|
25
|
+
"email",
|
|
26
|
+
"sms",
|
|
27
|
+
"push",
|
|
28
|
+
"terreno"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"main": "dist/index.js",
|
|
32
|
+
"name": "@terreno/comms",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"mongoose": "^8.0.0 || ^9.0.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+ssh://git@github.com/FlourishHealth/terreno.git"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"compile": "bun tsc",
|
|
45
|
+
"compile:watch": "bun tsc -w",
|
|
46
|
+
"dev": "bun tsc -w",
|
|
47
|
+
"lint": "biome check ./src",
|
|
48
|
+
"lint:fix": "biome check --write ./src",
|
|
49
|
+
"test": "bun test",
|
|
50
|
+
"test:ci": "bun test",
|
|
51
|
+
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
52
|
+
},
|
|
53
|
+
"types": "dist/index.d.ts",
|
|
54
|
+
"version": "56.0.0-beta.2"
|
|
55
|
+
}
|