@terreno/comms 57.4.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.js +33 -34
- package/dist/commsRetry.js +17 -25
- package/dist/commsService.js +674 -1098
- package/dist/models/commsMessage.js +64 -172
- package/dist/models/pushToken.js +5 -5
- package/dist/routes/commsDashboard.js +174 -325
- package/dist/routes/pushTokens.js +122 -217
- package/dist/templates.js +27 -41
- package/dist/webhooks/sendgridWebhooks.js +109 -226
- package/dist/webhooks/twilioWebhooks.js +59 -117
- package/package.json +6 -6
- package/tsconfig.json +6 -20
|
@@ -1,153 +1,85 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
3
|
exports.SendGridMailProvider = void 0;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
var apiKey = (_a = options === null || options === void 0 ? void 0 : options.apiKey) !== null && _a !== void 0 ? _a : process.env.SENDGRID_API_KEY;
|
|
4
|
+
const node_module_1 = require("node:module");
|
|
5
|
+
const api_1 = require("@terreno/api");
|
|
6
|
+
const nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
7
|
+
const EMAIL_ACTIVITY_BASE = "https://app.sendgrid.com/email_activity";
|
|
8
|
+
const resolveApiKey = (options) => {
|
|
9
|
+
const apiKey = options?.apiKey ?? process.env.SENDGRID_API_KEY;
|
|
74
10
|
if (!apiKey) {
|
|
75
11
|
throw new Error("SendGridMailProvider requires SENDGRID_API_KEY or an apiKey constructor option");
|
|
76
12
|
}
|
|
77
13
|
return apiKey;
|
|
78
14
|
};
|
|
79
|
-
|
|
15
|
+
const loadSendGridMail = () => {
|
|
80
16
|
try {
|
|
81
17
|
// Optional peer — keep `@sendgrid/mail` out of core dependencies.
|
|
82
18
|
return nodeRequire("@sendgrid/mail");
|
|
83
19
|
}
|
|
84
|
-
catch
|
|
20
|
+
catch {
|
|
85
21
|
throw new Error("SendGridMailProvider requires optional peer dependency @sendgrid/mail. " +
|
|
86
22
|
"Install it with: bun add @sendgrid/mail");
|
|
87
23
|
}
|
|
88
24
|
};
|
|
89
|
-
|
|
90
|
-
var _a;
|
|
25
|
+
const headerValue = (headers, name) => {
|
|
91
26
|
if (!headers) {
|
|
92
27
|
return undefined;
|
|
93
28
|
}
|
|
94
|
-
|
|
29
|
+
const value = headers[name] ?? headers[name.toLowerCase()];
|
|
95
30
|
if (Array.isArray(value)) {
|
|
96
31
|
return value[0];
|
|
97
32
|
}
|
|
98
33
|
return value;
|
|
99
34
|
};
|
|
100
|
-
|
|
101
|
-
|
|
35
|
+
const emailActivityUrl = (messageId) => {
|
|
36
|
+
const filters = encodeURIComponent(JSON.stringify([
|
|
102
37
|
{
|
|
103
38
|
comparisonType: "Contains",
|
|
104
39
|
selectedFieldName: "msg_id",
|
|
105
40
|
val: [messageId],
|
|
106
41
|
},
|
|
107
42
|
]));
|
|
108
|
-
return
|
|
43
|
+
return `${EMAIL_ACTIVITY_BASE}?filters=${filters}`;
|
|
109
44
|
};
|
|
110
|
-
|
|
111
|
-
var _a;
|
|
45
|
+
const firstSendGridErrorMessage = (body) => {
|
|
112
46
|
if (!body || typeof body !== "object") {
|
|
113
47
|
return undefined;
|
|
114
48
|
}
|
|
115
|
-
|
|
49
|
+
const errors = body.errors;
|
|
116
50
|
if (!Array.isArray(errors) || errors.length === 0) {
|
|
117
51
|
return undefined;
|
|
118
52
|
}
|
|
119
|
-
return
|
|
53
|
+
return errors[0]?.message;
|
|
120
54
|
};
|
|
121
|
-
|
|
55
|
+
const classifyHttpStatus = (statusCode) => {
|
|
122
56
|
if (statusCode === 401 || statusCode === 403) {
|
|
123
|
-
return { errorClass: "config", errorCode:
|
|
57
|
+
return { errorClass: "config", errorCode: `sendgrid-${statusCode}` };
|
|
124
58
|
}
|
|
125
59
|
if (statusCode === 429 || statusCode >= 500) {
|
|
126
|
-
return { errorClass: "transient", errorCode:
|
|
60
|
+
return { errorClass: "transient", errorCode: `sendgrid-${statusCode}` };
|
|
127
61
|
}
|
|
128
|
-
return { errorClass: "permanent", errorCode:
|
|
62
|
+
return { errorClass: "permanent", errorCode: `sendgrid-${statusCode}` };
|
|
129
63
|
};
|
|
130
|
-
|
|
131
|
-
var body = _a.body, error = _a.error, errorClass = _a.errorClass, errorCode = _a.errorCode;
|
|
64
|
+
const failedResult = ({ body, error, errorClass, errorCode, }) => {
|
|
132
65
|
if (errorClass === "config") {
|
|
133
|
-
api_1.logger.error(
|
|
66
|
+
api_1.logger.error(`[comms:sendgrid] ${errorCode}: ${error}`);
|
|
134
67
|
}
|
|
135
68
|
return {
|
|
136
69
|
accepted: false,
|
|
137
|
-
error
|
|
138
|
-
errorClass
|
|
139
|
-
errorCode
|
|
70
|
+
error,
|
|
71
|
+
errorClass,
|
|
72
|
+
errorCode,
|
|
140
73
|
isPermanentFailure: errorClass === "permanent",
|
|
141
74
|
metadata: body === undefined ? undefined : { sendGridResponse: body },
|
|
142
75
|
};
|
|
143
76
|
};
|
|
144
|
-
|
|
145
|
-
var _a;
|
|
77
|
+
const statusFromError = (error) => {
|
|
146
78
|
if (!error || typeof error !== "object") {
|
|
147
79
|
return undefined;
|
|
148
80
|
}
|
|
149
|
-
|
|
150
|
-
|
|
81
|
+
const withCode = error;
|
|
82
|
+
const responseStatus = withCode.response?.statusCode;
|
|
151
83
|
if (typeof responseStatus === "number") {
|
|
152
84
|
return responseStatus;
|
|
153
85
|
}
|
|
@@ -156,85 +88,83 @@ var statusFromError = function (error) {
|
|
|
156
88
|
}
|
|
157
89
|
return undefined;
|
|
158
90
|
};
|
|
159
|
-
|
|
160
|
-
var _a;
|
|
91
|
+
const bodyFromError = (error) => {
|
|
161
92
|
if (!error || typeof error !== "object") {
|
|
162
93
|
return undefined;
|
|
163
94
|
}
|
|
164
|
-
return
|
|
95
|
+
return error.response?.body;
|
|
165
96
|
};
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
this.fromEmail = options
|
|
176
|
-
this.fromName = options
|
|
177
|
-
this.sandboxMode =
|
|
97
|
+
class SendGridMailProvider {
|
|
98
|
+
id = "sendgrid";
|
|
99
|
+
client;
|
|
100
|
+
fromEmail;
|
|
101
|
+
fromName;
|
|
102
|
+
sandboxMode;
|
|
103
|
+
webhookVerificationKey;
|
|
104
|
+
constructor(options) {
|
|
105
|
+
const apiKey = resolveApiKey(options);
|
|
106
|
+
this.fromEmail = options?.fromEmail;
|
|
107
|
+
this.fromName = options?.fromName;
|
|
108
|
+
this.sandboxMode = options?.sandboxMode ?? process.env.NODE_ENV === "test";
|
|
178
109
|
this.webhookVerificationKey =
|
|
179
|
-
|
|
180
|
-
if (options
|
|
110
|
+
options?.webhookVerificationKey ?? process.env.SENDGRID_WEBHOOK_VERIFICATION_KEY;
|
|
111
|
+
if (options?.client) {
|
|
181
112
|
this.client = options.client;
|
|
182
113
|
return;
|
|
183
114
|
}
|
|
184
|
-
|
|
115
|
+
const sgMail = loadSendGridMail();
|
|
185
116
|
sgMail.setApiKey(apiKey);
|
|
186
117
|
this.client = sgMail;
|
|
187
118
|
}
|
|
188
|
-
|
|
189
|
-
return
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
_g.trys.push([1, 3, , 4]);
|
|
212
|
-
return [4 /*yield*/, this.client.send(payload)];
|
|
213
|
-
case 2:
|
|
214
|
-
_a = __read.apply(void 0, [_g.sent(), 1]), response = _a[0];
|
|
215
|
-
providerMessageId = headerValue(response.headers, "x-message-id");
|
|
216
|
-
return [2 /*return*/, {
|
|
217
|
-
accepted: true,
|
|
218
|
-
metadata: providerMessageId ? { consoleUrl: emailActivityUrl(providerMessageId) } : undefined,
|
|
219
|
-
providerMessageId: providerMessageId,
|
|
220
|
-
}];
|
|
221
|
-
case 3:
|
|
222
|
-
error_1 = _g.sent();
|
|
223
|
-
statusCode = (_e = statusFromError(error_1)) !== null && _e !== void 0 ? _e : 500;
|
|
224
|
-
body = bodyFromError(error_1);
|
|
225
|
-
_b = classifyHttpStatus(statusCode), errorClass = _b.errorClass, errorCode = _b.errorCode;
|
|
226
|
-
errorMessage = (_f = firstSendGridErrorMessage(body)) !== null && _f !== void 0 ? _f : (error_1 instanceof Error ? error_1.message : "SendGrid send failed");
|
|
227
|
-
return [2 /*return*/, failedResult({
|
|
228
|
-
body: body,
|
|
229
|
-
error: errorMessage,
|
|
230
|
-
errorClass: errorClass,
|
|
231
|
-
errorCode: errorCode,
|
|
232
|
-
})];
|
|
233
|
-
case 4: return [2 /*return*/];
|
|
119
|
+
getWebhookVerificationKey = () => {
|
|
120
|
+
return this.webhookVerificationKey;
|
|
121
|
+
};
|
|
122
|
+
async sendMail(message) {
|
|
123
|
+
const fromEmail = this.fromEmail ?? message.from;
|
|
124
|
+
if (!fromEmail) {
|
|
125
|
+
return failedResult({
|
|
126
|
+
error: "SendGridMailProvider requires fromEmail or MailMessage.from",
|
|
127
|
+
errorClass: "config",
|
|
128
|
+
errorCode: "sendgrid-missing-from",
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
const payload = {
|
|
132
|
+
from: this.fromName ? { email: fromEmail, name: this.fromName } : fromEmail,
|
|
133
|
+
subject: message.subject,
|
|
134
|
+
to: message.to,
|
|
135
|
+
...(message.html ? { html: message.html } : {}),
|
|
136
|
+
...(message.text ? { text: message.text } : {}),
|
|
137
|
+
...(message.replyTo ? { replyTo: message.replyTo } : {}),
|
|
138
|
+
...(message.templateId
|
|
139
|
+
? {
|
|
140
|
+
dynamicTemplateData: message.dynamicTemplateData ?? {},
|
|
141
|
+
templateId: message.templateId,
|
|
234
142
|
}
|
|
143
|
+
: {}),
|
|
144
|
+
...(this.sandboxMode ? { mailSettings: { sandboxMode: { enable: true } } } : {}),
|
|
145
|
+
};
|
|
146
|
+
try {
|
|
147
|
+
const [response] = await this.client.send(payload);
|
|
148
|
+
const providerMessageId = headerValue(response.headers, "x-message-id");
|
|
149
|
+
return {
|
|
150
|
+
accepted: true,
|
|
151
|
+
metadata: providerMessageId ? { consoleUrl: emailActivityUrl(providerMessageId) } : undefined,
|
|
152
|
+
providerMessageId,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
const statusCode = statusFromError(error) ?? 500;
|
|
157
|
+
const body = bodyFromError(error);
|
|
158
|
+
const { errorClass, errorCode } = classifyHttpStatus(statusCode);
|
|
159
|
+
const errorMessage = firstSendGridErrorMessage(body) ??
|
|
160
|
+
(error instanceof Error ? error.message : "SendGrid send failed");
|
|
161
|
+
return failedResult({
|
|
162
|
+
body,
|
|
163
|
+
error: errorMessage,
|
|
164
|
+
errorClass,
|
|
165
|
+
errorCode,
|
|
235
166
|
});
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
}());
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
240
170
|
exports.SendGridMailProvider = SendGridMailProvider;
|
|
@@ -1,111 +1,63 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
3
|
exports.TwilioSmsProvider = void 0;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var authToken = (_b = options === null || options === void 0 ? void 0 : options.authToken) !== null && _b !== void 0 ? _b : process.env.TWILIO_AUTH_TOKEN;
|
|
4
|
+
const node_module_1 = require("node:module");
|
|
5
|
+
const api_1 = require("@terreno/api");
|
|
6
|
+
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
7
|
+
const nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
8
|
+
const TWILIO_SMS_CONSOLE_BASE = "https://console.twilio.com/us1/monitor/logs/sms";
|
|
9
|
+
const twilioMessageConsoleUrl = (sid) => `${TWILIO_SMS_CONSOLE_BASE}/${sid}`;
|
|
10
|
+
const PERMANENT_CODES = new Set([21211, 21214, 21217, 21408, 21610, 30003, 30005, 30006, 30007]);
|
|
11
|
+
const TRANSIENT_CODES = new Set([20429, 30001, 30002]);
|
|
12
|
+
const CONFIG_CODES = new Set([20003, 20404]);
|
|
13
|
+
const resolveCredentials = (options) => {
|
|
14
|
+
const accountSid = options?.accountSid ?? process.env.TWILIO_ACCOUNT_SID;
|
|
15
|
+
const authToken = options?.authToken ?? process.env.TWILIO_AUTH_TOKEN;
|
|
64
16
|
if (!accountSid || !authToken) {
|
|
65
17
|
throw new Error("TwilioSmsProvider requires TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN or matching constructor options");
|
|
66
18
|
}
|
|
67
|
-
return { accountSid
|
|
19
|
+
return { accountSid, authToken };
|
|
68
20
|
};
|
|
69
|
-
|
|
21
|
+
const loadTwilio = () => {
|
|
70
22
|
try {
|
|
71
23
|
// Optional peer — keep `twilio` out of core dependencies.
|
|
72
24
|
return nodeRequire("twilio");
|
|
73
25
|
}
|
|
74
|
-
catch
|
|
26
|
+
catch {
|
|
75
27
|
throw new Error("TwilioSmsProvider requires optional peer dependency twilio. " +
|
|
76
28
|
"Install it with: bun add twilio");
|
|
77
29
|
}
|
|
78
30
|
};
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (!
|
|
31
|
+
const toE164 = (to) => {
|
|
32
|
+
const parsed = (0, libphonenumber_js_1.parsePhoneNumberFromString)(to);
|
|
33
|
+
if (!parsed?.isValid()) {
|
|
82
34
|
return undefined;
|
|
83
35
|
}
|
|
84
36
|
return parsed.format("E.164");
|
|
85
37
|
};
|
|
86
|
-
|
|
38
|
+
const invalidDestinationResult = () => ({
|
|
87
39
|
accepted: false,
|
|
88
40
|
error: "Destination must be a valid E.164 phone number",
|
|
89
41
|
errorClass: "permanent",
|
|
90
42
|
errorCode: "twilio-invalid-destination",
|
|
91
43
|
isPermanentFailure: true,
|
|
92
|
-
});
|
|
93
|
-
|
|
44
|
+
});
|
|
45
|
+
const twilioErrorFields = (error) => {
|
|
94
46
|
if (!error || typeof error !== "object") {
|
|
95
47
|
return {
|
|
96
48
|
message: error instanceof Error ? error.message : "Twilio send failed",
|
|
97
|
-
payload: { error
|
|
49
|
+
payload: { error },
|
|
98
50
|
};
|
|
99
51
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
52
|
+
const raw = error;
|
|
53
|
+
const code = typeof raw.code === "number" ? raw.code : undefined;
|
|
54
|
+
const status = typeof raw.status === "number" ? raw.status : undefined;
|
|
55
|
+
const message = typeof raw.message === "string" && raw.message.length > 0
|
|
104
56
|
? raw.message
|
|
105
57
|
: error instanceof Error
|
|
106
58
|
? error.message
|
|
107
59
|
: "Twilio send failed";
|
|
108
|
-
|
|
60
|
+
const payload = {};
|
|
109
61
|
if (code !== undefined) {
|
|
110
62
|
payload.code = code;
|
|
111
63
|
}
|
|
@@ -116,9 +68,9 @@ var twilioErrorFields = function (error) {
|
|
|
116
68
|
if (typeof raw.moreInfo === "string") {
|
|
117
69
|
payload.moreInfo = raw.moreInfo;
|
|
118
70
|
}
|
|
119
|
-
return { code
|
|
71
|
+
return { code, message, payload, status };
|
|
120
72
|
};
|
|
121
|
-
|
|
73
|
+
const classifyTwilioFailure = (fields) => {
|
|
122
74
|
if (fields.code !== undefined && PERMANENT_CODES.has(fields.code)) {
|
|
123
75
|
return { errorClass: "permanent", errorCode: String(fields.code) };
|
|
124
76
|
}
|
|
@@ -131,7 +83,7 @@ var classifyTwilioFailure = function (fields) {
|
|
|
131
83
|
if (fields.status === 429 || (fields.status !== undefined && fields.status >= 500)) {
|
|
132
84
|
return {
|
|
133
85
|
errorClass: "transient",
|
|
134
|
-
errorCode: fields.code !== undefined ? String(fields.code) :
|
|
86
|
+
errorCode: fields.code !== undefined ? String(fields.code) : `twilio-${fields.status}`,
|
|
135
87
|
};
|
|
136
88
|
}
|
|
137
89
|
if (fields.code !== undefined) {
|
|
@@ -139,94 +91,87 @@ var classifyTwilioFailure = function (fields) {
|
|
|
139
91
|
}
|
|
140
92
|
return { errorClass: "transient", errorCode: "twilio-network" };
|
|
141
93
|
};
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
94
|
+
const failedResult = (error) => {
|
|
95
|
+
const fields = twilioErrorFields(error);
|
|
96
|
+
const { errorClass, errorCode } = classifyTwilioFailure(fields);
|
|
145
97
|
if (errorClass === "config") {
|
|
146
|
-
api_1.logger.error(
|
|
98
|
+
api_1.logger.error(`[comms:twilio] ${errorCode}: ${fields.message}`);
|
|
147
99
|
}
|
|
148
100
|
return {
|
|
149
101
|
accepted: false,
|
|
150
102
|
error: fields.message,
|
|
151
|
-
errorClass
|
|
152
|
-
errorCode
|
|
103
|
+
errorClass,
|
|
104
|
+
errorCode,
|
|
153
105
|
isPermanentFailure: errorClass === "permanent",
|
|
154
106
|
metadata: { twilioError: fields.payload },
|
|
155
107
|
};
|
|
156
108
|
};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (!_this.statusCallbackUrl) {
|
|
167
|
-
_this.statusCallbackUrl = url;
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
var _c = resolveCredentials(options), accountSid = _c.accountSid, authToken = _c.authToken;
|
|
109
|
+
class TwilioSmsProvider {
|
|
110
|
+
id = "twilio";
|
|
111
|
+
authToken;
|
|
112
|
+
client;
|
|
113
|
+
fromNumber;
|
|
114
|
+
messagingServiceSid;
|
|
115
|
+
statusCallbackUrl;
|
|
116
|
+
constructor(options) {
|
|
117
|
+
const { accountSid, authToken } = resolveCredentials(options);
|
|
171
118
|
this.authToken = authToken;
|
|
172
|
-
this.fromNumber =
|
|
119
|
+
this.fromNumber = options?.fromNumber ?? process.env.TWILIO_FROM_NUMBER;
|
|
173
120
|
this.messagingServiceSid =
|
|
174
|
-
|
|
175
|
-
this.statusCallbackUrl = options
|
|
176
|
-
if (options
|
|
121
|
+
options?.messagingServiceSid ?? process.env.TWILIO_MESSAGING_SERVICE_SID;
|
|
122
|
+
this.statusCallbackUrl = options?.statusCallbackUrl;
|
|
123
|
+
if (options?.client) {
|
|
177
124
|
this.client = options.client;
|
|
178
125
|
return;
|
|
179
126
|
}
|
|
180
127
|
this.client = loadTwilio()(accountSid, authToken);
|
|
181
128
|
}
|
|
182
|
-
|
|
183
|
-
return
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
-
});
|
|
129
|
+
getAuthToken = () => {
|
|
130
|
+
return this.authToken;
|
|
131
|
+
};
|
|
132
|
+
applyDefaultStatusCallbackUrl = (url) => {
|
|
133
|
+
if (!this.statusCallbackUrl) {
|
|
134
|
+
this.statusCallbackUrl = url;
|
|
135
|
+
}
|
|
229
136
|
};
|
|
230
|
-
|
|
231
|
-
|
|
137
|
+
async sendSms(message) {
|
|
138
|
+
const to = toE164(message.to);
|
|
139
|
+
if (!to) {
|
|
140
|
+
return invalidDestinationResult();
|
|
141
|
+
}
|
|
142
|
+
const params = {
|
|
143
|
+
body: message.body,
|
|
144
|
+
to,
|
|
145
|
+
...(this.statusCallbackUrl ? { statusCallback: this.statusCallbackUrl } : {}),
|
|
146
|
+
};
|
|
147
|
+
if (this.messagingServiceSid) {
|
|
148
|
+
params.messagingServiceSid = this.messagingServiceSid;
|
|
149
|
+
}
|
|
150
|
+
else if (this.fromNumber) {
|
|
151
|
+
params.from = this.fromNumber;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
return {
|
|
155
|
+
accepted: false,
|
|
156
|
+
error: "TwilioSmsProvider requires TWILIO_MESSAGING_SERVICE_SID or TWILIO_FROM_NUMBER",
|
|
157
|
+
errorClass: "config",
|
|
158
|
+
errorCode: "twilio-missing-sender",
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const created = await (0, api_1.withApiErrorHandling)(() => this.client.messages.create(params), {
|
|
163
|
+
apiName: "twilio",
|
|
164
|
+
operation: "sendSms",
|
|
165
|
+
});
|
|
166
|
+
return {
|
|
167
|
+
accepted: true,
|
|
168
|
+
metadata: { consoleUrl: twilioMessageConsoleUrl(created.sid) },
|
|
169
|
+
providerMessageId: created.sid,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
return failedResult(error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
232
177
|
exports.TwilioSmsProvider = TwilioSmsProvider;
|