@naturalcycles/js-lib 14.241.2 → 14.242.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/datetime/localTime.js +4 -2
- package/dist/error/assert.d.ts +3 -2
- package/dist/error/assert.js +6 -14
- package/dist/error/error.model.d.ts +6 -1
- package/dist/error/error.util.d.ts +1 -1
- package/dist/error/error.util.js +1 -1
- package/dist/http/fetcher.js +1 -1
- package/dist/semver.js +0 -1
- package/dist/string/stringify.js +1 -1
- package/dist-esm/datetime/localTime.js +4 -2
- package/dist-esm/error/assert.js +6 -14
- package/dist-esm/error/error.util.js +1 -1
- package/dist-esm/http/fetcher.js +1 -1
- package/dist-esm/semver.js +0 -1
- package/dist-esm/string/stringify.js +1 -1
- package/package.json +1 -1
- package/src/datetime/localTime.ts +3 -1
- package/src/error/assert.ts +5 -14
- package/src/error/error.model.ts +6 -1
- package/src/error/error.util.ts +1 -1
- package/src/http/fetcher.ts +1 -1
- package/src/semver.ts +0 -1
- package/src/string/stringify.ts +1 -1
|
@@ -601,8 +601,6 @@ class LocalTimeFactory {
|
|
|
601
601
|
* Returns null if invalid
|
|
602
602
|
*/
|
|
603
603
|
parseOrNull(d) {
|
|
604
|
-
if (!d)
|
|
605
|
-
return null;
|
|
606
604
|
if (d instanceof LocalTime)
|
|
607
605
|
return d;
|
|
608
606
|
let date;
|
|
@@ -612,6 +610,10 @@ class LocalTimeFactory {
|
|
|
612
610
|
else if (typeof d === 'number') {
|
|
613
611
|
date = new Date(d * 1000);
|
|
614
612
|
}
|
|
613
|
+
else if (!d) {
|
|
614
|
+
// This check is after checking the number, to support `0`
|
|
615
|
+
return null;
|
|
616
|
+
}
|
|
615
617
|
else if (typeof d !== 'string') {
|
|
616
618
|
// unexpected type, e.g Function or something
|
|
617
619
|
return null;
|
package/dist/error/assert.d.ts
CHANGED
|
@@ -8,12 +8,13 @@ import { Class } from '..';
|
|
|
8
8
|
* vice-versa - for completely unexpected and 100% buggy "should never happen" cases.
|
|
9
9
|
*
|
|
10
10
|
* It'll result in http 500 on the server (cause that's the right code for "unexpected" errors).
|
|
11
|
-
* Pass {
|
|
11
|
+
* Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib).
|
|
12
12
|
*
|
|
13
13
|
* API is similar to Node's assert(), except:
|
|
14
14
|
* 1. Throws js-lib's AppError
|
|
15
15
|
* 2. Has a default message, if not provided
|
|
16
|
-
*
|
|
16
|
+
*
|
|
17
|
+
* Since 2024-07-10 it no longer sets `userFriendly: true` by default.
|
|
17
18
|
*/
|
|
18
19
|
export declare function _assert(condition: any, // will be evaluated as Boolean
|
|
19
20
|
message?: string, errorData?: ErrorData): asserts condition;
|
package/dist/error/assert.js
CHANGED
|
@@ -18,18 +18,18 @@ const __1 = require("..");
|
|
|
18
18
|
* vice-versa - for completely unexpected and 100% buggy "should never happen" cases.
|
|
19
19
|
*
|
|
20
20
|
* It'll result in http 500 on the server (cause that's the right code for "unexpected" errors).
|
|
21
|
-
* Pass {
|
|
21
|
+
* Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib).
|
|
22
22
|
*
|
|
23
23
|
* API is similar to Node's assert(), except:
|
|
24
24
|
* 1. Throws js-lib's AppError
|
|
25
25
|
* 2. Has a default message, if not provided
|
|
26
|
-
*
|
|
26
|
+
*
|
|
27
|
+
* Since 2024-07-10 it no longer sets `userFriendly: true` by default.
|
|
27
28
|
*/
|
|
28
29
|
function _assert(condition, // will be evaluated as Boolean
|
|
29
30
|
message, errorData) {
|
|
30
31
|
if (!condition) {
|
|
31
32
|
throw new __1.AssertionError(message || 'condition failed', {
|
|
32
|
-
userFriendly: true,
|
|
33
33
|
...errorData,
|
|
34
34
|
});
|
|
35
35
|
}
|
|
@@ -47,7 +47,6 @@ function _assertEquals(actual, expected, message, errorData) {
|
|
|
47
47
|
.filter(Boolean)
|
|
48
48
|
.join('\n');
|
|
49
49
|
throw new __1.AssertionError(msg, {
|
|
50
|
-
userFriendly: true,
|
|
51
50
|
...errorData,
|
|
52
51
|
});
|
|
53
52
|
}
|
|
@@ -65,16 +64,13 @@ function _assertDeepEquals(actual, expected, message, errorData) {
|
|
|
65
64
|
.filter(Boolean)
|
|
66
65
|
.join('\n');
|
|
67
66
|
throw new __1.AssertionError(msg, {
|
|
68
|
-
userFriendly: true,
|
|
69
67
|
...errorData,
|
|
70
68
|
});
|
|
71
69
|
}
|
|
72
70
|
}
|
|
73
71
|
function _assertIsError(err, errorClass = Error) {
|
|
74
72
|
if (!(err instanceof errorClass)) {
|
|
75
|
-
throw new __1.AssertionError(`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}
|
|
76
|
-
userFriendly: true,
|
|
77
|
-
});
|
|
73
|
+
throw new __1.AssertionError(`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}`);
|
|
78
74
|
}
|
|
79
75
|
}
|
|
80
76
|
/**
|
|
@@ -90,9 +86,7 @@ function _assertErrorClassOrRethrow(err, errorClass) {
|
|
|
90
86
|
}
|
|
91
87
|
function _assertIsErrorObject(obj) {
|
|
92
88
|
if (!(0, __1._isErrorObject)(obj)) {
|
|
93
|
-
throw new __1.AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}
|
|
94
|
-
userFriendly: true,
|
|
95
|
-
});
|
|
89
|
+
throw new __1.AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}`);
|
|
96
90
|
}
|
|
97
91
|
}
|
|
98
92
|
function _assertIsString(v, message) {
|
|
@@ -104,8 +98,6 @@ function _assertIsNumber(v, message) {
|
|
|
104
98
|
function _assertTypeOf(v, expectedType, message) {
|
|
105
99
|
if (typeof v !== expectedType) {
|
|
106
100
|
const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`;
|
|
107
|
-
throw new __1.AssertionError(msg
|
|
108
|
-
userFriendly: true,
|
|
109
|
-
});
|
|
101
|
+
throw new __1.AssertionError(msg);
|
|
110
102
|
}
|
|
111
103
|
}
|
|
@@ -10,7 +10,12 @@ export interface ErrorData {
|
|
|
10
10
|
*/
|
|
11
11
|
code?: string;
|
|
12
12
|
/**
|
|
13
|
-
* If error.message is user-friendly (can be shown to the user "as is")
|
|
13
|
+
* If error.message is user-friendly (can be shown to the user "as is").
|
|
14
|
+
*
|
|
15
|
+
* This is a hint to the Client on how it should render the error message.
|
|
16
|
+
* userFriendly true means it can be rendered "as is".
|
|
17
|
+
* userFriendly false (or undefined) means the message is not guaranteed to be user-friendly,
|
|
18
|
+
* so client can resort to more generic "Oops, something went wrong" message.
|
|
14
19
|
*/
|
|
15
20
|
userFriendly?: boolean;
|
|
16
21
|
/**
|
package/dist/error/error.util.js
CHANGED
package/dist/http/fetcher.js
CHANGED
|
@@ -339,7 +339,7 @@ class Fetcher {
|
|
|
339
339
|
};
|
|
340
340
|
let responseStatusCode = res.fetchResponse?.status || 0;
|
|
341
341
|
if (res.statusFamily === 2) {
|
|
342
|
-
// important to reset
|
|
342
|
+
// important to reset responseStatusCode to 0 in this case, as status 2xx can be misleading
|
|
343
343
|
res.statusFamily = undefined;
|
|
344
344
|
res.statusCode = undefined;
|
|
345
345
|
responseStatusCode = 0;
|
package/dist/semver.js
CHANGED
package/dist/string/stringify.js
CHANGED
|
@@ -71,7 +71,7 @@ function _stringify(obj, opt = {}) {
|
|
|
71
71
|
// }
|
|
72
72
|
// if (_isErrorObject(obj) && _isHttpErrorObject(obj)) {
|
|
73
73
|
// // Printing (0) to avoid ambiguity
|
|
74
|
-
// s = `${obj.name}(${obj.data.
|
|
74
|
+
// s = `${obj.name}(${obj.data.backendResponseStatusCode}): ${obj.message}`
|
|
75
75
|
// }
|
|
76
76
|
s = [obj.name, obj.message].filter(Boolean).join(': ');
|
|
77
77
|
if (typeof obj.code === 'string') {
|
|
@@ -596,8 +596,6 @@ class LocalTimeFactory {
|
|
|
596
596
|
* Returns null if invalid
|
|
597
597
|
*/
|
|
598
598
|
parseOrNull(d) {
|
|
599
|
-
if (!d)
|
|
600
|
-
return null;
|
|
601
599
|
if (d instanceof LocalTime)
|
|
602
600
|
return d;
|
|
603
601
|
let date;
|
|
@@ -607,6 +605,10 @@ class LocalTimeFactory {
|
|
|
607
605
|
else if (typeof d === 'number') {
|
|
608
606
|
date = new Date(d * 1000);
|
|
609
607
|
}
|
|
608
|
+
else if (!d) {
|
|
609
|
+
// This check is after checking the number, to support `0`
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
610
612
|
else if (typeof d !== 'string') {
|
|
611
613
|
// unexpected type, e.g Function or something
|
|
612
614
|
return null;
|
package/dist-esm/error/assert.js
CHANGED
|
@@ -7,18 +7,18 @@ import { _deepEquals, _isErrorObject, _stringify, AssertionError } from '..';
|
|
|
7
7
|
* vice-versa - for completely unexpected and 100% buggy "should never happen" cases.
|
|
8
8
|
*
|
|
9
9
|
* It'll result in http 500 on the server (cause that's the right code for "unexpected" errors).
|
|
10
|
-
* Pass {
|
|
10
|
+
* Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib).
|
|
11
11
|
*
|
|
12
12
|
* API is similar to Node's assert(), except:
|
|
13
13
|
* 1. Throws js-lib's AppError
|
|
14
14
|
* 2. Has a default message, if not provided
|
|
15
|
-
*
|
|
15
|
+
*
|
|
16
|
+
* Since 2024-07-10 it no longer sets `userFriendly: true` by default.
|
|
16
17
|
*/
|
|
17
18
|
export function _assert(condition, // will be evaluated as Boolean
|
|
18
19
|
message, errorData) {
|
|
19
20
|
if (!condition) {
|
|
20
21
|
throw new AssertionError(message || 'condition failed', {
|
|
21
|
-
userFriendly: true,
|
|
22
22
|
...errorData,
|
|
23
23
|
});
|
|
24
24
|
}
|
|
@@ -36,7 +36,6 @@ export function _assertEquals(actual, expected, message, errorData) {
|
|
|
36
36
|
.filter(Boolean)
|
|
37
37
|
.join('\n');
|
|
38
38
|
throw new AssertionError(msg, {
|
|
39
|
-
userFriendly: true,
|
|
40
39
|
...errorData,
|
|
41
40
|
});
|
|
42
41
|
}
|
|
@@ -54,16 +53,13 @@ export function _assertDeepEquals(actual, expected, message, errorData) {
|
|
|
54
53
|
.filter(Boolean)
|
|
55
54
|
.join('\n');
|
|
56
55
|
throw new AssertionError(msg, {
|
|
57
|
-
userFriendly: true,
|
|
58
56
|
...errorData,
|
|
59
57
|
});
|
|
60
58
|
}
|
|
61
59
|
}
|
|
62
60
|
export function _assertIsError(err, errorClass = Error) {
|
|
63
61
|
if (!(err instanceof errorClass)) {
|
|
64
|
-
throw new AssertionError(`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}
|
|
65
|
-
userFriendly: true,
|
|
66
|
-
});
|
|
62
|
+
throw new AssertionError(`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}`);
|
|
67
63
|
}
|
|
68
64
|
}
|
|
69
65
|
/**
|
|
@@ -79,9 +75,7 @@ export function _assertErrorClassOrRethrow(err, errorClass) {
|
|
|
79
75
|
}
|
|
80
76
|
export function _assertIsErrorObject(obj) {
|
|
81
77
|
if (!_isErrorObject(obj)) {
|
|
82
|
-
throw new AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}
|
|
83
|
-
userFriendly: true,
|
|
84
|
-
});
|
|
78
|
+
throw new AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}`);
|
|
85
79
|
}
|
|
86
80
|
}
|
|
87
81
|
export function _assertIsString(v, message) {
|
|
@@ -93,8 +87,6 @@ export function _assertIsNumber(v, message) {
|
|
|
93
87
|
export function _assertTypeOf(v, expectedType, message) {
|
|
94
88
|
if (typeof v !== expectedType) {
|
|
95
89
|
const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`;
|
|
96
|
-
throw new AssertionError(msg
|
|
97
|
-
userFriendly: true,
|
|
98
|
-
});
|
|
90
|
+
throw new AssertionError(msg);
|
|
99
91
|
}
|
|
100
92
|
}
|
package/dist-esm/http/fetcher.js
CHANGED
|
@@ -331,7 +331,7 @@ export class Fetcher {
|
|
|
331
331
|
});
|
|
332
332
|
let responseStatusCode = res.fetchResponse?.status || 0;
|
|
333
333
|
if (res.statusFamily === 2) {
|
|
334
|
-
// important to reset
|
|
334
|
+
// important to reset responseStatusCode to 0 in this case, as status 2xx can be misleading
|
|
335
335
|
res.statusFamily = undefined;
|
|
336
336
|
res.statusCode = undefined;
|
|
337
337
|
responseStatusCode = 0;
|
package/dist-esm/semver.js
CHANGED
|
@@ -67,7 +67,7 @@ export function _stringify(obj, opt = {}) {
|
|
|
67
67
|
// }
|
|
68
68
|
// if (_isErrorObject(obj) && _isHttpErrorObject(obj)) {
|
|
69
69
|
// // Printing (0) to avoid ambiguity
|
|
70
|
-
// s = `${obj.name}(${obj.data.
|
|
70
|
+
// s = `${obj.name}(${obj.data.backendResponseStatusCode}): ${obj.message}`
|
|
71
71
|
// }
|
|
72
72
|
s = [obj.name, obj.message].filter(Boolean).join(': ');
|
|
73
73
|
if (typeof obj.code === 'string') {
|
package/package.json
CHANGED
|
@@ -714,7 +714,6 @@ class LocalTimeFactory {
|
|
|
714
714
|
* Returns null if invalid
|
|
715
715
|
*/
|
|
716
716
|
parseOrNull(d: LocalTimeInputNullable): LocalTime | null {
|
|
717
|
-
if (!d) return null
|
|
718
717
|
if (d instanceof LocalTime) return d
|
|
719
718
|
|
|
720
719
|
let date
|
|
@@ -723,6 +722,9 @@ class LocalTimeFactory {
|
|
|
723
722
|
date = d
|
|
724
723
|
} else if (typeof d === 'number') {
|
|
725
724
|
date = new Date(d * 1000)
|
|
725
|
+
} else if (!d) {
|
|
726
|
+
// This check is after checking the number, to support `0`
|
|
727
|
+
return null
|
|
726
728
|
} else if (typeof (d as any) !== 'string') {
|
|
727
729
|
// unexpected type, e.g Function or something
|
|
728
730
|
return null
|
package/src/error/assert.ts
CHANGED
|
@@ -9,12 +9,13 @@ import { _deepEquals, _isErrorObject, _stringify, AssertionError, Class } from '
|
|
|
9
9
|
* vice-versa - for completely unexpected and 100% buggy "should never happen" cases.
|
|
10
10
|
*
|
|
11
11
|
* It'll result in http 500 on the server (cause that's the right code for "unexpected" errors).
|
|
12
|
-
* Pass {
|
|
12
|
+
* Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib).
|
|
13
13
|
*
|
|
14
14
|
* API is similar to Node's assert(), except:
|
|
15
15
|
* 1. Throws js-lib's AppError
|
|
16
16
|
* 2. Has a default message, if not provided
|
|
17
|
-
*
|
|
17
|
+
*
|
|
18
|
+
* Since 2024-07-10 it no longer sets `userFriendly: true` by default.
|
|
18
19
|
*/
|
|
19
20
|
export function _assert(
|
|
20
21
|
condition: any, // will be evaluated as Boolean
|
|
@@ -23,7 +24,6 @@ export function _assert(
|
|
|
23
24
|
): asserts condition {
|
|
24
25
|
if (!condition) {
|
|
25
26
|
throw new AssertionError(message || 'condition failed', {
|
|
26
|
-
userFriendly: true,
|
|
27
27
|
...errorData,
|
|
28
28
|
})
|
|
29
29
|
}
|
|
@@ -49,7 +49,6 @@ export function _assertEquals<T>(
|
|
|
49
49
|
.join('\n')
|
|
50
50
|
|
|
51
51
|
throw new AssertionError(msg, {
|
|
52
|
-
userFriendly: true,
|
|
53
52
|
...errorData,
|
|
54
53
|
})
|
|
55
54
|
}
|
|
@@ -75,7 +74,6 @@ export function _assertDeepEquals<T>(
|
|
|
75
74
|
.join('\n')
|
|
76
75
|
|
|
77
76
|
throw new AssertionError(msg, {
|
|
78
|
-
userFriendly: true,
|
|
79
77
|
...errorData,
|
|
80
78
|
})
|
|
81
79
|
}
|
|
@@ -88,9 +86,6 @@ export function _assertIsError<ERR extends Error = Error>(
|
|
|
88
86
|
if (!(err instanceof errorClass)) {
|
|
89
87
|
throw new AssertionError(
|
|
90
88
|
`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}`,
|
|
91
|
-
{
|
|
92
|
-
userFriendly: true,
|
|
93
|
-
},
|
|
94
89
|
)
|
|
95
90
|
}
|
|
96
91
|
}
|
|
@@ -114,9 +109,7 @@ export function _assertIsErrorObject<DATA_TYPE extends ErrorData = ErrorData>(
|
|
|
114
109
|
obj: any,
|
|
115
110
|
): asserts obj is ErrorObject<DATA_TYPE> {
|
|
116
111
|
if (!_isErrorObject(obj)) {
|
|
117
|
-
throw new AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}
|
|
118
|
-
userFriendly: true,
|
|
119
|
-
})
|
|
112
|
+
throw new AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}`)
|
|
120
113
|
}
|
|
121
114
|
}
|
|
122
115
|
|
|
@@ -131,8 +124,6 @@ export function _assertIsNumber(v: any, message?: string): asserts v is number {
|
|
|
131
124
|
export function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T {
|
|
132
125
|
if (typeof v !== expectedType) {
|
|
133
126
|
const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`
|
|
134
|
-
throw new AssertionError(msg
|
|
135
|
-
userFriendly: true,
|
|
136
|
-
})
|
|
127
|
+
throw new AssertionError(msg)
|
|
137
128
|
}
|
|
138
129
|
}
|
package/src/error/error.model.ts
CHANGED
|
@@ -12,7 +12,12 @@ export interface ErrorData {
|
|
|
12
12
|
code?: string
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* If error.message is user-friendly (can be shown to the user "as is")
|
|
15
|
+
* If error.message is user-friendly (can be shown to the user "as is").
|
|
16
|
+
*
|
|
17
|
+
* This is a hint to the Client on how it should render the error message.
|
|
18
|
+
* userFriendly true means it can be rendered "as is".
|
|
19
|
+
* userFriendly false (or undefined) means the message is not guaranteed to be user-friendly,
|
|
20
|
+
* so client can resort to more generic "Oops, something went wrong" message.
|
|
16
21
|
*/
|
|
17
22
|
userFriendly?: boolean
|
|
18
23
|
|
package/src/error/error.util.ts
CHANGED
package/src/http/fetcher.ts
CHANGED
|
@@ -436,7 +436,7 @@ export class Fetcher {
|
|
|
436
436
|
|
|
437
437
|
let responseStatusCode = res.fetchResponse?.status || 0
|
|
438
438
|
if (res.statusFamily === 2) {
|
|
439
|
-
// important to reset
|
|
439
|
+
// important to reset responseStatusCode to 0 in this case, as status 2xx can be misleading
|
|
440
440
|
res.statusFamily = undefined
|
|
441
441
|
res.statusCode = undefined
|
|
442
442
|
responseStatusCode = 0
|
package/src/semver.ts
CHANGED
package/src/string/stringify.ts
CHANGED
|
@@ -112,7 +112,7 @@ export function _stringify(obj: any, opt: StringifyOptions = {}): string {
|
|
|
112
112
|
// }
|
|
113
113
|
// if (_isErrorObject(obj) && _isHttpErrorObject(obj)) {
|
|
114
114
|
// // Printing (0) to avoid ambiguity
|
|
115
|
-
// s = `${obj.name}(${obj.data.
|
|
115
|
+
// s = `${obj.name}(${obj.data.backendResponseStatusCode}): ${obj.message}`
|
|
116
116
|
// }
|
|
117
117
|
|
|
118
118
|
s = [obj.name, obj.message].filter(Boolean).join(': ')
|