@bsb/base 9.0.0
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 +665 -0
- package/LICENSE.commercial +32 -0
- package/README.md +263 -0
- package/bsb-plugin.json +62 -0
- package/lib/base/BSBConfig.d.ts +130 -0
- package/lib/base/BSBConfig.js +95 -0
- package/lib/base/BSBConfig.js.map +1 -0
- package/lib/base/BSBEvents.d.ts +207 -0
- package/lib/base/BSBEvents.js +101 -0
- package/lib/base/BSBEvents.js.map +1 -0
- package/lib/base/BSBObservable.d.ts +178 -0
- package/lib/base/BSBObservable.js +91 -0
- package/lib/base/BSBObservable.js.map +1 -0
- package/lib/base/BSBService.d.ts +277 -0
- package/lib/base/BSBService.js +366 -0
- package/lib/base/BSBService.js.map +1 -0
- package/lib/base/BSBServiceClient.d.ts +135 -0
- package/lib/base/BSBServiceClient.js +130 -0
- package/lib/base/BSBServiceClient.js.map +1 -0
- package/lib/base/EventValidator.d.ts +137 -0
- package/lib/base/EventValidator.js +210 -0
- package/lib/base/EventValidator.js.map +1 -0
- package/lib/base/ObservableBackend.d.ts +281 -0
- package/lib/base/ObservableBackend.js +515 -0
- package/lib/base/ObservableBackend.js.map +1 -0
- package/lib/base/PluginConfig.d.ts +196 -0
- package/lib/base/PluginConfig.js +96 -0
- package/lib/base/PluginConfig.js.map +1 -0
- package/lib/base/PluginEvents.d.ts +140 -0
- package/lib/base/PluginEvents.js +268 -0
- package/lib/base/PluginEvents.js.map +1 -0
- package/lib/base/PluginObservable.d.ts +196 -0
- package/lib/base/PluginObservable.js +250 -0
- package/lib/base/PluginObservable.js.map +1 -0
- package/lib/base/ResourceContext.d.ts +70 -0
- package/lib/base/ResourceContext.js +54 -0
- package/lib/base/ResourceContext.js.map +1 -0
- package/lib/base/base.d.ts +264 -0
- package/lib/base/base.js +182 -0
- package/lib/base/base.js.map +1 -0
- package/lib/base/errorMessages.d.ts +56 -0
- package/lib/base/errorMessages.js +70 -0
- package/lib/base/errorMessages.js.map +1 -0
- package/lib/base/factory.d.ts +58 -0
- package/lib/base/factory.js +167 -0
- package/lib/base/factory.js.map +1 -0
- package/lib/base/functions.d.ts +117 -0
- package/lib/base/functions.js +152 -0
- package/lib/base/functions.js.map +1 -0
- package/lib/base/index.d.ts +44 -0
- package/lib/base/index.js +64 -0
- package/lib/base/index.js.map +1 -0
- package/lib/base/logFormatter.d.ts +50 -0
- package/lib/base/logFormatter.js +105 -0
- package/lib/base/logFormatter.js.map +1 -0
- package/lib/base/tools.d.ts +316 -0
- package/lib/base/tools.js +666 -0
- package/lib/base/tools.js.map +1 -0
- package/lib/cli.d.ts +28 -0
- package/lib/cli.js +254 -0
- package/lib/cli.js.map +1 -0
- package/lib/dev.d.ts +27 -0
- package/lib/dev.js +200 -0
- package/lib/dev.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.js +49 -0
- package/lib/index.js.map +1 -0
- package/lib/interfaces/events.d.ts +67 -0
- package/lib/interfaces/events.js +44 -0
- package/lib/interfaces/events.js.map +1 -0
- package/lib/interfaces/index.d.ts +38 -0
- package/lib/interfaces/index.js +59 -0
- package/lib/interfaces/index.js.map +1 -0
- package/lib/interfaces/logging.d.ts +106 -0
- package/lib/interfaces/logging.js +39 -0
- package/lib/interfaces/logging.js.map +1 -0
- package/lib/interfaces/metrics.d.ts +365 -0
- package/lib/interfaces/metrics.js +46 -0
- package/lib/interfaces/metrics.js.map +1 -0
- package/lib/interfaces/observable-types.d.ts +63 -0
- package/lib/interfaces/observable-types.js +49 -0
- package/lib/interfaces/observable-types.js.map +1 -0
- package/lib/interfaces/observable.d.ts +297 -0
- package/lib/interfaces/observable.js +29 -0
- package/lib/interfaces/observable.js.map +1 -0
- package/lib/interfaces/options.d.ts +164 -0
- package/lib/interfaces/options.js +56 -0
- package/lib/interfaces/options.js.map +1 -0
- package/lib/interfaces/plugins.d.ts +143 -0
- package/lib/interfaces/plugins.js +45 -0
- package/lib/interfaces/plugins.js.map +1 -0
- package/lib/interfaces/result.d.ts +129 -0
- package/lib/interfaces/result.js +162 -0
- package/lib/interfaces/result.js.map +1 -0
- package/lib/interfaces/schema-events.d.ts +378 -0
- package/lib/interfaces/schema-events.js +247 -0
- package/lib/interfaces/schema-events.js.map +1 -0
- package/lib/interfaces/schema-types.d.ts +407 -0
- package/lib/interfaces/schema-types.js +581 -0
- package/lib/interfaces/schema-types.js.map +1 -0
- package/lib/interfaces/service.d.ts +48 -0
- package/lib/interfaces/service.js +29 -0
- package/lib/interfaces/service.js.map +1 -0
- package/lib/interfaces/tools.d.ts +65 -0
- package/lib/interfaces/tools.js +50 -0
- package/lib/interfaces/tools.js.map +1 -0
- package/lib/plugins/config-default/index.d.ts +59 -0
- package/lib/plugins/config-default/index.js +197 -0
- package/lib/plugins/config-default/index.js.map +1 -0
- package/lib/plugins/config-default/interfaces.d.ts +92 -0
- package/lib/plugins/config-default/interfaces.js +36 -0
- package/lib/plugins/config-default/interfaces.js.map +1 -0
- package/lib/plugins/events-default/events/broadcast.d.ts +36 -0
- package/lib/plugins/events-default/events/broadcast.js +85 -0
- package/lib/plugins/events-default/events/broadcast.js.map +1 -0
- package/lib/plugins/events-default/events/emit.d.ts +38 -0
- package/lib/plugins/events-default/events/emit.js +104 -0
- package/lib/plugins/events-default/events/emit.js.map +1 -0
- package/lib/plugins/events-default/events/emitAndReturn.d.ts +36 -0
- package/lib/plugins/events-default/events/emitAndReturn.js +100 -0
- package/lib/plugins/events-default/events/emitAndReturn.js.map +1 -0
- package/lib/plugins/events-default/events/emitStreamAndReceiveStream.d.ts +38 -0
- package/lib/plugins/events-default/events/emitStreamAndReceiveStream.js +134 -0
- package/lib/plugins/events-default/events/emitStreamAndReceiveStream.js.map +1 -0
- package/lib/plugins/events-default/events/index.d.ts +30 -0
- package/lib/plugins/events-default/events/index.js +38 -0
- package/lib/plugins/events-default/events/index.js.map +1 -0
- package/lib/plugins/events-default/index.d.ts +57 -0
- package/lib/plugins/events-default/index.js +86 -0
- package/lib/plugins/events-default/index.js.map +1 -0
- package/lib/plugins/observable-default/index.d.ts +43 -0
- package/lib/plugins/observable-default/index.js +151 -0
- package/lib/plugins/observable-default/index.js.map +1 -0
- package/lib/schemas/config-default.json +34 -0
- package/lib/schemas/config-default.plugin.json +36 -0
- package/lib/schemas/events-default.json +18 -0
- package/lib/schemas/events-default.plugin.json +17 -0
- package/lib/schemas/observable-default.json +33 -0
- package/lib/schemas/observable-default.plugin.json +24 -0
- package/lib/scripts/bsb-client-cli.d.ts +21 -0
- package/lib/scripts/bsb-client-cli.js +701 -0
- package/lib/scripts/bsb-client-cli.js.map +1 -0
- package/lib/scripts/bsb-plugin-cli.d.ts +15 -0
- package/lib/scripts/bsb-plugin-cli.js +547 -0
- package/lib/scripts/bsb-plugin-cli.js.map +1 -0
- package/lib/scripts/export-schemas.d.ts +17 -0
- package/lib/scripts/export-schemas.js +205 -0
- package/lib/scripts/export-schemas.js.map +1 -0
- package/lib/scripts/extract-schemas-from-source.d.ts +23 -0
- package/lib/scripts/extract-schemas-from-source.js +604 -0
- package/lib/scripts/extract-schemas-from-source.js.map +1 -0
- package/lib/scripts/generate-client-types.d.ts +22 -0
- package/lib/scripts/generate-client-types.js +537 -0
- package/lib/scripts/generate-client-types.js.map +1 -0
- package/lib/scripts/generate-plugin-json.d.ts +17 -0
- package/lib/scripts/generate-plugin-json.js +219 -0
- package/lib/scripts/generate-plugin-json.js.map +1 -0
- package/lib/serviceBase/config.d.ts +83 -0
- package/lib/serviceBase/config.js +236 -0
- package/lib/serviceBase/config.js.map +1 -0
- package/lib/serviceBase/events.d.ts +91 -0
- package/lib/serviceBase/events.js +519 -0
- package/lib/serviceBase/events.js.map +1 -0
- package/lib/serviceBase/index.d.ts +33 -0
- package/lib/serviceBase/index.js +50 -0
- package/lib/serviceBase/index.js.map +1 -0
- package/lib/serviceBase/observable.d.ts +249 -0
- package/lib/serviceBase/observable.js +551 -0
- package/lib/serviceBase/observable.js.map +1 -0
- package/lib/serviceBase/plugins.d.ts +48 -0
- package/lib/serviceBase/plugins.js +184 -0
- package/lib/serviceBase/plugins.js.map +1 -0
- package/lib/serviceBase/serviceBase.d.ts +228 -0
- package/lib/serviceBase/serviceBase.js +420 -0
- package/lib/serviceBase/serviceBase.js.map +1 -0
- package/lib/serviceBase/services.d.ts +63 -0
- package/lib/serviceBase/services.js +346 -0
- package/lib/serviceBase/services.js.map +1 -0
- package/lib/tests.d.ts +27 -0
- package/lib/tests.js +44 -0
- package/lib/tests.js.map +1 -0
- package/package.json +91 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
3
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
7
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
* (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
11
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
12
|
+
* including the right to create derivative works that are not subject to the terms
|
|
13
|
+
* of the AGPL.
|
|
14
|
+
*
|
|
15
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
16
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
17
|
+
* will be provided upon request.
|
|
18
|
+
*
|
|
19
|
+
* This program is distributed in the hope that it will be useful,
|
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22
|
+
* GNU Affero General Public License for more details.
|
|
23
|
+
*
|
|
24
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
25
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
26
|
+
*/
|
|
27
|
+
import { BSBEventSchemas, Observable, ServiceClientEventSchemas } from "../interfaces";
|
|
28
|
+
import { BSBService, BSBServiceClientDefinition } from "./BSBService";
|
|
29
|
+
import { PluginEvents } from "./PluginEvents";
|
|
30
|
+
/**
|
|
31
|
+
* @hidden
|
|
32
|
+
* ONLY USE THIS IF YOU NEED SPECIFIC CLIENT LOGIC, OTHERWISE USE ServiceClient
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBServiceClient.html | API: BSBServiceClient}
|
|
36
|
+
*/
|
|
37
|
+
export declare abstract class BSBServiceClient<Service extends BSBService<any, any> = BSBService<any, BSBEventSchemas>> {
|
|
38
|
+
readonly events: PluginEvents<ServiceClientEventSchemas<BSBEventSchemas>>;
|
|
39
|
+
constructor(context: BSBService);
|
|
40
|
+
abstract readonly pluginName: string;
|
|
41
|
+
abstract readonly initBeforePlugins?: Array<string>;
|
|
42
|
+
abstract readonly initAfterPlugins?: Array<string>;
|
|
43
|
+
abstract readonly runBeforePlugins?: Array<string>;
|
|
44
|
+
abstract readonly runAfterPlugins?: Array<string>;
|
|
45
|
+
abstract dispose?(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Init
|
|
48
|
+
* Optional function to be called when the client is being initialized
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* **v9 BREAKING CHANGE**: Now requires Observable instead of DTrace.
|
|
52
|
+
*
|
|
53
|
+
* @param obs - Observable context with logging, metrics, and trace information
|
|
54
|
+
*/
|
|
55
|
+
abstract init?(obs: Observable): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Run
|
|
58
|
+
* Optional function to be called when the client is being run
|
|
59
|
+
*
|
|
60
|
+
* @remarks
|
|
61
|
+
* **v9 BREAKING CHANGE**: Now requires Observable instead of DTrace.
|
|
62
|
+
*
|
|
63
|
+
* @param obs - Observable context with logging, metrics, and trace information
|
|
64
|
+
*/
|
|
65
|
+
abstract run?(obs: Observable): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Instantiates a link to a Service Plugin.
|
|
69
|
+
*
|
|
70
|
+
* Create a new ServiceClient based on a plugin class/definition and then use the events from that plugin.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Example Service Plugin (just an example, not a real plugin):
|
|
75
|
+
*
|
|
76
|
+
* import { Plugin as Service1 } from "./myplugin";
|
|
77
|
+
*
|
|
78
|
+
* export class Plugin
|
|
79
|
+
* extends BSBService<null, Events> {
|
|
80
|
+
* private service1: ServiceClient<Service1>;
|
|
81
|
+
*
|
|
82
|
+
* constructor(config: BSBServiceConstructor) {
|
|
83
|
+
* super(config);
|
|
84
|
+
* this.service1 = new ServiceClient(Service1, this);
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* public async init(trace: DTrace) {
|
|
88
|
+
* this.events.onReturnableEvent("calculate", trace, async (iTrace: DTrace, a: number, b: number) => {
|
|
89
|
+
* this.log.info(iTrace, "Calculating {a} * {b}", { a, b });
|
|
90
|
+
* return a * b;
|
|
91
|
+
* });
|
|
92
|
+
* }
|
|
93
|
+
* }
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @group Services
|
|
97
|
+
* @category Plugins
|
|
98
|
+
*/
|
|
99
|
+
export declare class ServiceClient<Service extends BSBService<any, TEventSchemas>, TEventSchemas extends BSBEventSchemas = any, ServiceT extends {
|
|
100
|
+
PLUGIN_CLIENT: BSBServiceClientDefinition;
|
|
101
|
+
} = any> extends BSBServiceClient<Service> {
|
|
102
|
+
/**
|
|
103
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ServiceClient.html | API: ServiceClient}
|
|
104
|
+
*/
|
|
105
|
+
readonly pluginName: string;
|
|
106
|
+
readonly initBeforePlugins?: Array<string>;
|
|
107
|
+
readonly initAfterPlugins?: Array<string>;
|
|
108
|
+
readonly runBeforePlugins?: Array<string>;
|
|
109
|
+
readonly runAfterPlugins?: Array<string>;
|
|
110
|
+
dispose?(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Init (v9: Observable only)
|
|
113
|
+
*/
|
|
114
|
+
init?(obs: Observable): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Run (v9: Observable only)
|
|
117
|
+
*/
|
|
118
|
+
run?(obs: Observable): Promise<void>;
|
|
119
|
+
events: PluginEvents<ServiceClientEventSchemas<TEventSchemas>>;
|
|
120
|
+
constructor(service: ServiceT, context: BSBService);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @hidden
|
|
124
|
+
* DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS
|
|
125
|
+
*/
|
|
126
|
+
export declare class BSBServiceClientRef extends BSBServiceClient<any> {
|
|
127
|
+
pluginName: string;
|
|
128
|
+
initBeforePlugins?: string[] | undefined;
|
|
129
|
+
initAfterPlugins?: string[] | undefined;
|
|
130
|
+
runBeforePlugins?: string[] | undefined;
|
|
131
|
+
runAfterPlugins?: string[] | undefined;
|
|
132
|
+
dispose?(): void;
|
|
133
|
+
init?(obs: Observable): Promise<void>;
|
|
134
|
+
run?(obs: Observable): Promise<void>;
|
|
135
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
4
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
5
|
+
*
|
|
6
|
+
* This program is free software: you can redistribute it and/or modify
|
|
7
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
8
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
* (at your option) any later version.
|
|
10
|
+
*
|
|
11
|
+
* Alternatively, you may obtain a commercial license for this program.
|
|
12
|
+
* The commercial license allows you to use the Program in a closed-source manner,
|
|
13
|
+
* including the right to create derivative works that are not subject to the terms
|
|
14
|
+
* of the AGPL.
|
|
15
|
+
*
|
|
16
|
+
* To obtain a commercial license, please contact the copyright holders at
|
|
17
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
18
|
+
* will be provided upon request.
|
|
19
|
+
*
|
|
20
|
+
* This program is distributed in the hope that it will be useful,
|
|
21
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23
|
+
* GNU Affero General Public License for more details.
|
|
24
|
+
*
|
|
25
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
26
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.BSBServiceClientRef = exports.ServiceClient = exports.BSBServiceClient = void 0;
|
|
30
|
+
const interfaces_1 = require("../interfaces");
|
|
31
|
+
const errorMessages_1 = require("./errorMessages");
|
|
32
|
+
const tools_1 = require("./tools");
|
|
33
|
+
/**
|
|
34
|
+
* @hidden
|
|
35
|
+
*/
|
|
36
|
+
function internalTrace(span) {
|
|
37
|
+
return (0, interfaces_1.createFakeDTrace)("base/BSBServiceClient", span);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @hidden
|
|
41
|
+
* ONLY USE THIS IF YOU NEED SPECIFIC CLIENT LOGIC, OTHERWISE USE ServiceClient
|
|
42
|
+
*/
|
|
43
|
+
/**
|
|
44
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/BSBServiceClient.html | API: BSBServiceClient}
|
|
45
|
+
*/
|
|
46
|
+
class BSBServiceClient {
|
|
47
|
+
constructor(context) {
|
|
48
|
+
context._clients.push(this);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.BSBServiceClient = BSBServiceClient;
|
|
52
|
+
/**
|
|
53
|
+
* Instantiates a link to a Service Plugin.
|
|
54
|
+
*
|
|
55
|
+
* Create a new ServiceClient based on a plugin class/definition and then use the events from that plugin.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Example Service Plugin (just an example, not a real plugin):
|
|
60
|
+
*
|
|
61
|
+
* import { Plugin as Service1 } from "./myplugin";
|
|
62
|
+
*
|
|
63
|
+
* export class Plugin
|
|
64
|
+
* extends BSBService<null, Events> {
|
|
65
|
+
* private service1: ServiceClient<Service1>;
|
|
66
|
+
*
|
|
67
|
+
* constructor(config: BSBServiceConstructor) {
|
|
68
|
+
* super(config);
|
|
69
|
+
* this.service1 = new ServiceClient(Service1, this);
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* public async init(trace: DTrace) {
|
|
73
|
+
* this.events.onReturnableEvent("calculate", trace, async (iTrace: DTrace, a: number, b: number) => {
|
|
74
|
+
* this.log.info(iTrace, "Calculating {a} * {b}", { a, b });
|
|
75
|
+
* return a * b;
|
|
76
|
+
* });
|
|
77
|
+
* }
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @group Services
|
|
82
|
+
* @category Plugins
|
|
83
|
+
*/
|
|
84
|
+
class ServiceClient extends BSBServiceClient {
|
|
85
|
+
/**
|
|
86
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ServiceClient.html | API: ServiceClient}
|
|
87
|
+
*/
|
|
88
|
+
pluginName = "{UNSET SERVICE CLIENT PLUGIN NAME}";
|
|
89
|
+
initBeforePlugins;
|
|
90
|
+
initAfterPlugins;
|
|
91
|
+
runBeforePlugins;
|
|
92
|
+
runAfterPlugins;
|
|
93
|
+
constructor(service, context) {
|
|
94
|
+
super(context);
|
|
95
|
+
if (!tools_1.Tools.isObject(service.PLUGIN_CLIENT)) {
|
|
96
|
+
throw new errorMessages_1.BSBError(internalTrace("ServiceClient"), "Plugin client is not defined in the service!");
|
|
97
|
+
}
|
|
98
|
+
if (!tools_1.Tools.isString(service.PLUGIN_CLIENT.name)) {
|
|
99
|
+
throw new errorMessages_1.BSBError(internalTrace("ServiceClient"), "Plugin client name is not defined in the service!");
|
|
100
|
+
}
|
|
101
|
+
this.pluginName = service.PLUGIN_CLIENT.name;
|
|
102
|
+
this.initBeforePlugins = service.PLUGIN_CLIENT.initBeforePlugins;
|
|
103
|
+
this.initAfterPlugins = service.PLUGIN_CLIENT.initAfterPlugins;
|
|
104
|
+
this.runBeforePlugins = service.PLUGIN_CLIENT.runBeforePlugins;
|
|
105
|
+
this.runAfterPlugins = service.PLUGIN_CLIENT.runAfterPlugins;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.ServiceClient = ServiceClient;
|
|
109
|
+
/**
|
|
110
|
+
* @hidden
|
|
111
|
+
* DO NOT REFERENCE/USE THIS CLASS - IT IS AN INTERNALLY REFERENCED CLASS
|
|
112
|
+
*/
|
|
113
|
+
class BSBServiceClientRef extends BSBServiceClient {
|
|
114
|
+
pluginName = "";
|
|
115
|
+
initBeforePlugins;
|
|
116
|
+
initAfterPlugins;
|
|
117
|
+
runBeforePlugins;
|
|
118
|
+
runAfterPlugins;
|
|
119
|
+
dispose() {
|
|
120
|
+
throw new Error("Method not implemented.");
|
|
121
|
+
}
|
|
122
|
+
init(obs) {
|
|
123
|
+
throw new Error("Method not implemented.");
|
|
124
|
+
}
|
|
125
|
+
run(obs) {
|
|
126
|
+
throw new Error("Method not implemented.");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.BSBServiceClientRef = BSBServiceClientRef;
|
|
130
|
+
//# sourceMappingURL=BSBServiceClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BSBServiceClient.js","sourceRoot":"","sources":["../../src/base/BSBServiceClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAEH,8CAMuB;AAEvB,mDAA2C;AAE3C,mCAAgC;AAEhC;;GAEG;AACH,SAAS,aAAa,CAAC,IAAY;IACjC,OAAO,IAAA,6BAAgB,EAAC,uBAAuB,EAA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G;AACH,MAAa,mBACX,SAAQ,gBAAqB;IACtB,UAAU,GAAW,EAAE,CAAC;IACxB,iBAAiB,CAAwB;IACzC,gBAAgB,CAAwB;IACxC,gBAAgB,CAAwB;IACxC,eAAe,CAAwB;IAEvC,OAAO;QACZ,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IAEM,IAAI,CAAE,GAAe;QAC1B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IAEM,GAAG,CAAE,GAAe;QACzB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;CACF;AAnBD,kDAmBC"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
3
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
4
|
+
*
|
|
5
|
+
* This program is free software: you can redistribute it and/or modify
|
|
6
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
7
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
* (at your option) any later version.
|
|
9
|
+
*
|
|
10
|
+
* Alternatively, you may obtain a commercial license, please contact the copyright holders at
|
|
11
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
12
|
+
* will be provided upon request.
|
|
13
|
+
*
|
|
14
|
+
* This program is distributed in the hope that it will be useful,
|
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17
|
+
* GNU Affero General Public License for more details.
|
|
18
|
+
*
|
|
19
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
20
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
21
|
+
*/
|
|
22
|
+
import { z } from "zod";
|
|
23
|
+
import { IPluginLogging, DTrace, BSBType } from "../interfaces";
|
|
24
|
+
export interface ValidationResult<T = any> {
|
|
25
|
+
success: boolean;
|
|
26
|
+
data?: T;
|
|
27
|
+
error?: Error;
|
|
28
|
+
}
|
|
29
|
+
export interface EventValidationConfig {
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
logValidationErrors?: boolean;
|
|
32
|
+
throwOnValidationFailure?: boolean;
|
|
33
|
+
maxRetries?: number;
|
|
34
|
+
retryDelay?: number;
|
|
35
|
+
performanceThreshold?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Utility class for validating event data using Zod schemas.
|
|
39
|
+
* Provides performance monitoring and configurable validation behavior.
|
|
40
|
+
* @group Validation
|
|
41
|
+
* @category Tools
|
|
42
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html | API: EventValidator}
|
|
43
|
+
*/
|
|
44
|
+
export declare class EventValidator {
|
|
45
|
+
private config;
|
|
46
|
+
private logger?;
|
|
47
|
+
/**
|
|
48
|
+
* Create a new EventValidator instance.
|
|
49
|
+
* @param config - Validation configuration options
|
|
50
|
+
* @param logger - Optional logger for error reporting
|
|
51
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#constructor | API: EventValidator#constructor}
|
|
52
|
+
*/
|
|
53
|
+
constructor(config?: Partial<EventValidationConfig>, logger?: IPluginLogging);
|
|
54
|
+
/**
|
|
55
|
+
* Update validation configuration at runtime.
|
|
56
|
+
* @param config - New configuration options to merge
|
|
57
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#updateConfig | API: EventValidator#updateConfig}
|
|
58
|
+
*/
|
|
59
|
+
updateConfig(config: Partial<EventValidationConfig>): void;
|
|
60
|
+
/**
|
|
61
|
+
* Validate input data for an event.
|
|
62
|
+
* @param eventName - Name of the event being validated
|
|
63
|
+
* @param data - Data to validate
|
|
64
|
+
* @param schema - BSBType or Zod schema to validate against
|
|
65
|
+
* @param trace - Trace for logging context
|
|
66
|
+
* @param eventConfig - Override configuration for this validation
|
|
67
|
+
* @returns Validation result with parsed data or error
|
|
68
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#validateInput | API: EventValidator#validateInput}
|
|
69
|
+
*/
|
|
70
|
+
validateInput<T = unknown>(eventName: string, data: unknown, schema: BSBType | z.ZodSchema<T>, trace: DTrace, eventConfig?: Partial<EventValidationConfig>): ValidationResult<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Validate output/return data for an event.
|
|
73
|
+
* @param eventName - Name of the event being validated
|
|
74
|
+
* @param data - Data to validate
|
|
75
|
+
* @param schema - BSBType or Zod schema to validate against
|
|
76
|
+
* @param trace - Trace for logging context
|
|
77
|
+
* @param eventConfig - Override configuration for this validation
|
|
78
|
+
* @returns Validation result with parsed data or error
|
|
79
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#validateOutput | API: EventValidator#validateOutput}
|
|
80
|
+
*/
|
|
81
|
+
validateOutput<T = unknown>(eventName: string, data: unknown, schema: BSBType | z.ZodSchema<T>, trace: DTrace, eventConfig?: Partial<EventValidationConfig>): ValidationResult<T>;
|
|
82
|
+
/**
|
|
83
|
+
* Check if a schema is a BSBType or a Zod schema.
|
|
84
|
+
* @param schema - Schema to check
|
|
85
|
+
* @returns True if schema is a BSBType
|
|
86
|
+
* @private
|
|
87
|
+
*/
|
|
88
|
+
private isBSBType;
|
|
89
|
+
/**
|
|
90
|
+
* Validate event data using schemas from a validation schema map.
|
|
91
|
+
* @param eventName - Name of the event to validate
|
|
92
|
+
* @param data - Data to validate
|
|
93
|
+
* @param schemas - Map of event validation schemas
|
|
94
|
+
* @param trace - Trace for logging context
|
|
95
|
+
* @param direction - Whether validating 'input' or 'output' data
|
|
96
|
+
* @returns Validation result or null if no schema is defined
|
|
97
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#validateFromSchemas | API: EventValidator#validateFromSchemas}
|
|
98
|
+
*/
|
|
99
|
+
validateFromSchemas<T = unknown>(eventName: string, data: unknown, schemas: any, trace: DTrace, direction?: 'input' | 'output'): ValidationResult<T> | null;
|
|
100
|
+
/**
|
|
101
|
+
* Check if validation is enabled for the current configuration.
|
|
102
|
+
* @param eventConfig - Override configuration to check
|
|
103
|
+
* @returns True if validation is enabled
|
|
104
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#isValidationEnabled | API: EventValidator#isValidationEnabled}
|
|
105
|
+
*/
|
|
106
|
+
isValidationEnabled(eventConfig?: Partial<EventValidationConfig>): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Internal method to perform the actual validation with performance monitoring.
|
|
109
|
+
*/
|
|
110
|
+
private performValidation;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Custom error class for validation failures.
|
|
114
|
+
* @group Validation
|
|
115
|
+
* @category Errors
|
|
116
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ValidationError.html | API: ValidationError}
|
|
117
|
+
*/
|
|
118
|
+
export declare class ValidationError extends Error {
|
|
119
|
+
readonly zodError: z.ZodError<any>;
|
|
120
|
+
readonly eventName: string;
|
|
121
|
+
readonly direction: 'input' | 'output';
|
|
122
|
+
/**
|
|
123
|
+
* Create a new validation error.
|
|
124
|
+
* @param message - Error message
|
|
125
|
+
* @param zodError - The underlying Zod validation error
|
|
126
|
+
* @param eventName - Name of the event that failed validation
|
|
127
|
+
* @param direction - Whether the failure was on 'input' or 'output'
|
|
128
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ValidationError.html#constructor | API: ValidationError#constructor}
|
|
129
|
+
*/
|
|
130
|
+
constructor(message: string, zodError: z.ZodError<any>, eventName: string, direction: 'input' | 'output');
|
|
131
|
+
/**
|
|
132
|
+
* Get a formatted string representation of the validation errors.
|
|
133
|
+
* @returns Formatted error details
|
|
134
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ValidationError.html#getFormattedErrors | API: ValidationError#getFormattedErrors}
|
|
135
|
+
*/
|
|
136
|
+
getFormattedErrors(): string;
|
|
137
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* BSB (Better-Service-Base) is an event-bus based microservice framework.
|
|
4
|
+
* Copyright (C) 2016 - 2025 BetterCorp (PTY) Ltd
|
|
5
|
+
*
|
|
6
|
+
* This program is free software: you can redistribute it and/or modify
|
|
7
|
+
* it under the terms of the GNU Affero General Public License as published
|
|
8
|
+
* by the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
* (at your option) any later version.
|
|
10
|
+
*
|
|
11
|
+
* Alternatively, you may obtain a commercial license, please contact the copyright holders at
|
|
12
|
+
* https://www.bettercorp.dev. The terms and conditions of the commercial license
|
|
13
|
+
* will be provided upon request.
|
|
14
|
+
*
|
|
15
|
+
* This program is distributed in the hope that it will be useful,
|
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18
|
+
* GNU Affero General Public License for more details.
|
|
19
|
+
*
|
|
20
|
+
* You should have received a copy of the GNU Affero General Public License
|
|
21
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.ValidationError = exports.EventValidator = void 0;
|
|
25
|
+
const interfaces_1 = require("../interfaces");
|
|
26
|
+
const DEFAULT_VALIDATION_CONFIG = {
|
|
27
|
+
enabled: true,
|
|
28
|
+
logValidationErrors: true,
|
|
29
|
+
throwOnValidationFailure: true,
|
|
30
|
+
maxRetries: 0,
|
|
31
|
+
retryDelay: 0,
|
|
32
|
+
performanceThreshold: 100
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Utility class for validating event data using Zod schemas.
|
|
36
|
+
* Provides performance monitoring and configurable validation behavior.
|
|
37
|
+
* @group Validation
|
|
38
|
+
* @category Tools
|
|
39
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html | API: EventValidator}
|
|
40
|
+
*/
|
|
41
|
+
class EventValidator {
|
|
42
|
+
config;
|
|
43
|
+
logger;
|
|
44
|
+
/**
|
|
45
|
+
* Create a new EventValidator instance.
|
|
46
|
+
* @param config - Validation configuration options
|
|
47
|
+
* @param logger - Optional logger for error reporting
|
|
48
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#constructor | API: EventValidator#constructor}
|
|
49
|
+
*/
|
|
50
|
+
constructor(config = {}, logger) {
|
|
51
|
+
this.config = { ...DEFAULT_VALIDATION_CONFIG, ...config };
|
|
52
|
+
this.logger = logger;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Update validation configuration at runtime.
|
|
56
|
+
* @param config - New configuration options to merge
|
|
57
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#updateConfig | API: EventValidator#updateConfig}
|
|
58
|
+
*/
|
|
59
|
+
updateConfig(config) {
|
|
60
|
+
this.config = { ...this.config, ...config };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Validate input data for an event.
|
|
64
|
+
* @param eventName - Name of the event being validated
|
|
65
|
+
* @param data - Data to validate
|
|
66
|
+
* @param schema - BSBType or Zod schema to validate against
|
|
67
|
+
* @param trace - Trace for logging context
|
|
68
|
+
* @param eventConfig - Override configuration for this validation
|
|
69
|
+
* @returns Validation result with parsed data or error
|
|
70
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#validateInput | API: EventValidator#validateInput}
|
|
71
|
+
*/
|
|
72
|
+
validateInput(eventName, data, schema, trace, eventConfig) {
|
|
73
|
+
// Convert BSBType to Zod schema if needed
|
|
74
|
+
const zodSchema = this.isBSBType(schema) ? (0, interfaces_1.bsbToZod)(schema) : schema;
|
|
75
|
+
return this.performValidation(eventName, data, zodSchema, trace, 'input', eventConfig);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Validate output/return data for an event.
|
|
79
|
+
* @param eventName - Name of the event being validated
|
|
80
|
+
* @param data - Data to validate
|
|
81
|
+
* @param schema - BSBType or Zod schema to validate against
|
|
82
|
+
* @param trace - Trace for logging context
|
|
83
|
+
* @param eventConfig - Override configuration for this validation
|
|
84
|
+
* @returns Validation result with parsed data or error
|
|
85
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#validateOutput | API: EventValidator#validateOutput}
|
|
86
|
+
*/
|
|
87
|
+
validateOutput(eventName, data, schema, trace, eventConfig) {
|
|
88
|
+
// Convert BSBType to Zod schema if needed
|
|
89
|
+
const zodSchema = this.isBSBType(schema) ? (0, interfaces_1.bsbToZod)(schema) : schema;
|
|
90
|
+
return this.performValidation(eventName, data, zodSchema, trace, 'output', eventConfig);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Check if a schema is a BSBType or a Zod schema.
|
|
94
|
+
* @param schema - Schema to check
|
|
95
|
+
* @returns True if schema is a BSBType
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
isBSBType(schema) {
|
|
99
|
+
return schema && typeof schema === 'object' && '_bsb' in schema;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Validate event data using schemas from a validation schema map.
|
|
103
|
+
* @param eventName - Name of the event to validate
|
|
104
|
+
* @param data - Data to validate
|
|
105
|
+
* @param schemas - Map of event validation schemas
|
|
106
|
+
* @param trace - Trace for logging context
|
|
107
|
+
* @param direction - Whether validating 'input' or 'output' data
|
|
108
|
+
* @returns Validation result or null if no schema is defined
|
|
109
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#validateFromSchemas | API: EventValidator#validateFromSchemas}
|
|
110
|
+
*/
|
|
111
|
+
validateFromSchemas(eventName, data, schemas, trace, direction = 'input') {
|
|
112
|
+
const eventSchema = schemas[eventName];
|
|
113
|
+
if (!eventSchema) {
|
|
114
|
+
return null; // No validation schema defined for this event
|
|
115
|
+
}
|
|
116
|
+
const schema = direction === 'input' ? eventSchema.input : eventSchema.output;
|
|
117
|
+
if (!schema) {
|
|
118
|
+
return null; // No schema defined for this direction
|
|
119
|
+
}
|
|
120
|
+
return this.performValidation(eventName, data, schema, trace, direction, eventSchema.config);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Check if validation is enabled for the current configuration.
|
|
124
|
+
* @param eventConfig - Override configuration to check
|
|
125
|
+
* @returns True if validation is enabled
|
|
126
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/EventValidator.html#isValidationEnabled | API: EventValidator#isValidationEnabled}
|
|
127
|
+
*/
|
|
128
|
+
isValidationEnabled(eventConfig) {
|
|
129
|
+
const config = { ...this.config, ...eventConfig };
|
|
130
|
+
return config.enabled === true;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Internal method to perform the actual validation with performance monitoring.
|
|
134
|
+
*/
|
|
135
|
+
performValidation(eventName, data, schema, trace, direction, eventConfig) {
|
|
136
|
+
const config = { ...this.config, ...eventConfig };
|
|
137
|
+
if (!config.enabled) {
|
|
138
|
+
return { success: true, data: data };
|
|
139
|
+
}
|
|
140
|
+
const startTime = performance.now();
|
|
141
|
+
try {
|
|
142
|
+
const parsedData = schema.parse(data);
|
|
143
|
+
const duration = performance.now() - startTime;
|
|
144
|
+
// Log performance warning if threshold exceeded
|
|
145
|
+
if (config.performanceThreshold && duration > config.performanceThreshold) {
|
|
146
|
+
this.logger?.warn(trace, `Event validation for '${eventName}' ${direction} took ${duration.toFixed(2)}ms (threshold: ${config.performanceThreshold}ms)`);
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
success: true,
|
|
150
|
+
data: parsedData
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
const zodError = error;
|
|
155
|
+
if (config.logValidationErrors && this.logger) {
|
|
156
|
+
const errorMsg = `Event validation failed for '${eventName}' ${direction}: ${zodError.message}`;
|
|
157
|
+
this.logger.error(trace, errorMsg);
|
|
158
|
+
}
|
|
159
|
+
if (config.throwOnValidationFailure) {
|
|
160
|
+
// Throw the original Zod error
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
success: false,
|
|
165
|
+
error: error instanceof Error ? error : new Error(String(error))
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.EventValidator = EventValidator;
|
|
171
|
+
/**
|
|
172
|
+
* Custom error class for validation failures.
|
|
173
|
+
* @group Validation
|
|
174
|
+
* @category Errors
|
|
175
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ValidationError.html | API: ValidationError}
|
|
176
|
+
*/
|
|
177
|
+
class ValidationError extends Error {
|
|
178
|
+
zodError;
|
|
179
|
+
eventName;
|
|
180
|
+
direction;
|
|
181
|
+
/**
|
|
182
|
+
* Create a new validation error.
|
|
183
|
+
* @param message - Error message
|
|
184
|
+
* @param zodError - The underlying Zod validation error
|
|
185
|
+
* @param eventName - Name of the event that failed validation
|
|
186
|
+
* @param direction - Whether the failure was on 'input' or 'output'
|
|
187
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ValidationError.html#constructor | API: ValidationError#constructor}
|
|
188
|
+
*/
|
|
189
|
+
constructor(message, zodError, eventName, direction) {
|
|
190
|
+
super(message);
|
|
191
|
+
this.zodError = zodError;
|
|
192
|
+
this.eventName = eventName;
|
|
193
|
+
this.direction = direction;
|
|
194
|
+
this.name = 'ValidationError';
|
|
195
|
+
// Maintain proper stack trace for where our error was thrown
|
|
196
|
+
if (Error.captureStackTrace) {
|
|
197
|
+
Error.captureStackTrace(this, ValidationError);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Get a formatted string representation of the validation errors.
|
|
202
|
+
* @returns Formatted error details
|
|
203
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/classes/ValidationError.html#getFormattedErrors | API: ValidationError#getFormattedErrors}
|
|
204
|
+
*/
|
|
205
|
+
getFormattedErrors() {
|
|
206
|
+
return this.zodError.issues.map((err) => `${err.path.join('.')}: ${err.message}`).join('; ');
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.ValidationError = ValidationError;
|
|
210
|
+
//# sourceMappingURL=EventValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventValidator.js","sourceRoot":"","sources":["../../src/base/EventValidator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAGH,8CAA0E;AAiB1E,MAAM,yBAAyB,GAA0B;IACvD,OAAO,EAAE,IAAI;IACb,mBAAmB,EAAE,IAAI;IACzB,wBAAwB,EAAE,IAAI;IAC9B,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,CAAC;IACb,oBAAoB,EAAE,GAAG;CAC1B,CAAC;AAEF;;;;;;GAMG;AACH,MAAa,cAAc;IACjB,MAAM,CAAwB;IAC9B,MAAM,CAAkB;IAEhC;;;;;OAKG;IACH,YAAY,SAAyC,EAAE,EAAE,MAAuB;QAC9E,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,yBAAyB,EAAE,GAAG,MAAM,EAAE,CAAC;QAC1D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACI,YAAY,CAAC,MAAsC;QACxD,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;IAC9C,CAAC;IAED;;;;;;;;;OASG;IACI,aAAa,CAClB,SAAiB,EACjB,IAAa,EACb,MAAgC,EAChC,KAAa,EACb,WAA4C;QAE5C,0CAA0C;QAC1C,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAA,qBAAQ,EAAC,MAAM,CAAC,CAAC,CAAC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