@terreno/comms 57.2.0 → 57.3.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 +58 -0
- package/dist/adapters/expoPush.d.ts +54 -0
- package/dist/adapters/expoPush.js +400 -0
- package/dist/adapters/sendgrid.d.ts +3 -0
- package/dist/adapters/sendgrid.js +7 -1
- package/dist/adapters/twilioSms.d.ts +36 -0
- package/dist/adapters/twilioSms.js +232 -0
- package/dist/adapters/twilioVerify.d.ts +44 -0
- package/dist/adapters/twilioVerify.js +212 -0
- package/dist/commsApp.d.ts +4 -1
- package/dist/commsApp.js +28 -2
- package/dist/commsQuery.d.ts +30 -0
- package/dist/commsQuery.js +134 -0
- package/dist/commsRetry.d.ts +16 -0
- package/dist/commsRetry.js +79 -0
- package/dist/commsService.d.ts +11 -4
- package/dist/commsService.js +217 -47
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -1
- package/dist/routes/commsDashboard.d.ts +11 -0
- package/dist/routes/commsDashboard.js +414 -0
- package/dist/templates.d.ts +9 -0
- package/dist/templates.js +34 -1
- package/dist/types.d.ts +11 -0
- package/dist/webhooks/sendgridWebhooks.d.ts +16 -0
- package/dist/webhooks/sendgridWebhooks.js +251 -0
- package/dist/webhooks/twilioWebhooks.d.ts +18 -0
- package/dist/webhooks/twilioWebhooks.js +175 -0
- package/package.json +26 -5
- package/src/adapters/expoPush.test.ts +585 -0
- package/src/adapters/expoPush.ts +304 -0
- package/src/adapters/sendgrid.ts +8 -0
- package/src/adapters/twilioSms.test.ts +374 -0
- package/src/adapters/twilioSms.ts +237 -0
- package/src/adapters/twilioVerify.test.ts +207 -0
- package/src/adapters/twilioVerify.ts +227 -0
- package/src/commsApp.test.ts +13 -0
- package/src/commsApp.ts +38 -3
- package/src/commsQuery.test.ts +165 -0
- package/src/commsQuery.ts +162 -0
- package/src/commsRetry.ts +87 -0
- package/src/commsService.test.ts +68 -0
- package/src/commsService.ts +177 -20
- package/src/index.ts +9 -2
- package/src/routes/commsDashboard.test.ts +310 -0
- package/src/routes/commsDashboard.ts +327 -0
- package/src/templates.test.ts +63 -1
- package/src/templates.ts +47 -0
- package/src/tests/bunSetup.ts +8 -0
- package/src/types.ts +13 -0
- package/src/webhooks/sendgridWebhooks.test.ts +520 -0
- package/src/webhooks/sendgridWebhooks.ts +166 -0
- package/src/webhooks/twilioWebhooks.test.ts +352 -0
- package/src/webhooks/twilioWebhooks.ts +154 -0
- package/dist/routes/commsExplorer.d.ts +0 -7
- package/dist/routes/commsExplorer.js +0 -147
- package/src/routes/commsExplorer.ts +0 -114
|
@@ -0,0 +1,232 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.TwilioSmsProvider = void 0;
|
|
51
|
+
var node_module_1 = require("node:module");
|
|
52
|
+
var api_1 = require("@terreno/api");
|
|
53
|
+
var libphonenumber_js_1 = require("libphonenumber-js");
|
|
54
|
+
var nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
55
|
+
var TWILIO_SMS_CONSOLE_BASE = "https://console.twilio.com/us1/monitor/logs/sms";
|
|
56
|
+
var twilioMessageConsoleUrl = function (sid) { return "".concat(TWILIO_SMS_CONSOLE_BASE, "/").concat(sid); };
|
|
57
|
+
var PERMANENT_CODES = new Set([21211, 21214, 21217, 21408, 21610, 30003, 30005, 30006, 30007]);
|
|
58
|
+
var TRANSIENT_CODES = new Set([20429, 30001, 30002]);
|
|
59
|
+
var CONFIG_CODES = new Set([20003, 20404]);
|
|
60
|
+
var resolveCredentials = function (options) {
|
|
61
|
+
var _a, _b;
|
|
62
|
+
var accountSid = (_a = options === null || options === void 0 ? void 0 : options.accountSid) !== null && _a !== void 0 ? _a : process.env.TWILIO_ACCOUNT_SID;
|
|
63
|
+
var authToken = (_b = options === null || options === void 0 ? void 0 : options.authToken) !== null && _b !== void 0 ? _b : process.env.TWILIO_AUTH_TOKEN;
|
|
64
|
+
if (!accountSid || !authToken) {
|
|
65
|
+
throw new Error("TwilioSmsProvider requires TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN or matching constructor options");
|
|
66
|
+
}
|
|
67
|
+
return { accountSid: accountSid, authToken: authToken };
|
|
68
|
+
};
|
|
69
|
+
var loadTwilio = function () {
|
|
70
|
+
try {
|
|
71
|
+
// Optional peer — keep `twilio` out of core dependencies.
|
|
72
|
+
return nodeRequire("twilio");
|
|
73
|
+
}
|
|
74
|
+
catch (_a) {
|
|
75
|
+
throw new Error("TwilioSmsProvider requires optional peer dependency twilio. " +
|
|
76
|
+
"Install it with: bun add twilio");
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var toE164 = function (to) {
|
|
80
|
+
var parsed = (0, libphonenumber_js_1.parsePhoneNumberFromString)(to);
|
|
81
|
+
if (!(parsed === null || parsed === void 0 ? void 0 : parsed.isValid())) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return parsed.format("E.164");
|
|
85
|
+
};
|
|
86
|
+
var invalidDestinationResult = function () { return ({
|
|
87
|
+
accepted: false,
|
|
88
|
+
error: "Destination must be a valid E.164 phone number",
|
|
89
|
+
errorClass: "permanent",
|
|
90
|
+
errorCode: "twilio-invalid-destination",
|
|
91
|
+
isPermanentFailure: true,
|
|
92
|
+
}); };
|
|
93
|
+
var twilioErrorFields = function (error) {
|
|
94
|
+
if (!error || typeof error !== "object") {
|
|
95
|
+
return {
|
|
96
|
+
message: error instanceof Error ? error.message : "Twilio send failed",
|
|
97
|
+
payload: { error: error },
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
var raw = error;
|
|
101
|
+
var code = typeof raw.code === "number" ? raw.code : undefined;
|
|
102
|
+
var status = typeof raw.status === "number" ? raw.status : undefined;
|
|
103
|
+
var message = typeof raw.message === "string" && raw.message.length > 0
|
|
104
|
+
? raw.message
|
|
105
|
+
: error instanceof Error
|
|
106
|
+
? error.message
|
|
107
|
+
: "Twilio send failed";
|
|
108
|
+
var payload = {};
|
|
109
|
+
if (code !== undefined) {
|
|
110
|
+
payload.code = code;
|
|
111
|
+
}
|
|
112
|
+
if (status !== undefined) {
|
|
113
|
+
payload.status = status;
|
|
114
|
+
}
|
|
115
|
+
payload.message = message;
|
|
116
|
+
if (typeof raw.moreInfo === "string") {
|
|
117
|
+
payload.moreInfo = raw.moreInfo;
|
|
118
|
+
}
|
|
119
|
+
return { code: code, message: message, payload: payload, status: status };
|
|
120
|
+
};
|
|
121
|
+
var classifyTwilioFailure = function (fields) {
|
|
122
|
+
if (fields.code !== undefined && PERMANENT_CODES.has(fields.code)) {
|
|
123
|
+
return { errorClass: "permanent", errorCode: String(fields.code) };
|
|
124
|
+
}
|
|
125
|
+
if (fields.code !== undefined && CONFIG_CODES.has(fields.code)) {
|
|
126
|
+
return { errorClass: "config", errorCode: String(fields.code) };
|
|
127
|
+
}
|
|
128
|
+
if (fields.code !== undefined && TRANSIENT_CODES.has(fields.code)) {
|
|
129
|
+
return { errorClass: "transient", errorCode: String(fields.code) };
|
|
130
|
+
}
|
|
131
|
+
if (fields.status === 429 || (fields.status !== undefined && fields.status >= 500)) {
|
|
132
|
+
return {
|
|
133
|
+
errorClass: "transient",
|
|
134
|
+
errorCode: fields.code !== undefined ? String(fields.code) : "twilio-".concat(fields.status),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
if (fields.code !== undefined) {
|
|
138
|
+
return { errorClass: "transient", errorCode: String(fields.code) };
|
|
139
|
+
}
|
|
140
|
+
return { errorClass: "transient", errorCode: "twilio-network" };
|
|
141
|
+
};
|
|
142
|
+
var failedResult = function (error) {
|
|
143
|
+
var fields = twilioErrorFields(error);
|
|
144
|
+
var _a = classifyTwilioFailure(fields), errorClass = _a.errorClass, errorCode = _a.errorCode;
|
|
145
|
+
if (errorClass === "config") {
|
|
146
|
+
api_1.logger.error("[comms:twilio] ".concat(errorCode, ": ").concat(fields.message));
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
accepted: false,
|
|
150
|
+
error: fields.message,
|
|
151
|
+
errorClass: errorClass,
|
|
152
|
+
errorCode: errorCode,
|
|
153
|
+
isPermanentFailure: errorClass === "permanent",
|
|
154
|
+
metadata: { twilioError: fields.payload },
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
var TwilioSmsProvider = /** @class */ (function () {
|
|
158
|
+
function TwilioSmsProvider(options) {
|
|
159
|
+
var _this = this;
|
|
160
|
+
var _a, _b;
|
|
161
|
+
this.id = "twilio";
|
|
162
|
+
this.getAuthToken = function () {
|
|
163
|
+
return _this.authToken;
|
|
164
|
+
};
|
|
165
|
+
this.applyDefaultStatusCallbackUrl = function (url) {
|
|
166
|
+
if (!_this.statusCallbackUrl) {
|
|
167
|
+
_this.statusCallbackUrl = url;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var _c = resolveCredentials(options), accountSid = _c.accountSid, authToken = _c.authToken;
|
|
171
|
+
this.authToken = authToken;
|
|
172
|
+
this.fromNumber = (_a = options === null || options === void 0 ? void 0 : options.fromNumber) !== null && _a !== void 0 ? _a : process.env.TWILIO_FROM_NUMBER;
|
|
173
|
+
this.messagingServiceSid =
|
|
174
|
+
(_b = options === null || options === void 0 ? void 0 : options.messagingServiceSid) !== null && _b !== void 0 ? _b : process.env.TWILIO_MESSAGING_SERVICE_SID;
|
|
175
|
+
this.statusCallbackUrl = options === null || options === void 0 ? void 0 : options.statusCallbackUrl;
|
|
176
|
+
if (options === null || options === void 0 ? void 0 : options.client) {
|
|
177
|
+
this.client = options.client;
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
this.client = loadTwilio()(accountSid, authToken);
|
|
181
|
+
}
|
|
182
|
+
TwilioSmsProvider.prototype.sendSms = function (message) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
+
var to, params, created, error_1;
|
|
185
|
+
var _this = this;
|
|
186
|
+
return __generator(this, function (_a) {
|
|
187
|
+
switch (_a.label) {
|
|
188
|
+
case 0:
|
|
189
|
+
to = toE164(message.to);
|
|
190
|
+
if (!to) {
|
|
191
|
+
return [2 /*return*/, invalidDestinationResult()];
|
|
192
|
+
}
|
|
193
|
+
params = __assign({ body: message.body, to: to }, (this.statusCallbackUrl ? { statusCallback: this.statusCallbackUrl } : {}));
|
|
194
|
+
if (this.messagingServiceSid) {
|
|
195
|
+
params.messagingServiceSid = this.messagingServiceSid;
|
|
196
|
+
}
|
|
197
|
+
else if (this.fromNumber) {
|
|
198
|
+
params.from = this.fromNumber;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
return [2 /*return*/, {
|
|
202
|
+
accepted: false,
|
|
203
|
+
error: "TwilioSmsProvider requires TWILIO_MESSAGING_SERVICE_SID or TWILIO_FROM_NUMBER",
|
|
204
|
+
errorClass: "config",
|
|
205
|
+
errorCode: "twilio-missing-sender",
|
|
206
|
+
}];
|
|
207
|
+
}
|
|
208
|
+
_a.label = 1;
|
|
209
|
+
case 1:
|
|
210
|
+
_a.trys.push([1, 3, , 4]);
|
|
211
|
+
return [4 /*yield*/, (0, api_1.withApiErrorHandling)(function () { return _this.client.messages.create(params); }, {
|
|
212
|
+
apiName: "twilio",
|
|
213
|
+
operation: "sendSms",
|
|
214
|
+
})];
|
|
215
|
+
case 2:
|
|
216
|
+
created = _a.sent();
|
|
217
|
+
return [2 /*return*/, {
|
|
218
|
+
accepted: true,
|
|
219
|
+
metadata: { consoleUrl: twilioMessageConsoleUrl(created.sid) },
|
|
220
|
+
providerMessageId: created.sid,
|
|
221
|
+
}];
|
|
222
|
+
case 3:
|
|
223
|
+
error_1 = _a.sent();
|
|
224
|
+
return [2 /*return*/, failedResult(error_1)];
|
|
225
|
+
case 4: return [2 /*return*/];
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
return TwilioSmsProvider;
|
|
231
|
+
}());
|
|
232
|
+
exports.TwilioSmsProvider = TwilioSmsProvider;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { CheckVerificationOptions, SendResult, StartVerificationOptions, VerificationProvider, VerificationResult } from "../types";
|
|
2
|
+
export interface TwilioVerifyStartParams {
|
|
3
|
+
channel: "email" | "sms";
|
|
4
|
+
to: string;
|
|
5
|
+
}
|
|
6
|
+
export interface TwilioVerifyCheckParams {
|
|
7
|
+
code: string;
|
|
8
|
+
to: string;
|
|
9
|
+
}
|
|
10
|
+
export interface TwilioVerifyService {
|
|
11
|
+
verificationChecks: {
|
|
12
|
+
create: (params: TwilioVerifyCheckParams) => Promise<{
|
|
13
|
+
status: string;
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
verifications: {
|
|
17
|
+
create: (params: TwilioVerifyStartParams) => Promise<{
|
|
18
|
+
sid: string;
|
|
19
|
+
status: string;
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface TwilioVerifyClient {
|
|
24
|
+
verify: {
|
|
25
|
+
v2: {
|
|
26
|
+
services: (serviceSid: string) => TwilioVerifyService;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export interface TwilioVerifyProviderOptions {
|
|
31
|
+
accountSid?: string;
|
|
32
|
+
authToken?: string;
|
|
33
|
+
/** Injected client for tests. Production wiring loads `twilio`. */
|
|
34
|
+
client?: TwilioVerifyClient;
|
|
35
|
+
verifyServiceSid?: string;
|
|
36
|
+
}
|
|
37
|
+
export declare class TwilioVerifyProvider implements VerificationProvider {
|
|
38
|
+
readonly id = "twilio-verify";
|
|
39
|
+
private readonly client;
|
|
40
|
+
private readonly verifyServiceSid;
|
|
41
|
+
constructor(options?: TwilioVerifyProviderOptions);
|
|
42
|
+
startVerification(options: StartVerificationOptions): Promise<SendResult>;
|
|
43
|
+
checkVerification(options: CheckVerificationOptions): Promise<VerificationResult>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
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);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.TwilioVerifyProvider = void 0;
|
|
40
|
+
var node_module_1 = require("node:module");
|
|
41
|
+
var api_1 = require("@terreno/api");
|
|
42
|
+
var PERMANENT_CODES = new Set([60200, 60202, 60203]);
|
|
43
|
+
var TRANSIENT_CODES = new Set([20429]);
|
|
44
|
+
var CONFIG_CODES = new Set([20003, 20404]);
|
|
45
|
+
var TWILIO_VERIFY_CONSOLE_BASE = "https://console.twilio.com/us1/develop/verify/services";
|
|
46
|
+
var nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
47
|
+
var resolveCredentials = function (options) {
|
|
48
|
+
var _a, _b, _c;
|
|
49
|
+
var accountSid = (_a = options === null || options === void 0 ? void 0 : options.accountSid) !== null && _a !== void 0 ? _a : process.env.TWILIO_ACCOUNT_SID;
|
|
50
|
+
var authToken = (_b = options === null || options === void 0 ? void 0 : options.authToken) !== null && _b !== void 0 ? _b : process.env.TWILIO_AUTH_TOKEN;
|
|
51
|
+
var verifyServiceSid = (_c = options === null || options === void 0 ? void 0 : options.verifyServiceSid) !== null && _c !== void 0 ? _c : process.env.TWILIO_VERIFY_SERVICE_SID;
|
|
52
|
+
if (!accountSid || !authToken) {
|
|
53
|
+
throw new Error("TwilioVerifyProvider requires TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN or matching constructor options");
|
|
54
|
+
}
|
|
55
|
+
if (!verifyServiceSid) {
|
|
56
|
+
throw new Error("TwilioVerifyProvider requires TWILIO_VERIFY_SERVICE_SID or a verifyServiceSid constructor option");
|
|
57
|
+
}
|
|
58
|
+
return { accountSid: accountSid, authToken: authToken, verifyServiceSid: verifyServiceSid };
|
|
59
|
+
};
|
|
60
|
+
var loadTwilio = function () {
|
|
61
|
+
try {
|
|
62
|
+
// Optional peer — keep `twilio` out of core dependencies.
|
|
63
|
+
return nodeRequire("twilio");
|
|
64
|
+
}
|
|
65
|
+
catch (_a) {
|
|
66
|
+
throw new Error("TwilioVerifyProvider requires optional peer dependency twilio. " +
|
|
67
|
+
"Install it with: bun add twilio");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var checkErrorForStatus = function (status) {
|
|
71
|
+
if (status === "approved") {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
if (status === "max_attempts_reached") {
|
|
75
|
+
return "max-attempts";
|
|
76
|
+
}
|
|
77
|
+
return status;
|
|
78
|
+
};
|
|
79
|
+
var twilioErrorFields = function (error) {
|
|
80
|
+
if (!error || typeof error !== "object") {
|
|
81
|
+
return {
|
|
82
|
+
message: error instanceof Error ? error.message : "Twilio Verify request failed",
|
|
83
|
+
payload: { error: error },
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
var raw = error;
|
|
87
|
+
var code = typeof raw.code === "number" ? raw.code : undefined;
|
|
88
|
+
var status = typeof raw.status === "number" ? raw.status : undefined;
|
|
89
|
+
var message = typeof raw.message === "string" && raw.message.length > 0
|
|
90
|
+
? raw.message
|
|
91
|
+
: error instanceof Error
|
|
92
|
+
? error.message
|
|
93
|
+
: "Twilio Verify request failed";
|
|
94
|
+
var payload = { message: message };
|
|
95
|
+
if (code !== undefined) {
|
|
96
|
+
payload.code = code;
|
|
97
|
+
}
|
|
98
|
+
if (status !== undefined) {
|
|
99
|
+
payload.status = status;
|
|
100
|
+
}
|
|
101
|
+
if (typeof raw.moreInfo === "string") {
|
|
102
|
+
payload.moreInfo = raw.moreInfo;
|
|
103
|
+
}
|
|
104
|
+
return { code: code, message: message, payload: payload, status: status };
|
|
105
|
+
};
|
|
106
|
+
var classifyTwilioVerifyFailure = function (fields) {
|
|
107
|
+
if (fields.code !== undefined && PERMANENT_CODES.has(fields.code)) {
|
|
108
|
+
return { errorClass: "permanent", errorCode: String(fields.code) };
|
|
109
|
+
}
|
|
110
|
+
if (fields.code !== undefined && CONFIG_CODES.has(fields.code)) {
|
|
111
|
+
return { errorClass: "config", errorCode: String(fields.code) };
|
|
112
|
+
}
|
|
113
|
+
if (fields.code !== undefined && TRANSIENT_CODES.has(fields.code)) {
|
|
114
|
+
return { errorClass: "transient", errorCode: String(fields.code) };
|
|
115
|
+
}
|
|
116
|
+
if (fields.status === 429 || (fields.status !== undefined && fields.status >= 500)) {
|
|
117
|
+
return {
|
|
118
|
+
errorClass: "transient",
|
|
119
|
+
errorCode: fields.code !== undefined ? String(fields.code) : "twilio-".concat(fields.status),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (fields.code !== undefined) {
|
|
123
|
+
return { errorClass: "transient", errorCode: String(fields.code) };
|
|
124
|
+
}
|
|
125
|
+
return { errorClass: "transient", errorCode: "twilio-network" };
|
|
126
|
+
};
|
|
127
|
+
var failedStartResult = function (error) {
|
|
128
|
+
var fields = twilioErrorFields(error);
|
|
129
|
+
var _a = classifyTwilioVerifyFailure(fields), errorClass = _a.errorClass, errorCode = _a.errorCode;
|
|
130
|
+
if (errorClass === "config") {
|
|
131
|
+
api_1.logger.error("[comms:twilio-verify] ".concat(errorCode, ": ").concat(fields.message));
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
accepted: false,
|
|
135
|
+
error: fields.message,
|
|
136
|
+
errorClass: errorClass,
|
|
137
|
+
errorCode: errorCode,
|
|
138
|
+
isPermanentFailure: errorClass === "permanent",
|
|
139
|
+
metadata: { twilioError: fields.payload },
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
var TwilioVerifyProvider = /** @class */ (function () {
|
|
143
|
+
function TwilioVerifyProvider(options) {
|
|
144
|
+
this.id = "twilio-verify";
|
|
145
|
+
var _a = resolveCredentials(options), accountSid = _a.accountSid, authToken = _a.authToken, verifyServiceSid = _a.verifyServiceSid;
|
|
146
|
+
this.verifyServiceSid = verifyServiceSid;
|
|
147
|
+
if (options === null || options === void 0 ? void 0 : options.client) {
|
|
148
|
+
this.client = options.client;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.client = loadTwilio()(accountSid, authToken);
|
|
152
|
+
}
|
|
153
|
+
TwilioVerifyProvider.prototype.startVerification = function (options) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
155
|
+
var created, error_1;
|
|
156
|
+
var _this = this;
|
|
157
|
+
return __generator(this, function (_a) {
|
|
158
|
+
switch (_a.label) {
|
|
159
|
+
case 0:
|
|
160
|
+
_a.trys.push([0, 2, , 3]);
|
|
161
|
+
return [4 /*yield*/, (0, api_1.withApiErrorHandling)(function () {
|
|
162
|
+
return _this.client.verify.v2
|
|
163
|
+
.services(_this.verifyServiceSid)
|
|
164
|
+
.verifications.create({ channel: options.channel, to: options.to });
|
|
165
|
+
}, { apiName: "twilio-verify", operation: "startVerification" })];
|
|
166
|
+
case 1:
|
|
167
|
+
created = _a.sent();
|
|
168
|
+
return [2 /*return*/, {
|
|
169
|
+
accepted: true,
|
|
170
|
+
metadata: { consoleUrl: "".concat(TWILIO_VERIFY_CONSOLE_BASE, "/").concat(this.verifyServiceSid) },
|
|
171
|
+
providerMessageId: created.sid,
|
|
172
|
+
}];
|
|
173
|
+
case 2:
|
|
174
|
+
error_1 = _a.sent();
|
|
175
|
+
return [2 /*return*/, failedStartResult(error_1)];
|
|
176
|
+
case 3: return [2 /*return*/];
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
TwilioVerifyProvider.prototype.checkVerification = function (options) {
|
|
182
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
183
|
+
var checked, error, error_2, fields;
|
|
184
|
+
var _this = this;
|
|
185
|
+
return __generator(this, function (_a) {
|
|
186
|
+
switch (_a.label) {
|
|
187
|
+
case 0:
|
|
188
|
+
_a.trys.push([0, 2, , 3]);
|
|
189
|
+
return [4 /*yield*/, (0, api_1.withApiErrorHandling)(function () {
|
|
190
|
+
return _this.client.verify.v2
|
|
191
|
+
.services(_this.verifyServiceSid)
|
|
192
|
+
.verificationChecks.create({ code: options.code, to: options.to });
|
|
193
|
+
}, { apiName: "twilio-verify", operation: "checkVerification" })];
|
|
194
|
+
case 1:
|
|
195
|
+
checked = _a.sent();
|
|
196
|
+
error = checkErrorForStatus(checked.status);
|
|
197
|
+
if (error === undefined) {
|
|
198
|
+
return [2 /*return*/, { valid: true }];
|
|
199
|
+
}
|
|
200
|
+
return [2 /*return*/, { error: error, valid: false }];
|
|
201
|
+
case 2:
|
|
202
|
+
error_2 = _a.sent();
|
|
203
|
+
fields = twilioErrorFields(error_2);
|
|
204
|
+
return [2 /*return*/, { error: fields.message, valid: false }];
|
|
205
|
+
case 3: return [2 /*return*/];
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
return TwilioVerifyProvider;
|
|
211
|
+
}());
|
|
212
|
+
exports.TwilioVerifyProvider = TwilioVerifyProvider;
|
package/dist/commsApp.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { type TerrenoPlugin } from "@terreno/api";
|
|
1
|
+
import { type AdminContribution, type TerrenoPlugin, type WebhooksApp } from "@terreno/api";
|
|
2
2
|
import type express from "express";
|
|
3
3
|
import { CommsService } from "./commsService";
|
|
4
4
|
import type { CommsOptions } from "./types";
|
|
5
5
|
export interface CommsAppOptions extends CommsOptions {
|
|
6
6
|
basePath?: string;
|
|
7
|
+
webhookPublicUrl?: string;
|
|
8
|
+
webhooks?: WebhooksApp;
|
|
7
9
|
}
|
|
8
10
|
export declare const getCommsService: () => CommsService;
|
|
9
11
|
export declare class CommsApp implements TerrenoPlugin {
|
|
@@ -11,4 +13,5 @@ export declare class CommsApp implements TerrenoPlugin {
|
|
|
11
13
|
private readonly options;
|
|
12
14
|
constructor(options?: CommsAppOptions);
|
|
13
15
|
register(app: express.Application, openApi?: unknown): void;
|
|
16
|
+
adminContribution(): AdminContribution;
|
|
14
17
|
}
|
package/dist/commsApp.js
CHANGED
|
@@ -4,8 +4,10 @@ exports.CommsApp = exports.getCommsService = void 0;
|
|
|
4
4
|
var api_1 = require("@terreno/api");
|
|
5
5
|
var commsService_1 = require("./commsService");
|
|
6
6
|
var pushToken_1 = require("./models/pushToken");
|
|
7
|
-
var
|
|
7
|
+
var commsDashboard_1 = require("./routes/commsDashboard");
|
|
8
8
|
var pushTokens_1 = require("./routes/pushTokens");
|
|
9
|
+
var sendgridWebhooks_1 = require("./webhooks/sendgridWebhooks");
|
|
10
|
+
var twilioWebhooks_1 = require("./webhooks/twilioWebhooks");
|
|
9
11
|
var registeredCommsService;
|
|
10
12
|
var getCommsService = function () {
|
|
11
13
|
if (!registeredCommsService) {
|
|
@@ -30,7 +32,31 @@ var CommsApp = /** @class */ (function () {
|
|
|
30
32
|
"rejected safely until the unique token index exists: ".concat(String(error)));
|
|
31
33
|
});
|
|
32
34
|
(0, pushTokens_1.addPushTokenRoutes)(app, { basePath: "".concat(basePath, "/pushTokens"), openApi: openApi });
|
|
33
|
-
(0,
|
|
35
|
+
(0, commsDashboard_1.addCommsDashboardRoutes)(app, { basePath: basePath, openApi: openApi, service: this.service });
|
|
36
|
+
(0, twilioWebhooks_1.maybeRegisterTwilioCommsWebhooks)({
|
|
37
|
+
basePath: basePath,
|
|
38
|
+
publicUrl: this.options.webhookPublicUrl,
|
|
39
|
+
service: this.service,
|
|
40
|
+
sms: this.options.sms,
|
|
41
|
+
webhooks: this.options.webhooks,
|
|
42
|
+
});
|
|
43
|
+
(0, sendgridWebhooks_1.maybeRegisterSendGridCommsWebhooks)({
|
|
44
|
+
basePath: basePath,
|
|
45
|
+
mail: this.options.mail,
|
|
46
|
+
service: this.service,
|
|
47
|
+
webhooks: this.options.webhooks,
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
CommsApp.prototype.adminContribution = function () {
|
|
51
|
+
return {
|
|
52
|
+
customScreens: [
|
|
53
|
+
{
|
|
54
|
+
displayName: "Comms Dashboard",
|
|
55
|
+
icon: "paper-plane",
|
|
56
|
+
name: "comms",
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
};
|
|
34
60
|
};
|
|
35
61
|
return CommsApp;
|
|
36
62
|
}());
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { DateTime } from "luxon";
|
|
2
|
+
export interface CommsListFilters {
|
|
3
|
+
channel?: string;
|
|
4
|
+
endDate?: string;
|
|
5
|
+
errorClass?: string;
|
|
6
|
+
errorCode?: string;
|
|
7
|
+
provider?: string;
|
|
8
|
+
q?: string;
|
|
9
|
+
retriedFromId?: string;
|
|
10
|
+
startDate?: string;
|
|
11
|
+
status?: string;
|
|
12
|
+
templateId?: string;
|
|
13
|
+
to?: string;
|
|
14
|
+
userId?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface CommsListPagination {
|
|
17
|
+
limit: number;
|
|
18
|
+
page: number;
|
|
19
|
+
skip: number;
|
|
20
|
+
}
|
|
21
|
+
export declare const parseCommsListFilters: (source: Record<string, unknown>) => CommsListFilters;
|
|
22
|
+
export declare const parseCommsListPagination: (source: Record<string, unknown>) => CommsListPagination;
|
|
23
|
+
export declare const parseRetryManyLimit: (value: unknown) => number;
|
|
24
|
+
export declare const defaultStatsRange: () => {
|
|
25
|
+
endDate: DateTime;
|
|
26
|
+
startDate: DateTime;
|
|
27
|
+
};
|
|
28
|
+
export declare const buildCommsMessageMatch: (filters: CommsListFilters, options?: {
|
|
29
|
+
applyDefaultStatsRange?: boolean;
|
|
30
|
+
}) => Record<string, unknown>;
|