@ls-stack/utils 2.2.0 → 2.4.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/dist/{chunk-3PXKYWJF.js → chunk-KBFP7INB.js} +19 -16
- package/dist/chunk-VAAMRG4K.js +20 -0
- package/dist/main.d.ts +1 -0
- package/dist/parallelAsyncCalls.cjs +19 -15
- package/dist/parallelAsyncCalls.d.cts +1 -0
- package/dist/parallelAsyncCalls.d.ts +1 -0
- package/dist/parallelAsyncCalls.js +4 -3
- package/dist/rsResult.cjs +27 -17
- package/dist/rsResult.d.cts +30 -4
- package/dist/rsResult.d.ts +30 -4
- package/dist/rsResult.js +6 -3
- package/dist/safeJson.cjs +45 -0
- package/dist/safeJson.d.cts +6 -0
- package/dist/safeJson.d.ts +6 -0
- package/dist/safeJson.js +8 -0
- package/dist/typingFnUtils.cjs +10 -0
- package/dist/typingFnUtils.d.cts +13 -1
- package/dist/typingFnUtils.d.ts +13 -1
- package/dist/typingFnUtils.js +8 -0
- package/package.json +19 -11
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
safeJsonStringify
|
|
3
|
+
} from "./chunk-VAAMRG4K.js";
|
|
1
4
|
import {
|
|
2
5
|
isObject
|
|
3
6
|
} from "./chunk-4UGSP3L3.js";
|
|
@@ -47,15 +50,15 @@ function err(error) {
|
|
|
47
50
|
if (error instanceof Error) {
|
|
48
51
|
throw error;
|
|
49
52
|
}
|
|
50
|
-
throw
|
|
53
|
+
throw unknownToError(error);
|
|
51
54
|
},
|
|
52
55
|
mapOk: returnResult,
|
|
53
56
|
mapErr: errMap,
|
|
54
57
|
mapOkAndErr
|
|
55
58
|
};
|
|
56
59
|
}
|
|
57
|
-
function
|
|
58
|
-
return err(
|
|
60
|
+
function unknownToResultError(error) {
|
|
61
|
+
return err(unknownToError(error));
|
|
59
62
|
}
|
|
60
63
|
async function asyncUnwrap(result) {
|
|
61
64
|
const unwrapped = await result;
|
|
@@ -65,7 +68,7 @@ async function asyncUnwrap(result) {
|
|
|
65
68
|
if (unwrapped.error instanceof Error) {
|
|
66
69
|
throw unwrapped.error;
|
|
67
70
|
}
|
|
68
|
-
throw
|
|
71
|
+
throw unknownToError(unwrapped.error);
|
|
69
72
|
}
|
|
70
73
|
function asyncMap(resultPromise) {
|
|
71
74
|
return {
|
|
@@ -89,7 +92,7 @@ function asyncMap(resultPromise) {
|
|
|
89
92
|
var Result = {
|
|
90
93
|
ok,
|
|
91
94
|
err,
|
|
92
|
-
unknownToError,
|
|
95
|
+
unknownToError: unknownToResultError,
|
|
93
96
|
asyncUnwrap,
|
|
94
97
|
asyncMap
|
|
95
98
|
};
|
|
@@ -98,7 +101,7 @@ function resultify(fn, errorNormalizer) {
|
|
|
98
101
|
return ok(fn());
|
|
99
102
|
} catch (error) {
|
|
100
103
|
return err(
|
|
101
|
-
errorNormalizer ? errorNormalizer(error) :
|
|
104
|
+
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
102
105
|
);
|
|
103
106
|
}
|
|
104
107
|
}
|
|
@@ -107,11 +110,11 @@ async function asyncResultify(fn, errorNormalizer) {
|
|
|
107
110
|
return ok(await fn());
|
|
108
111
|
} catch (error) {
|
|
109
112
|
return err(
|
|
110
|
-
errorNormalizer ? errorNormalizer(error) :
|
|
113
|
+
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
111
114
|
);
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
|
-
function
|
|
117
|
+
function unknownToError(error) {
|
|
115
118
|
if (error instanceof Error) return error;
|
|
116
119
|
if (typeof error === "string") {
|
|
117
120
|
return new Error(error);
|
|
@@ -122,15 +125,14 @@ function normalizeError(error) {
|
|
|
122
125
|
{ cause: error }
|
|
123
126
|
);
|
|
124
127
|
}
|
|
125
|
-
return new Error(safeJsonStringify(error) ?? "unknown", {
|
|
128
|
+
return new Error(safeJsonStringify(error) ?? "unknown", {
|
|
129
|
+
cause: error
|
|
130
|
+
});
|
|
126
131
|
}
|
|
127
|
-
function
|
|
128
|
-
|
|
129
|
-
return JSON.stringify(value);
|
|
130
|
-
} catch (_) {
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
132
|
+
function normalizeError(error) {
|
|
133
|
+
return unknownToError(error);
|
|
133
134
|
}
|
|
135
|
+
var safeJsonStringify2 = safeJsonStringify;
|
|
134
136
|
function createTypedResult() {
|
|
135
137
|
return {
|
|
136
138
|
ok,
|
|
@@ -142,7 +144,8 @@ export {
|
|
|
142
144
|
Result,
|
|
143
145
|
resultify,
|
|
144
146
|
asyncResultify,
|
|
147
|
+
unknownToError,
|
|
145
148
|
normalizeError,
|
|
146
|
-
safeJsonStringify,
|
|
149
|
+
safeJsonStringify2 as safeJsonStringify,
|
|
147
150
|
createTypedResult
|
|
148
151
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/safeJson.ts
|
|
2
|
+
function safeJsonStringify(value) {
|
|
3
|
+
try {
|
|
4
|
+
return JSON.stringify(value);
|
|
5
|
+
} catch (_) {
|
|
6
|
+
return void 0;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function safeJsonParse(value) {
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(value);
|
|
12
|
+
} catch (_) {
|
|
13
|
+
return void 0;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
safeJsonStringify,
|
|
19
|
+
safeJsonParse
|
|
20
|
+
};
|
package/dist/main.d.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
///<reference path="promiseUtils.d.ts" />
|
|
19
19
|
///<reference path="rsResult.d.ts" />
|
|
20
20
|
///<reference path="runShellCmd.d.ts" />
|
|
21
|
+
///<reference path="safeJson.d.ts" />
|
|
21
22
|
///<reference path="shallowEqual.d.ts" />
|
|
22
23
|
///<reference path="sleep.d.ts" />
|
|
23
24
|
///<reference path="stringUtils.d.ts" />
|
|
@@ -34,6 +34,15 @@ function isObject(value) {
|
|
|
34
34
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// src/safeJson.ts
|
|
38
|
+
function safeJsonStringify(value) {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.stringify(value);
|
|
41
|
+
} catch (_) {
|
|
42
|
+
return void 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
// src/rsResult.ts
|
|
38
47
|
function okUnwrapOr() {
|
|
39
48
|
return this.value;
|
|
@@ -79,15 +88,15 @@ function err(error) {
|
|
|
79
88
|
if (error instanceof Error) {
|
|
80
89
|
throw error;
|
|
81
90
|
}
|
|
82
|
-
throw
|
|
91
|
+
throw unknownToError(error);
|
|
83
92
|
},
|
|
84
93
|
mapOk: returnResult,
|
|
85
94
|
mapErr: errMap,
|
|
86
95
|
mapOkAndErr
|
|
87
96
|
};
|
|
88
97
|
}
|
|
89
|
-
function
|
|
90
|
-
return err(
|
|
98
|
+
function unknownToResultError(error) {
|
|
99
|
+
return err(unknownToError(error));
|
|
91
100
|
}
|
|
92
101
|
async function asyncUnwrap(result) {
|
|
93
102
|
const unwrapped = await result;
|
|
@@ -97,7 +106,7 @@ async function asyncUnwrap(result) {
|
|
|
97
106
|
if (unwrapped.error instanceof Error) {
|
|
98
107
|
throw unwrapped.error;
|
|
99
108
|
}
|
|
100
|
-
throw
|
|
109
|
+
throw unknownToError(unwrapped.error);
|
|
101
110
|
}
|
|
102
111
|
function asyncMap(resultPromise) {
|
|
103
112
|
return {
|
|
@@ -121,11 +130,11 @@ function asyncMap(resultPromise) {
|
|
|
121
130
|
var Result = {
|
|
122
131
|
ok,
|
|
123
132
|
err,
|
|
124
|
-
unknownToError,
|
|
133
|
+
unknownToError: unknownToResultError,
|
|
125
134
|
asyncUnwrap,
|
|
126
135
|
asyncMap
|
|
127
136
|
};
|
|
128
|
-
function
|
|
137
|
+
function unknownToError(error) {
|
|
129
138
|
if (error instanceof Error) return error;
|
|
130
139
|
if (typeof error === "string") {
|
|
131
140
|
return new Error(error);
|
|
@@ -136,14 +145,9 @@ function normalizeError(error) {
|
|
|
136
145
|
{ cause: error }
|
|
137
146
|
);
|
|
138
147
|
}
|
|
139
|
-
return new Error(safeJsonStringify(error) ?? "unknown", {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
try {
|
|
143
|
-
return JSON.stringify(value);
|
|
144
|
-
} catch (_) {
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
148
|
+
return new Error(safeJsonStringify(error) ?? "unknown", {
|
|
149
|
+
cause: error
|
|
150
|
+
});
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
// src/sleep.ts
|
|
@@ -241,7 +245,7 @@ var ParallelAsyncResultCalls = class {
|
|
|
241
245
|
} catch (exception) {
|
|
242
246
|
throw {
|
|
243
247
|
metadata: call.metadata,
|
|
244
|
-
error:
|
|
248
|
+
error: unknownToError(exception)
|
|
245
249
|
};
|
|
246
250
|
}
|
|
247
251
|
})
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Result,
|
|
3
|
-
|
|
4
|
-
} from "./chunk-
|
|
3
|
+
unknownToError
|
|
4
|
+
} from "./chunk-KBFP7INB.js";
|
|
5
|
+
import "./chunk-VAAMRG4K.js";
|
|
5
6
|
import {
|
|
6
7
|
sleep
|
|
7
8
|
} from "./chunk-5DZT3Z5Z.js";
|
|
@@ -100,7 +101,7 @@ var ParallelAsyncResultCalls = class {
|
|
|
100
101
|
} catch (exception) {
|
|
101
102
|
throw {
|
|
102
103
|
metadata: call.metadata,
|
|
103
|
-
error:
|
|
104
|
+
error: unknownToError(exception)
|
|
104
105
|
};
|
|
105
106
|
}
|
|
106
107
|
})
|
package/dist/rsResult.cjs
CHANGED
|
@@ -25,7 +25,8 @@ __export(rsResult_exports, {
|
|
|
25
25
|
createTypedResult: () => createTypedResult,
|
|
26
26
|
normalizeError: () => normalizeError,
|
|
27
27
|
resultify: () => resultify,
|
|
28
|
-
safeJsonStringify: () =>
|
|
28
|
+
safeJsonStringify: () => safeJsonStringify2,
|
|
29
|
+
unknownToError: () => unknownToError
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(rsResult_exports);
|
|
31
32
|
|
|
@@ -34,6 +35,15 @@ function isObject(value) {
|
|
|
34
35
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
// src/safeJson.ts
|
|
39
|
+
function safeJsonStringify(value) {
|
|
40
|
+
try {
|
|
41
|
+
return JSON.stringify(value);
|
|
42
|
+
} catch (_) {
|
|
43
|
+
return void 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
37
47
|
// src/rsResult.ts
|
|
38
48
|
function okUnwrapOr() {
|
|
39
49
|
return this.value;
|
|
@@ -79,15 +89,15 @@ function err(error) {
|
|
|
79
89
|
if (error instanceof Error) {
|
|
80
90
|
throw error;
|
|
81
91
|
}
|
|
82
|
-
throw
|
|
92
|
+
throw unknownToError(error);
|
|
83
93
|
},
|
|
84
94
|
mapOk: returnResult,
|
|
85
95
|
mapErr: errMap,
|
|
86
96
|
mapOkAndErr
|
|
87
97
|
};
|
|
88
98
|
}
|
|
89
|
-
function
|
|
90
|
-
return err(
|
|
99
|
+
function unknownToResultError(error) {
|
|
100
|
+
return err(unknownToError(error));
|
|
91
101
|
}
|
|
92
102
|
async function asyncUnwrap(result) {
|
|
93
103
|
const unwrapped = await result;
|
|
@@ -97,7 +107,7 @@ async function asyncUnwrap(result) {
|
|
|
97
107
|
if (unwrapped.error instanceof Error) {
|
|
98
108
|
throw unwrapped.error;
|
|
99
109
|
}
|
|
100
|
-
throw
|
|
110
|
+
throw unknownToError(unwrapped.error);
|
|
101
111
|
}
|
|
102
112
|
function asyncMap(resultPromise) {
|
|
103
113
|
return {
|
|
@@ -121,7 +131,7 @@ function asyncMap(resultPromise) {
|
|
|
121
131
|
var Result = {
|
|
122
132
|
ok,
|
|
123
133
|
err,
|
|
124
|
-
unknownToError,
|
|
134
|
+
unknownToError: unknownToResultError,
|
|
125
135
|
asyncUnwrap,
|
|
126
136
|
asyncMap
|
|
127
137
|
};
|
|
@@ -130,7 +140,7 @@ function resultify(fn, errorNormalizer) {
|
|
|
130
140
|
return ok(fn());
|
|
131
141
|
} catch (error) {
|
|
132
142
|
return err(
|
|
133
|
-
errorNormalizer ? errorNormalizer(error) :
|
|
143
|
+
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
134
144
|
);
|
|
135
145
|
}
|
|
136
146
|
}
|
|
@@ -139,11 +149,11 @@ async function asyncResultify(fn, errorNormalizer) {
|
|
|
139
149
|
return ok(await fn());
|
|
140
150
|
} catch (error) {
|
|
141
151
|
return err(
|
|
142
|
-
errorNormalizer ? errorNormalizer(error) :
|
|
152
|
+
errorNormalizer ? errorNormalizer(error) : unknownToError(error)
|
|
143
153
|
);
|
|
144
154
|
}
|
|
145
155
|
}
|
|
146
|
-
function
|
|
156
|
+
function unknownToError(error) {
|
|
147
157
|
if (error instanceof Error) return error;
|
|
148
158
|
if (typeof error === "string") {
|
|
149
159
|
return new Error(error);
|
|
@@ -154,15 +164,14 @@ function normalizeError(error) {
|
|
|
154
164
|
{ cause: error }
|
|
155
165
|
);
|
|
156
166
|
}
|
|
157
|
-
return new Error(safeJsonStringify(error) ?? "unknown", {
|
|
167
|
+
return new Error(safeJsonStringify(error) ?? "unknown", {
|
|
168
|
+
cause: error
|
|
169
|
+
});
|
|
158
170
|
}
|
|
159
|
-
function
|
|
160
|
-
|
|
161
|
-
return JSON.stringify(value);
|
|
162
|
-
} catch (_) {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
171
|
+
function normalizeError(error) {
|
|
172
|
+
return unknownToError(error);
|
|
165
173
|
}
|
|
174
|
+
var safeJsonStringify2 = safeJsonStringify;
|
|
166
175
|
function createTypedResult() {
|
|
167
176
|
return {
|
|
168
177
|
ok,
|
|
@@ -176,5 +185,6 @@ function createTypedResult() {
|
|
|
176
185
|
createTypedResult,
|
|
177
186
|
normalizeError,
|
|
178
187
|
resultify,
|
|
179
|
-
safeJsonStringify
|
|
188
|
+
safeJsonStringify,
|
|
189
|
+
unknownToError
|
|
180
190
|
});
|
package/dist/rsResult.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { safeJsonStringify as safeJsonStringify$1 } from './safeJson.cjs';
|
|
2
|
+
|
|
1
3
|
type Ok<T> = {
|
|
2
4
|
ok: true;
|
|
3
5
|
error: false;
|
|
@@ -27,7 +29,7 @@ declare function ok(): OkResult<void, any>;
|
|
|
27
29
|
declare function ok<T>(value: T): OkResult<T, any>;
|
|
28
30
|
type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
|
|
29
31
|
declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
|
|
30
|
-
declare function
|
|
32
|
+
declare function unknownToResultError(error: unknown): ErrResult<Error, any>;
|
|
31
33
|
/** Unwraps a promise result */
|
|
32
34
|
declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
|
|
33
35
|
declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
|
|
@@ -64,7 +66,7 @@ type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrRes
|
|
|
64
66
|
declare const Result: {
|
|
65
67
|
ok: typeof ok;
|
|
66
68
|
err: typeof err;
|
|
67
|
-
unknownToError: typeof
|
|
69
|
+
unknownToError: typeof unknownToResultError;
|
|
68
70
|
asyncUnwrap: typeof asyncUnwrap;
|
|
69
71
|
asyncMap: typeof asyncMap;
|
|
70
72
|
};
|
|
@@ -72,12 +74,36 @@ declare const Result: {
|
|
|
72
74
|
declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
|
|
73
75
|
/** transform a async function in a result function */
|
|
74
76
|
declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
|
|
77
|
+
/**
|
|
78
|
+
* Converts an unknown error value into an Error object.
|
|
79
|
+
*
|
|
80
|
+
* @param error - The unknown value to convert to an Error
|
|
81
|
+
* @returns An Error object
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* try {
|
|
85
|
+
* // some code that might throw
|
|
86
|
+
* } catch (err) {
|
|
87
|
+
* const error = unknownToError(err); // Guaranteed to be Error instance
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* The function handles different error types:
|
|
91
|
+
* - Returns the error directly if it's already an Error instance
|
|
92
|
+
* - Converts strings into Error objects using the string as the message
|
|
93
|
+
* - For objects, tries to extract a message property or stringifies the object
|
|
94
|
+
* - For other values, stringifies them or uses 'unknown' as fallback
|
|
95
|
+
*
|
|
96
|
+
* The original error value is preserved as the `cause` property of the returned Error.
|
|
97
|
+
*/
|
|
98
|
+
declare function unknownToError(error: unknown): Error;
|
|
99
|
+
/** @deprecated use unknownToError instead, this will be removed in the next major version */
|
|
75
100
|
declare function normalizeError(error: unknown): Error;
|
|
76
|
-
|
|
101
|
+
/** @deprecated use safeJsonStringify from `@ls-stack/utils/safeJson` instead, this will be removed in the next major version */
|
|
102
|
+
declare const safeJsonStringify: typeof safeJsonStringify$1;
|
|
77
103
|
type TypedResult<T, E extends ResultValidErrors = Error> = {
|
|
78
104
|
ok: (value: T) => OkResult<T, E, T>;
|
|
79
105
|
err: (error: E) => ErrResult<E, T>;
|
|
80
106
|
};
|
|
81
107
|
declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
|
|
82
108
|
|
|
83
|
-
export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify };
|
|
109
|
+
export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify, unknownToError };
|
package/dist/rsResult.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { safeJsonStringify as safeJsonStringify$1 } from './safeJson.js';
|
|
2
|
+
|
|
1
3
|
type Ok<T> = {
|
|
2
4
|
ok: true;
|
|
3
5
|
error: false;
|
|
@@ -27,7 +29,7 @@ declare function ok(): OkResult<void, any>;
|
|
|
27
29
|
declare function ok<T>(value: T): OkResult<T, any>;
|
|
28
30
|
type ErrResult<E extends ResultValidErrors, T = any> = Err<E> & ResultMethods<T, E>;
|
|
29
31
|
declare function err<E extends ResultValidErrors>(error: E): ErrResult<E>;
|
|
30
|
-
declare function
|
|
32
|
+
declare function unknownToResultError(error: unknown): ErrResult<Error, any>;
|
|
31
33
|
/** Unwraps a promise result */
|
|
32
34
|
declare function asyncUnwrap<T>(result: Promise<Result<T, ResultValidErrors>>): Promise<T>;
|
|
33
35
|
declare function asyncMap<T, E extends ResultValidErrors>(resultPromise: Promise<Result<T, E>>): {
|
|
@@ -64,7 +66,7 @@ type Result<T, E extends ResultValidErrors = Error> = OkResult<T, E, T> | ErrRes
|
|
|
64
66
|
declare const Result: {
|
|
65
67
|
ok: typeof ok;
|
|
66
68
|
err: typeof err;
|
|
67
|
-
unknownToError: typeof
|
|
69
|
+
unknownToError: typeof unknownToResultError;
|
|
68
70
|
asyncUnwrap: typeof asyncUnwrap;
|
|
69
71
|
asyncMap: typeof asyncMap;
|
|
70
72
|
};
|
|
@@ -72,12 +74,36 @@ declare const Result: {
|
|
|
72
74
|
declare function resultify<T, E extends ResultValidErrors = Error>(fn: () => T, errorNormalizer?: (err: unknown) => E): Result<T, E>;
|
|
73
75
|
/** transform a async function in a result function */
|
|
74
76
|
declare function asyncResultify<T, E extends Error = Error>(fn: () => Promise<T>, errorNormalizer?: (err: unknown) => E): Promise<Result<Awaited<T>, E>>;
|
|
77
|
+
/**
|
|
78
|
+
* Converts an unknown error value into an Error object.
|
|
79
|
+
*
|
|
80
|
+
* @param error - The unknown value to convert to an Error
|
|
81
|
+
* @returns An Error object
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* try {
|
|
85
|
+
* // some code that might throw
|
|
86
|
+
* } catch (err) {
|
|
87
|
+
* const error = unknownToError(err); // Guaranteed to be Error instance
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* The function handles different error types:
|
|
91
|
+
* - Returns the error directly if it's already an Error instance
|
|
92
|
+
* - Converts strings into Error objects using the string as the message
|
|
93
|
+
* - For objects, tries to extract a message property or stringifies the object
|
|
94
|
+
* - For other values, stringifies them or uses 'unknown' as fallback
|
|
95
|
+
*
|
|
96
|
+
* The original error value is preserved as the `cause` property of the returned Error.
|
|
97
|
+
*/
|
|
98
|
+
declare function unknownToError(error: unknown): Error;
|
|
99
|
+
/** @deprecated use unknownToError instead, this will be removed in the next major version */
|
|
75
100
|
declare function normalizeError(error: unknown): Error;
|
|
76
|
-
|
|
101
|
+
/** @deprecated use safeJsonStringify from `@ls-stack/utils/safeJson` instead, this will be removed in the next major version */
|
|
102
|
+
declare const safeJsonStringify: typeof safeJsonStringify$1;
|
|
77
103
|
type TypedResult<T, E extends ResultValidErrors = Error> = {
|
|
78
104
|
ok: (value: T) => OkResult<T, E, T>;
|
|
79
105
|
err: (error: E) => ErrResult<E, T>;
|
|
80
106
|
};
|
|
81
107
|
declare function createTypedResult<T, E extends ResultValidErrors = Error>(): TypedResult<T, E>;
|
|
82
108
|
|
|
83
|
-
export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify };
|
|
109
|
+
export { Result, type TypedResult, asyncResultify, createTypedResult, normalizeError, resultify, safeJsonStringify, unknownToError };
|
package/dist/rsResult.js
CHANGED
|
@@ -4,8 +4,10 @@ import {
|
|
|
4
4
|
createTypedResult,
|
|
5
5
|
normalizeError,
|
|
6
6
|
resultify,
|
|
7
|
-
safeJsonStringify
|
|
8
|
-
|
|
7
|
+
safeJsonStringify,
|
|
8
|
+
unknownToError
|
|
9
|
+
} from "./chunk-KBFP7INB.js";
|
|
10
|
+
import "./chunk-VAAMRG4K.js";
|
|
9
11
|
import "./chunk-4UGSP3L3.js";
|
|
10
12
|
export {
|
|
11
13
|
Result,
|
|
@@ -13,5 +15,6 @@ export {
|
|
|
13
15
|
createTypedResult,
|
|
14
16
|
normalizeError,
|
|
15
17
|
resultify,
|
|
16
|
-
safeJsonStringify
|
|
18
|
+
safeJsonStringify,
|
|
19
|
+
unknownToError
|
|
17
20
|
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/safeJson.ts
|
|
21
|
+
var safeJson_exports = {};
|
|
22
|
+
__export(safeJson_exports, {
|
|
23
|
+
safeJsonParse: () => safeJsonParse,
|
|
24
|
+
safeJsonStringify: () => safeJsonStringify
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(safeJson_exports);
|
|
27
|
+
function safeJsonStringify(value) {
|
|
28
|
+
try {
|
|
29
|
+
return JSON.stringify(value);
|
|
30
|
+
} catch (_) {
|
|
31
|
+
return void 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function safeJsonParse(value) {
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(value);
|
|
37
|
+
} catch (_) {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
safeJsonParse,
|
|
44
|
+
safeJsonStringify
|
|
45
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** JSON.stringify can throw if the value is circular or contains functions, this function catches those errors and returns undefined */
|
|
2
|
+
declare function safeJsonStringify(value: unknown): string | undefined;
|
|
3
|
+
/** JSON.parse can throw if the value is not valid JSON, this function catches those errors and returns undefined */
|
|
4
|
+
declare function safeJsonParse(value: string): any;
|
|
5
|
+
|
|
6
|
+
export { safeJsonParse, safeJsonStringify };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** JSON.stringify can throw if the value is circular or contains functions, this function catches those errors and returns undefined */
|
|
2
|
+
declare function safeJsonStringify(value: unknown): string | undefined;
|
|
3
|
+
/** JSON.parse can throw if the value is not valid JSON, this function catches those errors and returns undefined */
|
|
4
|
+
declare function safeJsonParse(value: string): any;
|
|
5
|
+
|
|
6
|
+
export { safeJsonParse, safeJsonStringify };
|
package/dist/safeJson.js
ADDED
package/dist/typingFnUtils.cjs
CHANGED
|
@@ -22,6 +22,8 @@ var typingFnUtils_exports = {};
|
|
|
22
22
|
__export(typingFnUtils_exports, {
|
|
23
23
|
asNonPartial: () => asNonPartial,
|
|
24
24
|
asType: () => asType,
|
|
25
|
+
isObjKey: () => isObjKey,
|
|
26
|
+
isSubTypeOf: () => isSubTypeOf,
|
|
25
27
|
narrowStringToUnion: () => narrowStringToUnion,
|
|
26
28
|
typedObjectEntries: () => typedObjectEntries,
|
|
27
29
|
typedObjectKeys: () => typedObjectKeys
|
|
@@ -45,10 +47,18 @@ function narrowStringToUnion(key, union) {
|
|
|
45
47
|
}
|
|
46
48
|
return void 0;
|
|
47
49
|
}
|
|
50
|
+
function isSubTypeOf() {
|
|
51
|
+
return void 0;
|
|
52
|
+
}
|
|
53
|
+
function isObjKey(key, obj) {
|
|
54
|
+
return typeof key === "string" && key in obj;
|
|
55
|
+
}
|
|
48
56
|
// Annotate the CommonJS export names for ESM import in node:
|
|
49
57
|
0 && (module.exports = {
|
|
50
58
|
asNonPartial,
|
|
51
59
|
asType,
|
|
60
|
+
isObjKey,
|
|
61
|
+
isSubTypeOf,
|
|
52
62
|
narrowStringToUnion,
|
|
53
63
|
typedObjectEntries,
|
|
54
64
|
typedObjectKeys
|
package/dist/typingFnUtils.d.cts
CHANGED
|
@@ -11,5 +11,17 @@ declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (ke
|
|
|
11
11
|
declare function asType<T = unknown>(value: T): T;
|
|
12
12
|
/** narrow a string to a union of strings */
|
|
13
13
|
declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[]): T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Type helper to check if a type is a subtype of another type.
|
|
16
|
+
*
|
|
17
|
+
* @template BaseType - The base type to check against
|
|
18
|
+
* @template SubType - The type that should extend BaseType
|
|
19
|
+
* @returns {unknown} Returns undefined, only used for type checking
|
|
20
|
+
*/
|
|
21
|
+
declare function isSubTypeOf<BaseType, SubType extends BaseType>(): unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Type helper to narrow a string to a key of an object.
|
|
24
|
+
*/
|
|
25
|
+
declare function isObjKey<T extends Record<string, unknown>>(key: unknown, obj: T): key is keyof T;
|
|
14
26
|
|
|
15
|
-
export { asNonPartial, asType, narrowStringToUnion, typedObjectEntries, typedObjectKeys };
|
|
27
|
+
export { asNonPartial, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typedObjectEntries, typedObjectKeys };
|
package/dist/typingFnUtils.d.ts
CHANGED
|
@@ -11,5 +11,17 @@ declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (ke
|
|
|
11
11
|
declare function asType<T = unknown>(value: T): T;
|
|
12
12
|
/** narrow a string to a union of strings */
|
|
13
13
|
declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[]): T | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Type helper to check if a type is a subtype of another type.
|
|
16
|
+
*
|
|
17
|
+
* @template BaseType - The base type to check against
|
|
18
|
+
* @template SubType - The type that should extend BaseType
|
|
19
|
+
* @returns {unknown} Returns undefined, only used for type checking
|
|
20
|
+
*/
|
|
21
|
+
declare function isSubTypeOf<BaseType, SubType extends BaseType>(): unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Type helper to narrow a string to a key of an object.
|
|
24
|
+
*/
|
|
25
|
+
declare function isObjKey<T extends Record<string, unknown>>(key: unknown, obj: T): key is keyof T;
|
|
14
26
|
|
|
15
|
-
export { asNonPartial, asType, narrowStringToUnion, typedObjectEntries, typedObjectKeys };
|
|
27
|
+
export { asNonPartial, asType, isObjKey, isSubTypeOf, narrowStringToUnion, typedObjectEntries, typedObjectKeys };
|
package/dist/typingFnUtils.js
CHANGED
|
@@ -17,9 +17,17 @@ function narrowStringToUnion(key, union) {
|
|
|
17
17
|
}
|
|
18
18
|
return void 0;
|
|
19
19
|
}
|
|
20
|
+
function isSubTypeOf() {
|
|
21
|
+
return void 0;
|
|
22
|
+
}
|
|
23
|
+
function isObjKey(key, obj) {
|
|
24
|
+
return typeof key === "string" && key in obj;
|
|
25
|
+
}
|
|
20
26
|
export {
|
|
21
27
|
asNonPartial,
|
|
22
28
|
asType,
|
|
29
|
+
isObjKey,
|
|
30
|
+
isSubTypeOf,
|
|
23
31
|
narrowStringToUnion,
|
|
24
32
|
typedObjectEntries,
|
|
25
33
|
typedObjectKeys
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Typescript utils",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.4.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
@@ -119,6 +119,11 @@
|
|
|
119
119
|
"types": "./dist/runShellCmd.d.ts",
|
|
120
120
|
"require": "./dist/runShellCmd.cjs"
|
|
121
121
|
},
|
|
122
|
+
"./safeJson": {
|
|
123
|
+
"import": "./dist/safeJson.js",
|
|
124
|
+
"types": "./dist/safeJson.d.ts",
|
|
125
|
+
"require": "./dist/safeJson.cjs"
|
|
126
|
+
},
|
|
122
127
|
"./shallowEqual": {
|
|
123
128
|
"import": "./dist/shallowEqual.js",
|
|
124
129
|
"types": "./dist/shallowEqual.d.ts",
|
|
@@ -167,23 +172,23 @@
|
|
|
167
172
|
"@ls-stack/extended-lint": "^0.20.1",
|
|
168
173
|
"@types/eslint": "^9.6.1",
|
|
169
174
|
"@types/eslint__js": "^8.42.3",
|
|
170
|
-
"@types/node": "^22.10.
|
|
171
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
172
|
-
"@typescript-eslint/parser": "^8.
|
|
173
|
-
"@vitest/ui": "^
|
|
175
|
+
"@types/node": "^22.10.9",
|
|
176
|
+
"@typescript-eslint/eslint-plugin": "^8.21.0",
|
|
177
|
+
"@typescript-eslint/parser": "^8.21.0",
|
|
178
|
+
"@vitest/ui": "^3.0.4",
|
|
174
179
|
"dequal": "^2.0.3",
|
|
175
|
-
"eslint": "^9.
|
|
180
|
+
"eslint": "^9.18.0",
|
|
176
181
|
"eslint-plugin-unicorn": "^56.0.1",
|
|
177
182
|
"eslint-plugin-vitest": "^0.5.4",
|
|
178
|
-
"mitata": "^1.0.
|
|
183
|
+
"mitata": "^1.0.32",
|
|
179
184
|
"prettier": "3.4.2",
|
|
180
185
|
"prettier-plugin-organize-imports": "^4.1.0",
|
|
181
186
|
"tsm": "^2.3.0",
|
|
182
187
|
"tsup": "^8.3.5",
|
|
183
|
-
"typescript": "^5.7.
|
|
184
|
-
"typescript-eslint": "^8.
|
|
185
|
-
"vite": "^6.0.
|
|
186
|
-
"vitest": "^
|
|
188
|
+
"typescript": "^5.7.3",
|
|
189
|
+
"typescript-eslint": "^8.21.0",
|
|
190
|
+
"vite": "^6.0.11",
|
|
191
|
+
"vitest": "^3.0.4"
|
|
187
192
|
},
|
|
188
193
|
"typesVersions": {
|
|
189
194
|
"*": {
|
|
@@ -247,6 +252,9 @@
|
|
|
247
252
|
"runShellCmd": [
|
|
248
253
|
"./dist/runShellCmd.d.ts"
|
|
249
254
|
],
|
|
255
|
+
"safeJson": [
|
|
256
|
+
"./dist/safeJson.d.ts"
|
|
257
|
+
],
|
|
250
258
|
"shallowEqual": [
|
|
251
259
|
"./dist/shallowEqual.d.ts"
|
|
252
260
|
],
|