@naturalcycles/js-lib 14.277.0 → 15.0.1
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/cfg/frontend/tsconfig.json +3 -3
- package/dist/abort.js +1 -4
- package/dist/array/array.util.d.ts +1 -1
- package/dist/array/array.util.js +47 -88
- package/dist/array/range.d.ts +2 -2
- package/dist/array/range.js +7 -12
- package/dist/bot.js +6 -10
- package/dist/browser/adminService.d.ts +1 -1
- package/dist/browser/adminService.js +10 -14
- package/dist/browser/analytics.util.js +10 -15
- package/dist/browser/i18n/fetchTranslationLoader.d.ts +3 -3
- package/dist/browser/i18n/fetchTranslationLoader.js +1 -5
- package/dist/browser/i18n/translation.service.d.ts +1 -1
- package/dist/browser/i18n/translation.service.js +5 -10
- package/dist/browser/imageFitter.js +1 -5
- package/dist/browser/script.util.js +8 -12
- package/dist/browser/topbar.js +7 -10
- package/dist/datetime/dateInterval.d.ts +2 -2
- package/dist/datetime/dateInterval.js +7 -11
- package/dist/datetime/localDate.d.ts +3 -3
- package/dist/datetime/localDate.js +40 -44
- package/dist/datetime/localTime.d.ts +3 -3
- package/dist/datetime/localTime.js +36 -40
- package/dist/datetime/timeInterval.d.ts +2 -2
- package/dist/datetime/timeInterval.js +6 -10
- package/dist/datetime/wallTime.d.ts +3 -3
- package/dist/datetime/wallTime.js +5 -9
- package/dist/decorators/asyncMemo.decorator.d.ts +3 -3
- package/dist/decorators/asyncMemo.decorator.js +12 -17
- package/dist/decorators/createPromiseDecorator.js +3 -6
- package/dist/decorators/debounce.d.ts +1 -1
- package/dist/decorators/debounce.decorator.d.ts +1 -1
- package/dist/decorators/debounce.decorator.js +5 -9
- package/dist/decorators/debounce.js +2 -6
- package/dist/decorators/decorator.util.d.ts +1 -1
- package/dist/decorators/decorator.util.js +3 -8
- package/dist/decorators/logMethod.decorator.d.ts +1 -1
- package/dist/decorators/logMethod.decorator.js +11 -14
- package/dist/decorators/memo.decorator.d.ts +3 -3
- package/dist/decorators/memo.decorator.js +11 -16
- package/dist/decorators/memo.util.d.ts +2 -2
- package/dist/decorators/memo.util.js +11 -17
- package/dist/decorators/memoFn.d.ts +3 -3
- package/dist/decorators/memoFn.js +3 -6
- package/dist/decorators/memoFnAsync.d.ts +3 -3
- package/dist/decorators/memoFnAsync.js +5 -8
- package/dist/decorators/memoSimple.decorator.d.ts +1 -1
- package/dist/decorators/memoSimple.decorator.js +6 -10
- package/dist/decorators/retry.decorator.d.ts +1 -1
- package/dist/decorators/retry.decorator.js +3 -6
- package/dist/decorators/swarmSafe.decorator.js +3 -7
- package/dist/decorators/timeout.decorator.d.ts +1 -1
- package/dist/decorators/timeout.decorator.js +7 -10
- package/dist/define.d.ts +1 -1
- package/dist/define.js +11 -19
- package/dist/deviceIdService.js +7 -11
- package/dist/enum.util.d.ts +1 -1
- package/dist/enum.util.js +20 -42
- package/dist/env/buildInfo.d.ts +1 -1
- package/dist/env/buildInfo.js +3 -6
- package/dist/env.js +2 -6
- package/dist/error/assert.d.ts +3 -3
- package/dist/error/assert.js +30 -44
- package/dist/error/error.model.d.ts +2 -2
- package/dist/error/error.model.js +1 -2
- package/dist/error/error.util.d.ts +1 -1
- package/dist/error/error.util.js +25 -44
- package/dist/error/errorMode.js +2 -5
- package/dist/error/try.d.ts +2 -2
- package/dist/error/try.js +15 -23
- package/dist/error/tryCatch.d.ts +2 -2
- package/dist/error/tryCatch.js +6 -11
- package/dist/form.util.d.ts +1 -1
- package/dist/form.util.js +2 -6
- package/dist/http/fetcher.d.ts +3 -3
- package/dist/http/fetcher.js +48 -53
- package/dist/http/fetcher.model.d.ts +5 -5
- package/dist/http/fetcher.model.js +1 -2
- package/dist/http/http.model.js +1 -4
- package/dist/index.d.ts +96 -96
- package/dist/index.js +96 -99
- package/dist/is.util.d.ts +2 -2
- package/dist/is.util.js +12 -27
- package/dist/iter/asyncIterable2.d.ts +3 -3
- package/dist/iter/asyncIterable2.js +8 -12
- package/dist/iter/iterable2.d.ts +2 -2
- package/dist/iter/iterable2.js +8 -12
- package/dist/json-schema/from-data/generateJsonSchemaFromData.d.ts +1 -1
- package/dist/json-schema/from-data/generateJsonSchemaFromData.js +5 -8
- package/dist/json-schema/jsonSchema.cnst.d.ts +1 -1
- package/dist/json-schema/jsonSchema.cnst.js +1 -4
- package/dist/json-schema/jsonSchema.model.d.ts +1 -1
- package/dist/json-schema/jsonSchema.model.js +1 -2
- package/dist/json-schema/jsonSchema.util.d.ts +2 -2
- package/dist/json-schema/jsonSchema.util.js +5 -8
- package/dist/json-schema/jsonSchemaBuilder.d.ts +2 -2
- package/dist/json-schema/jsonSchemaBuilder.js +16 -25
- package/dist/json-schema/jsonSchemas.d.ts +2 -2
- package/dist/json-schema/jsonSchemas.js +5 -8
- package/dist/log/commonLogger.js +14 -21
- package/dist/math/accumulatingAverage.js +1 -5
- package/dist/math/math.util.js +11 -19
- package/dist/math/sma.js +1 -5
- package/dist/math/stack.util.js +11 -16
- package/dist/nanoid.js +2 -6
- package/dist/number/createDeterministicRandom.js +1 -4
- package/dist/number/number.util.d.ts +1 -1
- package/dist/number/number.util.js +9 -20
- package/dist/object/deepEquals.js +3 -8
- package/dist/object/map2.js +1 -5
- package/dist/object/object.util.d.ts +2 -2
- package/dist/object/object.util.js +40 -70
- package/dist/object/set2.js +1 -5
- package/dist/object/sortObject.d.ts +1 -1
- package/dist/object/sortObject.js +1 -4
- package/dist/object/sortObjectDeep.js +1 -4
- package/dist/polyfill.js +1 -4
- package/dist/promise/abortable.d.ts +1 -1
- package/dist/promise/abortable.js +2 -7
- package/dist/promise/pDefer.js +1 -4
- package/dist/promise/pDelay.d.ts +2 -2
- package/dist/promise/pDelay.js +4 -8
- package/dist/promise/pFilter.d.ts +1 -1
- package/dist/promise/pFilter.js +1 -4
- package/dist/promise/pHang.js +1 -4
- package/dist/promise/pMap.d.ts +2 -2
- package/dist/promise/pMap.js +17 -20
- package/dist/promise/pProps.js +1 -4
- package/dist/promise/pQueue.d.ts +2 -2
- package/dist/promise/pQueue.js +7 -11
- package/dist/promise/pRetry.d.ts +1 -1
- package/dist/promise/pRetry.js +8 -12
- package/dist/promise/pState.js +1 -4
- package/dist/promise/pTimeout.d.ts +3 -3
- package/dist/promise/pTimeout.js +7 -11
- package/dist/semver.d.ts +1 -1
- package/dist/semver.js +13 -18
- package/dist/string/case.js +9 -14
- package/dist/string/escape.js +2 -6
- package/dist/string/hash.util.d.ts +1 -1
- package/dist/string/hash.util.js +4 -10
- package/dist/string/json.util.d.ts +1 -1
- package/dist/string/json.util.js +5 -10
- package/dist/string/leven.js +1 -4
- package/dist/string/lodash/unicodeWords.js +1 -4
- package/dist/string/lodash/words.js +3 -6
- package/dist/string/pupa.d.ts +1 -1
- package/dist/string/pupa.js +4 -9
- package/dist/string/readingTime.d.ts +1 -1
- package/dist/string/readingTime.js +1 -4
- package/dist/string/regex.js +1 -4
- package/dist/string/safeJsonStringify.d.ts +1 -1
- package/dist/string/safeJsonStringify.js +1 -4
- package/dist/string/slugify.js +1 -4
- package/dist/string/string.util.js +15 -32
- package/dist/string/stringify.d.ts +1 -1
- package/dist/string/stringify.js +10 -14
- package/dist/string/url.util.d.ts +1 -1
- package/dist/string/url.util.js +2 -6
- package/dist/time/time.util.d.ts +1 -1
- package/dist/time/time.util.js +3 -8
- package/dist/typeFest.js +1 -2
- package/dist/types.d.ts +1 -1
- package/dist/types.js +17 -27
- package/dist/unit/size.util.js +5 -12
- package/dist/web.d.ts +1 -1
- package/dist/web.js +1 -5
- package/dist/zod/index.d.ts +2 -2
- package/dist/zod/index.js +4 -10
- package/dist/zod/zod.shared.schemas.js +36 -39
- package/dist/zod/zod.util.js +7 -14
- package/package.json +18 -30
- package/src/array/array.util.ts +3 -3
- package/src/array/range.ts +2 -2
- package/src/bot.ts +1 -1
- package/src/browser/adminService.ts +4 -4
- package/src/browser/analytics.util.ts +2 -2
- package/src/browser/i18n/fetchTranslationLoader.ts +3 -3
- package/src/browser/i18n/translation.service.ts +2 -2
- package/src/browser/script.util.ts +2 -2
- package/src/datetime/dateInterval.ts +3 -3
- package/src/datetime/localDate.ts +5 -5
- package/src/datetime/localTime.ts +6 -6
- package/src/datetime/timeInterval.ts +3 -3
- package/src/datetime/wallTime.ts +4 -4
- package/src/decorators/asyncMemo.decorator.ts +7 -7
- package/src/decorators/createPromiseDecorator.ts +1 -1
- package/src/decorators/debounce.decorator.ts +2 -2
- package/src/decorators/debounce.ts +1 -1
- package/src/decorators/decorator.util.ts +1 -1
- package/src/decorators/logMethod.decorator.ts +4 -4
- package/src/decorators/memo.decorator.ts +7 -7
- package/src/decorators/memo.util.ts +4 -4
- package/src/decorators/memoFn.ts +4 -4
- package/src/decorators/memoFnAsync.ts +5 -5
- package/src/decorators/memoSimple.decorator.ts +4 -4
- package/src/decorators/retry.decorator.ts +2 -2
- package/src/decorators/swarmSafe.decorator.ts +2 -2
- package/src/decorators/timeout.decorator.ts +4 -4
- package/src/define.ts +3 -3
- package/src/deviceIdService.ts +3 -3
- package/src/enum.util.ts +1 -1
- package/src/env/buildInfo.ts +2 -2
- package/src/error/assert.ts +7 -7
- package/src/error/error.model.ts +2 -2
- package/src/error/error.util.ts +5 -5
- package/src/error/try.ts +5 -5
- package/src/error/tryCatch.ts +3 -3
- package/src/form.util.ts +1 -1
- package/src/http/fetcher.model.ts +5 -5
- package/src/http/fetcher.ts +20 -15
- package/src/index.ts +96 -96
- package/src/is.util.ts +2 -2
- package/src/iter/asyncIterable2.ts +3 -3
- package/src/iter/iterable2.ts +2 -2
- package/src/json-schema/from-data/generateJsonSchemaFromData.ts +3 -3
- package/src/json-schema/jsonSchema.cnst.ts +1 -1
- package/src/json-schema/jsonSchema.model.ts +1 -1
- package/src/json-schema/jsonSchema.util.ts +4 -4
- package/src/json-schema/jsonSchemaBuilder.ts +5 -5
- package/src/json-schema/jsonSchemas.ts +2 -2
- package/src/math/math.util.ts +2 -2
- package/src/math/stack.util.ts +1 -1
- package/src/number/number.util.ts +1 -1
- package/src/object/object.util.ts +3 -3
- package/src/object/sortObject.ts +1 -1
- package/src/promise/abortable.ts +1 -1
- package/src/promise/pDelay.ts +3 -3
- package/src/promise/pFilter.ts +1 -1
- package/src/promise/pMap.ts +2 -2
- package/src/promise/pQueue.ts +4 -4
- package/src/promise/pRetry.ts +2 -2
- package/src/promise/pTimeout.ts +4 -4
- package/src/semver.ts +3 -3
- package/src/string/case.ts +2 -2
- package/src/string/hash.util.ts +1 -1
- package/src/string/json.util.ts +2 -2
- package/src/string/lodash/words.ts +1 -1
- package/src/string/pupa.ts +2 -2
- package/src/string/readingTime.ts +1 -1
- package/src/string/safeJsonStringify.ts +1 -1
- package/src/string/stringify.ts +4 -4
- package/src/string/url.util.ts +1 -1
- package/src/time/time.util.ts +1 -1
- package/src/types.ts +2 -2
- package/src/web.ts +1 -1
- package/src/zod/index.ts +2 -2
- package/src/zod/zod.util.ts +1 -1
- package/dist-esm/abort.js +0 -12
- package/dist-esm/array/array.util.js +0 -458
- package/dist-esm/array/range.js +0 -34
- package/dist-esm/bot.js +0 -130
- package/dist-esm/browser/adminService.js +0 -94
- package/dist-esm/browser/analytics.util.js +0 -54
- package/dist-esm/browser/i18n/fetchTranslationLoader.js +0 -13
- package/dist-esm/browser/i18n/translation.service.js +0 -56
- package/dist-esm/browser/imageFitter.js +0 -65
- package/dist-esm/browser/script.util.js +0 -46
- package/dist-esm/browser/topbar.js +0 -135
- package/dist-esm/datetime/dateInterval.js +0 -80
- package/dist-esm/datetime/localDate.js +0 -719
- package/dist-esm/datetime/localTime.js +0 -996
- package/dist-esm/datetime/timeInterval.js +0 -88
- package/dist-esm/datetime/wallTime.js +0 -70
- package/dist-esm/decorators/asyncMemo.decorator.js +0 -111
- package/dist-esm/decorators/createPromiseDecorator.js +0 -82
- package/dist-esm/decorators/debounce.decorator.js +0 -17
- package/dist-esm/decorators/debounce.js +0 -114
- package/dist-esm/decorators/decorator.util.js +0 -31
- package/dist-esm/decorators/logMethod.decorator.js +0 -85
- package/dist-esm/decorators/memo.decorator.js +0 -80
- package/dist-esm/decorators/memo.util.js +0 -97
- package/dist-esm/decorators/memoFn.js +0 -29
- package/dist-esm/decorators/memoFnAsync.js +0 -35
- package/dist-esm/decorators/memoSimple.decorator.js +0 -55
- package/dist-esm/decorators/retry.decorator.js +0 -9
- package/dist-esm/decorators/swarmSafe.decorator.js +0 -38
- package/dist-esm/decorators/timeout.decorator.js +0 -19
- package/dist-esm/define.js +0 -109
- package/dist-esm/deviceIdService.js +0 -105
- package/dist-esm/enum.util.js +0 -157
- package/dist-esm/env/buildInfo.js +0 -19
- package/dist-esm/env.js +0 -19
- package/dist-esm/error/assert.js +0 -122
- package/dist-esm/error/error.model.js +0 -1
- package/dist-esm/error/error.util.js +0 -337
- package/dist-esm/error/errorMode.js +0 -20
- package/dist-esm/error/try.js +0 -105
- package/dist-esm/error/tryCatch.js +0 -41
- package/dist-esm/form.util.js +0 -16
- package/dist-esm/http/fetcher.js +0 -704
- package/dist-esm/http/fetcher.model.js +0 -3
- package/dist-esm/http/http.model.js +0 -1
- package/dist-esm/index.js +0 -96
- package/dist-esm/is.util.js +0 -70
- package/dist-esm/iter/asyncIterable2.js +0 -103
- package/dist-esm/iter/iterable2.js +0 -87
- package/dist-esm/json-schema/from-data/generateJsonSchemaFromData.js +0 -86
- package/dist-esm/json-schema/jsonSchema.cnst.js +0 -38
- package/dist-esm/json-schema/jsonSchema.model.js +0 -1
- package/dist-esm/json-schema/jsonSchema.util.js +0 -27
- package/dist-esm/json-schema/jsonSchemaBuilder.js +0 -352
- package/dist-esm/json-schema/jsonSchemas.js +0 -6
- package/dist-esm/log/commonLogger.js +0 -78
- package/dist-esm/math/accumulatingAverage.js +0 -32
- package/dist-esm/math/math.util.js +0 -85
- package/dist-esm/math/sma.js +0 -43
- package/dist-esm/math/stack.util.js +0 -82
- package/dist-esm/nanoid.js +0 -57
- package/dist-esm/number/createDeterministicRandom.js +0 -18
- package/dist-esm/number/number.util.js +0 -109
- package/dist-esm/object/deepEquals.js +0 -170
- package/dist-esm/object/map2.js +0 -21
- package/dist-esm/object/object.util.js +0 -435
- package/dist-esm/object/set2.js +0 -15
- package/dist-esm/object/sortObject.js +0 -21
- package/dist-esm/object/sortObjectDeep.js +0 -17
- package/dist-esm/polyfill.js +0 -6
- package/dist-esm/promise/abortable.js +0 -31
- package/dist-esm/promise/pDefer.js +0 -17
- package/dist-esm/promise/pDelay.js +0 -37
- package/dist-esm/promise/pFilter.js +0 -5
- package/dist-esm/promise/pHang.js +0 -6
- package/dist-esm/promise/pMap.js +0 -174
- package/dist-esm/promise/pProps.js +0 -20
- package/dist-esm/promise/pQueue.js +0 -101
- package/dist-esm/promise/pRetry.js +0 -64
- package/dist-esm/promise/pState.js +0 -14
- package/dist-esm/promise/pTimeout.js +0 -64
- package/dist-esm/semver.js +0 -149
- package/dist-esm/string/case.js +0 -24
- package/dist-esm/string/escape.js +0 -49
- package/dist-esm/string/hash.util.js +0 -67
- package/dist-esm/string/json.util.js +0 -46
- package/dist-esm/string/leven.js +0 -77
- package/dist-esm/string/lodash/unicodeWords.js +0 -68
- package/dist-esm/string/lodash/words.js +0 -31
- package/dist-esm/string/pupa.js +0 -54
- package/dist-esm/string/readingTime.js +0 -102
- package/dist-esm/string/regex.js +0 -6
- package/dist-esm/string/safeJsonStringify.js +0 -39
- package/dist-esm/string/slugify.js +0 -66
- package/dist-esm/string/string.util.js +0 -99
- package/dist-esm/string/stringify.js +0 -133
- package/dist-esm/string/url.util.js +0 -42
- package/dist-esm/time/time.util.js +0 -61
- package/dist-esm/typeFest.js +0 -2
- package/dist-esm/types.js +0 -66
- package/dist-esm/unit/size.util.js +0 -43
- package/dist-esm/web.js +0 -40
- package/dist-esm/zod/index.js +0 -4
- package/dist-esm/zod/zod.shared.schemas.js +0 -92
- package/dist-esm/zod/zod.util.js +0 -52
package/dist-esm/env.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Use it to detect SSR/Node.js environment.
|
|
3
|
-
*
|
|
4
|
-
* Will return `true` in Node.js.
|
|
5
|
-
* Will return `false` in the Browser.
|
|
6
|
-
*/
|
|
7
|
-
export function isServerSide() {
|
|
8
|
-
return !isClientSide();
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Use it to detect Browser (not SSR/Node) environment.
|
|
12
|
-
*
|
|
13
|
-
* Will return `true` in the Browser.
|
|
14
|
-
* Will return `false` in Node.js.
|
|
15
|
-
*/
|
|
16
|
-
export function isClientSide() {
|
|
17
|
-
// eslint-disable-next-line unicorn/prefer-global-this
|
|
18
|
-
return typeof window !== 'undefined' && !!window?.document;
|
|
19
|
-
}
|
package/dist-esm/error/assert.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { _deepEquals } from '../object/deepEquals';
|
|
2
|
-
import { _stringify } from '../string/stringify';
|
|
3
|
-
import { TS_2000, TS_2500 } from '../zod/zod.shared.schemas';
|
|
4
|
-
import { _isBackendErrorResponseObject, _isErrorObject, AssertionError } from './error.util';
|
|
5
|
-
/**
|
|
6
|
-
* Evaluates the `condition` (casts it to Boolean).
|
|
7
|
-
* Expects it to be truthy, otherwise throws AppError.
|
|
8
|
-
*
|
|
9
|
-
* Should be used NOT for "expected" / user-facing errors, but
|
|
10
|
-
* vice-versa - for completely unexpected and 100% buggy "should never happen" cases.
|
|
11
|
-
*
|
|
12
|
-
* It'll result in http 500 on the server (cause that's the right code for "unexpected" errors).
|
|
13
|
-
* Pass { backendResponseStatusCode: x } at errorData argument to override the http code (will be picked up by backend-lib).
|
|
14
|
-
*
|
|
15
|
-
* API is similar to Node's assert(), except:
|
|
16
|
-
* 1. Throws js-lib's AppError
|
|
17
|
-
* 2. Has a default message, if not provided
|
|
18
|
-
*
|
|
19
|
-
* Since 2024-07-10 it no longer sets `userFriendly: true` by default.
|
|
20
|
-
*/
|
|
21
|
-
export function _assert(condition, // will be evaluated as Boolean
|
|
22
|
-
message, errorData) {
|
|
23
|
-
if (!condition) {
|
|
24
|
-
throw new AssertionError(message || 'condition failed', {
|
|
25
|
-
...errorData,
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Like _assert(), but prints more helpful error message.
|
|
31
|
-
* API is similar to Node's assert.equals().
|
|
32
|
-
*
|
|
33
|
-
* Does SHALLOW, but strict equality (===), use _assertDeepEquals() for deep equality.
|
|
34
|
-
*/
|
|
35
|
-
export function _assertEquals(actual, expected, message, errorData) {
|
|
36
|
-
if (actual !== expected) {
|
|
37
|
-
const msg = message ||
|
|
38
|
-
['not equal', `expected: ${_stringify(expected)}`, `got : ${_stringify(actual)}`]
|
|
39
|
-
.filter(Boolean)
|
|
40
|
-
.join('\n');
|
|
41
|
-
throw new AssertionError(msg, {
|
|
42
|
-
...errorData,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Like _assert(), but prints more helpful error message.
|
|
48
|
-
* API is similar to Node's assert.deepEquals().
|
|
49
|
-
*
|
|
50
|
-
* Does DEEP equality via _deepEquals()
|
|
51
|
-
*/
|
|
52
|
-
export function _assertDeepEquals(actual, expected, message, errorData) {
|
|
53
|
-
if (!_deepEquals(actual, expected)) {
|
|
54
|
-
const msg = message ||
|
|
55
|
-
['not deeply equal', `expected: ${_stringify(expected)}`, `got : ${_stringify(actual)}`]
|
|
56
|
-
.filter(Boolean)
|
|
57
|
-
.join('\n');
|
|
58
|
-
throw new AssertionError(msg, {
|
|
59
|
-
...errorData,
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
export function _assertIsError(err, errorClass = Error) {
|
|
64
|
-
if (!(err instanceof errorClass)) {
|
|
65
|
-
throw new AssertionError(`Expected to be instanceof ${errorClass.name}, actual typeof: ${typeof err}`);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Asserts that passed object is indeed an Error of defined ErrorClass.
|
|
70
|
-
* If yes - returns peacefully (with TypeScript assertion).
|
|
71
|
-
* In not - throws (re-throws) that error up.
|
|
72
|
-
*/
|
|
73
|
-
export function _assertErrorClassOrRethrow(err, errorClass) {
|
|
74
|
-
if (!(err instanceof errorClass)) {
|
|
75
|
-
// re-throw
|
|
76
|
-
throw err;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
export function _assertIsErrorObject(obj) {
|
|
80
|
-
if (!_isErrorObject(obj)) {
|
|
81
|
-
throw new AssertionError(`Expected to be ErrorObject, actual typeof: ${typeof obj}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
export function _assertIsBackendErrorResponseObject(obj) {
|
|
85
|
-
if (!_isBackendErrorResponseObject(obj)) {
|
|
86
|
-
throw new AssertionError(`Expected to be BackendErrorResponseObject, actual typeof: ${typeof obj}`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
export function _assertIsString(v, message) {
|
|
90
|
-
_assertTypeOf(v, 'string', message);
|
|
91
|
-
}
|
|
92
|
-
export function _assertIsNumber(v, message) {
|
|
93
|
-
_assertTypeOf(v, 'number', message);
|
|
94
|
-
}
|
|
95
|
-
export function _assertTypeOf(v, expectedType, message) {
|
|
96
|
-
// biome-ignore lint/suspicious/useValidTypeof: ok
|
|
97
|
-
if (typeof v !== expectedType) {
|
|
98
|
-
const msg = message || `Expected typeof ${expectedType}, actual typeof: ${typeof v}`;
|
|
99
|
-
throw new AssertionError(msg);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Casts an arbitrary number as UnixTimestamp.
|
|
104
|
-
* Right now does not perform any validation (unlike `asUnixTimestamp2000`),
|
|
105
|
-
* but only type casting.
|
|
106
|
-
*/
|
|
107
|
-
export function asUnixTimestamp(n) {
|
|
108
|
-
return n;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Casts an arbitrary number as UnixTimestamp2000.
|
|
112
|
-
* Throws if the number is not inside 2000-01-01 and 2500-01-01 time interval,
|
|
113
|
-
* which would indicate a bug.
|
|
114
|
-
*/
|
|
115
|
-
export function asUnixTimestamp2000(n) {
|
|
116
|
-
if (!n || n < TS_2000 || n > TS_2500) {
|
|
117
|
-
throw new AssertionError(`Number is not a valid UnixTimestamp2000: ${n}`, {
|
|
118
|
-
fingerprint: 'asUnixTimestamp2000',
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
return n;
|
|
122
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
import { isServerSide } from '../env';
|
|
2
|
-
import { _jsonParseIfPossible } from '../string/json.util';
|
|
3
|
-
import { _truncate, _truncateMiddle } from '../string/string.util';
|
|
4
|
-
import { _stringify } from '../string/stringify';
|
|
5
|
-
/**
|
|
6
|
-
* Useful to ensure that error in `catch (err) { ... }`
|
|
7
|
-
* is indeed an Error (and not e.g `string` or `undefined`).
|
|
8
|
-
* 99% of the cases it will be Error already.
|
|
9
|
-
* Becomes more useful since TypeScript 4.4 made `err` of type `unknown` by default.
|
|
10
|
-
*
|
|
11
|
-
* Alternatively, if you're sure it's Error - you can use `_assertIsError(err)`.
|
|
12
|
-
*/
|
|
13
|
-
export function _anyToError(o, errorClass = Error, errorData) {
|
|
14
|
-
let e;
|
|
15
|
-
if (o instanceof errorClass) {
|
|
16
|
-
e = o;
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
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 = _anyToErrorObject(o);
|
|
21
|
-
e = _errorObjectToError(errorObject, errorClass);
|
|
22
|
-
}
|
|
23
|
-
if (errorData) {
|
|
24
|
-
;
|
|
25
|
-
e.data = {
|
|
26
|
-
...e.data,
|
|
27
|
-
...errorData,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
return e;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Converts "anything" to ErrorObject.
|
|
34
|
-
* Detects if it's HttpErrorResponse, HttpErrorObject, ErrorObject, Error, etc..
|
|
35
|
-
* If object is Error - Error.message will be used.
|
|
36
|
-
* Objects (not Errors) get converted to prettified JSON string (via `_stringify`).
|
|
37
|
-
*/
|
|
38
|
-
export function _anyToErrorObject(o, errorData) {
|
|
39
|
-
let eo;
|
|
40
|
-
if (_isErrorLike(o)) {
|
|
41
|
-
eo = _errorLikeToErrorObject(o);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
o = _jsonParseIfPossible(o);
|
|
45
|
-
if (_isBackendErrorResponseObject(o)) {
|
|
46
|
-
eo = o.error;
|
|
47
|
-
}
|
|
48
|
-
else if (_isErrorObject(o)) {
|
|
49
|
-
eo = o;
|
|
50
|
-
}
|
|
51
|
-
else if (_isErrorLike(o)) {
|
|
52
|
-
eo = _errorLikeToErrorObject(o);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
// Here we are sure it has no `data` property,
|
|
56
|
-
// so, fair to return `data: {}` in the end
|
|
57
|
-
// Also we're sure it includes no "error name", e.g no `Error: ...`,
|
|
58
|
-
// so, fair to include `name: 'Error'`
|
|
59
|
-
const message = _stringify(o);
|
|
60
|
-
eo = {
|
|
61
|
-
name: 'Error',
|
|
62
|
-
message,
|
|
63
|
-
data: {}, // empty
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
Object.assign(eo.data, errorData);
|
|
68
|
-
return eo;
|
|
69
|
-
}
|
|
70
|
-
export function _errorLikeToErrorObject(e) {
|
|
71
|
-
// If it's already an ErrorObject - just return it
|
|
72
|
-
// AppError satisfies ErrorObject interface
|
|
73
|
-
// Error does not satisfy (lacks `data`)
|
|
74
|
-
// UPD: no, we expect a "plain object" here as an output,
|
|
75
|
-
// because Error classes sometimes have non-enumerable properties (e.g data)
|
|
76
|
-
if (!(e instanceof Error) && _isErrorObject(e)) {
|
|
77
|
-
return e;
|
|
78
|
-
}
|
|
79
|
-
const obj = {
|
|
80
|
-
name: e.name,
|
|
81
|
-
message: e.message,
|
|
82
|
-
data: { ...e.data }, // empty by default
|
|
83
|
-
};
|
|
84
|
-
if (e.stack)
|
|
85
|
-
obj.stack = e.stack;
|
|
86
|
-
if (e.cause) {
|
|
87
|
-
obj.cause = _anyToErrorObject(e.cause);
|
|
88
|
-
}
|
|
89
|
-
return obj;
|
|
90
|
-
}
|
|
91
|
-
export function _errorObjectToError(o, errorClass = Error) {
|
|
92
|
-
if (o instanceof errorClass)
|
|
93
|
-
return o;
|
|
94
|
-
// Here we pass constructor values assuming it's AppError or sub-class of it
|
|
95
|
-
// If not - will be checked at the next step
|
|
96
|
-
// We cannot check `if (errorClass instanceof AppError)`, only `err instanceof AppError`
|
|
97
|
-
const { name, cause } = o;
|
|
98
|
-
const err = new errorClass(o.message, o.data, { name, cause });
|
|
99
|
-
// name: err.name, // cannot be assigned to a readonly property like this
|
|
100
|
-
// stack: o.stack, // also readonly e.g in Firefox
|
|
101
|
-
if (o.stack) {
|
|
102
|
-
Object.defineProperty(err, 'stack', {
|
|
103
|
-
value: o.stack,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
if (!(err instanceof AppError)) {
|
|
107
|
-
// Following actions are only needed for non-AppError-like errors
|
|
108
|
-
Object.defineProperties(err, {
|
|
109
|
-
name: {
|
|
110
|
-
value: name,
|
|
111
|
-
configurable: true,
|
|
112
|
-
writable: true,
|
|
113
|
-
},
|
|
114
|
-
data: {
|
|
115
|
-
value: o.data,
|
|
116
|
-
writable: true,
|
|
117
|
-
configurable: true,
|
|
118
|
-
enumerable: false,
|
|
119
|
-
},
|
|
120
|
-
cause: {
|
|
121
|
-
value: cause,
|
|
122
|
-
writable: true,
|
|
123
|
-
configurable: true,
|
|
124
|
-
enumerable: true,
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
Object.defineProperty(err.constructor, 'name', {
|
|
128
|
-
value: name,
|
|
129
|
-
configurable: true,
|
|
130
|
-
writable: true,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
return err;
|
|
134
|
-
}
|
|
135
|
-
// These "common" error classes will not be printed as part of the Error snippet
|
|
136
|
-
const commonErrorClasses = new Set([
|
|
137
|
-
'Error',
|
|
138
|
-
'AppError',
|
|
139
|
-
'AssertionError',
|
|
140
|
-
'HttpRequestError',
|
|
141
|
-
'JoiValidationError',
|
|
142
|
-
]);
|
|
143
|
-
/**
|
|
144
|
-
* Provides a short semi-user-friendly error message snippet,
|
|
145
|
-
* that would allow to give a hint to the user what went wrong,
|
|
146
|
-
* also to developers and CS to distinguish between different errors.
|
|
147
|
-
*
|
|
148
|
-
* It's not supposed to have full information about the error, just a small extract from it.
|
|
149
|
-
*/
|
|
150
|
-
export function _errorSnippet(err, opt = {}) {
|
|
151
|
-
const { maxLineLength = 60, maxLines = 3 } = opt;
|
|
152
|
-
const e = _anyToErrorObject(err);
|
|
153
|
-
const lines = [errorObjectToSnippet(e)];
|
|
154
|
-
let { cause } = e;
|
|
155
|
-
while (cause && lines.length < maxLines) {
|
|
156
|
-
lines.push('Caused by ' + errorObjectToSnippet(cause));
|
|
157
|
-
cause = cause.cause; // insert DiCaprio Inception meme
|
|
158
|
-
}
|
|
159
|
-
return lines.map(line => _truncate(line, maxLineLength)).join('\n');
|
|
160
|
-
function errorObjectToSnippet(e) {
|
|
161
|
-
// Return snippet if it was already prepared
|
|
162
|
-
if (e.data.snippet)
|
|
163
|
-
return e.data.snippet;
|
|
164
|
-
// Code already serves the purpose of the snippet, so we can just return it
|
|
165
|
-
if (e.data.code)
|
|
166
|
-
return e.data.code;
|
|
167
|
-
return [
|
|
168
|
-
!commonErrorClasses.has(e.name) && e.name,
|
|
169
|
-
// replace "1+ white space characters" with a single space
|
|
170
|
-
e.message.replaceAll(/\s+/gm, ' ').trim(),
|
|
171
|
-
]
|
|
172
|
-
.filter(Boolean)
|
|
173
|
-
.join(': ');
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
export function _isBackendErrorResponseObject(o) {
|
|
177
|
-
return _isErrorObject(o?.error);
|
|
178
|
-
}
|
|
179
|
-
export function _isHttpRequestErrorObject(o) {
|
|
180
|
-
return !!o && o.name === 'HttpRequestError' && typeof o.data?.requestUrl === 'string';
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Note: any instance of AppError is also automatically an ErrorObject
|
|
184
|
-
*/
|
|
185
|
-
export function _isErrorObject(o) {
|
|
186
|
-
return (!!o &&
|
|
187
|
-
typeof o === 'object' &&
|
|
188
|
-
typeof o.name === 'string' &&
|
|
189
|
-
typeof o.message === 'string' &&
|
|
190
|
-
typeof o.data === 'object');
|
|
191
|
-
}
|
|
192
|
-
export function _isErrorLike(o) {
|
|
193
|
-
return !!o && typeof o === 'object' && typeof o.name === 'string' && typeof o.message === 'string';
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Convenience function to safely add properties to Error's `data` object
|
|
197
|
-
* (even if it wasn't previously existing).
|
|
198
|
-
* Mutates err.
|
|
199
|
-
* Returns err for convenience, so you can re-throw it directly.
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
*
|
|
203
|
-
* try {} catch (err) {
|
|
204
|
-
* throw _errorDataAppend(err, {
|
|
205
|
-
* backendResponseStatusCode: 401,
|
|
206
|
-
* })
|
|
207
|
-
* }
|
|
208
|
-
*/
|
|
209
|
-
export function _errorDataAppend(err, data) {
|
|
210
|
-
var _a;
|
|
211
|
-
if (!data)
|
|
212
|
-
return err;
|
|
213
|
-
(_a = err).data || (_a.data = {}); // create err.data if it doesn't exist
|
|
214
|
-
// Using Object.assign instead of ...data to not override err.data's non-enumerable properties
|
|
215
|
-
Object.assign(err.data, data);
|
|
216
|
-
return err;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Base class for all our (not system) errors.
|
|
220
|
-
*
|
|
221
|
-
* message - "technical" message. Frontend decides to show it or not.
|
|
222
|
-
* data - optional "any" payload.
|
|
223
|
-
* data.userFriendly - if present, will be displayed to the User as is.
|
|
224
|
-
*
|
|
225
|
-
* Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
|
|
226
|
-
*/
|
|
227
|
-
export class AppError extends Error {
|
|
228
|
-
/**
|
|
229
|
-
* Experimental alternative static constructor.
|
|
230
|
-
*/
|
|
231
|
-
static of(opt) {
|
|
232
|
-
return new AppError(opt.message, opt.data, {
|
|
233
|
-
name: opt.name,
|
|
234
|
-
cause: opt.cause,
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
constructor(message, data = {}, opt = {}) {
|
|
238
|
-
super(message);
|
|
239
|
-
// Here we default to `this.constructor.name` on Node, but to 'AppError' on the Frontend
|
|
240
|
-
// because Frontend tends to minify class names, so `constructor.name` is not reliable
|
|
241
|
-
const { name = isServerSide() ? this.constructor.name : 'AppError', cause } = opt;
|
|
242
|
-
Object.defineProperties(this, {
|
|
243
|
-
name: {
|
|
244
|
-
value: name,
|
|
245
|
-
configurable: true,
|
|
246
|
-
writable: true,
|
|
247
|
-
},
|
|
248
|
-
data: {
|
|
249
|
-
value: data,
|
|
250
|
-
writable: true,
|
|
251
|
-
configurable: true,
|
|
252
|
-
enumerable: false,
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
|
-
if (cause) {
|
|
256
|
-
Object.defineProperty(this, 'cause', {
|
|
257
|
-
value: _anyToErrorObject(cause),
|
|
258
|
-
writable: true,
|
|
259
|
-
configurable: true,
|
|
260
|
-
enumerable: true, // unlike standard - setting it to true for "visibility"
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
// this is to allow changing this.constuctor.name to a non-minified version
|
|
264
|
-
Object.defineProperty(this.constructor, 'name', {
|
|
265
|
-
value: name,
|
|
266
|
-
configurable: true,
|
|
267
|
-
writable: true,
|
|
268
|
-
});
|
|
269
|
-
// todo: check if it's needed at all!
|
|
270
|
-
// if (Error.captureStackTrace) {
|
|
271
|
-
// Error.captureStackTrace(this, this.constructor)
|
|
272
|
-
// } else {
|
|
273
|
-
// Object.defineProperty(this, 'stack', {
|
|
274
|
-
// value: new Error().stack, // eslint-disable-line unicorn/error-message
|
|
275
|
-
// writable: true,
|
|
276
|
-
// configurable: true,
|
|
277
|
-
// })
|
|
278
|
-
// }
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Error that is thrown when Http Request was made and returned an error.
|
|
283
|
-
* Thrown by, for example, Fetcher.
|
|
284
|
-
*
|
|
285
|
-
* On the Frontend this Error class represents the error when calling the API,
|
|
286
|
-
* contains all the necessary request and response information.
|
|
287
|
-
*
|
|
288
|
-
* On the Backend, similarly, it represents the error when calling some 3rd-party API
|
|
289
|
-
* (backend-to-backend call).
|
|
290
|
-
* On the Backend it often propagates all the way to the Backend error handler,
|
|
291
|
-
* where it would be wrapped in BackendErrorResponseObject.
|
|
292
|
-
*
|
|
293
|
-
* Please note that `ErrorData.backendResponseStatusCode` is NOT exactly the same as
|
|
294
|
-
* `HttpRequestErrorData.responseStatusCode`.
|
|
295
|
-
* E.g 3rd-party call may return 401, but our Backend will still wrap it into an 500 error
|
|
296
|
-
* (by default).
|
|
297
|
-
*/
|
|
298
|
-
export class HttpRequestError extends AppError {
|
|
299
|
-
constructor(message, data, opt) {
|
|
300
|
-
if (data.response) {
|
|
301
|
-
Object.defineProperty(data, 'response', {
|
|
302
|
-
enumerable: false,
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
super(message, data, { ...opt, name: 'HttpRequestError' });
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
export class AssertionError extends AppError {
|
|
309
|
-
constructor(message, data) {
|
|
310
|
-
super(message, data, { name: 'AssertionError' });
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
export class JsonParseError extends AppError {
|
|
314
|
-
constructor(data) {
|
|
315
|
-
const message = ['Failed to parse', data.text && _truncateMiddle(data.text, 200)]
|
|
316
|
-
.filter(Boolean)
|
|
317
|
-
.join(': ');
|
|
318
|
-
super(message, data, { name: 'JsonParseError' });
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
export class TimeoutError extends AppError {
|
|
322
|
-
constructor(message, data, opt) {
|
|
323
|
-
super(message, data, { ...opt, name: 'TimeoutError' });
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* It is thrown when Error was expected, but didn't happen
|
|
328
|
-
* ("pass" happened instead).
|
|
329
|
-
* "Pass" means "no error".
|
|
330
|
-
*/
|
|
331
|
-
export class UnexpectedPassError extends AppError {
|
|
332
|
-
constructor(message) {
|
|
333
|
-
super(message || 'expected error was not thrown', {}, {
|
|
334
|
-
name: 'UnexpectedPassError',
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Allows to define error-controlling behaviour for batch operations.
|
|
3
|
-
*
|
|
4
|
-
* @default is THROW_IMMEDIATELY in most cases
|
|
5
|
-
*/
|
|
6
|
-
export var ErrorMode;
|
|
7
|
-
(function (ErrorMode) {
|
|
8
|
-
/**
|
|
9
|
-
* Usually a default behaviour, similar as "exit early".
|
|
10
|
-
*/
|
|
11
|
-
ErrorMode["THROW_IMMEDIATELY"] = "THROW_IMMEDIATELY";
|
|
12
|
-
/**
|
|
13
|
-
* Don't throw on errors, but collect them and throw as AggregateError in the end.
|
|
14
|
-
*/
|
|
15
|
-
ErrorMode["THROW_AGGREGATED"] = "THROW_AGGREGATED";
|
|
16
|
-
/**
|
|
17
|
-
* Completely suppress errors, do not aggregate nor throw anything. Resilient mode.
|
|
18
|
-
*/
|
|
19
|
-
ErrorMode["SUPPRESS"] = "SUPPRESS";
|
|
20
|
-
})(ErrorMode || (ErrorMode = {}));
|
package/dist-esm/error/try.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { _stringify } from '../string/stringify';
|
|
2
|
-
import { _assertErrorClassOrRethrow } from './assert';
|
|
3
|
-
import { UnexpectedPassError } from './error.util';
|
|
4
|
-
/**
|
|
5
|
-
* Calls a function, returns a Tuple of [error, value].
|
|
6
|
-
* Allows to write shorter code that avoids `try/catch`.
|
|
7
|
-
* Useful e.g. in unit tests.
|
|
8
|
-
*
|
|
9
|
-
* Similar to pTry, but for sync functions.
|
|
10
|
-
*
|
|
11
|
-
* ERR is typed as Error, not `unknown`. While unknown would be more correct,
|
|
12
|
-
* according to recent TypeScript, Error gives more developer convenience.
|
|
13
|
-
* In our code we NEVER throw non-errors.
|
|
14
|
-
* Only possibility of non-error is in the 3rd-party library code, in these cases it
|
|
15
|
-
* can be manually cast to `unknown` for extra safety.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
*
|
|
19
|
-
* const [err, v] = _try(() => someFunction())
|
|
20
|
-
* if (err) ...do something...
|
|
21
|
-
* v // go ahead and use v
|
|
22
|
-
*/
|
|
23
|
-
export function _try(fn, errorClass) {
|
|
24
|
-
try {
|
|
25
|
-
return [null, fn()];
|
|
26
|
-
}
|
|
27
|
-
catch (err) {
|
|
28
|
-
if (errorClass) {
|
|
29
|
-
_assertErrorClassOrRethrow(err, errorClass);
|
|
30
|
-
}
|
|
31
|
-
return [err, null];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Like _try, but for Promises.
|
|
36
|
-
*/
|
|
37
|
-
export async function pTry(promise, errorClass) {
|
|
38
|
-
try {
|
|
39
|
-
return [null, await promise];
|
|
40
|
-
}
|
|
41
|
-
catch (err) {
|
|
42
|
-
if (errorClass) {
|
|
43
|
-
_assertErrorClassOrRethrow(err, errorClass);
|
|
44
|
-
}
|
|
45
|
-
return [err, null];
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Calls `fn`, expects is to throw, catches the expected error and returns.
|
|
50
|
-
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
51
|
-
*
|
|
52
|
-
* If `errorClass` is passed:
|
|
53
|
-
* 1. It automatically infers it's type
|
|
54
|
-
* 2. It does `instanceof` check and throws if wrong Error instance was thrown.
|
|
55
|
-
*/
|
|
56
|
-
export function _expectedError(fn, errorClass) {
|
|
57
|
-
try {
|
|
58
|
-
fn();
|
|
59
|
-
}
|
|
60
|
-
catch (err) {
|
|
61
|
-
if (errorClass && !(err instanceof errorClass)) {
|
|
62
|
-
console.warn(`_expectedError expected ${errorClass.constructor.name} but got different error class`);
|
|
63
|
-
throw err;
|
|
64
|
-
}
|
|
65
|
-
return err; // this is expected!
|
|
66
|
-
}
|
|
67
|
-
// Unexpected!
|
|
68
|
-
throw new UnexpectedPassError();
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Awaits passed `promise`, expects is to throw (reject), catches the expected error and returns.
|
|
72
|
-
* If error was NOT thrown - throws UnexpectedPassError instead.
|
|
73
|
-
*
|
|
74
|
-
* If `errorClass` is passed:
|
|
75
|
-
* 1. It automatically infers it's type
|
|
76
|
-
* 2. It does `instanceof` check and throws if wrong Error instance was thrown.
|
|
77
|
-
*/
|
|
78
|
-
export async function pExpectedError(promise, errorClass) {
|
|
79
|
-
try {
|
|
80
|
-
await promise;
|
|
81
|
-
}
|
|
82
|
-
catch (err) {
|
|
83
|
-
if (errorClass && !(err instanceof errorClass)) {
|
|
84
|
-
console.warn(`pExpectedError expected ${errorClass.constructor.name} but got different error class`);
|
|
85
|
-
throw err;
|
|
86
|
-
}
|
|
87
|
-
return err; // this is expected!
|
|
88
|
-
}
|
|
89
|
-
// Unexpected!
|
|
90
|
-
throw new UnexpectedPassError();
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Shortcut function to simplify error snapshot-matching in tests.
|
|
94
|
-
*/
|
|
95
|
-
export async function pExpectedErrorString(promise, errorClass) {
|
|
96
|
-
const err = await pExpectedError(promise, errorClass);
|
|
97
|
-
return _stringify(err);
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Shortcut function to simplify error snapshot-matching in tests.
|
|
101
|
-
*/
|
|
102
|
-
export function _expectedErrorString(fn, errorClass) {
|
|
103
|
-
const err = _expectedError(fn, errorClass);
|
|
104
|
-
return _stringify(err);
|
|
105
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { _anyToError, _since } from '../index';
|
|
2
|
-
/**
|
|
3
|
-
* Decorates a function with "try/catch", so it'll never reject/throw.
|
|
4
|
-
* Only applies to async functions (or, turns sync function into async).
|
|
5
|
-
*
|
|
6
|
-
* Allows to pass onError callback.
|
|
7
|
-
*
|
|
8
|
-
* @experimental
|
|
9
|
-
*/
|
|
10
|
-
export function _tryCatch(fn, opt = {}) {
|
|
11
|
-
const { onError, logError = true, logSuccess = false, logger = console } = opt;
|
|
12
|
-
const fname = fn.name || 'anonymous';
|
|
13
|
-
return async function (...args) {
|
|
14
|
-
const started = Date.now();
|
|
15
|
-
try {
|
|
16
|
-
const r = await fn.apply(this, args);
|
|
17
|
-
if (logSuccess) {
|
|
18
|
-
logger.log(`tryCatch.${fname} succeeded in ${_since(started)}`);
|
|
19
|
-
}
|
|
20
|
-
return r;
|
|
21
|
-
}
|
|
22
|
-
catch (err) {
|
|
23
|
-
if (logError) {
|
|
24
|
-
logger.warn(`tryCatch.${fname} error in ${_since(started)}:`, err);
|
|
25
|
-
}
|
|
26
|
-
if (onError) {
|
|
27
|
-
try {
|
|
28
|
-
return await onError(_anyToError(err));
|
|
29
|
-
}
|
|
30
|
-
catch { }
|
|
31
|
-
}
|
|
32
|
-
// returns undefined, but doesn't rethrow
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
37
|
-
export const _TryCatch = (opt = {}) => (_target, _key, descriptor) => {
|
|
38
|
-
const originalFn = descriptor.value;
|
|
39
|
-
descriptor.value = _tryCatch(originalFn, opt);
|
|
40
|
-
return descriptor;
|
|
41
|
-
};
|
package/dist-esm/form.util.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Convert any object to FormData.
|
|
3
|
-
* Please note that every key and value of FormData is `string`.
|
|
4
|
-
* Even if you pass a number - it'll be converted to string.
|
|
5
|
-
* Think URLSearchParams.
|
|
6
|
-
*/
|
|
7
|
-
export function objectToFormData(obj = {}) {
|
|
8
|
-
const fd = new FormData();
|
|
9
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
10
|
-
fd.append(k, v);
|
|
11
|
-
}
|
|
12
|
-
return fd;
|
|
13
|
-
}
|
|
14
|
-
export function formDataToObject(formData) {
|
|
15
|
-
return Object.fromEntries(formData);
|
|
16
|
-
}
|