@rapidrest/core 1.0.1
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/LICENSE +23 -0
- package/README.rst +37 -0
- package/dist/lib/AlertUtils.js +239 -0
- package/dist/lib/AlertUtils.js.map +1 -0
- package/dist/lib/ApiError.js +34 -0
- package/dist/lib/ApiError.js.map +1 -0
- package/dist/lib/ClassLoader.js +159 -0
- package/dist/lib/ClassLoader.js.map +1 -0
- package/dist/lib/FileUtils.js +190 -0
- package/dist/lib/FileUtils.js.map +1 -0
- package/dist/lib/JWTUtils.js +111 -0
- package/dist/lib/JWTUtils.js.map +1 -0
- package/dist/lib/Logger.js +28 -0
- package/dist/lib/Logger.js.map +1 -0
- package/dist/lib/MessagingUtils.js +218 -0
- package/dist/lib/MessagingUtils.js.map +1 -0
- package/dist/lib/NotificationsUtils.js +52 -0
- package/dist/lib/NotificationsUtils.js.map +1 -0
- package/dist/lib/OASUtils.js +207 -0
- package/dist/lib/OASUtils.js.map +1 -0
- package/dist/lib/ObjectFactory.js +302 -0
- package/dist/lib/ObjectFactory.js.map +1 -0
- package/dist/lib/ObjectUtils.js +49 -0
- package/dist/lib/ObjectUtils.js.map +1 -0
- package/dist/lib/StringUtils.js +104 -0
- package/dist/lib/StringUtils.js.map +1 -0
- package/dist/lib/TelemetryUtils.js +159 -0
- package/dist/lib/TelemetryUtils.js.map +1 -0
- package/dist/lib/UserUtils.js +138 -0
- package/dist/lib/UserUtils.js.map +1 -0
- package/dist/lib/ValidationUtils.js +130 -0
- package/dist/lib/ValidationUtils.js.map +1 -0
- package/dist/lib/decorators/ObjectDecorators.js +95 -0
- package/dist/lib/decorators/ObjectDecorators.js.map +1 -0
- package/dist/lib/decorators/index.js +2 -0
- package/dist/lib/decorators/index.js.map +1 -0
- package/dist/lib/index.js +18 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/sleep.js +15 -0
- package/dist/lib/sleep.js.map +1 -0
- package/dist/lib/threads/ThreadLogger.js +43 -0
- package/dist/lib/threads/ThreadLogger.js.map +1 -0
- package/dist/lib/threads/ThreadPool.js +270 -0
- package/dist/lib/threads/ThreadPool.js.map +1 -0
- package/dist/lib/threads/ThreadWorker.js +25 -0
- package/dist/lib/threads/ThreadWorker.js.map +1 -0
- package/dist/lib/threads/ThreadWorkerEntry.js +77 -0
- package/dist/lib/threads/ThreadWorkerEntry.js.map +1 -0
- package/dist/lib/threads/index.js +3 -0
- package/dist/lib/threads/index.js.map +1 -0
- package/dist/types/AlertUtils.d.ts +132 -0
- package/dist/types/ApiError.d.ts +13 -0
- package/dist/types/ClassLoader.d.ts +87 -0
- package/dist/types/FileUtils.d.ts +46 -0
- package/dist/types/JWTUtils.d.ts +139 -0
- package/dist/types/Logger.d.ts +7 -0
- package/dist/types/MessagingUtils.d.ts +77 -0
- package/dist/types/NotificationsUtils.d.ts +32 -0
- package/dist/types/OASUtils.d.ts +54 -0
- package/dist/types/ObjectFactory.d.ts +77 -0
- package/dist/types/ObjectUtils.d.ts +17 -0
- package/dist/types/StringUtils.d.ts +52 -0
- package/dist/types/TelemetryUtils.d.ts +84 -0
- package/dist/types/UserUtils.d.ts +61 -0
- package/dist/types/ValidationUtils.d.ts +66 -0
- package/dist/types/decorators/ObjectDecorators.d.ts +55 -0
- package/dist/types/decorators/index.d.ts +1 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/sleep.d.ts +5 -0
- package/dist/types/threads/ThreadLogger.d.ts +15 -0
- package/dist/types/threads/ThreadPool.d.ts +115 -0
- package/dist/types/threads/ThreadWorker.d.ts +41 -0
- package/dist/types/threads/ThreadWorkerEntry.d.ts +1 -0
- package/dist/types/threads/index.d.ts +2 -0
- package/docs/Makefile +20 -0
- package/docs/conf.py +58 -0
- package/docs/index.rst +17 -0
- package/docs/make.bat +35 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/README.md +21 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Config.md +47 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Destroy.md +27 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Init.md +33 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Inject.md +45 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Logger.md +27 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Nullable.md +27 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/functions/Validator.md +39 -0
- package/docs/reference/@rapidrest/namespaces/ObjectDecorators/type-aliases/ValidatorFunction.md +31 -0
- package/docs/reference/README.md +73 -0
- package/docs/reference/classes/AlertUtils.md +158 -0
- package/docs/reference/classes/ApiError.md +263 -0
- package/docs/reference/classes/ClassLoader.md +224 -0
- package/docs/reference/classes/Event.md +109 -0
- package/docs/reference/classes/EventUtils.md +123 -0
- package/docs/reference/classes/FileUtils.md +193 -0
- package/docs/reference/classes/JWTUtils.md +91 -0
- package/docs/reference/classes/MessagingUtils.md +145 -0
- package/docs/reference/classes/OASUtils.md +213 -0
- package/docs/reference/classes/ObjectFactory.md +274 -0
- package/docs/reference/classes/ObjectUtils.md +60 -0
- package/docs/reference/classes/StringUtils.md +169 -0
- package/docs/reference/classes/ThreadPool.md +249 -0
- package/docs/reference/classes/ThreadWorker.md +91 -0
- package/docs/reference/classes/UserUtils.md +239 -0
- package/docs/reference/classes/ValidationUtils.md +294 -0
- package/docs/reference/enumerations/AlertPriority.md +61 -0
- package/docs/reference/enumerations/JWTUtilsCompressionMethods.md +21 -0
- package/docs/reference/enumerations/WorkerMessageType.md +43 -0
- package/docs/reference/functions/sleep.md +25 -0
- package/docs/reference/interfaces/Alert.md +101 -0
- package/docs/reference/interfaces/AlertAttachment.md +51 -0
- package/docs/reference/interfaces/AlertClose.md +31 -0
- package/docs/reference/interfaces/AlertUtilsAttachmentOptions.md +42 -0
- package/docs/reference/interfaces/AlertUtilsOptions.md +41 -0
- package/docs/reference/interfaces/InstanceOptions.md +35 -0
- package/docs/reference/interfaces/JWTPayload.md +143 -0
- package/docs/reference/interfaces/JWTUser.md +55 -0
- package/docs/reference/interfaces/JWTUtilsConfig.md +45 -0
- package/docs/reference/interfaces/JWTUtilsPayloadKeyOptions.md +68 -0
- package/docs/reference/interfaces/JWTUtilsPayloadOptions.md +40 -0
- package/docs/reference/interfaces/JWTUtilsPayloadPasswordOptions.md +78 -0
- package/docs/reference/interfaces/NewEvent.md +25 -0
- package/docs/reference/interfaces/OriginSettings.md +25 -0
- package/docs/reference/interfaces/SlackConfig.md +33 -0
- package/docs/reference/interfaces/SmtpAuth.md +25 -0
- package/docs/reference/interfaces/SmtpConfig.md +41 -0
- package/docs/reference/interfaces/Template.md +81 -0
- package/docs/reference/interfaces/TemplateMapBase.md +13 -0
- package/docs/reference/interfaces/TwilioConfig.md +33 -0
- package/docs/reference/interfaces/WorkerMessage.md +31 -0
- package/docs/reference/interfaces/WorkerOptions.md +61 -0
- package/docs/reference/type-aliases/BooleanFunc.md +23 -0
- package/docs/reference/type-aliases/TemplateMap.md +17 -0
- package/docs/reference/type-aliases/WorkerCallback.md +31 -0
- package/docs/reference/variables/Logger.md +21 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2020-2026 Jean-Philippe Steinmetz <caskater47@gmail.com>
|
|
4
|
+
Copyright (C) 2020-2026 Arthur Thompson
|
|
5
|
+
Copyright (C) 2024 Thomas Buscaglia
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.rst
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
=======================
|
|
2
|
+
RapidREST: Core Library
|
|
3
|
+
=======================
|
|
4
|
+
|
|
5
|
+
A set of common utilities shared amongst all RapidREST based applications and servers.
|
|
6
|
+
|
|
7
|
+
Features
|
|
8
|
+
========
|
|
9
|
+
|
|
10
|
+
* Alert & Notification System
|
|
11
|
+
* Class Loader
|
|
12
|
+
* File management utilities
|
|
13
|
+
* JSON Web Token utilities
|
|
14
|
+
* Object Factory
|
|
15
|
+
* OpenAPI utilities
|
|
16
|
+
* String utilities
|
|
17
|
+
* Telemetry utilities
|
|
18
|
+
* Thread-pool Manager
|
|
19
|
+
* User authentication utilities
|
|
20
|
+
* Validation utilities
|
|
21
|
+
|
|
22
|
+
Getting Started
|
|
23
|
+
===============
|
|
24
|
+
|
|
25
|
+
Yarn
|
|
26
|
+
~~~~
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
yarn add @rapidrest/core
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
NPM
|
|
33
|
+
~~~
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
npm i @rapidrest/core
|
|
37
|
+
```
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
import axios from "axios";
|
|
5
|
+
import FormData from "form-data";
|
|
6
|
+
import handlebars from "handlebars";
|
|
7
|
+
import JSZip from "jszip";
|
|
8
|
+
import { sleep } from "./sleep.js";
|
|
9
|
+
const MAX_ATTEMPTS = 60;
|
|
10
|
+
const MAX_CHARS_ALIAS = 512;
|
|
11
|
+
const MAX_CHARS_DESCRIPTION = 15000;
|
|
12
|
+
const MAX_CHARS_ENTITY = 512;
|
|
13
|
+
const MAX_CHARS_MESSAGE = 130;
|
|
14
|
+
const MAX_CHARS_NOTE = 25000;
|
|
15
|
+
const MAX_CHARS_SOURCE = 100;
|
|
16
|
+
const MAX_CHARS_TAGS = 50;
|
|
17
|
+
const MAX_TAGS = 20;
|
|
18
|
+
/**
|
|
19
|
+
* Describes the level of priority for a given alert and an importance for triaging the problem.
|
|
20
|
+
*/
|
|
21
|
+
export var AlertPriority;
|
|
22
|
+
(function (AlertPriority) {
|
|
23
|
+
/** Describes an alert that affects critical operating functionality or infrastructure. */
|
|
24
|
+
AlertPriority["Critical"] = "P1";
|
|
25
|
+
/** Describes an alert that affects key systems that have great impact on end-user experiences. */
|
|
26
|
+
AlertPriority["Severe"] = "P2";
|
|
27
|
+
/** Describes an alert that affects basic systems that may create a poor end-user experience. */
|
|
28
|
+
AlertPriority["Important"] = "P3";
|
|
29
|
+
/** Describes an alert that has no material affect on the end-user experience but with potential to escalate. */
|
|
30
|
+
AlertPriority["Warning"] = "P4";
|
|
31
|
+
/** Describes an alert that has no material affect on end-user experience and no potential for escalation. */
|
|
32
|
+
AlertPriority["Notice"] = "P5";
|
|
33
|
+
})(AlertPriority || (AlertPriority = {}));
|
|
34
|
+
/**
|
|
35
|
+
* The `AlertUtils` class is used to send alerts about important system events that have occurred
|
|
36
|
+
* and require further monitoring or intervention.
|
|
37
|
+
*/
|
|
38
|
+
export class AlertUtils {
|
|
39
|
+
/** The logging utility to use. */
|
|
40
|
+
logger;
|
|
41
|
+
/** The value of the Authorization header to apply to all outgoing HTTP requests. */
|
|
42
|
+
auth;
|
|
43
|
+
/** The full url of the REST API service that alerts will be sent to. */
|
|
44
|
+
serviceUrl;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new instance of `AuthUtils` with the provided defaults.
|
|
47
|
+
* @param options The configuration options to use.
|
|
48
|
+
*/
|
|
49
|
+
constructor(options) {
|
|
50
|
+
this.auth = options.auth;
|
|
51
|
+
this.logger = options.logger;
|
|
52
|
+
this.serviceUrl = options.serviceUrl;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Attempts to close the existing alert with the given identifier.
|
|
56
|
+
* @param id The unique identifier of the alert to close.
|
|
57
|
+
* @returns True if the operation was successful, otherwise false.
|
|
58
|
+
*/
|
|
59
|
+
async close(id, data = {}) {
|
|
60
|
+
try {
|
|
61
|
+
if (data.note) {
|
|
62
|
+
data.note = data.note.substring(0, MAX_CHARS_NOTE);
|
|
63
|
+
}
|
|
64
|
+
if (data.source) {
|
|
65
|
+
data.source = data.source.substring(0, MAX_CHARS_SOURCE);
|
|
66
|
+
}
|
|
67
|
+
const url = `${this.serviceUrl}/${id}/close`;
|
|
68
|
+
const response = await axios.post(url, data, {
|
|
69
|
+
headers: {
|
|
70
|
+
Authorization: this.auth,
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return response.status >= 200 && response.status < 300;
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
this.logger.error("Failed to close alert with id " + id);
|
|
77
|
+
this.logger.error(err.message);
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Attempts to retrieve the existing alert with the given identifier.
|
|
83
|
+
* @param id The unique identifier of the alert to retrieve.
|
|
84
|
+
* @returns The retrieved alert if successful, otherwise `null`.
|
|
85
|
+
*/
|
|
86
|
+
async get(id) {
|
|
87
|
+
try {
|
|
88
|
+
const url = `${this.serviceUrl}/${id}`;
|
|
89
|
+
const response = await axios.get(url, {
|
|
90
|
+
headers: {
|
|
91
|
+
Authorization: this.auth,
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return response.status >= 200 && response.status < 300 ? response.data : null;
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
this.logger.error("Failed to retrieve alert with id " + id);
|
|
98
|
+
this.logger.error(err.message);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Sends the provided alert to the configured monitoring service.
|
|
104
|
+
*
|
|
105
|
+
* @param alert The alert to send.
|
|
106
|
+
* @param vars A map of vars to perform replacement on for the alert's various properties.
|
|
107
|
+
* @param attachments The attachments to upload along with the alert.
|
|
108
|
+
* @returns The unique identifier of the created alert if the operation was successful, otherwise `null`.
|
|
109
|
+
*/
|
|
110
|
+
async send(alert, vars = {}, attachments) {
|
|
111
|
+
try {
|
|
112
|
+
// Truncate the various properties to the maximimum allowed by the most restrictive known API (e.g. OpsGenie)
|
|
113
|
+
alert.alias = alert.alias.substring(0, MAX_CHARS_ALIAS);
|
|
114
|
+
alert.description = alert.description.substring(0, MAX_CHARS_DESCRIPTION);
|
|
115
|
+
if (alert.entity) {
|
|
116
|
+
alert.entity = alert.entity.substring(0, MAX_CHARS_ENTITY);
|
|
117
|
+
}
|
|
118
|
+
alert.message = alert.message.substring(0, MAX_CHARS_MESSAGE);
|
|
119
|
+
if (alert.note) {
|
|
120
|
+
alert.note = alert.note.substring(0, MAX_CHARS_NOTE);
|
|
121
|
+
}
|
|
122
|
+
alert.source = alert.source.substring(0, MAX_CHARS_SOURCE);
|
|
123
|
+
if (alert.tags) {
|
|
124
|
+
let tags = [];
|
|
125
|
+
for (let i = 0; i < MAX_TAGS; i++) {
|
|
126
|
+
const tag = alert.tags[i];
|
|
127
|
+
tags.push(tag.substring(0, MAX_CHARS_TAGS));
|
|
128
|
+
}
|
|
129
|
+
alert.tags = tags;
|
|
130
|
+
}
|
|
131
|
+
// Process properties as templates
|
|
132
|
+
let template = handlebars.compile(alert.description);
|
|
133
|
+
alert.description = template(vars);
|
|
134
|
+
template = handlebars.compile(alert.message);
|
|
135
|
+
alert.message = template(vars);
|
|
136
|
+
if (alert.note) {
|
|
137
|
+
template = handlebars.compile(alert.note);
|
|
138
|
+
alert.note = template(vars);
|
|
139
|
+
}
|
|
140
|
+
let response = await axios.post(this.serviceUrl, alert, {
|
|
141
|
+
headers: {
|
|
142
|
+
Authorization: this.auth,
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
const requestId = response.status >= 200 && response.status < 300 ? response.data.requestId : null;
|
|
146
|
+
if (!requestId) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
// Query the API for the final alert id
|
|
150
|
+
let id = null;
|
|
151
|
+
let count = 0;
|
|
152
|
+
while (!id && count < MAX_ATTEMPTS) {
|
|
153
|
+
try {
|
|
154
|
+
const url = `${this.serviceUrl}/requests/${requestId}}`;
|
|
155
|
+
const response = await axios.get(url, {
|
|
156
|
+
headers: {
|
|
157
|
+
Authorization: this.auth,
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
if (response.status >= 200 && response.status < 300) {
|
|
161
|
+
if (response.data.success && response.data.alertId) {
|
|
162
|
+
id = response.data.alertId;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
// We get here when the request isn't finished processing yet. OpsGenie's API is... meh.
|
|
168
|
+
// Wait a second before retrying
|
|
169
|
+
await sleep(1000);
|
|
170
|
+
}
|
|
171
|
+
count++;
|
|
172
|
+
}
|
|
173
|
+
// Now upload any attachments if they were provided
|
|
174
|
+
if (id && attachments) {
|
|
175
|
+
if (attachments.zip) {
|
|
176
|
+
// Package all attachments into a single zip file for upload
|
|
177
|
+
const zip = new JSZip();
|
|
178
|
+
for (const file of attachments.files) {
|
|
179
|
+
zip.file(file.filename, file.data);
|
|
180
|
+
}
|
|
181
|
+
const data = await zip.generateAsync({ type: "nodebuffer" });
|
|
182
|
+
await this.addAttachment(id, {
|
|
183
|
+
contentType: "application/zip",
|
|
184
|
+
data,
|
|
185
|
+
filename: `${id}.zip`,
|
|
186
|
+
size: data.length
|
|
187
|
+
}, attachments.indexFile);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
// Upload each attachment individually
|
|
191
|
+
for (const file of attachments.files) {
|
|
192
|
+
await this.addAttachment(id, file, attachments.indexFile);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return id;
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
this.logger?.error("Failed to send alert.");
|
|
200
|
+
this.logger?.error(err.response?.data?.message || err.message);
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Uploads a single attachment to the alert with the given unique identifier.
|
|
206
|
+
* @param id The unique identifier of the alert to add an attachment for.
|
|
207
|
+
* @param attachment The file to upload as an attachment.
|
|
208
|
+
* @param indexFile Sets the indexFile parameter of the request.
|
|
209
|
+
* @returns True if the operation was successful, otherwise false.
|
|
210
|
+
*/
|
|
211
|
+
async addAttachment(id, attachment, indexFile) {
|
|
212
|
+
try {
|
|
213
|
+
const form = new FormData();
|
|
214
|
+
form.append("file", attachment.data, {
|
|
215
|
+
contentType: attachment.contentType,
|
|
216
|
+
filename: attachment.filename,
|
|
217
|
+
knownLength: attachment.size
|
|
218
|
+
});
|
|
219
|
+
let url = `${this.serviceUrl}/${id}/attachments`;
|
|
220
|
+
if (indexFile) {
|
|
221
|
+
url += "&indexFile=" + indexFile;
|
|
222
|
+
}
|
|
223
|
+
const response = await axios.post(url, form, {
|
|
224
|
+
headers: {
|
|
225
|
+
Authorization: this.auth,
|
|
226
|
+
"Content-Type": "multipart/form-data"
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
return response.status >= 200 && response.status < 300;
|
|
230
|
+
}
|
|
231
|
+
catch (err) {
|
|
232
|
+
// Don't fail the rest of the request if attachments fail
|
|
233
|
+
this.logger?.error(`Failed to upload attachment ${attachment.filename} to alert ${id}.`);
|
|
234
|
+
this.logger?.error(err.response?.data?.message || err.message);
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=AlertUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AlertUtils.js","sourceRoot":"","sources":["../../src/AlertUtils.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,OAAO,KAAwB,MAAM,OAAO,CAAC;AAC7C,OAAO,QAAQ,MAAM,WAAW,CAAC;AAEjC,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,MAAM,YAAY,GAAW,EAAE,CAAC;AAChC,MAAM,eAAe,GAAW,GAAG,CAAC;AACpC,MAAM,qBAAqB,GAAW,KAAK,CAAC;AAC5C,MAAM,gBAAgB,GAAW,GAAG,CAAC;AACrC,MAAM,iBAAiB,GAAW,GAAG,CAAC;AACtC,MAAM,cAAc,GAAW,KAAK,CAAC;AACrC,MAAM,gBAAgB,GAAW,GAAG,CAAC;AACrC,MAAM,cAAc,GAAW,EAAE,CAAC;AAClC,MAAM,QAAQ,GAAW,EAAE,CAAC;AAE5B;;GAEG;AACH,MAAM,CAAN,IAAY,aAWX;AAXD,WAAY,aAAa;IACrB,0FAA0F;IAC1F,gCAAe,CAAA;IACf,kGAAkG;IAClG,8BAAa,CAAA;IACb,gGAAgG;IAChG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
+
/**
|
|
3
|
+
* Describes an error that originates from a API service. This class extends the standard `Error` class to include
|
|
4
|
+
* a unique code that can be used to trace an error to source code as well as the HTTP response status returned with
|
|
5
|
+
* the error.
|
|
6
|
+
*/
|
|
7
|
+
export class ApiError extends Error {
|
|
8
|
+
/** The unique code of the error. */
|
|
9
|
+
code;
|
|
10
|
+
/** The HTTP status associated with the error. */
|
|
11
|
+
status;
|
|
12
|
+
ApiMessageTemplate(message, templateVariables) {
|
|
13
|
+
if (!message) {
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
if (message && templateVariables) {
|
|
17
|
+
return StringUtils.findAndReplace(message, templateVariables);
|
|
18
|
+
}
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
21
|
+
constructor(code, status, message, templateVariables) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.status = status;
|
|
25
|
+
try {
|
|
26
|
+
this.message = this.ApiMessageTemplate(message, templateVariables);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
// NO-OP
|
|
30
|
+
}
|
|
31
|
+
Object.setPrototypeOf(this, ApiError.prototype);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=ApiError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiError.js","sourceRoot":"","sources":["../../src/ApiError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;;;GAIG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IAC/B,oCAAoC;IAC7B,IAAI,CAAS;IACpB,iDAAiD;IAC1C,MAAM,CAAS;IAEf,kBAAkB,CAAC,OAA2B,EAAE,iBAAuB;QAC1E,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,OAAO,EAAE,CAAC;QACd,CAAC;QACD,IAAI,OAAO,IAAI,iBAAiB,EAAE,CAAC;YAC/B,OAAO,WAAW,CAAC,cAAc,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;QAClE,CAAC;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;IAED,YAAY,IAAY,EAAE,MAAc,EAAE,OAAgB,EAAE,iBAAuB;QAC/E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC;YACD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;QACvE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,SAAS;QACb,CAAC;QACD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACJ"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import path from "path";
|
|
6
|
+
/**
|
|
7
|
+
* The *ClassLoader* provides a container for dynamically loading classes at runtime using C#/Java style namespaces.
|
|
8
|
+
* Namespaces are determined from the folder structure relative to the root.
|
|
9
|
+
*
|
|
10
|
+
* Class names are derived either directly from the modules export name or inferred from the module's file path when
|
|
11
|
+
* the `default` export is used.
|
|
12
|
+
*
|
|
13
|
+
* For example, given the following example TypeScript module at relative path `com/company/MyClass.ts` would register
|
|
14
|
+
* a class with fully qualified name `com.company.MyClass`.
|
|
15
|
+
*
|
|
16
|
+
* ```javascript
|
|
17
|
+
* export default class MyClass {
|
|
18
|
+
* // ...
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* In the event that multiple exports are defined, the fully qualified name will take on the export name instead of the
|
|
23
|
+
* module name. Thus, the following example module at relative path `com/company/MyClasses.ts` would register the following
|
|
24
|
+
* classes.
|
|
25
|
+
* - `com.company.MyClass`
|
|
26
|
+
* - `com.company.MyEnum`
|
|
27
|
+
*
|
|
28
|
+
* ```javascript
|
|
29
|
+
* export class MyClass {
|
|
30
|
+
* // ...
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* export enum MyEnum {
|
|
34
|
+
* // ...
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* **IMPORTANT**: If you use *ClassLoader* to dynamically load TypeScript files (.ts, .cts, .mts, etc.) at runtime using
|
|
39
|
+
* an ESM-only project you must use the `ts-node/esm` module loader. This can be specified by passing the `--loader` to
|
|
40
|
+
* node at runtime.
|
|
41
|
+
*
|
|
42
|
+
* ```
|
|
43
|
+
* node --loader ts-node/esm <module>
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @author Jean-Philippe Steinmetz
|
|
47
|
+
*/
|
|
48
|
+
export class ClassLoader {
|
|
49
|
+
/** The map containnig all loaded classes. */
|
|
50
|
+
classes = new Map();
|
|
51
|
+
ignore = [];
|
|
52
|
+
/** Indicates if TypeScript classes should be loaded. */
|
|
53
|
+
includeTypeScript = true;
|
|
54
|
+
/** Indicates if JavaScript classes should be loaded. */
|
|
55
|
+
includeJavaScript = true;
|
|
56
|
+
/** The path to the root directory containing all classes on disk. */
|
|
57
|
+
rootDir = ".";
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new instance of `ClassLoader` with the specified defaults.
|
|
60
|
+
*
|
|
61
|
+
* @param rootDir The root directory to load all classes from.
|
|
62
|
+
* @param includeJavaScript Set to `true` to load all TypeScript classes from the given `rootDir`, otherwise set to `false.
|
|
63
|
+
* @param includeTypeScript Set to `true` to load all JavaScript classes from the given `rootDir`, otherwise set to `false.
|
|
64
|
+
* @param ignore A list of regex pattern of file paths to ignore.
|
|
65
|
+
*/
|
|
66
|
+
constructor(rootDir, includeJavaScript = true, includeTypeScript = true, ignore = []) {
|
|
67
|
+
this.rootDir = path.resolve(rootDir);
|
|
68
|
+
this.includeJavaScript = includeJavaScript;
|
|
69
|
+
this.includeTypeScript = includeTypeScript;
|
|
70
|
+
this.ignore = ignore;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns the class with the specified fully qualified name.
|
|
74
|
+
*
|
|
75
|
+
* @param fqn The fully qualified name of the class to return.
|
|
76
|
+
* @returns The class definition for the given fully qualified name if found, otherwise `undefined`.
|
|
77
|
+
*/
|
|
78
|
+
getClass(fqn) {
|
|
79
|
+
return this.classes.get(fqn);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Returns the map containing all classes that have been loaded.
|
|
83
|
+
*/
|
|
84
|
+
getClasses() {
|
|
85
|
+
return this.classes;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Returns `true` if a class exists with the specified fully qualified name.
|
|
89
|
+
*
|
|
90
|
+
* @param fqn The fully qualified name of the class to search.
|
|
91
|
+
* @returns `true` if a class definition exists for the given fully qualified name, otherwise `false`.
|
|
92
|
+
*/
|
|
93
|
+
hasClass(fqn) {
|
|
94
|
+
return this.classes.get(fqn) ? true : false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Loads all modules exports contained in the directory specified. The folder must be a child
|
|
98
|
+
* directory to the `rootDir` parameter passed in to the constructor.
|
|
99
|
+
*
|
|
100
|
+
* @param dir The directory, relative to `rootDir`, containing modules to load.
|
|
101
|
+
*/
|
|
102
|
+
async load(dir = "") {
|
|
103
|
+
let fqp = path.resolve(path.join(this.rootDir, dir));
|
|
104
|
+
let files = fs.readdirSync(fqp, { withFileTypes: true });
|
|
105
|
+
for (let file of files) {
|
|
106
|
+
// Is the file in the ignore list?
|
|
107
|
+
let skipFile = false;
|
|
108
|
+
for (const iPath of this.ignore) {
|
|
109
|
+
if (file.name.match(iPath)) {
|
|
110
|
+
skipFile = true;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (skipFile) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
let extension = path.extname(file.name);
|
|
118
|
+
if (!extension) {
|
|
119
|
+
extension = file.name;
|
|
120
|
+
}
|
|
121
|
+
let relpath = path.relative(this.rootDir, fqp);
|
|
122
|
+
let fullpath = path.resolve(path.join(this.rootDir, relpath, file.name));
|
|
123
|
+
let pkg = relpath.replace(new RegExp("\\" + path.sep, "g"), ".");
|
|
124
|
+
if (file.isDirectory()) {
|
|
125
|
+
let subdir = path.join(dir, file.name);
|
|
126
|
+
await this.load(subdir);
|
|
127
|
+
}
|
|
128
|
+
else if (this.includeJavaScript && extension.match(/^\.(js|cjs|mjs)$/)) {
|
|
129
|
+
const mod = await import(fullpath);
|
|
130
|
+
if (mod) {
|
|
131
|
+
for (let name in mod) {
|
|
132
|
+
let clazz = mod[name];
|
|
133
|
+
let fqn = `${pkg.length > 0 ? pkg + "." : ""}${name === "default" ? file.name.split(".")[0] : name}`;
|
|
134
|
+
clazz.fqn = fqn;
|
|
135
|
+
this.classes.set(fqn, clazz);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
throw new Error("Failed to load module file: " + fullpath);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (this.includeTypeScript && extension.match(/^\.(ts|cts|mts|tsx)$/)) {
|
|
143
|
+
const mod = await import(fullpath);
|
|
144
|
+
if (mod) {
|
|
145
|
+
for (let name in mod) {
|
|
146
|
+
let clazz = mod[name];
|
|
147
|
+
let fqn = `${pkg.length > 0 ? pkg + "." : ""}${name === "default" ? file.name.split(".")[0] : name}`;
|
|
148
|
+
clazz.fqn = fqn;
|
|
149
|
+
this.classes.set(fqn, clazz);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
throw new Error("Failed to load module file: " + fullpath);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
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