@nu-art/ts-common 0.401.0 → 0.401.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/consts/consts.d.ts +1 -0
- package/consts/consts.js +1 -0
- package/core/application.d.ts +28 -0
- package/core/application.js +28 -0
- package/core/dispatcher.d.ts +140 -1
- package/core/dispatcher.js +128 -1
- package/core/error-handling.d.ts +49 -0
- package/core/error-handling.js +19 -0
- package/core/exceptions/exceptions.d.ts +111 -12
- package/core/exceptions/exceptions.js +112 -13
- package/core/exceptions/http-codes.d.ts +15 -0
- package/core/exceptions/http-codes.js +24 -0
- package/core/exceptions/types.d.ts +23 -0
- package/core/logger/index.d.ts +1 -0
- package/core/logger/index.js +1 -0
- package/core/module-manager.d.ts +127 -2
- package/core/module-manager.js +137 -3
- package/core/module.d.ts +170 -2
- package/core/module.js +171 -1
- package/db/consts.d.ts +9 -0
- package/db/consts.js +9 -0
- package/db/dependencies-test.d.ts +20 -0
- package/db/dependencies-test.js +7 -0
- package/db/types.d.ts +73 -0
- package/esm.d.ts +15 -0
- package/esm.js +15 -0
- package/index.d.ts +1 -14
- package/index.js +1 -14
- package/mem-storage/MemStorage.d.ts +156 -0
- package/mem-storage/MemStorage.js +158 -0
- package/modules/CSVModule.d.ts +27 -0
- package/modules/CSVModule.js +12 -0
- package/modules/CSVModuleV3.d.ts +70 -0
- package/modules/CSVModuleV3.js +70 -0
- package/modules/JSONCSVModule.d.ts +36 -0
- package/modules/JSONCSVModule.js +31 -0
- package/modules/csv-serializer.d.ts +29 -0
- package/modules/csv-serializer.js +17 -0
- package/package.json +25 -8
- package/testing/index.d.ts +1 -0
- package/testing/index.js +1 -0
- package/testing/workspace-creator.d.ts +71 -1
- package/testing/workspace-creator.js +55 -1
- package/testing.d.ts +2 -2
- package/testing.js +2 -2
- package/tools/Replacer.d.ts +34 -1
- package/tools/Replacer.js +34 -1
- package/utils/FileSystemUtils.d.ts +142 -1
- package/utils/FileSystemUtils.js +159 -7
- package/utils/array-tools.d.ts +130 -17
- package/utils/array-tools.js +130 -17
- package/utils/conflict-tools.d.ts +11 -0
- package/utils/conflict-tools.js +11 -0
- package/utils/crypto-tools.d.ts +50 -1
- package/utils/crypto-tools.js +52 -3
- package/utils/date-time-tools.d.ts +175 -12
- package/utils/date-time-tools.js +189 -12
- package/utils/db-object-tools.d.ts +63 -1
- package/utils/db-object-tools.js +63 -1
- package/utils/debounce.d.ts +84 -8
- package/utils/debounce.js +71 -8
- package/utils/exception-tools.d.ts +9 -0
- package/utils/exception-tools.js +12 -0
- package/utils/filter-tools.d.ts +45 -19
- package/utils/filter-tools.js +46 -19
- package/utils/hash-tools.d.ts +61 -0
- package/utils/hash-tools.js +61 -0
- package/utils/json-tools.d.ts +17 -0
- package/utils/json-tools.js +17 -0
- package/utils/merge-tools.d.ts +61 -0
- package/utils/merge-tools.js +55 -0
- package/utils/mimetype-tools.d.ts +19 -0
- package/utils/mimetype-tools.js +19 -0
- package/utils/number-tools.d.ts +47 -0
- package/utils/number-tools.js +47 -0
- package/utils/object-tools.d.ts +94 -1
- package/utils/object-tools.js +94 -1
- package/utils/promise-tools.d.ts +23 -5
- package/utils/promise-tools.js +20 -5
- package/utils/query-params.d.ts +34 -0
- package/utils/query-params.js +28 -0
- package/utils/queue-v2.d.ts +114 -1
- package/utils/queue-v2.js +108 -1
- package/utils/queue.d.ts +72 -1
- package/utils/queue.js +72 -1
- package/utils/random-tools.d.ts +25 -1
- package/utils/random-tools.js +25 -1
- package/utils/storage-capacity-tools.d.ts +7 -0
- package/utils/storage-capacity-tools.js +7 -0
- package/utils/string-tools.d.ts +143 -0
- package/utils/string-tools.js +144 -1
- package/utils/time-proxy.d.ts +50 -0
- package/utils/time-proxy.js +51 -0
- package/utils/tools.d.ts +104 -0
- package/utils/tools.js +99 -0
- package/utils/types.d.ts +297 -4
- package/utils/types.js +5 -0
- package/utils/ui-tools.d.ts +62 -22
- package/utils/ui-tools.js +53 -22
- package/utils/url-tools.d.ts +13 -2
- package/utils/url-tools.js +13 -2
- package/utils/version-tools.d.ts +28 -7
- package/utils/version-tools.js +28 -7
- package/validator/type-validators.d.ts +85 -0
- package/validator/type-validators.js +86 -1
- package/validator/validator-core.d.ts +74 -5
- package/validator/validator-core.js +52 -0
- package/validator/validators.d.ts +81 -0
- package/validator/validators.js +81 -0
- package/core/debug-flags.d.ts +0 -26
- package/core/debug-flags.js +0 -79
- package/core/logger/BeLogged.d.ts +0 -15
- package/core/logger/BeLogged.js +0 -54
- package/core/logger/LogClient.d.ts +0 -17
- package/core/logger/LogClient.js +0 -62
- package/core/logger/LogClient_BaseRotate.d.ts +0 -20
- package/core/logger/LogClient_BaseRotate.js +0 -59
- package/core/logger/LogClient_Browser.d.ts +0 -9
- package/core/logger/LogClient_Browser.js +0 -75
- package/core/logger/LogClient_BrowserGroups.d.ts +0 -23
- package/core/logger/LogClient_BrowserGroups.js +0 -86
- package/core/logger/LogClient_ConsoleProxy.d.ts +0 -23
- package/core/logger/LogClient_ConsoleProxy.js +0 -88
- package/core/logger/LogClient_File.d.ts +0 -11
- package/core/logger/LogClient_File.js +0 -55
- package/core/logger/LogClient_Function.d.ts +0 -8
- package/core/logger/LogClient_Function.js +0 -52
- package/core/logger/LogClient_MemBuffer.d.ts +0 -17
- package/core/logger/LogClient_MemBuffer.js +0 -81
- package/core/logger/LogClient_Terminal.d.ts +0 -11
- package/core/logger/LogClient_Terminal.js +0 -55
- package/core/logger/Logger.d.ts +0 -38
- package/core/logger/Logger.js +0 -129
- package/core/logger/types.d.ts +0 -10
- package/core/logger/types.js +0 -32
- package/core/logger/utils.d.ts +0 -5
- package/core/logger/utils.js +0 -71
- package/modules/CliParamsModule.d.ts +0 -27
- package/modules/CliParamsModule.js +0 -66
- package/replacer-v2/ReplacerV2.d.ts +0 -9
- package/replacer-v2/ReplacerV2.js +0 -16
- package/testing/consts.d.ts +0 -14
- package/testing/consts.js +0 -87
- package/testing/test-template.test.d.ts +0 -1
- package/testing/test-template.test.js +0 -41
- package/testing/types.d.ts +0 -30
- package/testing/types.js +0 -18
- package/tools/get-log-style.d.ts +0 -14
- package/tools/get-log-style.js +0 -34
package/consts/consts.d.ts
CHANGED
package/consts/consts.js
CHANGED
package/core/application.d.ts
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
import { ModuleManager } from "./module-manager.js";
|
|
2
|
+
/**
|
|
3
|
+
* Application class that extends ModuleManager with startup callback support.
|
|
4
|
+
*
|
|
5
|
+
* Provides a convenient way to execute code after all modules have been initialized.
|
|
6
|
+
* The `onStarted` callback is executed asynchronously and errors are caught and logged
|
|
7
|
+
* but don't prevent the application from continuing.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const app = new Application();
|
|
12
|
+
* app.addModulePack([ModuleA, ModuleB]);
|
|
13
|
+
* app.setConfig({ ... });
|
|
14
|
+
* app.build(async () => {
|
|
15
|
+
* // This runs after all modules are initialized
|
|
16
|
+
* await startServer();
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
2
20
|
export declare class Application extends ModuleManager {
|
|
3
21
|
constructor();
|
|
22
|
+
/**
|
|
23
|
+
* Initializes all modules and optionally executes a startup callback.
|
|
24
|
+
*
|
|
25
|
+
* The callback is executed asynchronously (fire-and-forget). If the callback
|
|
26
|
+
* returns data, it will be logged. Errors in the callback are caught and logged
|
|
27
|
+
* but don't affect the application state.
|
|
28
|
+
*
|
|
29
|
+
* @param onStarted - Optional async callback to execute after initialization.
|
|
30
|
+
* The callback's return value (if any) will be logged.
|
|
31
|
+
*/
|
|
4
32
|
build(onStarted?: () => Promise<any>): void;
|
|
5
33
|
}
|
package/core/application.js
CHANGED
|
@@ -16,10 +16,38 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
import { ModuleManager } from "./module-manager.js";
|
|
19
|
+
/**
|
|
20
|
+
* Application class that extends ModuleManager with startup callback support.
|
|
21
|
+
*
|
|
22
|
+
* Provides a convenient way to execute code after all modules have been initialized.
|
|
23
|
+
* The `onStarted` callback is executed asynchronously and errors are caught and logged
|
|
24
|
+
* but don't prevent the application from continuing.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const app = new Application();
|
|
29
|
+
* app.addModulePack([ModuleA, ModuleB]);
|
|
30
|
+
* app.setConfig({ ... });
|
|
31
|
+
* app.build(async () => {
|
|
32
|
+
* // This runs after all modules are initialized
|
|
33
|
+
* await startServer();
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
19
37
|
export class Application extends ModuleManager {
|
|
20
38
|
constructor() {
|
|
21
39
|
super();
|
|
22
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Initializes all modules and optionally executes a startup callback.
|
|
43
|
+
*
|
|
44
|
+
* The callback is executed asynchronously (fire-and-forget). If the callback
|
|
45
|
+
* returns data, it will be logged. Errors in the callback are caught and logged
|
|
46
|
+
* but don't affect the application state.
|
|
47
|
+
*
|
|
48
|
+
* @param onStarted - Optional async callback to execute after initialization.
|
|
49
|
+
* The callback's return value (if any) will be logged.
|
|
50
|
+
*/
|
|
23
51
|
build(onStarted) {
|
|
24
52
|
super.build();
|
|
25
53
|
onStarted && onStarted()
|
package/core/dispatcher.d.ts
CHANGED
|
@@ -1,20 +1,159 @@
|
|
|
1
1
|
import { FunctionKeys, ReturnPromiseType } from '../utils/types.js';
|
|
2
|
-
import { Logger } from './logger/
|
|
2
|
+
import { Logger } from './logger/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Type helper that extracts parameter types from a method on type T.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type containing the method
|
|
7
|
+
* @template K - The method key
|
|
8
|
+
*/
|
|
3
9
|
export type ParamResolver<T, K extends keyof T> = T[K] extends (...args: any) => any ? Parameters<T[K]> : never;
|
|
10
|
+
/**
|
|
11
|
+
* Type helper that extracts return type from a method on type T, unwrapping Promises.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The type containing the method
|
|
14
|
+
* @template K - The method key
|
|
15
|
+
*/
|
|
4
16
|
export type ReturnTypeResolver<T, K extends keyof T> = T[K] extends (...args: any) => any ? ReturnPromiseType<T[K]> : never;
|
|
17
|
+
/**
|
|
18
|
+
* Base processor class that filters and processes modules based on method presence.
|
|
19
|
+
*
|
|
20
|
+
* Processor finds all modules that have a specific method and allows processing
|
|
21
|
+
* them in various ways (sync, async parallel, async serial).
|
|
22
|
+
*
|
|
23
|
+
* The `modulesResolver` must be set by ModuleManager during initialization to
|
|
24
|
+
* provide access to all registered modules.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The interface/type that modules should implement
|
|
27
|
+
* @template K - The method name to look for on modules
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* interface EventHandler {
|
|
32
|
+
* onEvent(data: string): void;
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* const processor = new Processor<EventHandler, 'onEvent'>('onEvent');
|
|
36
|
+
* processor.processModules(module => {
|
|
37
|
+
* module.onEvent('data');
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
5
41
|
export declare class Processor<T, K extends FunctionKeys<T>> extends Logger {
|
|
42
|
+
/** Function that returns all registered modules (set by ModuleManager) */
|
|
6
43
|
static modulesResolver: () => any[];
|
|
44
|
+
/** The method name this processor is looking for */
|
|
7
45
|
readonly method: K;
|
|
46
|
+
/** Filter function that checks if a module has the target method */
|
|
8
47
|
protected readonly filter: (listener: any) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new Processor for a specific method.
|
|
50
|
+
*
|
|
51
|
+
* @param method - The method name to look for on modules
|
|
52
|
+
*/
|
|
9
53
|
constructor(method: K);
|
|
54
|
+
/**
|
|
55
|
+
* Processes all matching modules synchronously.
|
|
56
|
+
*
|
|
57
|
+
* Filters modules that have the target method and applies the processor
|
|
58
|
+
* function to each, collecting results in an array.
|
|
59
|
+
*
|
|
60
|
+
* @param processor - Function to apply to each matching module
|
|
61
|
+
* @returns Array of results from processing each module
|
|
62
|
+
*/
|
|
10
63
|
processModules<R>(processor: (item: T) => R): R[];
|
|
64
|
+
/**
|
|
65
|
+
* Processes all matching modules asynchronously in parallel.
|
|
66
|
+
*
|
|
67
|
+
* All modules are processed concurrently using Promise.all. Use this
|
|
68
|
+
* when modules can be processed independently and order doesn't matter.
|
|
69
|
+
*
|
|
70
|
+
* @param processor - Async function to apply to each matching module
|
|
71
|
+
* @returns Promise that resolves to an array of results
|
|
72
|
+
*/
|
|
11
73
|
processModulesAsync<R>(processor: (item: T) => Promise<R>): Promise<R[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Processes all matching modules asynchronously in serial (one after another).
|
|
76
|
+
*
|
|
77
|
+
* Modules are processed sequentially, waiting for each to complete before
|
|
78
|
+
* starting the next. Use this when order matters or when operations must
|
|
79
|
+
* not overlap.
|
|
80
|
+
*
|
|
81
|
+
* @param processor - Async function to apply to each matching module
|
|
82
|
+
* @returns Promise that resolves to an array of results in processing order
|
|
83
|
+
*/
|
|
12
84
|
processModulesAsyncSerial<R>(processor: (item: T) => Promise<R>): Promise<R[]>;
|
|
85
|
+
/**
|
|
86
|
+
* Filters all modules to those that have the target method.
|
|
87
|
+
*
|
|
88
|
+
* @returns Array of modules that implement the target method
|
|
89
|
+
*/
|
|
13
90
|
filterModules(): any[];
|
|
14
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Event dispatcher that calls a specific method on all modules that implement it.
|
|
94
|
+
*
|
|
95
|
+
* Dispatcher extends Processor to provide a convenient way to invoke a method
|
|
96
|
+
* on all matching modules with proper type safety. It automatically:
|
|
97
|
+
* - Finds all modules that have the target method
|
|
98
|
+
* - Calls the method with the provided parameters
|
|
99
|
+
* - Collects and returns the results
|
|
100
|
+
*
|
|
101
|
+
* Supports three dispatch modes:
|
|
102
|
+
* - **Synchronous**: Calls all modules immediately, returns array of results
|
|
103
|
+
* - **Async Parallel**: Calls all modules concurrently, returns Promise of results
|
|
104
|
+
* - **Async Serial**: Calls modules one at a time, returns Promise of results in order
|
|
105
|
+
*
|
|
106
|
+
* @template T - The interface that modules should implement
|
|
107
|
+
* @template K - The method name to dispatch
|
|
108
|
+
* @template P - Parameter types for the method (inferred from T[K])
|
|
109
|
+
* @template R - Return type for the method (inferred from T[K], unwrapped from Promise)
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* interface ConfigValidator {
|
|
114
|
+
* validateConfig(config: any): boolean;
|
|
115
|
+
* }
|
|
116
|
+
*
|
|
117
|
+
* const dispatcher = new Dispatcher<ConfigValidator, 'validateConfig'>('validateConfig');
|
|
118
|
+
* const results = dispatcher.dispatchModule(myConfig);
|
|
119
|
+
* // results is boolean[]
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
15
122
|
export declare class Dispatcher<T, K extends FunctionKeys<T>, P extends ParamResolver<T, K> = ParamResolver<T, K>, R extends ReturnTypeResolver<T, K> = ReturnTypeResolver<T, K>> extends Processor<T, K> {
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new Dispatcher for a specific method.
|
|
125
|
+
*
|
|
126
|
+
* @param method - The method name to dispatch to modules
|
|
127
|
+
*/
|
|
16
128
|
constructor(method: K);
|
|
129
|
+
/**
|
|
130
|
+
* Dispatches the method call to all matching modules synchronously.
|
|
131
|
+
*
|
|
132
|
+
* Calls the target method on each module that implements it, passing
|
|
133
|
+
* the provided parameters. Returns an array of all return values.
|
|
134
|
+
*
|
|
135
|
+
* @param p - Parameters to pass to the method (spread arguments)
|
|
136
|
+
* @returns Array of return values from each module
|
|
137
|
+
*/
|
|
17
138
|
dispatchModule(...p: P): R[];
|
|
139
|
+
/**
|
|
140
|
+
* Dispatches the method call to all matching modules asynchronously in parallel.
|
|
141
|
+
*
|
|
142
|
+
* All modules are called concurrently. Use this when modules can process
|
|
143
|
+
* independently and order doesn't matter.
|
|
144
|
+
*
|
|
145
|
+
* @param p - Parameters to pass to the method (spread arguments)
|
|
146
|
+
* @returns Promise that resolves to an array of return values
|
|
147
|
+
*/
|
|
18
148
|
dispatchModuleAsync(...p: P): Promise<R[]>;
|
|
149
|
+
/**
|
|
150
|
+
* Dispatches the method call to all matching modules asynchronously in serial.
|
|
151
|
+
*
|
|
152
|
+
* Modules are called one at a time, waiting for each to complete before
|
|
153
|
+
* calling the next. Use this when order matters or operations must not overlap.
|
|
154
|
+
*
|
|
155
|
+
* @param p - Parameters to pass to the method (spread arguments)
|
|
156
|
+
* @returns Promise that resolves to an array of return values in call order
|
|
157
|
+
*/
|
|
19
158
|
dispatchModuleAsyncSerial(...p: P): Promise<R[]>;
|
|
20
159
|
}
|
package/core/dispatcher.js
CHANGED
|
@@ -15,22 +15,82 @@
|
|
|
15
15
|
* See the License for the specific language governing permissions and
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
|
-
import { Logger } from './logger/
|
|
18
|
+
import { Logger } from './logger/index.js';
|
|
19
|
+
/**
|
|
20
|
+
* Base processor class that filters and processes modules based on method presence.
|
|
21
|
+
*
|
|
22
|
+
* Processor finds all modules that have a specific method and allows processing
|
|
23
|
+
* them in various ways (sync, async parallel, async serial).
|
|
24
|
+
*
|
|
25
|
+
* The `modulesResolver` must be set by ModuleManager during initialization to
|
|
26
|
+
* provide access to all registered modules.
|
|
27
|
+
*
|
|
28
|
+
* @template T - The interface/type that modules should implement
|
|
29
|
+
* @template K - The method name to look for on modules
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* interface EventHandler {
|
|
34
|
+
* onEvent(data: string): void;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const processor = new Processor<EventHandler, 'onEvent'>('onEvent');
|
|
38
|
+
* processor.processModules(module => {
|
|
39
|
+
* module.onEvent('data');
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
19
43
|
export class Processor extends Logger {
|
|
44
|
+
/** Function that returns all registered modules (set by ModuleManager) */
|
|
20
45
|
static modulesResolver;
|
|
46
|
+
/** The method name this processor is looking for */
|
|
21
47
|
method;
|
|
48
|
+
/** Filter function that checks if a module has the target method */
|
|
22
49
|
filter;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new Processor for a specific method.
|
|
52
|
+
*
|
|
53
|
+
* @param method - The method name to look for on modules
|
|
54
|
+
*/
|
|
23
55
|
constructor(method) {
|
|
24
56
|
super(method);
|
|
25
57
|
this.method = method;
|
|
26
58
|
this.filter = (listener) => !!listener[this.method];
|
|
27
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Processes all matching modules synchronously.
|
|
62
|
+
*
|
|
63
|
+
* Filters modules that have the target method and applies the processor
|
|
64
|
+
* function to each, collecting results in an array.
|
|
65
|
+
*
|
|
66
|
+
* @param processor - Function to apply to each matching module
|
|
67
|
+
* @returns Array of results from processing each module
|
|
68
|
+
*/
|
|
28
69
|
processModules(processor) {
|
|
29
70
|
return this.filterModules().filter(this.filter).map(processor);
|
|
30
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Processes all matching modules asynchronously in parallel.
|
|
74
|
+
*
|
|
75
|
+
* All modules are processed concurrently using Promise.all. Use this
|
|
76
|
+
* when modules can be processed independently and order doesn't matter.
|
|
77
|
+
*
|
|
78
|
+
* @param processor - Async function to apply to each matching module
|
|
79
|
+
* @returns Promise that resolves to an array of results
|
|
80
|
+
*/
|
|
31
81
|
async processModulesAsync(processor) {
|
|
32
82
|
return Promise.all(this.filterModules().map(processor));
|
|
33
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Processes all matching modules asynchronously in serial (one after another).
|
|
86
|
+
*
|
|
87
|
+
* Modules are processed sequentially, waiting for each to complete before
|
|
88
|
+
* starting the next. Use this when order matters or when operations must
|
|
89
|
+
* not overlap.
|
|
90
|
+
*
|
|
91
|
+
* @param processor - Async function to apply to each matching module
|
|
92
|
+
* @returns Promise that resolves to an array of results in processing order
|
|
93
|
+
*/
|
|
34
94
|
async processModulesAsyncSerial(processor) {
|
|
35
95
|
const modules = this.filterModules();
|
|
36
96
|
const toRet = [];
|
|
@@ -39,21 +99,79 @@ export class Processor extends Logger {
|
|
|
39
99
|
}
|
|
40
100
|
return toRet;
|
|
41
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Filters all modules to those that have the target method.
|
|
104
|
+
*
|
|
105
|
+
* @returns Array of modules that implement the target method
|
|
106
|
+
*/
|
|
42
107
|
filterModules() {
|
|
43
108
|
const listeners = Dispatcher.modulesResolver();
|
|
44
109
|
return listeners.filter(this.filter);
|
|
45
110
|
}
|
|
46
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Event dispatcher that calls a specific method on all modules that implement it.
|
|
114
|
+
*
|
|
115
|
+
* Dispatcher extends Processor to provide a convenient way to invoke a method
|
|
116
|
+
* on all matching modules with proper type safety. It automatically:
|
|
117
|
+
* - Finds all modules that have the target method
|
|
118
|
+
* - Calls the method with the provided parameters
|
|
119
|
+
* - Collects and returns the results
|
|
120
|
+
*
|
|
121
|
+
* Supports three dispatch modes:
|
|
122
|
+
* - **Synchronous**: Calls all modules immediately, returns array of results
|
|
123
|
+
* - **Async Parallel**: Calls all modules concurrently, returns Promise of results
|
|
124
|
+
* - **Async Serial**: Calls modules one at a time, returns Promise of results in order
|
|
125
|
+
*
|
|
126
|
+
* @template T - The interface that modules should implement
|
|
127
|
+
* @template K - The method name to dispatch
|
|
128
|
+
* @template P - Parameter types for the method (inferred from T[K])
|
|
129
|
+
* @template R - Return type for the method (inferred from T[K], unwrapped from Promise)
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* interface ConfigValidator {
|
|
134
|
+
* validateConfig(config: any): boolean;
|
|
135
|
+
* }
|
|
136
|
+
*
|
|
137
|
+
* const dispatcher = new Dispatcher<ConfigValidator, 'validateConfig'>('validateConfig');
|
|
138
|
+
* const results = dispatcher.dispatchModule(myConfig);
|
|
139
|
+
* // results is boolean[]
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
47
142
|
export class Dispatcher extends Processor {
|
|
143
|
+
/**
|
|
144
|
+
* Creates a new Dispatcher for a specific method.
|
|
145
|
+
*
|
|
146
|
+
* @param method - The method name to dispatch to modules
|
|
147
|
+
*/
|
|
48
148
|
constructor(method) {
|
|
49
149
|
super(method);
|
|
50
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Dispatches the method call to all matching modules synchronously.
|
|
153
|
+
*
|
|
154
|
+
* Calls the target method on each module that implements it, passing
|
|
155
|
+
* the provided parameters. Returns an array of all return values.
|
|
156
|
+
*
|
|
157
|
+
* @param p - Parameters to pass to the method (spread arguments)
|
|
158
|
+
* @returns Array of return values from each module
|
|
159
|
+
*/
|
|
51
160
|
dispatchModule(...p) {
|
|
52
161
|
return this.processModules((listener) => {
|
|
53
162
|
// @ts-ignore
|
|
54
163
|
return (listener[this.method])(...p);
|
|
55
164
|
});
|
|
56
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Dispatches the method call to all matching modules asynchronously in parallel.
|
|
168
|
+
*
|
|
169
|
+
* All modules are called concurrently. Use this when modules can process
|
|
170
|
+
* independently and order doesn't matter.
|
|
171
|
+
*
|
|
172
|
+
* @param p - Parameters to pass to the method (spread arguments)
|
|
173
|
+
* @returns Promise that resolves to an array of return values
|
|
174
|
+
*/
|
|
57
175
|
async dispatchModuleAsync(...p) {
|
|
58
176
|
return this.processModulesAsync((listener) => {
|
|
59
177
|
// const newVar = this.resolveListenerName(listener);
|
|
@@ -66,6 +184,15 @@ export class Dispatcher extends Processor {
|
|
|
66
184
|
// return 'name' in listener ? listener.name :
|
|
67
185
|
// 'constructor' in listener ? listener['constructor']['name'] : '';
|
|
68
186
|
// }
|
|
187
|
+
/**
|
|
188
|
+
* Dispatches the method call to all matching modules asynchronously in serial.
|
|
189
|
+
*
|
|
190
|
+
* Modules are called one at a time, waiting for each to complete before
|
|
191
|
+
* calling the next. Use this when order matters or operations must not overlap.
|
|
192
|
+
*
|
|
193
|
+
* @param p - Parameters to pass to the method (spread arguments)
|
|
194
|
+
* @returns Promise that resolves to an array of return values in call order
|
|
195
|
+
*/
|
|
69
196
|
async dispatchModuleAsyncSerial(...p) {
|
|
70
197
|
return this.processModulesAsyncSerial((listener) => {
|
|
71
198
|
// @ts-ignore
|
package/core/error-handling.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { Module } from './module.js';
|
|
2
2
|
import { Dispatcher } from './dispatcher.js';
|
|
3
3
|
import { CustomException } from './exceptions/exceptions.js';
|
|
4
|
+
/**
|
|
5
|
+
* Severity levels for server errors, ordered from least to most severe.
|
|
6
|
+
*/
|
|
4
7
|
export declare enum ServerErrorSeverity {
|
|
5
8
|
Debug = "Debug",
|
|
6
9
|
Info = "Info",
|
|
@@ -8,16 +11,62 @@ export declare enum ServerErrorSeverity {
|
|
|
8
11
|
Error = "Error",
|
|
9
12
|
Critical = "Critical"
|
|
10
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Array of error severities in ordinal order (least to most severe).
|
|
16
|
+
* Useful for comparisons and sorting.
|
|
17
|
+
*/
|
|
11
18
|
export declare const ServerErrorSeverity_Ordinal: ServerErrorSeverity[];
|
|
19
|
+
/**
|
|
20
|
+
* Structure for error messages that may contain nested messages.
|
|
21
|
+
*/
|
|
12
22
|
export type ErrorMessage = {
|
|
23
|
+
/** Primary error message */
|
|
13
24
|
message: string;
|
|
25
|
+
/** Optional array of nested/inner error messages */
|
|
14
26
|
innerMessages?: string[];
|
|
15
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Interface for modules that handle application notifications.
|
|
30
|
+
*
|
|
31
|
+
* Modules implementing this interface will receive notifications about
|
|
32
|
+
* application-level events (errors, warnings, etc.) via the dispatcher.
|
|
33
|
+
*/
|
|
16
34
|
export interface OnApplicationNotification {
|
|
35
|
+
/**
|
|
36
|
+
* Processes an application notification.
|
|
37
|
+
*
|
|
38
|
+
* @param errorLevel - Severity level of the notification
|
|
39
|
+
* @param module - The module that generated the notification
|
|
40
|
+
* @param message - Error message structure
|
|
41
|
+
*/
|
|
17
42
|
__processApplicationNotification(errorLevel: ServerErrorSeverity, module: Module, message: ErrorMessage): Promise<void>;
|
|
18
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Dispatcher for application notifications.
|
|
46
|
+
*
|
|
47
|
+
* Use this to notify all modules that implement `OnApplicationNotification`
|
|
48
|
+
* about application-level events.
|
|
49
|
+
*/
|
|
19
50
|
export declare const dispatch_onApplicationNotification: Dispatcher<OnApplicationNotification, "__processApplicationNotification", [errorLevel: ServerErrorSeverity, module: Module<any, any, import("../index.js").Validator<any> | import("../index.js").TypeValidator<any>>, message: ErrorMessage], void>;
|
|
51
|
+
/**
|
|
52
|
+
* Interface for modules that handle application exceptions.
|
|
53
|
+
*
|
|
54
|
+
* Modules implementing this interface will receive exceptions that occur
|
|
55
|
+
* at the application level via the dispatcher.
|
|
56
|
+
*/
|
|
20
57
|
export interface OnApplicationException {
|
|
58
|
+
/**
|
|
59
|
+
* Processes an application exception.
|
|
60
|
+
*
|
|
61
|
+
* @param e - The exception that occurred
|
|
62
|
+
* @param module - The module where the exception occurred
|
|
63
|
+
*/
|
|
21
64
|
__processApplicationException(e: CustomException, module: Module): Promise<void>;
|
|
22
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Dispatcher for application exceptions.
|
|
68
|
+
*
|
|
69
|
+
* Use this to notify all modules that implement `OnApplicationException`
|
|
70
|
+
* about exceptions that occur at the application level.
|
|
71
|
+
*/
|
|
23
72
|
export declare const dispatch_onApplicationException: Dispatcher<OnApplicationException, "__processApplicationException", [e: CustomException, module: Module<any, any, import("../index.js").Validator<any> | import("../index.js").TypeValidator<any>>], void>;
|
package/core/error-handling.js
CHANGED
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
import { Dispatcher } from './dispatcher.js';
|
|
19
|
+
/**
|
|
20
|
+
* Severity levels for server errors, ordered from least to most severe.
|
|
21
|
+
*/
|
|
19
22
|
export var ServerErrorSeverity;
|
|
20
23
|
(function (ServerErrorSeverity) {
|
|
21
24
|
ServerErrorSeverity["Debug"] = "Debug";
|
|
@@ -24,6 +27,10 @@ export var ServerErrorSeverity;
|
|
|
24
27
|
ServerErrorSeverity["Error"] = "Error";
|
|
25
28
|
ServerErrorSeverity["Critical"] = "Critical";
|
|
26
29
|
})(ServerErrorSeverity || (ServerErrorSeverity = {}));
|
|
30
|
+
/**
|
|
31
|
+
* Array of error severities in ordinal order (least to most severe).
|
|
32
|
+
* Useful for comparisons and sorting.
|
|
33
|
+
*/
|
|
27
34
|
export const ServerErrorSeverity_Ordinal = [
|
|
28
35
|
ServerErrorSeverity.Debug,
|
|
29
36
|
ServerErrorSeverity.Info,
|
|
@@ -31,5 +38,17 @@ export const ServerErrorSeverity_Ordinal = [
|
|
|
31
38
|
ServerErrorSeverity.Error,
|
|
32
39
|
ServerErrorSeverity.Critical
|
|
33
40
|
];
|
|
41
|
+
/**
|
|
42
|
+
* Dispatcher for application notifications.
|
|
43
|
+
*
|
|
44
|
+
* Use this to notify all modules that implement `OnApplicationNotification`
|
|
45
|
+
* about application-level events.
|
|
46
|
+
*/
|
|
34
47
|
export const dispatch_onApplicationNotification = new Dispatcher('__processApplicationNotification');
|
|
48
|
+
/**
|
|
49
|
+
* Dispatcher for application exceptions.
|
|
50
|
+
*
|
|
51
|
+
* Use this to notify all modules that implement `OnApplicationException`
|
|
52
|
+
* about exceptions that occur at the application level.
|
|
53
|
+
*/
|
|
35
54
|
export const dispatch_onApplicationException = new Dispatcher('__processApplicationException');
|