@naturalcycles/js-lib 14.129.0 → 14.130.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/decorators/asyncMemo.decorator.d.ts +2 -2
- package/dist/decorators/createPromiseDecorator.d.ts +6 -6
- package/dist/decorators/debounce.d.ts +2 -2
- package/dist/decorators/memo.decorator.d.ts +2 -2
- package/dist/decorators/memo.util.d.ts +7 -7
- package/dist/error/app.error.d.ts +1 -1
- package/dist/error/app.error.js +2 -2
- package/dist/error/assert.d.ts +0 -1
- package/dist/error/assert.js +0 -3
- package/dist/error/error.model.d.ts +1 -0
- package/dist/error/error.util.d.ts +4 -4
- package/dist/error/error.util.js +26 -13
- package/dist/error/errorMode.d.ts +1 -1
- package/dist/error/errorMode.js +1 -1
- package/dist/error/http.error.d.ts +1 -1
- package/dist/error/http.error.js +2 -2
- package/dist/error/tryCatch.d.ts +1 -1
- package/dist/http/fetcher.js +4 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/json-schema/jsonSchemaBuilder.d.ts +1 -1
- package/dist/promise/pDefer.d.ts +2 -2
- package/dist/promise/pMap.js +3 -4
- package/dist/promise/pRetry.d.ts +1 -1
- package/dist/promise/pTimeout.d.ts +1 -1
- package/dist/string/pupa.d.ts +2 -2
- package/dist/string/readingTime.d.ts +1 -1
- package/dist/string/stringifyAny.d.ts +6 -0
- package/dist/string/stringifyAny.js +13 -1
- package/dist-esm/error/app.error.js +2 -2
- package/dist-esm/error/assert.js +0 -3
- package/dist-esm/error/error.util.js +26 -11
- package/dist-esm/error/errorMode.js +1 -1
- package/dist-esm/error/http.error.js +2 -2
- package/dist-esm/http/fetcher.js +4 -3
- package/dist-esm/index.js +0 -1
- package/dist-esm/promise/pMap.js +3 -4
- package/dist-esm/string/stringifyAny.js +13 -1
- package/package.json +1 -1
- package/src/decorators/asyncMemo.decorator.ts +2 -2
- package/src/decorators/createPromiseDecorator.ts +4 -4
- package/src/decorators/debounce.ts +2 -2
- package/src/decorators/memo.decorator.ts +2 -2
- package/src/decorators/memo.util.ts +7 -7
- package/src/error/app.error.ts +2 -2
- package/src/error/assert.ts +1 -5
- package/src/error/error.model.ts +10 -0
- package/src/error/error.util.ts +26 -21
- package/src/error/errorMode.ts +1 -1
- package/src/error/http.error.ts +2 -2
- package/src/error/tryCatch.ts +1 -1
- package/src/http/fetcher.ts +5 -8
- package/src/index.ts +0 -1
- package/src/json-schema/jsonSchemaBuilder.ts +1 -1
- package/src/promise/pDefer.ts +2 -2
- package/src/promise/pMap.ts +3 -4
- package/src/promise/pRetry.ts +1 -1
- package/src/promise/pTimeout.ts +1 -1
- package/src/string/pupa.ts +1 -1
- package/src/string/readingTime.ts +1 -1
- package/src/string/stringifyAny.ts +23 -1
- package/dist/promise/AggregatedError.d.ts +0 -10
- package/dist/promise/AggregatedError.js +0 -34
- package/dist-esm/promise/AggregatedError.js +0 -30
- package/src/promise/AggregatedError.ts +0 -36
|
@@ -6,11 +6,11 @@ export interface AsyncMemoOptions {
|
|
|
6
6
|
* Function that creates an instance of `MemoCache`.
|
|
7
7
|
* e.g LRUMemoCache from `@naturalcycles/nodejs-lib`.
|
|
8
8
|
*/
|
|
9
|
-
cacheFactory
|
|
9
|
+
cacheFactory?: () => AsyncMemoCache;
|
|
10
10
|
/**
|
|
11
11
|
* Provide a custom implementation of CacheKey function.
|
|
12
12
|
*/
|
|
13
|
-
cacheKeyFn
|
|
13
|
+
cacheKeyFn?: (args: any[]) => any;
|
|
14
14
|
/**
|
|
15
15
|
* Default true.
|
|
16
16
|
*
|
|
@@ -4,15 +4,15 @@ export interface PromiseDecoratorCfg<RES = any, PARAMS = any> {
|
|
|
4
4
|
* Called BEFORE the original function.
|
|
5
5
|
* If Promise is returned - it will be awaited.
|
|
6
6
|
*/
|
|
7
|
-
beforeFn
|
|
7
|
+
beforeFn?: (r: PromiseDecoratorResp<PARAMS>) => void | Promise<void>;
|
|
8
8
|
/**
|
|
9
9
|
* Called just AFTER the original function.
|
|
10
10
|
* The output of this hook will be passed further,
|
|
11
11
|
* so, pay attention to pass through (or modify) the result.
|
|
12
12
|
*/
|
|
13
|
-
thenFn
|
|
13
|
+
thenFn?: (r: PromiseDecoratorResp<PARAMS> & {
|
|
14
14
|
res: RES;
|
|
15
|
-
})
|
|
15
|
+
}) => RES;
|
|
16
16
|
/**
|
|
17
17
|
* Called on Promise.reject.
|
|
18
18
|
* If `catchFn` is not present - will re-throw the error,
|
|
@@ -20,14 +20,14 @@ export interface PromiseDecoratorCfg<RES = any, PARAMS = any> {
|
|
|
20
20
|
* If `catchFn` is present - it's responsible for handling or re-throwing the error.
|
|
21
21
|
* Whatever `catchFn` returns - passed to the original output.
|
|
22
22
|
*/
|
|
23
|
-
catchFn
|
|
23
|
+
catchFn?: (r: PromiseDecoratorResp<PARAMS> & {
|
|
24
24
|
err: any;
|
|
25
|
-
})
|
|
25
|
+
}) => RES;
|
|
26
26
|
/**
|
|
27
27
|
* Fires AFTER thenFn / catchFn, like a usual Promise.finally().
|
|
28
28
|
* Doesn't have access to neither res nor err (same as Promise.finally).
|
|
29
29
|
*/
|
|
30
|
-
finallyFn
|
|
30
|
+
finallyFn?: (r: PromiseDecoratorResp<PARAMS>) => any;
|
|
31
31
|
}
|
|
32
32
|
export interface PromiseDecoratorResp<PARAMS> {
|
|
33
33
|
decoratorParams: PARAMS;
|
|
@@ -6,11 +6,11 @@ export interface MemoOptions {
|
|
|
6
6
|
* Function that creates an instance of `MemoCache`.
|
|
7
7
|
* e.g LRUMemoCache from `@naturalcycles/nodejs-lib`
|
|
8
8
|
*/
|
|
9
|
-
cacheFactory
|
|
9
|
+
cacheFactory?: () => MemoCache;
|
|
10
10
|
/**
|
|
11
11
|
* Provide a custom implementation of CacheKey function.
|
|
12
12
|
*/
|
|
13
|
-
cacheKeyFn
|
|
13
|
+
cacheKeyFn?: (args: any[]) => any;
|
|
14
14
|
/**
|
|
15
15
|
* Defaults to true.
|
|
16
16
|
* Set to false to skip caching errors.
|
|
@@ -2,14 +2,14 @@ import type { Promisable } from '../typeFest';
|
|
|
2
2
|
export type MemoSerializer = (args: any[]) => any;
|
|
3
3
|
export declare const jsonMemoSerializer: MemoSerializer;
|
|
4
4
|
export interface MemoCache<KEY = any, VALUE = any> {
|
|
5
|
-
has(k: KEY)
|
|
6
|
-
get(k: KEY)
|
|
7
|
-
set(k: KEY, v: VALUE | Error)
|
|
5
|
+
has: (k: KEY) => boolean;
|
|
6
|
+
get: (k: KEY) => VALUE | Error | undefined;
|
|
7
|
+
set: (k: KEY, v: VALUE | Error) => void;
|
|
8
8
|
/**
|
|
9
9
|
* Clear is only called when `.dropCache()` is called.
|
|
10
10
|
* Otherwise the Cache is "persistent" (never cleared).
|
|
11
11
|
*/
|
|
12
|
-
clear()
|
|
12
|
+
clear: () => void;
|
|
13
13
|
}
|
|
14
14
|
export interface AsyncMemoCache<KEY = any, VALUE = any> {
|
|
15
15
|
/**
|
|
@@ -17,13 +17,13 @@ export interface AsyncMemoCache<KEY = any, VALUE = any> {
|
|
|
17
17
|
* This also means that you CANNOT store `undefined` value in the Cache, as it'll be treated as a MISS.
|
|
18
18
|
* You CAN store `null` value instead, it will be treated as a HIT.
|
|
19
19
|
*/
|
|
20
|
-
get(k: KEY)
|
|
21
|
-
set(k: KEY, v: VALUE | Error)
|
|
20
|
+
get: (k: KEY) => Promisable<VALUE | Error | undefined>;
|
|
21
|
+
set: (k: KEY, v: VALUE | Error) => Promisable<void>;
|
|
22
22
|
/**
|
|
23
23
|
* Clear is only called when `.dropCache()` is called.
|
|
24
24
|
* Otherwise the Cache is "persistent" (never cleared).
|
|
25
25
|
*/
|
|
26
|
-
clear()
|
|
26
|
+
clear: () => Promisable<void>;
|
|
27
27
|
}
|
|
28
28
|
export declare class MapMemoCache<KEY = any, VALUE = any> implements MemoCache<KEY, VALUE>, AsyncMemoCache<KEY, VALUE> {
|
|
29
29
|
private m;
|
|
@@ -10,5 +10,5 @@ import type { ErrorData } from './error.model';
|
|
|
10
10
|
*/
|
|
11
11
|
export declare class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
|
|
12
12
|
data: DATA_TYPE;
|
|
13
|
-
constructor(message: string, data?: DATA_TYPE);
|
|
13
|
+
constructor(message: string, data?: DATA_TYPE, opt?: ErrorOptions);
|
|
14
14
|
}
|
package/dist/error/app.error.js
CHANGED
|
@@ -11,8 +11,8 @@ exports.AppError = void 0;
|
|
|
11
11
|
* Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
|
|
12
12
|
*/
|
|
13
13
|
class AppError extends Error {
|
|
14
|
-
constructor(message, data = {}) {
|
|
15
|
-
super(message);
|
|
14
|
+
constructor(message, data = {}, opt) {
|
|
15
|
+
super(message, opt);
|
|
16
16
|
Object.defineProperty(this, 'name', {
|
|
17
17
|
value: this.constructor.name,
|
|
18
18
|
configurable: true,
|
package/dist/error/assert.d.ts
CHANGED
|
@@ -36,5 +36,4 @@ export declare function _assertIsString(v: any, message?: string): asserts v is
|
|
|
36
36
|
export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
|
|
37
37
|
export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
|
|
38
38
|
export declare class AssertionError extends AppError {
|
|
39
|
-
constructor(message: string, data?: ErrorData);
|
|
40
39
|
}
|
package/dist/error/assert.js
CHANGED
|
@@ -103,8 +103,5 @@ function _assertTypeOf(v, expectedType, message) {
|
|
|
103
103
|
}
|
|
104
104
|
exports._assertTypeOf = _assertTypeOf;
|
|
105
105
|
class AssertionError extends app_error_1.AppError {
|
|
106
|
-
constructor(message, data = {}) {
|
|
107
|
-
super(message, data);
|
|
108
|
-
}
|
|
109
106
|
}
|
|
110
107
|
exports.AssertionError = AssertionError;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse,
|
|
1
|
+
import type { ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Class } from '..';
|
|
2
2
|
import { AppError } from '..';
|
|
3
3
|
/**
|
|
4
4
|
* Useful to ensure that error in `catch (err) { ... }`
|
|
@@ -8,15 +8,15 @@ import { AppError } from '..';
|
|
|
8
8
|
*
|
|
9
9
|
* Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
|
|
10
10
|
*/
|
|
11
|
-
export declare function _anyToError<ERROR_TYPE extends Error = Error>(o: any, errorClass?: Class<ERROR_TYPE>, errorData?: ErrorData
|
|
11
|
+
export declare function _anyToError<ERROR_TYPE extends Error = Error>(o: any, errorClass?: Class<ERROR_TYPE>, errorData?: ErrorData): ERROR_TYPE;
|
|
12
12
|
/**
|
|
13
13
|
* Converts "anything" to ErrorObject.
|
|
14
14
|
* Detects if it's HttpErrorResponse, HttpErrorObject, ErrorObject, Error, etc..
|
|
15
15
|
* If object is Error - Error.message will be used.
|
|
16
16
|
* Objects (not Errors) get converted to prettified JSON string (via `_stringifyAny`).
|
|
17
17
|
*/
|
|
18
|
-
export declare function _anyToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(o: any, errorData?: Partial<DATA_TYPE
|
|
19
|
-
export declare function _errorToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(e: AppError<DATA_TYPE> | Error
|
|
18
|
+
export declare function _anyToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(o: any, errorData?: Partial<DATA_TYPE>): ErrorObject<DATA_TYPE>;
|
|
19
|
+
export declare function _errorToErrorObject<DATA_TYPE extends ErrorData = ErrorData>(e: AppError<DATA_TYPE> | Error): ErrorObject<DATA_TYPE>;
|
|
20
20
|
export declare function _errorObjectToAppError<DATA_TYPE extends ErrorData>(o: ErrorObject<DATA_TYPE>): AppError<DATA_TYPE>;
|
|
21
21
|
export declare function _errorObjectToError<DATA_TYPE extends ErrorData, ERROR_TYPE extends Error>(o: ErrorObject<DATA_TYPE>, errorClass?: Class<ERROR_TYPE>): ERROR_TYPE;
|
|
22
22
|
export declare function _isHttpErrorResponse(o: any): o is HttpErrorResponse;
|
package/dist/error/error.util.js
CHANGED
|
@@ -10,14 +10,14 @@ const __1 = require("..");
|
|
|
10
10
|
*
|
|
11
11
|
* Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
|
|
12
12
|
*/
|
|
13
|
-
function _anyToError(o, errorClass = Error, errorData
|
|
13
|
+
function _anyToError(o, errorClass = Error, errorData) {
|
|
14
14
|
let e;
|
|
15
15
|
if (o instanceof errorClass) {
|
|
16
16
|
e = o;
|
|
17
17
|
}
|
|
18
18
|
else {
|
|
19
19
|
// If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
|
|
20
|
-
const errorObject =
|
|
20
|
+
const errorObject = _anyToErrorObject(o);
|
|
21
21
|
e = _errorObjectToError(errorObject, errorClass);
|
|
22
22
|
}
|
|
23
23
|
if (errorData) {
|
|
@@ -36,10 +36,10 @@ exports._anyToError = _anyToError;
|
|
|
36
36
|
* If object is Error - Error.message will be used.
|
|
37
37
|
* Objects (not Errors) get converted to prettified JSON string (via `_stringifyAny`).
|
|
38
38
|
*/
|
|
39
|
-
function _anyToErrorObject(o, errorData
|
|
39
|
+
function _anyToErrorObject(o, errorData) {
|
|
40
40
|
let eo;
|
|
41
41
|
if (o instanceof Error) {
|
|
42
|
-
eo = _errorToErrorObject(o
|
|
42
|
+
eo = _errorToErrorObject(o);
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
45
|
o = (0, __1._jsonParseIfPossible)(o);
|
|
@@ -54,10 +54,7 @@ function _anyToErrorObject(o, errorData, opt) {
|
|
|
54
54
|
// so, fair to return `data: {}` in the end
|
|
55
55
|
// Also we're sure it includes no "error name", e.g no `Error: ...`,
|
|
56
56
|
// so, fair to include `name: 'Error'`
|
|
57
|
-
const message = (0, __1._stringifyAny)(o
|
|
58
|
-
includeErrorData: true,
|
|
59
|
-
...opt,
|
|
60
|
-
});
|
|
57
|
+
const message = (0, __1._stringifyAny)(o);
|
|
61
58
|
eo = {
|
|
62
59
|
name: 'Error',
|
|
63
60
|
message,
|
|
@@ -69,14 +66,15 @@ function _anyToErrorObject(o, errorData, opt) {
|
|
|
69
66
|
return eo;
|
|
70
67
|
}
|
|
71
68
|
exports._anyToErrorObject = _anyToErrorObject;
|
|
72
|
-
function _errorToErrorObject(e
|
|
69
|
+
function _errorToErrorObject(e) {
|
|
73
70
|
const obj = {
|
|
74
71
|
name: e.name,
|
|
75
72
|
message: e.message,
|
|
76
|
-
data: { ...e.data },
|
|
73
|
+
data: { ...e.data },
|
|
74
|
+
stack: e.stack,
|
|
77
75
|
};
|
|
78
|
-
if (
|
|
79
|
-
obj.
|
|
76
|
+
if (e.cause) {
|
|
77
|
+
obj.cause = _anyToErrorObject(e.cause);
|
|
80
78
|
}
|
|
81
79
|
return obj;
|
|
82
80
|
}
|
|
@@ -106,6 +104,14 @@ function _errorObjectToError(o, errorClass = Error) {
|
|
|
106
104
|
value: o.stack,
|
|
107
105
|
});
|
|
108
106
|
}
|
|
107
|
+
if (o.cause) {
|
|
108
|
+
Object.defineProperty(err, 'cause', {
|
|
109
|
+
value: o.cause,
|
|
110
|
+
writable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
enumerable: false,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
109
115
|
return err;
|
|
110
116
|
}
|
|
111
117
|
exports._errorObjectToError = _errorObjectToError;
|
|
@@ -124,9 +130,16 @@ exports._isHttpErrorObject = _isHttpErrorObject;
|
|
|
124
130
|
* Note: any instance of AppError is also automatically an ErrorObject
|
|
125
131
|
*/
|
|
126
132
|
function _isErrorObject(o) {
|
|
127
|
-
return (!!o &&
|
|
133
|
+
return (!!o &&
|
|
134
|
+
typeof o === 'object' &&
|
|
135
|
+
typeof o.name === 'string' &&
|
|
136
|
+
typeof o.message === 'string' &&
|
|
137
|
+
typeof o.data === 'object');
|
|
128
138
|
}
|
|
129
139
|
exports._isErrorObject = _isErrorObject;
|
|
140
|
+
// export function _isErrorLike(o: any): o is ErrorLike {
|
|
141
|
+
// return !!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string'
|
|
142
|
+
// }
|
|
130
143
|
/**
|
|
131
144
|
* Convenience function to safely add properties to Error's `data` object
|
|
132
145
|
* (even if it wasn't previously existing)
|
|
@@ -9,7 +9,7 @@ export declare enum ErrorMode {
|
|
|
9
9
|
*/
|
|
10
10
|
THROW_IMMEDIATELY = "THROW_IMMEDIATELY",
|
|
11
11
|
/**
|
|
12
|
-
* Don't throw on errors, but collect them and throw as
|
|
12
|
+
* Don't throw on errors, but collect them and throw as AggregateError in the end.
|
|
13
13
|
*/
|
|
14
14
|
THROW_AGGREGATED = "THROW_AGGREGATED",
|
|
15
15
|
/**
|
package/dist/error/errorMode.js
CHANGED
|
@@ -13,7 +13,7 @@ var ErrorMode;
|
|
|
13
13
|
*/
|
|
14
14
|
ErrorMode["THROW_IMMEDIATELY"] = "THROW_IMMEDIATELY";
|
|
15
15
|
/**
|
|
16
|
-
* Don't throw on errors, but collect them and throw as
|
|
16
|
+
* Don't throw on errors, but collect them and throw as AggregateError in the end.
|
|
17
17
|
*/
|
|
18
18
|
ErrorMode["THROW_AGGREGATED"] = "THROW_AGGREGATED";
|
|
19
19
|
/**
|
|
@@ -4,5 +4,5 @@ import type { HttpErrorData } from './error.model';
|
|
|
4
4
|
* Base class for HTTP errors - errors that define HTTP error code.
|
|
5
5
|
*/
|
|
6
6
|
export declare class HttpError<DATA_TYPE extends HttpErrorData = HttpErrorData> extends AppError<DATA_TYPE> {
|
|
7
|
-
constructor(message: string, data: DATA_TYPE);
|
|
7
|
+
constructor(message: string, data: DATA_TYPE, opt?: ErrorOptions);
|
|
8
8
|
}
|
package/dist/error/http.error.js
CHANGED
|
@@ -6,8 +6,8 @@ const app_error_1 = require("./app.error");
|
|
|
6
6
|
* Base class for HTTP errors - errors that define HTTP error code.
|
|
7
7
|
*/
|
|
8
8
|
class HttpError extends app_error_1.AppError {
|
|
9
|
-
constructor(message, data) {
|
|
10
|
-
super(message, data);
|
|
9
|
+
constructor(message, data, opt) {
|
|
10
|
+
super(message, data, opt);
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
exports.HttpError = HttpError;
|
package/dist/error/tryCatch.d.ts
CHANGED
package/dist/http/fetcher.js
CHANGED
|
@@ -151,11 +151,12 @@ class Fetcher {
|
|
|
151
151
|
res.body = JSON.parse(text, req.jsonReviver);
|
|
152
152
|
}
|
|
153
153
|
catch (err) {
|
|
154
|
-
|
|
155
|
-
res.err =
|
|
154
|
+
const { message } = (0, error_util_1._anyToError)(err);
|
|
155
|
+
res.err = new http_error_1.HttpError([signature, message].join('\n'), {
|
|
156
156
|
httpStatusCode: 0,
|
|
157
157
|
url: req.url,
|
|
158
|
-
})
|
|
158
|
+
});
|
|
159
|
+
res.ok = false;
|
|
159
160
|
}
|
|
160
161
|
}
|
|
161
162
|
else {
|
package/dist/index.d.ts
CHANGED
|
@@ -37,7 +37,6 @@ export * from './object/deepEquals';
|
|
|
37
37
|
export * from './object/object.util';
|
|
38
38
|
export * from './object/sortObject';
|
|
39
39
|
export * from './object/sortObjectDeep';
|
|
40
|
-
export * from './promise/AggregatedError';
|
|
41
40
|
export * from './promise/pDefer';
|
|
42
41
|
export * from './promise/pDelay';
|
|
43
42
|
export * from './promise/pFilter';
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,6 @@ tslib_1.__exportStar(require("./object/deepEquals"), exports);
|
|
|
41
41
|
tslib_1.__exportStar(require("./object/object.util"), exports);
|
|
42
42
|
tslib_1.__exportStar(require("./object/sortObject"), exports);
|
|
43
43
|
tslib_1.__exportStar(require("./object/sortObjectDeep"), exports);
|
|
44
|
-
tslib_1.__exportStar(require("./promise/AggregatedError"), exports);
|
|
45
44
|
tslib_1.__exportStar(require("./promise/pDefer"), exports);
|
|
46
45
|
tslib_1.__exportStar(require("./promise/pDelay"), exports);
|
|
47
46
|
tslib_1.__exportStar(require("./promise/pFilter"), exports);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import type { BaseDBEntity, JsonSchemaAllOf, JsonSchemaArray, JsonSchemaOneOf, JsonSchemaTuple, SavedDBEntity, AnyObject } from '../index';
|
|
3
3
|
import type { JsonSchema, JsonSchemaAny, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaRef, JsonSchemaString } from './jsonSchema.model';
|
|
4
4
|
export interface JsonSchemaBuilder<T = unknown> {
|
|
5
|
-
build()
|
|
5
|
+
build: () => JsonSchema<T>;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
8
|
* Fluent (chainable) API to manually create Json Schemas.
|
package/dist/promise/pDefer.d.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* Similar to Deferred object, which is also a promise itself (instead of deferred.promise).
|
|
3
3
|
*/
|
|
4
4
|
export interface DeferredPromise<T = void> extends Promise<T> {
|
|
5
|
-
resolve(a?: T)
|
|
6
|
-
reject(e?: Error)
|
|
5
|
+
resolve: (a?: T) => void;
|
|
6
|
+
reject: (e?: Error) => void;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Returns DeferredPromise - a Promise that has .resolve() and .reject() methods.
|
package/dist/promise/pMap.js
CHANGED
|
@@ -10,7 +10,6 @@ Improvements:
|
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.pMap = void 0;
|
|
12
12
|
const __1 = require("..");
|
|
13
|
-
const AggregatedError_1 = require("./AggregatedError");
|
|
14
13
|
/**
|
|
15
14
|
* Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled,
|
|
16
15
|
* or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned
|
|
@@ -70,7 +69,7 @@ async function pMap(iterable, mapper, opt = {}) {
|
|
|
70
69
|
}
|
|
71
70
|
}
|
|
72
71
|
if (errors.length) {
|
|
73
|
-
throw new
|
|
72
|
+
throw new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`);
|
|
74
73
|
}
|
|
75
74
|
return ret;
|
|
76
75
|
}
|
|
@@ -90,7 +89,7 @@ async function pMap(iterable, mapper, opt = {}) {
|
|
|
90
89
|
}
|
|
91
90
|
});
|
|
92
91
|
if (errors.length) {
|
|
93
|
-
throw new
|
|
92
|
+
throw new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`);
|
|
94
93
|
}
|
|
95
94
|
return ret;
|
|
96
95
|
}
|
|
@@ -106,7 +105,7 @@ async function pMap(iterable, mapper, opt = {}) {
|
|
|
106
105
|
isSettled = true;
|
|
107
106
|
const r = ret.filter(r => r !== __1.SKIP);
|
|
108
107
|
if (errors.length) {
|
|
109
|
-
reject(new
|
|
108
|
+
reject(new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`));
|
|
110
109
|
}
|
|
111
110
|
else {
|
|
112
111
|
resolve(r);
|
package/dist/promise/pRetry.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ export interface PRetryOptions {
|
|
|
35
35
|
*
|
|
36
36
|
* @default () => true
|
|
37
37
|
*/
|
|
38
|
-
predicate
|
|
38
|
+
predicate?: (err: Error, attempt: number, maxAttempts: number) => boolean;
|
|
39
39
|
/**
|
|
40
40
|
* Log the first attempt (which is not a "retry" yet).
|
|
41
41
|
*
|
|
@@ -20,7 +20,7 @@ export interface PTimeoutOptions {
|
|
|
20
20
|
* err (which is TimeoutError) is passed as an argument for convenience, so it can
|
|
21
21
|
* be logged or such. You don't have to consume it in any way though.
|
|
22
22
|
*/
|
|
23
|
-
onTimeout
|
|
23
|
+
onTimeout?: (err: TimeoutError) => any;
|
|
24
24
|
/**
|
|
25
25
|
* Defaults to true.
|
|
26
26
|
* If true - preserves the stack trace in case of a Timeout (usually - very useful!).
|
package/dist/string/pupa.d.ts
CHANGED
|
@@ -11,10 +11,10 @@ export interface PupaOptions {
|
|
|
11
11
|
/**
|
|
12
12
|
* Performs arbitrary operation for each interpolation. If the returned value was `undefined`, it behaves differently depending on the `ignoreMissing` option. Otherwise, the returned value will be interpolated into a string (and escaped when double-braced) and embedded into the template.
|
|
13
13
|
*/
|
|
14
|
-
transform
|
|
14
|
+
transform?: (data: {
|
|
15
15
|
value: any;
|
|
16
16
|
key: string;
|
|
17
|
-
})
|
|
17
|
+
}) => unknown;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* API: https://github.com/sindresorhus/pupa
|
|
@@ -4,7 +4,7 @@ export interface ReadingTimeOptions {
|
|
|
4
4
|
* A function that returns a boolean value depending on if a character is considered as a word bound.
|
|
5
5
|
* Default: spaces, new lines and tabs
|
|
6
6
|
*/
|
|
7
|
-
wordBound
|
|
7
|
+
wordBound?: (char: string) => boolean;
|
|
8
8
|
/**
|
|
9
9
|
* Default 200
|
|
10
10
|
*/
|
|
@@ -32,6 +32,12 @@ export interface StringifyAnyOptions {
|
|
|
32
32
|
* @default false
|
|
33
33
|
*/
|
|
34
34
|
includeErrorStack?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Set to false to skip including Error.cause.
|
|
37
|
+
*
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
includeErrorCause?: boolean;
|
|
35
41
|
/**
|
|
36
42
|
* Allows to pass custom "stringify function".
|
|
37
43
|
* E.g in Node.js you can pass `util.inspect` instead.
|
|
@@ -4,6 +4,7 @@ exports._stringifyAny = exports.setGlobalStringifyFunction = void 0;
|
|
|
4
4
|
const error_util_1 = require("../error/error.util");
|
|
5
5
|
const json_util_1 = require("./json.util");
|
|
6
6
|
const safeJsonStringify_1 = require("./safeJsonStringify");
|
|
7
|
+
const supportsAggregateError = typeof globalThis.AggregateError === 'function';
|
|
7
8
|
let globalStringifyFunction = safeJsonStringify_1._safeJsonStringify;
|
|
8
9
|
/**
|
|
9
10
|
* Allows to set Global "stringifyFunction" that will be used to "pretty-print" objects
|
|
@@ -59,6 +60,7 @@ function _stringifyAny(obj, opt = {}) {
|
|
|
59
60
|
return _stringifyAny(obj.error, opt);
|
|
60
61
|
}
|
|
61
62
|
if (obj instanceof Error || (0, error_util_1._isErrorObject)(obj)) {
|
|
63
|
+
const { includeErrorCause = true } = opt;
|
|
62
64
|
//
|
|
63
65
|
// Error or ErrorObject
|
|
64
66
|
//
|
|
@@ -67,7 +69,7 @@ function _stringifyAny(obj, opt = {}) {
|
|
|
67
69
|
// if (obj?.name === 'Error') {
|
|
68
70
|
// s = obj.message
|
|
69
71
|
// }
|
|
70
|
-
s = [obj.name, obj.message].join(': ');
|
|
72
|
+
s = [obj.name, obj.message].filter(Boolean).join(': ');
|
|
71
73
|
if (opt.includeErrorStack && obj.stack) {
|
|
72
74
|
// Here we're using the previously-generated "title line" (e.g "Error: some_message"),
|
|
73
75
|
// concatenating it with the Stack (but without the title line of the Stack)
|
|
@@ -92,6 +94,16 @@ function _stringifyAny(obj, opt = {}) {
|
|
|
92
94
|
// Error that has no `data`, but has `code` property
|
|
93
95
|
s = [s, `code: ${obj.code}`].join('\n');
|
|
94
96
|
}
|
|
97
|
+
if (supportsAggregateError && obj instanceof AggregateError && obj.errors.length) {
|
|
98
|
+
s = [
|
|
99
|
+
s,
|
|
100
|
+
`${obj.errors.length} error(s):`,
|
|
101
|
+
...obj.errors.map((err, i) => `${i + 1}. ${_stringifyAny(err, opt)}`),
|
|
102
|
+
].join('\n');
|
|
103
|
+
}
|
|
104
|
+
if (obj.cause && includeErrorCause) {
|
|
105
|
+
s = s + '\ncaused by: ' + _stringifyAny(obj.cause, opt);
|
|
106
|
+
}
|
|
95
107
|
}
|
|
96
108
|
else if (typeof obj === 'string') {
|
|
97
109
|
//
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
* Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
|
|
9
9
|
*/
|
|
10
10
|
export class AppError extends Error {
|
|
11
|
-
constructor(message, data = {}) {
|
|
12
|
-
super(message);
|
|
11
|
+
constructor(message, data = {}, opt) {
|
|
12
|
+
super(message, opt);
|
|
13
13
|
Object.defineProperty(this, 'name', {
|
|
14
14
|
value: this.constructor.name,
|
|
15
15
|
configurable: true,
|
package/dist-esm/error/assert.js
CHANGED
|
@@ -7,14 +7,14 @@ import { AppError, _jsonParseIfPossible, _stringifyAny } from '..';
|
|
|
7
7
|
*
|
|
8
8
|
* Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
|
|
9
9
|
*/
|
|
10
|
-
export function _anyToError(o, errorClass = Error, errorData
|
|
10
|
+
export function _anyToError(o, errorClass = Error, errorData) {
|
|
11
11
|
let e;
|
|
12
12
|
if (o instanceof errorClass) {
|
|
13
13
|
e = o;
|
|
14
14
|
}
|
|
15
15
|
else {
|
|
16
16
|
// If it's an instance of Error, but ErrorClass is something else (e.g AppError) - it'll be "repacked" into AppError
|
|
17
|
-
const errorObject =
|
|
17
|
+
const errorObject = _anyToErrorObject(o);
|
|
18
18
|
e = _errorObjectToError(errorObject, errorClass);
|
|
19
19
|
}
|
|
20
20
|
if (errorData) {
|
|
@@ -29,11 +29,10 @@ export function _anyToError(o, errorClass = Error, errorData, opt) {
|
|
|
29
29
|
* If object is Error - Error.message will be used.
|
|
30
30
|
* Objects (not Errors) get converted to prettified JSON string (via `_stringifyAny`).
|
|
31
31
|
*/
|
|
32
|
-
export function _anyToErrorObject(o, errorData
|
|
33
|
-
var _a;
|
|
32
|
+
export function _anyToErrorObject(o, errorData) {
|
|
34
33
|
let eo;
|
|
35
34
|
if (o instanceof Error) {
|
|
36
|
-
eo = _errorToErrorObject(o
|
|
35
|
+
eo = _errorToErrorObject(o);
|
|
37
36
|
}
|
|
38
37
|
else {
|
|
39
38
|
o = _jsonParseIfPossible(o);
|
|
@@ -48,7 +47,7 @@ export function _anyToErrorObject(o, errorData, opt) {
|
|
|
48
47
|
// so, fair to return `data: {}` in the end
|
|
49
48
|
// Also we're sure it includes no "error name", e.g no `Error: ...`,
|
|
50
49
|
// so, fair to include `name: 'Error'`
|
|
51
|
-
const message = _stringifyAny(o
|
|
50
|
+
const message = _stringifyAny(o);
|
|
52
51
|
eo = {
|
|
53
52
|
name: 'Error',
|
|
54
53
|
message,
|
|
@@ -59,14 +58,15 @@ export function _anyToErrorObject(o, errorData, opt) {
|
|
|
59
58
|
Object.assign(eo.data, errorData);
|
|
60
59
|
return eo;
|
|
61
60
|
}
|
|
62
|
-
export function _errorToErrorObject(e
|
|
61
|
+
export function _errorToErrorObject(e) {
|
|
63
62
|
const obj = {
|
|
64
63
|
name: e.name,
|
|
65
64
|
message: e.message,
|
|
66
|
-
data: Object.assign({}, e.data),
|
|
65
|
+
data: Object.assign({}, e.data),
|
|
66
|
+
stack: e.stack,
|
|
67
67
|
};
|
|
68
|
-
if (
|
|
69
|
-
obj.
|
|
68
|
+
if (e.cause) {
|
|
69
|
+
obj.cause = _anyToErrorObject(e.cause);
|
|
70
70
|
}
|
|
71
71
|
return obj;
|
|
72
72
|
}
|
|
@@ -94,6 +94,14 @@ export function _errorObjectToError(o, errorClass = Error) {
|
|
|
94
94
|
value: o.stack,
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
|
+
if (o.cause) {
|
|
98
|
+
Object.defineProperty(err, 'cause', {
|
|
99
|
+
value: o.cause,
|
|
100
|
+
writable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
enumerable: false,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
97
105
|
return err;
|
|
98
106
|
}
|
|
99
107
|
export function _isHttpErrorResponse(o) {
|
|
@@ -110,8 +118,15 @@ export function _isHttpErrorObject(o) {
|
|
|
110
118
|
* Note: any instance of AppError is also automatically an ErrorObject
|
|
111
119
|
*/
|
|
112
120
|
export function _isErrorObject(o) {
|
|
113
|
-
return (!!o &&
|
|
121
|
+
return (!!o &&
|
|
122
|
+
typeof o === 'object' &&
|
|
123
|
+
typeof o.name === 'string' &&
|
|
124
|
+
typeof o.message === 'string' &&
|
|
125
|
+
typeof o.data === 'object');
|
|
114
126
|
}
|
|
127
|
+
// export function _isErrorLike(o: any): o is ErrorLike {
|
|
128
|
+
// return !!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string'
|
|
129
|
+
// }
|
|
115
130
|
/**
|
|
116
131
|
* Convenience function to safely add properties to Error's `data` object
|
|
117
132
|
* (even if it wasn't previously existing)
|
|
@@ -10,7 +10,7 @@ export var ErrorMode;
|
|
|
10
10
|
*/
|
|
11
11
|
ErrorMode["THROW_IMMEDIATELY"] = "THROW_IMMEDIATELY";
|
|
12
12
|
/**
|
|
13
|
-
* Don't throw on errors, but collect them and throw as
|
|
13
|
+
* Don't throw on errors, but collect them and throw as AggregateError in the end.
|
|
14
14
|
*/
|
|
15
15
|
ErrorMode["THROW_AGGREGATED"] = "THROW_AGGREGATED";
|
|
16
16
|
/**
|