@moreapp/common-nodejs 0.13.0 → 0.13.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/dist/MailService.d.ts +23 -0
- package/dist/MailService.js +69 -0
- package/dist/__tests__/MailService.test.d.ts +1 -0
- package/dist/__tests__/MailService.test.js +125 -0
- package/dist/observability/__tests__/ObservabilityServer.test.js +2 -0
- package/dist/observability/__tests__/TerminationHandler.test.js +5 -0
- package/package.json +27 -27
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface Recipient {
|
|
2
|
+
email: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
type: "to" | "cc" | "bcc";
|
|
5
|
+
}
|
|
6
|
+
export interface Attachment {
|
|
7
|
+
name: string;
|
|
8
|
+
type: string;
|
|
9
|
+
data: string;
|
|
10
|
+
inline: boolean;
|
|
11
|
+
}
|
|
12
|
+
type MailSendResponse = {
|
|
13
|
+
id: string;
|
|
14
|
+
};
|
|
15
|
+
export declare class MailService {
|
|
16
|
+
readonly service: string;
|
|
17
|
+
readonly baseUrl: string;
|
|
18
|
+
readonly username: string;
|
|
19
|
+
readonly password: string;
|
|
20
|
+
constructor(service: string, baseUrl: string, username: string, password: string);
|
|
21
|
+
send(customerId: number, recipients: Recipient[], subject: string, body: string, attachments: Attachment[], fromName?: string, replyTo?: string, meta?: Record<string, unknown>): Promise<MailSendResponse | undefined>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MailService = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const logger_1 = require("./logger");
|
|
9
|
+
class MailService {
|
|
10
|
+
service;
|
|
11
|
+
baseUrl;
|
|
12
|
+
username;
|
|
13
|
+
password;
|
|
14
|
+
constructor(service, baseUrl, username, password) {
|
|
15
|
+
this.service = service;
|
|
16
|
+
this.baseUrl = baseUrl;
|
|
17
|
+
this.username = username;
|
|
18
|
+
this.password = password;
|
|
19
|
+
this.service = service;
|
|
20
|
+
this.baseUrl = baseUrl;
|
|
21
|
+
this.username = username;
|
|
22
|
+
this.password = password;
|
|
23
|
+
}
|
|
24
|
+
async send(customerId, recipients, subject, body, attachments, fromName, replyTo, meta) {
|
|
25
|
+
if (recipients.length === 0) {
|
|
26
|
+
logger_1.logger.info("No recipients, not sending mail");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const response = await axios_1.default.post(`${this.baseUrl}/api/mail`, {
|
|
31
|
+
customerId,
|
|
32
|
+
fromName,
|
|
33
|
+
replyTo,
|
|
34
|
+
recipients,
|
|
35
|
+
content: {
|
|
36
|
+
type: "html",
|
|
37
|
+
html: body,
|
|
38
|
+
subject,
|
|
39
|
+
attachments,
|
|
40
|
+
},
|
|
41
|
+
meta: {
|
|
42
|
+
...meta,
|
|
43
|
+
service: this.service,
|
|
44
|
+
},
|
|
45
|
+
}, {
|
|
46
|
+
auth: {
|
|
47
|
+
username: this.username,
|
|
48
|
+
password: this.password,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
return response.data;
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
if (axios_1.default.isAxiosError(e) && e.response) {
|
|
55
|
+
if (e.code === "ECONNRESET") {
|
|
56
|
+
logger_1.logger.warn("socket hang up error detected, setting hook for retry");
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
const { status, data } = e.response;
|
|
60
|
+
logger_1.logger.error("Failed to send email", { response: { status, data } });
|
|
61
|
+
throw new Error(`Failed to send email. ${data.message}`);
|
|
62
|
+
}
|
|
63
|
+
const error = e;
|
|
64
|
+
logger_1.logger.error("Failed to send email", { code: error.code, message: error.message });
|
|
65
|
+
throw new Error("Failed to send email");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.MailService = MailService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const vitest_1 = require("vitest");
|
|
40
|
+
const axios_1 = __importStar(require("axios"));
|
|
41
|
+
const nock_1 = __importDefault(require("nock"));
|
|
42
|
+
const MailService_1 = require("../MailService");
|
|
43
|
+
const logger_1 = require("../logger");
|
|
44
|
+
const baseUrl = "http://notifications.test";
|
|
45
|
+
const username = "user";
|
|
46
|
+
const password = "pass";
|
|
47
|
+
const recipients = [{ email: "someone@example.com", name: "Someone", type: "to" }];
|
|
48
|
+
const attachments = [
|
|
49
|
+
{ name: "file.pdf", type: "application/pdf", data: "base64", inline: false },
|
|
50
|
+
];
|
|
51
|
+
const newService = () => new MailService_1.MailService("some-service-name", baseUrl, username, password);
|
|
52
|
+
(0, vitest_1.describe)("MailService", () => {
|
|
53
|
+
(0, vitest_1.afterEach)(() => {
|
|
54
|
+
nock_1.default.cleanAll();
|
|
55
|
+
vitest_1.vi.restoreAllMocks();
|
|
56
|
+
});
|
|
57
|
+
(0, vitest_1.test)("does not send when there are no recipients", async () => {
|
|
58
|
+
const infoSpy = vitest_1.vi.spyOn(logger_1.logger, "info").mockImplementation(() => logger_1.logger);
|
|
59
|
+
const postSpy = vitest_1.vi.spyOn(axios_1.default, "post");
|
|
60
|
+
const result = await newService().send(1, [], "Subject", "<p>Body</p>", attachments);
|
|
61
|
+
(0, vitest_1.expect)(result).toBeUndefined();
|
|
62
|
+
(0, vitest_1.expect)(postSpy).not.toHaveBeenCalled();
|
|
63
|
+
(0, vitest_1.expect)(infoSpy).toHaveBeenCalledWith("No recipients, not sending mail");
|
|
64
|
+
});
|
|
65
|
+
(0, vitest_1.test)("sends mail and returns the response data", async () => {
|
|
66
|
+
let requestBody;
|
|
67
|
+
const apiMock = (0, nock_1.default)(baseUrl)
|
|
68
|
+
.post("/api/mail", (body) => {
|
|
69
|
+
requestBody = body;
|
|
70
|
+
return true;
|
|
71
|
+
})
|
|
72
|
+
.basicAuth({ user: username, pass: password })
|
|
73
|
+
.reply(200, { id: "mail-123" });
|
|
74
|
+
const result = await newService().send(42, recipients, "Hello", "<p>Body</p>", attachments, "MoreApp", "reply@example.com", { correlationId: "abc" });
|
|
75
|
+
(0, vitest_1.expect)(result).toEqual({ id: "mail-123" });
|
|
76
|
+
(0, vitest_1.expect)(requestBody).toEqual({
|
|
77
|
+
customerId: 42,
|
|
78
|
+
fromName: "MoreApp",
|
|
79
|
+
replyTo: "reply@example.com",
|
|
80
|
+
recipients,
|
|
81
|
+
content: {
|
|
82
|
+
type: "html",
|
|
83
|
+
html: "<p>Body</p>",
|
|
84
|
+
subject: "Hello",
|
|
85
|
+
attachments,
|
|
86
|
+
},
|
|
87
|
+
meta: {
|
|
88
|
+
correlationId: "abc",
|
|
89
|
+
service: "some-service-name",
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
apiMock.done();
|
|
93
|
+
});
|
|
94
|
+
(0, vitest_1.test)("throws with the server message on an error response", async () => {
|
|
95
|
+
vitest_1.vi.spyOn(logger_1.logger, "error").mockImplementation(() => logger_1.logger);
|
|
96
|
+
const apiMock = (0, nock_1.default)(baseUrl).post("/api/mail").reply(400, { message: "Invalid recipient" });
|
|
97
|
+
await (0, vitest_1.expect)(newService().send(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toThrow("Failed to send email. Invalid recipient");
|
|
98
|
+
apiMock.done();
|
|
99
|
+
});
|
|
100
|
+
(0, vitest_1.test)("rethrows the original error on ECONNRESET so the caller can retry", async () => {
|
|
101
|
+
vitest_1.vi.spyOn(logger_1.logger, "warn").mockImplementation(() => logger_1.logger);
|
|
102
|
+
const econnreset = new axios_1.AxiosError("socket hang up", "ECONNRESET");
|
|
103
|
+
econnreset.response = {
|
|
104
|
+
status: 500,
|
|
105
|
+
data: {},
|
|
106
|
+
statusText: "",
|
|
107
|
+
headers: {},
|
|
108
|
+
config: {},
|
|
109
|
+
};
|
|
110
|
+
vitest_1.vi.spyOn(axios_1.default, "post").mockRejectedValue(econnreset);
|
|
111
|
+
await (0, vitest_1.expect)(newService().send(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toBe(econnreset);
|
|
112
|
+
});
|
|
113
|
+
(0, vitest_1.test)("throws a generic error for non-axios failures", async () => {
|
|
114
|
+
vitest_1.vi.spyOn(logger_1.logger, "error").mockImplementation(() => logger_1.logger);
|
|
115
|
+
vitest_1.vi.spyOn(axios_1.default, "post").mockRejectedValue(new Error("boom"));
|
|
116
|
+
await (0, vitest_1.expect)(newService().send(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toThrow("Failed to send email");
|
|
117
|
+
});
|
|
118
|
+
(0, vitest_1.test)("throws a generic error for an axios error without a response", async () => {
|
|
119
|
+
vitest_1.vi.spyOn(logger_1.logger, "error").mockImplementation(() => logger_1.logger);
|
|
120
|
+
// An axios error with no `response` (e.g. a timeout / connection failure)
|
|
121
|
+
// should fall through to the generic error path.
|
|
122
|
+
vitest_1.vi.spyOn(axios_1.default, "post").mockRejectedValue(new axios_1.AxiosError("timeout", "ETIMEDOUT"));
|
|
123
|
+
await (0, vitest_1.expect)(newService().send(1, recipients, "Subject", "<p>Body</p>", attachments)).rejects.toThrow("Failed to send email");
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -66,6 +66,7 @@ vitest_1.vi.mock("../../logger", () => ({
|
|
|
66
66
|
nrOfActiveRequests: vitest_1.vi.fn(() => 0),
|
|
67
67
|
};
|
|
68
68
|
const hs = await ObservabilityServer_1.default.create(requestTracker, { port: 0 });
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
69
70
|
(0, vitest_1.expect)(fakeServer.listen).toHaveBeenCalled();
|
|
70
71
|
// /live
|
|
71
72
|
const resLive = createRes();
|
|
@@ -97,6 +98,7 @@ vitest_1.vi.mock("../../logger", () => ({
|
|
|
97
98
|
capturedHandler({ url: "/unknown" }, res404);
|
|
98
99
|
(0, vitest_1.expect)(res404.statusCode).toBe(404);
|
|
99
100
|
await hs.stop();
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
100
102
|
(0, vitest_1.expect)(fakeServer.close).toHaveBeenCalled();
|
|
101
103
|
createServerSpy.mockRestore();
|
|
102
104
|
});
|
|
@@ -113,6 +113,7 @@ vitest_1.vi.mock("node:timers", () => ({
|
|
|
113
113
|
const th = new TerminationHandler_1.default(server, hs, requestTracker);
|
|
114
114
|
th.shutdown();
|
|
115
115
|
(0, vitest_1.expect)(hs.setReady).toHaveBeenCalledWith(false);
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
116
117
|
(0, vitest_1.expect)(server.close).toHaveBeenCalled();
|
|
117
118
|
// First interval tick should see 0 active and exit
|
|
118
119
|
intervalCb?.();
|
|
@@ -158,9 +159,11 @@ vitest_1.vi.mock("node:timers", () => ({
|
|
|
158
159
|
th.shutdown();
|
|
159
160
|
// Before timeout callback, no exit
|
|
160
161
|
(0, vitest_1.expect)(exitSpy).not.toHaveBeenCalled();
|
|
162
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
161
163
|
(0, vitest_1.expect)(socket.destroy).not.toHaveBeenCalled();
|
|
162
164
|
// Trigger shutdown timeout (index 0 is the 30s timeout)
|
|
163
165
|
timeoutCallbacks[0]?.();
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
164
167
|
(0, vitest_1.expect)(socket.destroy).toHaveBeenCalled();
|
|
165
168
|
(0, vitest_1.expect)(exitSpy).toHaveBeenCalledWith(1);
|
|
166
169
|
});
|
|
@@ -210,6 +213,7 @@ vitest_1.vi.mock("node:timers", () => ({
|
|
|
210
213
|
server.emit("error", new Error("EADDRINUSE"));
|
|
211
214
|
(0, vitest_1.expect)(vitest_1.vi.mocked(logger_1.logger.error)).toHaveBeenCalledWith("Server threw an unrecoverable error, shutting down", vitest_1.expect.any(Error));
|
|
212
215
|
(0, vitest_1.expect)(hs.setReady).toHaveBeenCalledWith(false);
|
|
216
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
213
217
|
(0, vitest_1.expect)(server.close).toHaveBeenCalled();
|
|
214
218
|
intervalCb?.();
|
|
215
219
|
await new Promise((resolve) => setImmediate(resolve));
|
|
@@ -228,6 +232,7 @@ vitest_1.vi.mock("node:timers", () => ({
|
|
|
228
232
|
th.shutdown();
|
|
229
233
|
th.shutdown();
|
|
230
234
|
(0, vitest_1.expect)(hs.setReady).toHaveBeenCalledTimes(1);
|
|
235
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
231
236
|
(0, vitest_1.expect)(server.close).toHaveBeenCalledTimes(1);
|
|
232
237
|
(0, vitest_1.expect)(vitest_1.vi.mocked(timers.setInterval)).toHaveBeenCalledTimes(1);
|
|
233
238
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moreapp/common-nodejs",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.2",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,19 +30,19 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@opentelemetry/api": "1.9.1",
|
|
33
|
-
"@opentelemetry/core": "2.
|
|
34
|
-
"@opentelemetry/exporter-trace-otlp-grpc": "0.
|
|
35
|
-
"@opentelemetry/instrumentation": "0.
|
|
36
|
-
"@opentelemetry/instrumentation-dns": "0.
|
|
37
|
-
"@opentelemetry/instrumentation-express": "0.
|
|
38
|
-
"@opentelemetry/instrumentation-http": "0.
|
|
39
|
-
"@opentelemetry/instrumentation-winston": "0.
|
|
40
|
-
"@opentelemetry/resources": "2.
|
|
41
|
-
"@opentelemetry/sdk-node": "0.
|
|
42
|
-
"@opentelemetry/sdk-trace-base": "2.
|
|
43
|
-
"@opentelemetry/sdk-trace-node": "2.
|
|
44
|
-
"@opentelemetry/semantic-conventions": "1.
|
|
45
|
-
"axios": "1.
|
|
33
|
+
"@opentelemetry/core": "2.10.0",
|
|
34
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "0.221.0",
|
|
35
|
+
"@opentelemetry/instrumentation": "0.221.0",
|
|
36
|
+
"@opentelemetry/instrumentation-dns": "0.64.0",
|
|
37
|
+
"@opentelemetry/instrumentation-express": "0.69.0",
|
|
38
|
+
"@opentelemetry/instrumentation-http": "0.221.0",
|
|
39
|
+
"@opentelemetry/instrumentation-winston": "0.65.0",
|
|
40
|
+
"@opentelemetry/resources": "2.10.0",
|
|
41
|
+
"@opentelemetry/sdk-node": "0.221.0",
|
|
42
|
+
"@opentelemetry/sdk-trace-base": "2.10.0",
|
|
43
|
+
"@opentelemetry/sdk-trace-node": "2.10.0",
|
|
44
|
+
"@opentelemetry/semantic-conventions": "1.43.0",
|
|
45
|
+
"axios": "1.19.0",
|
|
46
46
|
"content-disposition-parser": "1.0.2",
|
|
47
47
|
"date-and-time": "4.5.1",
|
|
48
48
|
"express": "5.2.1",
|
|
@@ -53,24 +53,24 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/express": "5.0.6",
|
|
56
|
-
"@types/lodash": "4.17.
|
|
57
|
-
"@types/node": "24.13.
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "8.
|
|
59
|
-
"@typescript-eslint/parser": "8.
|
|
60
|
-
"@vitest/coverage-v8": "4.1.
|
|
61
|
-
"@vitest/ui": "4.1.
|
|
62
|
-
"eslint": "9.39.
|
|
56
|
+
"@types/lodash": "4.17.25",
|
|
57
|
+
"@types/node": "24.13.3",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "8.67.0",
|
|
59
|
+
"@typescript-eslint/parser": "8.67.0",
|
|
60
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
61
|
+
"@vitest/ui": "4.1.10",
|
|
62
|
+
"eslint": "9.39.5",
|
|
63
63
|
"eslint-config-prettier": "10.1.8",
|
|
64
64
|
"eslint-plugin-prettier": "5.5.6",
|
|
65
65
|
"husky": "9.1.7",
|
|
66
|
-
"lint-staged": "17.0
|
|
67
|
-
"nock": "14.0.
|
|
68
|
-
"prettier": "3.
|
|
66
|
+
"lint-staged": "17.3.0",
|
|
67
|
+
"nock": "14.0.17",
|
|
68
|
+
"prettier": "3.9.6",
|
|
69
69
|
"ts-node": "10.9.2",
|
|
70
70
|
"typescript": "6.0.3",
|
|
71
|
-
"typescript-eslint": "8.
|
|
72
|
-
"vitest": "4.1.
|
|
73
|
-
"vitest-mock-extended": "
|
|
71
|
+
"typescript-eslint": "8.67.0",
|
|
72
|
+
"vitest": "4.1.10",
|
|
73
|
+
"vitest-mock-extended": "5.1.1"
|
|
74
74
|
},
|
|
75
75
|
"prettier": {
|
|
76
76
|
"printWidth": 100,
|