@terreno/comms 57.0.0 → 57.1.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/dist/adapters/sendgrid.d.ts +27 -0
- package/dist/adapters/sendgrid.js +234 -0
- package/dist/commsService.d.ts +26 -1
- package/dist/commsService.js +792 -111
- package/dist/index.d.ts +2 -2
- package/dist/modelTypes.d.ts +42 -1
- package/dist/models/commsMessage.js +200 -4
- package/dist/types.d.ts +50 -14
- package/package.json +20 -3
- package/src/adapters/sendgrid.test.ts +307 -0
- package/src/adapters/sendgrid.ts +224 -0
- package/src/commsService.test.ts +747 -24
- package/src/commsService.ts +684 -76
- package/src/index.ts +6 -0
- package/src/modelTypes.ts +43 -1
- package/src/models/commsMessage.test.ts +56 -0
- package/src/models/commsMessage.ts +150 -2
- package/src/tests/bunSetup.ts +1 -0
- package/src/types.test.ts +56 -0
- package/src/types.ts +57 -16
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MailMessage, MailProvider, SendResult } from "../types";
|
|
2
|
+
interface SendGridResponse {
|
|
3
|
+
body?: unknown;
|
|
4
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
5
|
+
statusCode?: number;
|
|
6
|
+
}
|
|
7
|
+
interface SendGridClient {
|
|
8
|
+
send: (message: Record<string, unknown>) => Promise<[SendGridResponse, Record<string, unknown>]>;
|
|
9
|
+
}
|
|
10
|
+
export interface SendGridMailProviderOptions {
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
/** Injected client for tests. Production wiring loads `@sendgrid/mail`. */
|
|
13
|
+
client?: SendGridClient;
|
|
14
|
+
fromEmail?: string;
|
|
15
|
+
fromName?: string;
|
|
16
|
+
sandboxMode?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class SendGridMailProvider implements MailProvider {
|
|
19
|
+
readonly id = "sendgrid";
|
|
20
|
+
private readonly client;
|
|
21
|
+
private readonly fromEmail?;
|
|
22
|
+
private readonly fromName?;
|
|
23
|
+
private readonly sandboxMode;
|
|
24
|
+
constructor(options?: SendGridMailProviderOptions);
|
|
25
|
+
sendMail(message: MailMessage): Promise<SendResult>;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,234 @@
|
|
|
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.SendGridMailProvider = void 0;
|
|
67
|
+
var node_module_1 = require("node:module");
|
|
68
|
+
var api_1 = require("@terreno/api");
|
|
69
|
+
var nodeRequire = (0, node_module_1.createRequire)(__filename);
|
|
70
|
+
var EMAIL_ACTIVITY_BASE = "https://app.sendgrid.com/email_activity";
|
|
71
|
+
var resolveApiKey = function (options) {
|
|
72
|
+
var _a;
|
|
73
|
+
var apiKey = (_a = options === null || options === void 0 ? void 0 : options.apiKey) !== null && _a !== void 0 ? _a : process.env.SENDGRID_API_KEY;
|
|
74
|
+
if (!apiKey) {
|
|
75
|
+
throw new Error("SendGridMailProvider requires SENDGRID_API_KEY or an apiKey constructor option");
|
|
76
|
+
}
|
|
77
|
+
return apiKey;
|
|
78
|
+
};
|
|
79
|
+
var loadSendGridMail = function () {
|
|
80
|
+
try {
|
|
81
|
+
// Optional peer — keep `@sendgrid/mail` out of core dependencies.
|
|
82
|
+
return nodeRequire("@sendgrid/mail");
|
|
83
|
+
}
|
|
84
|
+
catch (_a) {
|
|
85
|
+
throw new Error("SendGridMailProvider requires optional peer dependency @sendgrid/mail. " +
|
|
86
|
+
"Install it with: bun add @sendgrid/mail");
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var headerValue = function (headers, name) {
|
|
90
|
+
var _a;
|
|
91
|
+
if (!headers) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
var value = (_a = headers[name]) !== null && _a !== void 0 ? _a : headers[name.toLowerCase()];
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
return value[0];
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
};
|
|
100
|
+
var emailActivityUrl = function (messageId) {
|
|
101
|
+
var filters = encodeURIComponent(JSON.stringify([
|
|
102
|
+
{
|
|
103
|
+
comparisonType: "Contains",
|
|
104
|
+
selectedFieldName: "msg_id",
|
|
105
|
+
val: [messageId],
|
|
106
|
+
},
|
|
107
|
+
]));
|
|
108
|
+
return "".concat(EMAIL_ACTIVITY_BASE, "?filters=").concat(filters);
|
|
109
|
+
};
|
|
110
|
+
var firstSendGridErrorMessage = function (body) {
|
|
111
|
+
var _a;
|
|
112
|
+
if (!body || typeof body !== "object") {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
var errors = body.errors;
|
|
116
|
+
if (!Array.isArray(errors) || errors.length === 0) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
return (_a = errors[0]) === null || _a === void 0 ? void 0 : _a.message;
|
|
120
|
+
};
|
|
121
|
+
var classifyHttpStatus = function (statusCode) {
|
|
122
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
123
|
+
return { errorClass: "config", errorCode: "sendgrid-".concat(statusCode) };
|
|
124
|
+
}
|
|
125
|
+
if (statusCode === 429 || statusCode >= 500) {
|
|
126
|
+
return { errorClass: "transient", errorCode: "sendgrid-".concat(statusCode) };
|
|
127
|
+
}
|
|
128
|
+
return { errorClass: "permanent", errorCode: "sendgrid-".concat(statusCode) };
|
|
129
|
+
};
|
|
130
|
+
var failedResult = function (_a) {
|
|
131
|
+
var body = _a.body, error = _a.error, errorClass = _a.errorClass, errorCode = _a.errorCode;
|
|
132
|
+
if (errorClass === "config") {
|
|
133
|
+
api_1.logger.error("[comms:sendgrid] ".concat(errorCode, ": ").concat(error));
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
accepted: false,
|
|
137
|
+
error: error,
|
|
138
|
+
errorClass: errorClass,
|
|
139
|
+
errorCode: errorCode,
|
|
140
|
+
isPermanentFailure: errorClass === "permanent",
|
|
141
|
+
metadata: body === undefined ? undefined : { sendGridResponse: body },
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
var statusFromError = function (error) {
|
|
145
|
+
var _a;
|
|
146
|
+
if (!error || typeof error !== "object") {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
var withCode = error;
|
|
150
|
+
var responseStatus = (_a = withCode.response) === null || _a === void 0 ? void 0 : _a.statusCode;
|
|
151
|
+
if (typeof responseStatus === "number") {
|
|
152
|
+
return responseStatus;
|
|
153
|
+
}
|
|
154
|
+
if (typeof withCode.code === "number") {
|
|
155
|
+
return withCode.code;
|
|
156
|
+
}
|
|
157
|
+
return undefined;
|
|
158
|
+
};
|
|
159
|
+
var bodyFromError = function (error) {
|
|
160
|
+
var _a;
|
|
161
|
+
if (!error || typeof error !== "object") {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
return (_a = error.response) === null || _a === void 0 ? void 0 : _a.body;
|
|
165
|
+
};
|
|
166
|
+
var SendGridMailProvider = /** @class */ (function () {
|
|
167
|
+
function SendGridMailProvider(options) {
|
|
168
|
+
var _a;
|
|
169
|
+
this.id = "sendgrid";
|
|
170
|
+
var apiKey = resolveApiKey(options);
|
|
171
|
+
this.fromEmail = options === null || options === void 0 ? void 0 : options.fromEmail;
|
|
172
|
+
this.fromName = options === null || options === void 0 ? void 0 : options.fromName;
|
|
173
|
+
this.sandboxMode = (_a = options === null || options === void 0 ? void 0 : options.sandboxMode) !== null && _a !== void 0 ? _a : process.env.NODE_ENV === "test";
|
|
174
|
+
if (options === null || options === void 0 ? void 0 : options.client) {
|
|
175
|
+
this.client = options.client;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
var sgMail = loadSendGridMail();
|
|
179
|
+
sgMail.setApiKey(apiKey);
|
|
180
|
+
this.client = sgMail;
|
|
181
|
+
}
|
|
182
|
+
SendGridMailProvider.prototype.sendMail = function (message) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
184
|
+
var fromEmail, payload, _a, response, providerMessageId, error_1, statusCode, body, _b, errorClass, errorCode, errorMessage;
|
|
185
|
+
var _c, _d, _e, _f;
|
|
186
|
+
return __generator(this, function (_g) {
|
|
187
|
+
switch (_g.label) {
|
|
188
|
+
case 0:
|
|
189
|
+
fromEmail = (_c = this.fromEmail) !== null && _c !== void 0 ? _c : message.from;
|
|
190
|
+
if (!fromEmail) {
|
|
191
|
+
return [2 /*return*/, failedResult({
|
|
192
|
+
error: "SendGridMailProvider requires fromEmail or MailMessage.from",
|
|
193
|
+
errorClass: "config",
|
|
194
|
+
errorCode: "sendgrid-missing-from",
|
|
195
|
+
})];
|
|
196
|
+
}
|
|
197
|
+
payload = __assign(__assign(__assign(__assign(__assign({ from: this.fromName ? { email: fromEmail, name: this.fromName } : fromEmail, subject: message.subject, to: message.to }, (message.html ? { html: message.html } : {})), (message.text ? { text: message.text } : {})), (message.replyTo ? { replyTo: message.replyTo } : {})), (message.templateId
|
|
198
|
+
? {
|
|
199
|
+
dynamicTemplateData: (_d = message.dynamicTemplateData) !== null && _d !== void 0 ? _d : {},
|
|
200
|
+
templateId: message.templateId,
|
|
201
|
+
}
|
|
202
|
+
: {})), (this.sandboxMode ? { mailSettings: { sandboxMode: { enable: true } } } : {}));
|
|
203
|
+
_g.label = 1;
|
|
204
|
+
case 1:
|
|
205
|
+
_g.trys.push([1, 3, , 4]);
|
|
206
|
+
return [4 /*yield*/, this.client.send(payload)];
|
|
207
|
+
case 2:
|
|
208
|
+
_a = __read.apply(void 0, [_g.sent(), 1]), response = _a[0];
|
|
209
|
+
providerMessageId = headerValue(response.headers, "x-message-id");
|
|
210
|
+
return [2 /*return*/, {
|
|
211
|
+
accepted: true,
|
|
212
|
+
metadata: providerMessageId ? { consoleUrl: emailActivityUrl(providerMessageId) } : undefined,
|
|
213
|
+
providerMessageId: providerMessageId,
|
|
214
|
+
}];
|
|
215
|
+
case 3:
|
|
216
|
+
error_1 = _g.sent();
|
|
217
|
+
statusCode = (_e = statusFromError(error_1)) !== null && _e !== void 0 ? _e : 500;
|
|
218
|
+
body = bodyFromError(error_1);
|
|
219
|
+
_b = classifyHttpStatus(statusCode), errorClass = _b.errorClass, errorCode = _b.errorCode;
|
|
220
|
+
errorMessage = (_f = firstSendGridErrorMessage(body)) !== null && _f !== void 0 ? _f : (error_1 instanceof Error ? error_1.message : "SendGrid send failed");
|
|
221
|
+
return [2 /*return*/, failedResult({
|
|
222
|
+
body: body,
|
|
223
|
+
error: errorMessage,
|
|
224
|
+
errorClass: errorClass,
|
|
225
|
+
errorCode: errorCode,
|
|
226
|
+
})];
|
|
227
|
+
case 4: return [2 /*return*/];
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
return SendGridMailProvider;
|
|
233
|
+
}());
|
|
234
|
+
exports.SendGridMailProvider = SendGridMailProvider;
|
package/dist/commsService.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CheckVerificationOptions, CommsOptions, MailMessage, SendPushToUserMessage, SendResult, SmsMessage, StartVerificationOptions, VerificationResult } from "./types";
|
|
1
|
+
import type { CheckVerificationOptions, CommsOptions, DeliveryEvent, MailMessage, OptOutEvent, SendPushToUserMessage, SendResult, SmsMessage, StartVerificationOptions, VerificationResult } from "./types";
|
|
2
2
|
export declare class CommsService {
|
|
3
3
|
private readonly options;
|
|
4
4
|
private readonly consoleMail;
|
|
@@ -6,12 +6,37 @@ export declare class CommsService {
|
|
|
6
6
|
private readonly consoleSms;
|
|
7
7
|
private readonly consoleVerification;
|
|
8
8
|
constructor(options?: CommsOptions);
|
|
9
|
+
recordDeliveryEvent(event: DeliveryEvent): Promise<void>;
|
|
10
|
+
recordOptOut(event: OptOutEvent): Promise<void>;
|
|
11
|
+
clearExpiredPayloads(): Promise<number>;
|
|
9
12
|
private recipientForLog;
|
|
13
|
+
private retainPayloadDays;
|
|
14
|
+
private invokeHook;
|
|
15
|
+
private appendHookError;
|
|
16
|
+
private applyBeforeSend;
|
|
17
|
+
private defaultPayload;
|
|
18
|
+
private resolvePayload;
|
|
19
|
+
private payloadExpiresAt;
|
|
20
|
+
private cloneHookErrors;
|
|
21
|
+
private cloneContext;
|
|
22
|
+
private resultsByToken;
|
|
23
|
+
private attemptFromResult;
|
|
10
24
|
private logResult;
|
|
25
|
+
private patchHookErrors;
|
|
26
|
+
private notifyOutcomeHooks;
|
|
27
|
+
private retryOnce;
|
|
28
|
+
private persistRetry;
|
|
29
|
+
private finalizeWithRetry;
|
|
30
|
+
private cancelledResult;
|
|
31
|
+
private sendMailOnce;
|
|
32
|
+
private sendSmsOnce;
|
|
33
|
+
private sendPushOnce;
|
|
34
|
+
private startVerificationOnce;
|
|
11
35
|
private mailProvider;
|
|
12
36
|
private smsProvider;
|
|
13
37
|
private pushProvider;
|
|
14
38
|
private verificationProvider;
|
|
39
|
+
private applyMailDefaults;
|
|
15
40
|
sendMail(message: MailMessage): Promise<SendResult>;
|
|
16
41
|
sendSms(message: SmsMessage): Promise<SendResult>;
|
|
17
42
|
sendPushToUser(message: SendPushToUserMessage): Promise<SendResult[]>;
|