@moreapp/common-nodejs 0.13.2 → 0.13.4
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/MailService.d.ts +3 -1
- package/dist/MailService.js +37 -13
- package/dist/__tests__/MailService.test.js +58 -6
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +4 -4
package/dist/MailService.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export declare class MailService {
|
|
|
18
18
|
readonly username: string;
|
|
19
19
|
readonly password: string;
|
|
20
20
|
constructor(service: string, baseUrl: string, username: string, password: string);
|
|
21
|
-
|
|
21
|
+
sendTemplatedMail(emailAddress: string, template: string, language: string, customerId?: number, variables?: Record<string, unknown>): Promise<MailSendResponse | undefined>;
|
|
22
|
+
sendRawMail(customerId: number, recipients: Recipient[], subject: string, body: string, attachments: Attachment[], fromName?: string, replyTo?: string, meta?: Record<string, unknown>): Promise<MailSendResponse | undefined>;
|
|
23
|
+
private sendMail;
|
|
22
24
|
}
|
|
23
25
|
export {};
|
package/dist/MailService.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.MailService = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const logger_1 = require("./logger");
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
9
10
|
class MailService {
|
|
10
11
|
service;
|
|
11
12
|
baseUrl;
|
|
@@ -21,25 +22,48 @@ class MailService {
|
|
|
21
22
|
this.username = username;
|
|
22
23
|
this.password = password;
|
|
23
24
|
}
|
|
24
|
-
async
|
|
25
|
-
|
|
25
|
+
async sendTemplatedMail(emailAddress, template, language, customerId, variables) {
|
|
26
|
+
const recipient = {
|
|
27
|
+
type: "to",
|
|
28
|
+
email: emailAddress,
|
|
29
|
+
};
|
|
30
|
+
return this.sendMail({
|
|
31
|
+
recipients: [recipient],
|
|
32
|
+
customerId,
|
|
33
|
+
content: {
|
|
34
|
+
type: "template",
|
|
35
|
+
template,
|
|
36
|
+
variables: variables ?? {},
|
|
37
|
+
language,
|
|
38
|
+
attachments: [],
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async sendRawMail(customerId, recipients, subject, body, attachments, fromName, replyTo, meta) {
|
|
43
|
+
return this.sendMail({
|
|
44
|
+
recipients,
|
|
45
|
+
customerId,
|
|
46
|
+
content: {
|
|
47
|
+
type: "html",
|
|
48
|
+
subject,
|
|
49
|
+
html: body,
|
|
50
|
+
attachments,
|
|
51
|
+
},
|
|
52
|
+
fromName,
|
|
53
|
+
replyTo,
|
|
54
|
+
meta,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async sendMail(mailRequest) {
|
|
58
|
+
if ((0, lodash_1.isEmpty)(mailRequest.recipients)) {
|
|
26
59
|
logger_1.logger.info("No recipients, not sending mail");
|
|
27
60
|
return;
|
|
28
61
|
}
|
|
29
62
|
try {
|
|
30
63
|
const response = await axios_1.default.post(`${this.baseUrl}/api/mail`, {
|
|
31
|
-
|
|
32
|
-
fromName,
|
|
33
|
-
replyTo,
|
|
34
|
-
recipients,
|
|
35
|
-
content: {
|
|
36
|
-
type: "html",
|
|
37
|
-
html: body,
|
|
38
|
-
subject,
|
|
39
|
-
attachments,
|
|
40
|
-
},
|
|
64
|
+
...mailRequest,
|
|
41
65
|
meta: {
|
|
42
|
-
...meta,
|
|
66
|
+
...mailRequest.meta,
|
|
43
67
|
service: this.service,
|
|
44
68
|
},
|
|
45
69
|
}, {
|
|
@@ -57,7 +57,7 @@ const newService = () => new MailService_1.MailService("some-service-name", base
|
|
|
57
57
|
(0, vitest_1.test)("does not send when there are no recipients", async () => {
|
|
58
58
|
const infoSpy = vitest_1.vi.spyOn(logger_1.logger, "info").mockImplementation(() => logger_1.logger);
|
|
59
59
|
const postSpy = vitest_1.vi.spyOn(axios_1.default, "post");
|
|
60
|
-
const result = await newService().
|
|
60
|
+
const result = await newService().sendRawMail(1, [], "Subject", "<p>Body</p>", attachments);
|
|
61
61
|
(0, vitest_1.expect)(result).toBeUndefined();
|
|
62
62
|
(0, vitest_1.expect)(postSpy).not.toHaveBeenCalled();
|
|
63
63
|
(0, vitest_1.expect)(infoSpy).toHaveBeenCalledWith("No recipients, not sending mail");
|
|
@@ -71,7 +71,7 @@ const newService = () => new MailService_1.MailService("some-service-name", base
|
|
|
71
71
|
})
|
|
72
72
|
.basicAuth({ user: username, pass: password })
|
|
73
73
|
.reply(200, { id: "mail-123" });
|
|
74
|
-
const result = await newService().
|
|
74
|
+
const result = await newService().sendRawMail(42, recipients, "Hello", "<p>Body</p>", attachments, "MoreApp", "reply@example.com", { correlationId: "abc" });
|
|
75
75
|
(0, vitest_1.expect)(result).toEqual({ id: "mail-123" });
|
|
76
76
|
(0, vitest_1.expect)(requestBody).toEqual({
|
|
77
77
|
customerId: 42,
|
|
@@ -91,10 +91,62 @@ const newService = () => new MailService_1.MailService("some-service-name", base
|
|
|
91
91
|
});
|
|
92
92
|
apiMock.done();
|
|
93
93
|
});
|
|
94
|
+
(0, vitest_1.test)("sends a templated mail to a single recipient and returns the response data", async () => {
|
|
95
|
+
let requestBody;
|
|
96
|
+
const apiMock = (0, nock_1.default)(baseUrl)
|
|
97
|
+
.post("/api/mail", (body) => {
|
|
98
|
+
requestBody = body;
|
|
99
|
+
return true;
|
|
100
|
+
})
|
|
101
|
+
.basicAuth({ user: username, pass: password })
|
|
102
|
+
.reply(200, { id: "mail-789" });
|
|
103
|
+
const result = await newService().sendTemplatedMail("someone@example.com", "welcome", "en", 42, {
|
|
104
|
+
firstName: "Someone",
|
|
105
|
+
});
|
|
106
|
+
(0, vitest_1.expect)(result).toEqual({ id: "mail-789" });
|
|
107
|
+
(0, vitest_1.expect)(requestBody).toEqual({
|
|
108
|
+
customerId: 42,
|
|
109
|
+
recipients: [{ type: "to", email: "someone@example.com" }],
|
|
110
|
+
content: {
|
|
111
|
+
type: "template",
|
|
112
|
+
template: "welcome",
|
|
113
|
+
language: "en",
|
|
114
|
+
variables: { firstName: "Someone" },
|
|
115
|
+
attachments: [],
|
|
116
|
+
},
|
|
117
|
+
meta: {
|
|
118
|
+
service: "some-service-name",
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
apiMock.done();
|
|
122
|
+
});
|
|
123
|
+
(0, vitest_1.test)("defaults templated mail variables to an empty object when omitted", async () => {
|
|
124
|
+
let requestBody;
|
|
125
|
+
const apiMock = (0, nock_1.default)(baseUrl)
|
|
126
|
+
.post("/api/mail", (body) => {
|
|
127
|
+
requestBody = body;
|
|
128
|
+
return true;
|
|
129
|
+
})
|
|
130
|
+
.reply(200, { id: "mail-000" });
|
|
131
|
+
const result = await newService().sendTemplatedMail("someone@example.com", "welcome", "en");
|
|
132
|
+
(0, vitest_1.expect)(result).toEqual({ id: "mail-000" });
|
|
133
|
+
(0, vitest_1.expect)(requestBody).toMatchObject({
|
|
134
|
+
content: {
|
|
135
|
+
type: "template",
|
|
136
|
+
template: "welcome",
|
|
137
|
+
language: "en",
|
|
138
|
+
variables: {},
|
|
139
|
+
attachments: [],
|
|
140
|
+
},
|
|
141
|
+
meta: { service: "some-service-name" },
|
|
142
|
+
});
|
|
143
|
+
(0, vitest_1.expect)(requestBody).not.toHaveProperty("customerId");
|
|
144
|
+
apiMock.done();
|
|
145
|
+
});
|
|
94
146
|
(0, vitest_1.test)("throws with the server message on an error response", async () => {
|
|
95
147
|
vitest_1.vi.spyOn(logger_1.logger, "error").mockImplementation(() => logger_1.logger);
|
|
96
148
|
const apiMock = (0, nock_1.default)(baseUrl).post("/api/mail").reply(400, { message: "Invalid recipient" });
|
|
97
|
-
await (0, vitest_1.expect)(newService().
|
|
149
|
+
await (0, vitest_1.expect)(newService().sendRawMail(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toThrow("Failed to send email. Invalid recipient");
|
|
98
150
|
apiMock.done();
|
|
99
151
|
});
|
|
100
152
|
(0, vitest_1.test)("rethrows the original error on ECONNRESET so the caller can retry", async () => {
|
|
@@ -108,18 +160,18 @@ const newService = () => new MailService_1.MailService("some-service-name", base
|
|
|
108
160
|
config: {},
|
|
109
161
|
};
|
|
110
162
|
vitest_1.vi.spyOn(axios_1.default, "post").mockRejectedValue(econnreset);
|
|
111
|
-
await (0, vitest_1.expect)(newService().
|
|
163
|
+
await (0, vitest_1.expect)(newService().sendRawMail(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toBe(econnreset);
|
|
112
164
|
});
|
|
113
165
|
(0, vitest_1.test)("throws a generic error for non-axios failures", async () => {
|
|
114
166
|
vitest_1.vi.spyOn(logger_1.logger, "error").mockImplementation(() => logger_1.logger);
|
|
115
167
|
vitest_1.vi.spyOn(axios_1.default, "post").mockRejectedValue(new Error("boom"));
|
|
116
|
-
await (0, vitest_1.expect)(newService().
|
|
168
|
+
await (0, vitest_1.expect)(newService().sendRawMail(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toThrow("Failed to send email");
|
|
117
169
|
});
|
|
118
170
|
(0, vitest_1.test)("throws a generic error for an axios error without a response", async () => {
|
|
119
171
|
vitest_1.vi.spyOn(logger_1.logger, "error").mockImplementation(() => logger_1.logger);
|
|
120
172
|
// An axios error with no `response` (e.g. a timeout / connection failure)
|
|
121
173
|
// should fall through to the generic error path.
|
|
122
174
|
vitest_1.vi.spyOn(axios_1.default, "post").mockRejectedValue(new axios_1.AxiosError("timeout", "ETIMEDOUT"));
|
|
123
|
-
await (0, vitest_1.expect)(newService().
|
|
175
|
+
await (0, vitest_1.expect)(newService().sendRawMail(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toThrow("Failed to send email");
|
|
124
176
|
});
|
|
125
177
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -28,3 +28,4 @@ __exportStar(require("./utils"), exports);
|
|
|
28
28
|
__exportStar(require("./utils/environmentVariable"), exports);
|
|
29
29
|
__exportStar(require("./observability"), exports);
|
|
30
30
|
__exportStar(require("./observability/tracer"), exports);
|
|
31
|
+
__exportStar(require("./MailService"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moreapp/common-nodejs",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.4",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -57,8 +57,8 @@
|
|
|
57
57
|
"@types/node": "24.13.3",
|
|
58
58
|
"@typescript-eslint/eslint-plugin": "8.67.0",
|
|
59
59
|
"@typescript-eslint/parser": "8.67.0",
|
|
60
|
-
"@vitest/coverage-v8": "4.1.
|
|
61
|
-
"@vitest/ui": "4.1.
|
|
60
|
+
"@vitest/coverage-v8": "4.1.11",
|
|
61
|
+
"@vitest/ui": "4.1.11",
|
|
62
62
|
"eslint": "9.39.5",
|
|
63
63
|
"eslint-config-prettier": "10.1.8",
|
|
64
64
|
"eslint-plugin-prettier": "5.5.6",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"ts-node": "10.9.2",
|
|
70
70
|
"typescript": "6.0.3",
|
|
71
71
|
"typescript-eslint": "8.67.0",
|
|
72
|
-
"vitest": "4.1.
|
|
72
|
+
"vitest": "4.1.11",
|
|
73
73
|
"vitest-mock-extended": "5.1.1"
|
|
74
74
|
},
|
|
75
75
|
"prettier": {
|