@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,45 @@
|
|
|
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.PluginTypes = void 0;
|
|
30
|
+
/**
|
|
31
|
+
* @hidden
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/modules.html#module-interfaces_plugins | API: interfaces/plugins}
|
|
35
|
+
*/
|
|
36
|
+
exports.PluginTypes = {
|
|
37
|
+
config: "config",
|
|
38
|
+
events: "events",
|
|
39
|
+
observable: "observable",
|
|
40
|
+
service: "service",
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/modules.html#module-interfaces_plugins | API: interfaces/plugins}
|
|
44
|
+
*/
|
|
45
|
+
//# sourceMappingURL=plugins.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugins.js","sourceRoot":"","sources":["../../src/interfaces/plugins.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;;AAcH;;GAEG;AACH;;GAEG;AACU,QAAA,WAAW,GAAG;IACzB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,UAAU,EAAE,YAAY;IACxB,OAAO,EAAE,SAAS;CACV,CAAC;AA2JX;;GAEG"}
|
|
@@ -0,0 +1,129 @@
|
|
|
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
|
+
/**
|
|
28
|
+
* Result pattern for better error handling without exceptions
|
|
29
|
+
*
|
|
30
|
+
* @group Errors
|
|
31
|
+
* @category Core
|
|
32
|
+
*/
|
|
33
|
+
export type Result<T, E = Error> = {
|
|
34
|
+
success: true;
|
|
35
|
+
data: T;
|
|
36
|
+
error?: never;
|
|
37
|
+
} | {
|
|
38
|
+
success: false;
|
|
39
|
+
data?: never;
|
|
40
|
+
error: E;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Creates a successful result
|
|
44
|
+
*
|
|
45
|
+
* @param data - The successful data
|
|
46
|
+
* @returns Success result
|
|
47
|
+
*
|
|
48
|
+
* @group Errors
|
|
49
|
+
* @category Utilities
|
|
50
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/functions/Ok.html | API: Ok}
|
|
51
|
+
*/
|
|
52
|
+
export declare function Ok<T>(data: T): Result<T, never>;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a failed result
|
|
55
|
+
*
|
|
56
|
+
* @param error - The error that occurred
|
|
57
|
+
* @returns Failed result
|
|
58
|
+
*
|
|
59
|
+
* @group Errors
|
|
60
|
+
* @category Utilities
|
|
61
|
+
*/
|
|
62
|
+
export declare function Err<E>(error: E): Result<never, E>;
|
|
63
|
+
/**
|
|
64
|
+
* Maps a Result's success value to a new value
|
|
65
|
+
*
|
|
66
|
+
* @param result - The result to map
|
|
67
|
+
* @param fn - Function to transform the success value
|
|
68
|
+
* @returns New result with transformed value
|
|
69
|
+
*
|
|
70
|
+
* @group Errors
|
|
71
|
+
* @category Utilities
|
|
72
|
+
*/
|
|
73
|
+
export declare function mapResult<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E>;
|
|
74
|
+
/**
|
|
75
|
+
* Maps a Result's error value to a new error
|
|
76
|
+
*
|
|
77
|
+
* @param result - The result to map
|
|
78
|
+
* @param fn - Function to transform the error value
|
|
79
|
+
* @returns New result with transformed error
|
|
80
|
+
*
|
|
81
|
+
* @group Errors
|
|
82
|
+
* @category Utilities
|
|
83
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/functions/mapError.html | API: mapError}
|
|
84
|
+
*/
|
|
85
|
+
export declare function mapError<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F>;
|
|
86
|
+
/**
|
|
87
|
+
* Chains multiple operations that return Results
|
|
88
|
+
*
|
|
89
|
+
* @param result - The initial result
|
|
90
|
+
* @param fn - Function that takes success value and returns new Result
|
|
91
|
+
* @returns Chained result
|
|
92
|
+
*
|
|
93
|
+
* @group Errors
|
|
94
|
+
* @category Utilities
|
|
95
|
+
*/
|
|
96
|
+
export declare function flatMapResult<T, U, E>(result: Result<T, E>, fn: (value: T) => Result<U, E>): Result<U, E>;
|
|
97
|
+
/**
|
|
98
|
+
* Unwraps a Result, throwing the error if it failed
|
|
99
|
+
*
|
|
100
|
+
* @param result - The result to unwrap
|
|
101
|
+
* @returns The success value
|
|
102
|
+
* @throws The error if result failed
|
|
103
|
+
*
|
|
104
|
+
* @group Errors
|
|
105
|
+
* @category Utilities
|
|
106
|
+
*/
|
|
107
|
+
export declare function unwrap<T, E>(result: Result<T, E>): T;
|
|
108
|
+
/**
|
|
109
|
+
* Unwraps a Result, returning a default value if it failed
|
|
110
|
+
*
|
|
111
|
+
* @param result - The result to unwrap
|
|
112
|
+
* @param defaultValue - Value to return if result failed
|
|
113
|
+
* @returns The success value or default value
|
|
114
|
+
*
|
|
115
|
+
* @group Errors
|
|
116
|
+
* @category Utilities
|
|
117
|
+
*/
|
|
118
|
+
export declare function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T;
|
|
119
|
+
/**
|
|
120
|
+
* Converts a promise that might throw to a Result
|
|
121
|
+
*
|
|
122
|
+
* @param promise - Promise that might throw
|
|
123
|
+
* @returns Promise that resolves to a Result
|
|
124
|
+
*
|
|
125
|
+
* @group Errors
|
|
126
|
+
* @category Utilities
|
|
127
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/functions/fromPromise.html | API: fromPromise}
|
|
128
|
+
*/
|
|
129
|
+
export declare function fromPromise<T>(promise: Promise<T>): Promise<Result<T, Error>>;
|
|
@@ -0,0 +1,162 @@
|
|
|
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.Ok = Ok;
|
|
30
|
+
exports.Err = Err;
|
|
31
|
+
exports.mapResult = mapResult;
|
|
32
|
+
exports.mapError = mapError;
|
|
33
|
+
exports.flatMapResult = flatMapResult;
|
|
34
|
+
exports.unwrap = unwrap;
|
|
35
|
+
exports.unwrapOr = unwrapOr;
|
|
36
|
+
exports.fromPromise = fromPromise;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a successful result
|
|
39
|
+
*
|
|
40
|
+
* @param data - The successful data
|
|
41
|
+
* @returns Success result
|
|
42
|
+
*
|
|
43
|
+
* @group Errors
|
|
44
|
+
* @category Utilities
|
|
45
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/functions/Ok.html | API: Ok}
|
|
46
|
+
*/
|
|
47
|
+
function Ok(data) {
|
|
48
|
+
return { success: true, data };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a failed result
|
|
52
|
+
*
|
|
53
|
+
* @param error - The error that occurred
|
|
54
|
+
* @returns Failed result
|
|
55
|
+
*
|
|
56
|
+
* @group Errors
|
|
57
|
+
* @category Utilities
|
|
58
|
+
*/
|
|
59
|
+
function Err(error) {
|
|
60
|
+
return { success: false, error };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Maps a Result's success value to a new value
|
|
64
|
+
*
|
|
65
|
+
* @param result - The result to map
|
|
66
|
+
* @param fn - Function to transform the success value
|
|
67
|
+
* @returns New result with transformed value
|
|
68
|
+
*
|
|
69
|
+
* @group Errors
|
|
70
|
+
* @category Utilities
|
|
71
|
+
*/
|
|
72
|
+
function mapResult(result, fn) {
|
|
73
|
+
if (result.success) {
|
|
74
|
+
return Ok(fn(result.data));
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Maps a Result's error value to a new error
|
|
80
|
+
*
|
|
81
|
+
* @param result - The result to map
|
|
82
|
+
* @param fn - Function to transform the error value
|
|
83
|
+
* @returns New result with transformed error
|
|
84
|
+
*
|
|
85
|
+
* @group Errors
|
|
86
|
+
* @category Utilities
|
|
87
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/functions/mapError.html | API: mapError}
|
|
88
|
+
*/
|
|
89
|
+
function mapError(result, fn) {
|
|
90
|
+
if (!result.success) {
|
|
91
|
+
return Err(fn(result.error));
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Chains multiple operations that return Results
|
|
97
|
+
*
|
|
98
|
+
* @param result - The initial result
|
|
99
|
+
* @param fn - Function that takes success value and returns new Result
|
|
100
|
+
* @returns Chained result
|
|
101
|
+
*
|
|
102
|
+
* @group Errors
|
|
103
|
+
* @category Utilities
|
|
104
|
+
*/
|
|
105
|
+
function flatMapResult(result, fn) {
|
|
106
|
+
if (result.success) {
|
|
107
|
+
return fn(result.data);
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Unwraps a Result, throwing the error if it failed
|
|
113
|
+
*
|
|
114
|
+
* @param result - The result to unwrap
|
|
115
|
+
* @returns The success value
|
|
116
|
+
* @throws The error if result failed
|
|
117
|
+
*
|
|
118
|
+
* @group Errors
|
|
119
|
+
* @category Utilities
|
|
120
|
+
*/
|
|
121
|
+
function unwrap(result) {
|
|
122
|
+
if (result.success) {
|
|
123
|
+
return result.data;
|
|
124
|
+
}
|
|
125
|
+
throw result.error;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Unwraps a Result, returning a default value if it failed
|
|
129
|
+
*
|
|
130
|
+
* @param result - The result to unwrap
|
|
131
|
+
* @param defaultValue - Value to return if result failed
|
|
132
|
+
* @returns The success value or default value
|
|
133
|
+
*
|
|
134
|
+
* @group Errors
|
|
135
|
+
* @category Utilities
|
|
136
|
+
*/
|
|
137
|
+
function unwrapOr(result, defaultValue) {
|
|
138
|
+
if (result.success) {
|
|
139
|
+
return result.data;
|
|
140
|
+
}
|
|
141
|
+
return defaultValue;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Converts a promise that might throw to a Result
|
|
145
|
+
*
|
|
146
|
+
* @param promise - Promise that might throw
|
|
147
|
+
* @returns Promise that resolves to a Result
|
|
148
|
+
*
|
|
149
|
+
* @group Errors
|
|
150
|
+
* @category Utilities
|
|
151
|
+
* @see {@link https://bsbcode.dev/languages/nodejs/types/functions/fromPromise.html | API: fromPromise}
|
|
152
|
+
*/
|
|
153
|
+
async function fromPromise(promise) {
|
|
154
|
+
try {
|
|
155
|
+
const data = await promise;
|
|
156
|
+
return Ok(data);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
return Err(error instanceof Error ? error : new Error(String(error)));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/interfaces/result.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;;AAsBH,gBAEC;AAWD,kBAEC;AAYD,8BAQC;AAaD,4BAQC;AAYD,sCAQC;AAYD,wBAKC;AAYD,4BAKC;AAYD,kCAOC;AA3ID;;;;;;;;;GASG;AACH,SAAgB,EAAE,CAAI,IAAO;IAC3B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AACjC,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,GAAG,CAAI,KAAQ;IAC7B,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AACnC,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,SAAS,CACvB,MAAoB,EACpB,EAAmB;IAEnB,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,QAAQ,CACtB,MAAoB,EACpB,EAAmB;IAEnB,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,OAAO,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,aAAa,CAC3B,MAAoB,EACpB,EAA8B;IAE9B,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,MAAM,CAAO,MAAoB;IAC/C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,MAAM,CAAC,IAAI,CAAC;IACrB,CAAC;IACD,MAAM,MAAM,CAAC,KAAK,CAAC;AACrB,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAO,MAAoB,EAAE,YAAe;IAClE,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,MAAM,CAAC,IAAI,CAAC;IACrB,CAAC;IACD,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;;;;;;;;GASG;AACI,KAAK,UAAU,WAAW,CAAI,OAAmB;IACtD,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC;QAC3B,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACxE,CAAC;AACH,CAAC"}
|