@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
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
/**
|
|
5
|
+
* Utility functions for working with strings.
|
|
6
|
+
*
|
|
7
|
+
* @author Jean-Philippe Steinmetz
|
|
8
|
+
*/
|
|
9
|
+
export class StringUtils {
|
|
10
|
+
/**
|
|
11
|
+
* Returns a list of all parameters contained within the string. A parameter is a bracket delimited substring
|
|
12
|
+
* (e.g. /my/{key}/with/{id}).
|
|
13
|
+
*
|
|
14
|
+
* @param {any} str The string to search for parameters.
|
|
15
|
+
* @returns {array} A list of parameters contained in the provided string.
|
|
16
|
+
*/
|
|
17
|
+
static getParameters(str) {
|
|
18
|
+
let results = new Array();
|
|
19
|
+
for (let i = 0; i < str.length; i++) {
|
|
20
|
+
let start = str.indexOf("{", i);
|
|
21
|
+
if (start !== -1) {
|
|
22
|
+
let end = str.indexOf("}", start);
|
|
23
|
+
if (end !== -1) {
|
|
24
|
+
results.push(str.substring(start + 1, end));
|
|
25
|
+
i = end;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return results;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Performs a search and replace on the provided contents with the map of variable replacements. The contents
|
|
39
|
+
* must use Mustache formatted tokens such as `{{toreplace}}`.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} contents The stringt to perform the find and replace on.
|
|
42
|
+
* @param {object} variables A map of key=>value pairs to search for and replace.
|
|
43
|
+
*/
|
|
44
|
+
static findAndReplace(contents, variables) {
|
|
45
|
+
let output = contents;
|
|
46
|
+
// Go through all variables and perform replacement
|
|
47
|
+
for (let key in variables) {
|
|
48
|
+
// Perform replacement on the variable value itself. This allows nested variable replacement.
|
|
49
|
+
if (variables[key]) {
|
|
50
|
+
let value = variables[key];
|
|
51
|
+
for (let key2 in variables) {
|
|
52
|
+
if (variables[key2]) {
|
|
53
|
+
let value2 = variables[key2];
|
|
54
|
+
value = value.toString().replace(new RegExp("(\\{\\{" + key2 + "\\}\\})", "g"), value2);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
output = output.replace(new RegExp("(\\{\\{" + key + "\\}\\})", "g"), value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return output;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Replaces all instances of the match regex pattern with the contents of the inner regular expression pattern for
|
|
64
|
+
* the given string.
|
|
65
|
+
*
|
|
66
|
+
* e.g.
|
|
67
|
+
*
|
|
68
|
+
* let result = replaceAll('/my/path/{id}', new RegExp('\\{([^\\}]+)\\}'), ':');
|
|
69
|
+
* console.log(result); // -> /my/path/:id
|
|
70
|
+
*
|
|
71
|
+
* @param {string} str The string to perform replacement on.
|
|
72
|
+
* @param {RegExp} match The regular expression pattern to match containing an outer and inner pattern.
|
|
73
|
+
* @param {string} prefix The prefix to prepend the replacement text with.
|
|
74
|
+
* @returns {string} The fully replaced contents of the string.
|
|
75
|
+
*/
|
|
76
|
+
static replaceAll(str, match, prefix) {
|
|
77
|
+
let result = str;
|
|
78
|
+
let matches = str.match(match);
|
|
79
|
+
while (matches) {
|
|
80
|
+
result = result.replace(matches[0], prefix + matches[1]);
|
|
81
|
+
matches = result.match(match);
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Converts the first character in the given string to be lowercase (e.g. myVariable).
|
|
87
|
+
*
|
|
88
|
+
* @param {string} str The string to convert to camelCase.
|
|
89
|
+
* @returns {string} The string converted to camelCase.
|
|
90
|
+
*/
|
|
91
|
+
static toCamelCase(str) {
|
|
92
|
+
return str.charAt(0).toLocaleLowerCase() + str.substring(1);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Converts the first character in the given string to be uppercase (e.g. MyVariable).
|
|
96
|
+
*
|
|
97
|
+
* @param {string} str The string to convert to PascalCase.
|
|
98
|
+
* @returns {string} The string converted to PascalCase.
|
|
99
|
+
*/
|
|
100
|
+
static toPascalCase(str) {
|
|
101
|
+
return str.charAt(0).toLocaleUpperCase() + str.substring(1);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=StringUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StringUtils.js","sourceRoot":"","sources":["../../src/StringUtils.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAE/E;;;;GAIG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Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
import axios from "axios";
|
|
5
|
+
import * as uuid from "uuid";
|
|
6
|
+
import { JWTUtils } from "./JWTUtils.js";
|
|
7
|
+
/**
|
|
8
|
+
* Describes a single telemetry event. A telemetry event is when something occurs in the system.
|
|
9
|
+
*
|
|
10
|
+
* @author Jean-Philippe Steinmetz <rapidrests@gmail.com>
|
|
11
|
+
*/
|
|
12
|
+
export class Event {
|
|
13
|
+
/**
|
|
14
|
+
* The universally unique identifier of the event.
|
|
15
|
+
*/
|
|
16
|
+
uid;
|
|
17
|
+
/**
|
|
18
|
+
* The name of the environment that the event originated from. This is typically `dev` or `prod`.
|
|
19
|
+
*/
|
|
20
|
+
environment;
|
|
21
|
+
/**
|
|
22
|
+
* The unique name of the service or client that the event originated from.
|
|
23
|
+
*/
|
|
24
|
+
origin;
|
|
25
|
+
/**
|
|
26
|
+
* The date and time that the event occured.
|
|
27
|
+
*/
|
|
28
|
+
timestamp = new Date();
|
|
29
|
+
/**
|
|
30
|
+
* The type of event being recorded.
|
|
31
|
+
*/
|
|
32
|
+
type;
|
|
33
|
+
/**
|
|
34
|
+
* The universally unique identifer of the user that sent the event.
|
|
35
|
+
*/
|
|
36
|
+
userId;
|
|
37
|
+
constructor(config, userId, data) {
|
|
38
|
+
this.uid = uuid.v4();
|
|
39
|
+
this.environment = process.env.NODE_ENV ? process.env.NODE_ENV : "prod";
|
|
40
|
+
const serviceName = config.get("service_name");
|
|
41
|
+
this.origin = serviceName ? `${serviceName}:${config.get("version")}` : "unknown";
|
|
42
|
+
this.type = data.type;
|
|
43
|
+
this.userId = userId;
|
|
44
|
+
// Copy all fields in data to this object
|
|
45
|
+
const tmp = data;
|
|
46
|
+
for (let key in tmp) {
|
|
47
|
+
if (key !== "environment" && key !== "origin" && key !== "userId") {
|
|
48
|
+
this[key] = tmp[key];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Provides a common set of static functions for recording and working with telemetry `Event` instances.
|
|
55
|
+
*
|
|
56
|
+
* The `init` function must be called before any other function can be called.
|
|
57
|
+
*
|
|
58
|
+
* The `record` function is used to send an event to a configured `telemetry_services` service for permanent storage.
|
|
59
|
+
*
|
|
60
|
+
* The `on` function allows code within the same application or service to listen for outgoing events that have been
|
|
61
|
+
* sent via the `record` function.
|
|
62
|
+
*
|
|
63
|
+
* @author Jean-Philippe Steinmetz <rapidrests@gmail.com>
|
|
64
|
+
*/
|
|
65
|
+
export class EventUtils {
|
|
66
|
+
static initialized = false;
|
|
67
|
+
static config;
|
|
68
|
+
static listeners = new Map();
|
|
69
|
+
static logger;
|
|
70
|
+
static token;
|
|
71
|
+
static userId;
|
|
72
|
+
/**
|
|
73
|
+
* Initializes `EventUtils` with the provided defaults.
|
|
74
|
+
*
|
|
75
|
+
* @param config The application configuration to use.
|
|
76
|
+
* @param logger The logging utility to use.
|
|
77
|
+
* @param jwtToken The user's JWT token to send telemetry events on behalf of.
|
|
78
|
+
*/
|
|
79
|
+
static init(config, logger, jwtToken) {
|
|
80
|
+
EventUtils.config = config;
|
|
81
|
+
EventUtils.listeners = new Map();
|
|
82
|
+
EventUtils.logger = logger;
|
|
83
|
+
EventUtils.token = jwtToken;
|
|
84
|
+
// Extract the user id from the token
|
|
85
|
+
const payload = JWTUtils.decodeToken(config.get("auth"), jwtToken);
|
|
86
|
+
EventUtils.userId = payload.profile.uid;
|
|
87
|
+
EventUtils.initialized = true;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Sends the given event to the telemetry service for permanent recording.
|
|
91
|
+
*
|
|
92
|
+
* @param evt The event to send and record.
|
|
93
|
+
* @param type The type of event to record. Overrides any `type` value set in `evt`.
|
|
94
|
+
*/
|
|
95
|
+
static async record(evt, type) {
|
|
96
|
+
try {
|
|
97
|
+
if (!EventUtils.initialized) {
|
|
98
|
+
throw new Error("EventUtils has not been initialized. Did you call EventUtils.init()?");
|
|
99
|
+
}
|
|
100
|
+
if (type) {
|
|
101
|
+
evt.type = type;
|
|
102
|
+
}
|
|
103
|
+
if (!evt.type || evt.type.length === 0) {
|
|
104
|
+
throw new Error("Events must have a `type` specified.");
|
|
105
|
+
}
|
|
106
|
+
// Create the event instance that will be sent for storage
|
|
107
|
+
const event = evt instanceof Event ? evt : new Event(EventUtils.config, EventUtils.userId, evt);
|
|
108
|
+
// Send to the telemetry service
|
|
109
|
+
const baseUrl = EventUtils.config.get("telemetry_services:url");
|
|
110
|
+
if (baseUrl) {
|
|
111
|
+
const url = baseUrl + "/events";
|
|
112
|
+
const response = await axios.post(url, event, {
|
|
113
|
+
headers: {
|
|
114
|
+
Authorization: "jwt " + EventUtils.token,
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
EventUtils.logger?.debug("Failed to send telemetry event. telemetry_services:url has not been set in the config.");
|
|
120
|
+
}
|
|
121
|
+
// Notify any registered listeners
|
|
122
|
+
const callbacks = EventUtils.listeners.get(evt.type);
|
|
123
|
+
if (callbacks) {
|
|
124
|
+
for (let func of callbacks) {
|
|
125
|
+
func(event);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
if (!EventUtils.logger) {
|
|
131
|
+
console.warn(`Failed to send telemetry event. Error: ${err.message}`);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
EventUtils.logger.warn(`Failed to send telemetry event. Error: ${err.message}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Registers a `callback` function to receive notifications of recorded events of the given `type`. Note that only
|
|
140
|
+
* events originating from the same application or service will be notified.
|
|
141
|
+
*
|
|
142
|
+
* @param type The type of event to be notified of.
|
|
143
|
+
* @param callback The function to call when an event is recorded.
|
|
144
|
+
*/
|
|
145
|
+
static on(type, callback) {
|
|
146
|
+
if (!EventUtils.initialized) {
|
|
147
|
+
throw new Error("EventUtils has not been initialized. Did you call EventUtils.init()?");
|
|
148
|
+
}
|
|
149
|
+
if (!EventUtils.listeners.has(type)) {
|
|
150
|
+
EventUtils.listeners.set(type, []);
|
|
151
|
+
}
|
|
152
|
+
const callbacks = EventUtils.listeners.get(type);
|
|
153
|
+
if (callbacks) {
|
|
154
|
+
callbacks.push(callback);
|
|
155
|
+
EventUtils.listeners.set(type, callbacks);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=TelemetryUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TelemetryUtils.js","sourceRoot":"","sources":["../../src/TelemetryUtils.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,OAAO,KAAwB,MAAM,OAAO,CAAC;AAC7C,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAYzC;;;;GAIG;AACH,MAAM,OAAO,KAAK;IACd;;OAEG;IACa,GAAG,CAAS;IAE5B;;OAEG;IACa,WAAW,CAAS;IAEpC;;OAEG;IACa,MAAM,CAAS;IAE/B;;OAEG;IACa,SAAS,GAAS,IAAI,IAAI,EAAE,CAAC;IAE7C;;OAEG;IACa,IAAI,CAAS;IAE7B;;OAEG;IACa,MAAM,CAAS;IAE/B,YAAY,MAAW,EAAE,MAAc,EAAE,IAAc;QACnD,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,EAAE,EAAE,CAAC;QACrB,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC;QACxE,MAAM,WAAW,GAAW,MAAM,CAAC,GAAG,CAAC,cAAc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////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
/**
|
|
5
|
+
* Utilities for working with authenticated user objects. An user object is expected to have the following
|
|
6
|
+
* properties.
|
|
7
|
+
*
|
|
8
|
+
* * `uid` - Universally unique identifier for the user
|
|
9
|
+
* * `email` - Unique e-mail address for the user
|
|
10
|
+
* * `roles` - A list of unique names indicating the permissions of the user.
|
|
11
|
+
* * `verified` - Indicates if the user's e-mail address has been verified.
|
|
12
|
+
*
|
|
13
|
+
* @author Jean-Philippe Steinmetz <rapidrests@gmail.com>
|
|
14
|
+
*/
|
|
15
|
+
export class UserUtils {
|
|
16
|
+
/**
|
|
17
|
+
* Returns `true` if the given user object is a member of the organization with the specified uid, otherwise returns `false`.
|
|
18
|
+
* @param user The user object to inspect.
|
|
19
|
+
* @param organizationUid The universally unique identifier of the persona to search for.
|
|
20
|
+
*/
|
|
21
|
+
static hasOrganization(user, organizationUid) {
|
|
22
|
+
if (user && user.orgs && Array.isArray(user.orgs)) {
|
|
23
|
+
for (const uid of user.orgs) {
|
|
24
|
+
if (uid === organizationUid) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns `true` if the given user object contains is a member of an organization matching one of the uids in the specified list, otherwise returns `false`.
|
|
33
|
+
* @param user The user object to inspect.
|
|
34
|
+
* @param organizationUids The list of universally unique identifiers to search for.
|
|
35
|
+
*/
|
|
36
|
+
static hasOrganizations(user, organizationUids) {
|
|
37
|
+
if (user && user.orgs && Array.isArray(user.orgs) && Array.isArray(organizationUids)) {
|
|
38
|
+
for (const organizationUid of organizationUids) {
|
|
39
|
+
for (const uid of user.orgs) {
|
|
40
|
+
if (uid === organizationUid) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns `true` if the given user object contains a `Persona` object with the specified uid, otherwise returns `false`.
|
|
50
|
+
* @param user The user object to inspect.
|
|
51
|
+
* @param personaUid The universally unique identifier of the persona to search for.
|
|
52
|
+
*/
|
|
53
|
+
static hasPersona(user, personaUid) {
|
|
54
|
+
if (user && user.personas && Array.isArray(user.personas) && personaUid) {
|
|
55
|
+
for (const persona of user.personas) {
|
|
56
|
+
if (persona.uid === personaUid) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns `true` if the given user object contains a `Persona` object matching one of the uids in the specified list, otherwise returns `false`.
|
|
65
|
+
* @param user The user object to inspect.
|
|
66
|
+
* @param personaUid The list of universally unique identifiers to search for.
|
|
67
|
+
*/
|
|
68
|
+
static hasPersonas(user, personaUids) {
|
|
69
|
+
if (user && user.personas && Array.isArray(user.personas) && Array.isArray(personaUids)) {
|
|
70
|
+
for (const personaUid of personaUids) {
|
|
71
|
+
for (const persona of user.personas) {
|
|
72
|
+
if (persona.uid === personaUid) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns the unique identifier of the specified type associated with the given user.
|
|
82
|
+
*
|
|
83
|
+
* @param user The user to retrieve the external id from.
|
|
84
|
+
* @param type The type of external provider to retrieve.
|
|
85
|
+
* @returns The unique id of the external provider for the given type if found, otherwise `undefined`.
|
|
86
|
+
*/
|
|
87
|
+
static getExternalId(user, type) {
|
|
88
|
+
let result = undefined;
|
|
89
|
+
if (user && Array.isArray(user.externalIds)) {
|
|
90
|
+
for (const externalId of user.externalIds) {
|
|
91
|
+
const parts = externalId.split(":");
|
|
92
|
+
if (parts.length === 2 && parts[0] === type) {
|
|
93
|
+
result = parts[1];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns `true` if the given user object has a role with the specified name, otherwise returns `false`.
|
|
101
|
+
*
|
|
102
|
+
* @param user The user object to inspect.
|
|
103
|
+
* @param role The unique name of the role to search for.
|
|
104
|
+
* @param orgUid The unique identifier of an organization whose role will be verified.
|
|
105
|
+
*/
|
|
106
|
+
static hasRole(user, role, orgUid) {
|
|
107
|
+
let result = false;
|
|
108
|
+
if (user) {
|
|
109
|
+
if (user.roles) {
|
|
110
|
+
if (Array.isArray(user.roles)) {
|
|
111
|
+
result = user.roles.includes(role);
|
|
112
|
+
if (!result && orgUid !== "") {
|
|
113
|
+
result = user.roles.includes(`${orgUid}.${role}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns `true` if the given user object has at least one role from the specified list of names, otherwise returns `false`.
|
|
122
|
+
*
|
|
123
|
+
* @param user The user object to inspect.
|
|
124
|
+
* @param roles A list of unique names of the roles to search for.
|
|
125
|
+
* @param orgUid The unique identifier of an organization whose role will be verified.
|
|
126
|
+
*/
|
|
127
|
+
static hasRoles(user, roles, orgUid) {
|
|
128
|
+
if (user && user.roles && Array.isArray(user.roles) && Array.isArray(roles)) {
|
|
129
|
+
for (const role of roles) {
|
|
130
|
+
if (UserUtils.hasRole(user, role, orgUid)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=UserUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserUtils.js","sourceRoot":"","sources":["../../src/UserUtils.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAE/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AAC;YACP,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;gBACb,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;oBAC5B,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBAEnC,IAAI,CAAC,MAAM,IAAI,MAAM,KAAK,EAAE,EAAE,CAAC;wBAC3B,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC;oBACtD,CAAC;gBACL,CAAC;YACL,CAAC;QACL,CAAC;QAED,OAAO,MAAM,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,QAAQ,CAAC,IAAS,EAAE,KAAgB,EAAE,MAAe;QAC/D,IAAI,IAAI,IAAI,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1E,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACvB,IAAI,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC;oBACxC,OAAO,IAAI,CAAC;gBAChB,CAAC;YACL,CAAC;QACL,CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;CACJ"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
import validator from "validator";
|
|
5
|
+
/**
|
|
6
|
+
* A collection of validation functions to use with the `@Validator()` decorator.
|
|
7
|
+
*/
|
|
8
|
+
export class ValidationUtils {
|
|
9
|
+
/**
|
|
10
|
+
* A simple wrapper that calls the specified boolean validation function with the given value and returns the
|
|
11
|
+
* value as a result if the validation function passes. Otherwise throws an error.
|
|
12
|
+
*
|
|
13
|
+
* @param val The value to validate.
|
|
14
|
+
* @param func The boolean validation function to check the value.
|
|
15
|
+
* @throws Throws an error if the value fails the boolean validation test.
|
|
16
|
+
*/
|
|
17
|
+
static check(val, func) {
|
|
18
|
+
if (!func(val)) {
|
|
19
|
+
throw new Error(`Value failed validation: ${func.name}`);
|
|
20
|
+
}
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Validates that the provided string represents a ISO, RFC or UTC date or timestamp.
|
|
25
|
+
*/
|
|
26
|
+
static checkDate(val) {
|
|
27
|
+
if (!validator.isDate(val)) {
|
|
28
|
+
throw new Error("Value is not a Date.");
|
|
29
|
+
}
|
|
30
|
+
return val;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Validates that the provided string represents a valid e-mail address.
|
|
34
|
+
*/
|
|
35
|
+
static checkEmail(val) {
|
|
36
|
+
if (!validator.isEmail(val)) {
|
|
37
|
+
throw new Error("Value is not a valid email addresss.");
|
|
38
|
+
}
|
|
39
|
+
return val;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validates that the provided array is not empty.
|
|
43
|
+
*/
|
|
44
|
+
static checkEmpty(val) {
|
|
45
|
+
if (val && val.length === 0) {
|
|
46
|
+
throw new Error("Value cannot be empty.");
|
|
47
|
+
}
|
|
48
|
+
return val;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Validates that the provided string is a valid IP address.
|
|
52
|
+
*/
|
|
53
|
+
static checkIP(val) {
|
|
54
|
+
if (!validator.isIP(val)) {
|
|
55
|
+
throw new Error("Value is not a valid IP address.");
|
|
56
|
+
}
|
|
57
|
+
return val;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validates that the provided string is valid JSON (note: uses `JSON.parse`).
|
|
61
|
+
*/
|
|
62
|
+
static checkJSON(val) {
|
|
63
|
+
if (!validator.isJSON(val)) {
|
|
64
|
+
throw new Error("Value is not valid JSON.");
|
|
65
|
+
}
|
|
66
|
+
return val;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Validates that the provided string matches the regexp pattern /[a-zA-Z0-9_\-\.@:\+]+/.
|
|
70
|
+
*/
|
|
71
|
+
static checkName(val) {
|
|
72
|
+
if (!/[a-zA-Z0-9_\-]+/.test(val)) {
|
|
73
|
+
throw new Error("Value is not a name matching pattern /[a-zA-Z0-9_\-\.@:\+]+/");
|
|
74
|
+
}
|
|
75
|
+
return val;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Validates that the provided object is not null or empty.
|
|
79
|
+
*/
|
|
80
|
+
static checkNull(val) {
|
|
81
|
+
if (typeof val !== "boolean" && !val) {
|
|
82
|
+
throw new Error("Value cannot be null.");
|
|
83
|
+
}
|
|
84
|
+
return val;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Validates that the provided string represents a valid phone number.
|
|
88
|
+
*/
|
|
89
|
+
static checkPhone(val) {
|
|
90
|
+
if (!validator.isMobilePhone(val)) {
|
|
91
|
+
throw new Error("Value is not a valid phone number.");
|
|
92
|
+
}
|
|
93
|
+
return val;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Validates that the provided string represents a semantic version.
|
|
97
|
+
*/
|
|
98
|
+
static checkSemVer(val) {
|
|
99
|
+
if (!validator.isSemVer(val)) {
|
|
100
|
+
throw new Error("Value is not a valid semantic version.");
|
|
101
|
+
}
|
|
102
|
+
return val;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Validates that the provided string is a valid URL.
|
|
106
|
+
*/
|
|
107
|
+
static checkURL(val) {
|
|
108
|
+
if (!validator.isURL(val)) {
|
|
109
|
+
throw new Error("Value is not a URL.");
|
|
110
|
+
}
|
|
111
|
+
return val;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Validates that the provided string is a valid UUID.
|
|
115
|
+
*/
|
|
116
|
+
static checkUUID(val) {
|
|
117
|
+
if (!validator.isUUID(val)) {
|
|
118
|
+
throw new Error("Value is not a UUID.");
|
|
119
|
+
}
|
|
120
|
+
return val;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Validates that the provided value is an entity `version` number (e.g. `value > 0`).
|
|
124
|
+
*/
|
|
125
|
+
static checkVersion(val) {
|
|
126
|
+
const num = Number(val);
|
|
127
|
+
return Math.max(num, 0);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=ValidationUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValidationUtils.js","sourceRoot":"","sources":["../../src/ValidationUtils.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,OAAO,SAAS,MAAM,WAAW,CAAC;AAOlC;;GAEG;AACH,MAAM,OAAO,eAAe;IACxB;;;;;;;OAOG;IACI,MAAM,CAAC,KAAK,CAAC,GAAQ,EAAE,IAAiB;QAC3C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,4BAA4B,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAC7D,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,SAAS,CAAC,GAAW;QAC/B,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,UAAU,CAAC,GAAW;QAChC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC5D,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AAK,CAAC,oCAAoC,CAAC,CAAC;QAC1D,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,WAAW,CAAC,GAAW;QACjC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;QAC9D,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,QAAQ,CAAC,GAAW;QAC9B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QAC3C,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,SAAS,CAAC,GAAW;QAC/B,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,GAAG,CAAC;IACf,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,YAAY,CAAC,GAAQ;QAC/B,MAAM,GAAG,GAAW,MAAM,CAAC,GAAG,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IAC5B,CAAC;CACJ"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2020-2026 Jean-Philippe Steinmetz
|
|
3
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
4
|
+
import "reflect-metadata";
|
|
5
|
+
/**
|
|
6
|
+
* Apply this to a class function to mark it as a destructor to be called by the `ObjectFactory` during cleanup.
|
|
7
|
+
*/
|
|
8
|
+
export function Destroy(target, propertyKey) {
|
|
9
|
+
Reflect.defineMetadata("rrst:destructor", true, target, propertyKey);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Injects an object instance to the decorated property of the given name and type using the provided arguments
|
|
13
|
+
* if no object has been created yet.
|
|
14
|
+
* @param type The fully qualified name or type of the class to instantiate. If a type is given it's class name will be inferred
|
|
15
|
+
* via the constructor name.
|
|
16
|
+
* @param name The unique name to give the class instance. Set to `undefined` if you wish to force a new object is created.
|
|
17
|
+
* @param initialize Set to `true` to initialize the object after creation, otherwise set to `false`. Default is `true`.
|
|
18
|
+
* @param args The set of constructor arguments to use during construction
|
|
19
|
+
*/
|
|
20
|
+
export function Inject(type, options) {
|
|
21
|
+
return function (target, propertyKey) {
|
|
22
|
+
options = {
|
|
23
|
+
...options,
|
|
24
|
+
name: options?.name || "default",
|
|
25
|
+
};
|
|
26
|
+
Reflect.defineMetadata("rrst:injectObject", {
|
|
27
|
+
options,
|
|
28
|
+
type,
|
|
29
|
+
}, target, propertyKey);
|
|
30
|
+
Object.defineProperty(target, propertyKey, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: undefined,
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Apply this to a function to be executed once a new object instance has been created and all dependencies injected.
|
|
39
|
+
* Note: If the decorated function returns a Promise it is not gauranteed to finish execution before the object is
|
|
40
|
+
* returned during the instantiation process.
|
|
41
|
+
*/
|
|
42
|
+
export function Init(target, propertyKey, descriptor) {
|
|
43
|
+
Reflect.defineMetadata("rrst:initialize", true, target, propertyKey);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Apply this to a property to have a configuration variable be injected at instantiation. If no path is given, the
|
|
47
|
+
* global configuration object is injected.
|
|
48
|
+
*
|
|
49
|
+
* @param path The path to the configuration variable to inject.
|
|
50
|
+
* @param defaultValue Set to the desired default value. If `undefined` is specified then an error is thrown if
|
|
51
|
+
* no config variable is found at the given path.
|
|
52
|
+
*/
|
|
53
|
+
export function Config(path, defaultValue = undefined) {
|
|
54
|
+
return function (target, propertyKey) {
|
|
55
|
+
Reflect.defineMetadata("rrst:injectConfig", path ? path : true, target, propertyKey);
|
|
56
|
+
Reflect.defineMetadata("rrst:injectConfigDefault", defaultValue, target, propertyKey);
|
|
57
|
+
const key = `__${String(propertyKey)}`;
|
|
58
|
+
Object.defineProperty(target, propertyKey, {
|
|
59
|
+
enumerable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: undefined,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Apply this to a property to have the logger utility injected at instantiation.
|
|
67
|
+
*/
|
|
68
|
+
export function Logger(target, propertyKey) {
|
|
69
|
+
Reflect.defineMetadata("rrst:injectLogger", true, target, propertyKey);
|
|
70
|
+
const key = `__${String(propertyKey)}`;
|
|
71
|
+
Object.defineProperty(target, propertyKey, {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: undefined,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Apply this to a property to indicate that the value can be `null` or `undefined.`
|
|
79
|
+
*
|
|
80
|
+
* @param nullable Set to `true` to indicate that the property value can be `null`, otherwise set to `false`. Default is `false`.
|
|
81
|
+
*/
|
|
82
|
+
export function Nullable(target, propertyKey) {
|
|
83
|
+
Reflect.defineMetadata("rrst:nullable", true, target, propertyKey);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Apply this to a property to specify the function that will be used to perform validation of the value.
|
|
87
|
+
*
|
|
88
|
+
* @param func The validation function to use for the given property.
|
|
89
|
+
*/
|
|
90
|
+
export function Validator(func) {
|
|
91
|
+
return function (target, propertyKey) {
|
|
92
|
+
Reflect.defineMetadata("rrst:validator", func, target, propertyKey);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=ObjectDecorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ObjectDecorators.js","sourceRoot":"","sources":["../../../src/decorators/ObjectDecorators.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,kDAAkD;AAClD,+EAA+E;AAC/E,OAAO,kBAAkB,CAAC;AAG1B;;GAEG;AACH,MAAM,UAAU,OAAO,CAAC,MAAW,EAAE,WAAmB;IACpD,OAAO,CAAC,cAAc,CAAC,iBAAiB,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAC,IAAS,EAAE,OAAyB;IACvD,OAAO,UAAU,MAAW,EAAE,WAA4B;QACtD,OAAO,GAAG;YACN,GAAG,OAAO;YACV,IAAI,EAAE,OAAO,EAAE,IAAI,IAAI,SAAS;SACnC,CAAC;QACF,OAAO,CAAC,cAAc,CAClB,mBAAmB,EACnB;YACI,OAAO;YACP,IAAI;SACP,EACD,MAAM,EACN,WAAW,CACd,CAAC;QACF,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;YACvC,UAAU,EAAE,IAAI;YAChB,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,SAAS;SACnB,CAAC,CAAC;IACP,CAAC,CAAC;AACN,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAAC,MAAW,EAAE,WAAmB,EAAE,UAA8B;IACjF,OAAO,CAAC,cAAc,CAAC,iBAAiB,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,MAAM,CAAC,IAAa,EAAE,eAAoB,SAAS;IAC/D,OAAO,UAAU,MAAW,EAAE,WAA4B;QACtD,OAAO,CAAC,cAAc,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QACrF,OAAO,C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