@naturalcycles/js-lib 15.16.1 → 15.17.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.
Files changed (38) hide show
  1. package/dist/browser/analytics.util.js +1 -1
  2. package/dist/decorators/logMethod.decorator.d.ts +1 -1
  3. package/dist/decorators/logMethod.decorator.js +3 -3
  4. package/dist/decorators/retry.decorator.d.ts +1 -1
  5. package/dist/decorators/retry.decorator.js +1 -1
  6. package/dist/error/tryCatch.d.ts +1 -1
  7. package/dist/error/tryCatch.js +1 -1
  8. package/dist/index.d.ts +1 -0
  9. package/dist/json-schema/jsonSchema.util.js +1 -1
  10. package/dist/json-schema/jsonSchemaBuilder.d.ts +1 -2
  11. package/dist/json-schema/jsonSchemaBuilder.js +3 -2
  12. package/dist/math/stack.util.js +1 -1
  13. package/dist/promise/pMap.d.ts +2 -2
  14. package/dist/promise/pMap.js +1 -1
  15. package/dist/promise/pRetry.d.ts +2 -2
  16. package/dist/promise/pRetry.js +2 -2
  17. package/dist/typeFest.d.ts +0 -49
  18. package/dist/types.d.ts +48 -0
  19. package/dist/types.js +1 -0
  20. package/dist/validation/validation.d.ts +30 -0
  21. package/dist/validation/validation.js +1 -0
  22. package/package.json +10 -3
  23. package/src/browser/analytics.util.ts +1 -1
  24. package/src/decorators/logMethod.decorator.ts +4 -4
  25. package/src/decorators/retry.decorator.ts +2 -2
  26. package/src/error/tryCatch.ts +2 -2
  27. package/src/index.ts +1 -0
  28. package/src/json-schema/jsonSchema.util.ts +1 -1
  29. package/src/json-schema/jsonSchemaBuilder.ts +7 -8
  30. package/src/math/stack.util.ts +1 -1
  31. package/src/promise/pMap.ts +2 -2
  32. package/src/promise/pRetry.ts +4 -3
  33. package/src/typeFest.ts +0 -61
  34. package/src/types.ts +62 -0
  35. package/src/validation/validation.ts +35 -0
  36. package/dist/log/index.d.ts +0 -1
  37. package/dist/log/index.js +0 -1
  38. package/src/log/index.ts +0 -1
@@ -1,4 +1,4 @@
1
- import { isServerSide } from '../index.js';
1
+ import { isServerSide } from '../env.js';
2
2
  import { loadScript } from './script.util.js';
3
3
  /**
4
4
  * Pass enabled = false to only init globalThis.gtag, but not load actual gtag script (e.g in dev mode).
@@ -1,4 +1,4 @@
1
- import type { CommonLogger } from '../log/index.js';
1
+ import type { CommonLogger } from '../log/commonLogger.js';
2
2
  /**
3
3
  * $r - result
4
4
  *
@@ -1,7 +1,7 @@
1
1
  import { _ms } from '../datetime/time.util.js';
2
- import { _assert } from '../error/index.js';
3
- import { SimpleMovingAverage } from '../math/index.js';
4
- import { _stringify } from '../string/index.js';
2
+ import { _assert } from '../error/assert.js';
3
+ import { SimpleMovingAverage } from '../math/sma.js';
4
+ import { _stringify } from '../string/stringify.js';
5
5
  import { _getArgsSignature, _getMethodSignature } from './decorator.util.js';
6
6
  /**
7
7
  * Console-logs when method had started, when it finished, time taken and if error happened.
@@ -1,2 +1,2 @@
1
- import type { PRetryOptions } from '../promise/index.js';
1
+ import type { PRetryOptions } from '../promise/pRetry.js';
2
2
  export declare function _Retry(opt?: PRetryOptions): MethodDecorator;
@@ -1,4 +1,4 @@
1
- import { pRetryFn } from '../promise/index.js';
1
+ import { pRetryFn } from '../promise/pRetry.js';
2
2
  // eslint-disable-next-line @typescript-eslint/naming-convention
3
3
  export function _Retry(opt = {}) {
4
4
  return (_target, _key, descriptor) => {
@@ -1,4 +1,4 @@
1
- import type { CommonLogger } from '../log/index.js';
1
+ import type { CommonLogger } from '../log/commonLogger.js';
2
2
  import type { AnyFunction } from '../types.js';
3
3
  export interface TryCatchOptions {
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { _since } from '../datetime/index.js';
1
+ import { _since } from '../datetime/time.util.js';
2
2
  import { _anyToError } from './error.util.js';
3
3
  /**
4
4
  * Decorates a function with "try/catch", so it'll never reject/throw.
package/dist/index.d.ts CHANGED
@@ -14,4 +14,5 @@ export * from './number/createDeterministicRandom.js';
14
14
  export * from './number/number.util.js';
15
15
  export * from './polyfill.js';
16
16
  export * from './unit/size.util.js';
17
+ export type * from './validation/validation.js';
17
18
  export * from './web.js';
@@ -1,4 +1,4 @@
1
- import { _uniq } from '../index.js';
1
+ import { _uniq } from '../array/array.util.js';
2
2
  import { _filterNullishValues } from '../object/object.util.js';
3
3
  /**
4
4
  * Merges s2 into s1 (mutates s1) and returns s1.
@@ -1,6 +1,5 @@
1
1
  import type { AnyObject, BaseDBEntity } from '../types.js';
2
- import { type JsonSchemaAllOf, type JsonSchemaArray, type JsonSchemaOneOf, type JsonSchemaTuple } from './index.js';
3
- import type { JsonSchema, JsonSchemaAny, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaRef, JsonSchemaString } from './jsonSchema.model.js';
2
+ import type { JsonSchema, JsonSchemaAllOf, JsonSchemaAny, JsonSchemaArray, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaOneOf, JsonSchemaRef, JsonSchemaString, JsonSchemaTuple } from './jsonSchema.model.js';
4
3
  export interface JsonSchemaBuilder<T = unknown> {
5
4
  build: () => JsonSchema<T>;
6
5
  }
@@ -1,7 +1,8 @@
1
1
  import { _uniq } from '../array/array.util.js';
2
- import { _deepCopy, _sortObject } from '../object/index.js';
3
- import { mergeJsonSchemaObjects, } from './index.js';
2
+ import { _deepCopy } from '../object/object.util.js';
3
+ import { _sortObject } from '../object/sortObject.js';
4
4
  import { JSON_SCHEMA_ORDER } from './jsonSchema.cnst.js';
5
+ import { mergeJsonSchemaObjects } from './jsonSchema.util.js';
5
6
  /**
6
7
  * Fluent (chainable) API to manually create Json Schemas.
7
8
  * Inspired by Joi
@@ -1,5 +1,5 @@
1
1
  import { _range } from '../array/range.js';
2
- import { _average, _percentile, _percentiles } from '../math/index.js';
2
+ import { _average, _percentile, _percentiles } from './math.util.js';
3
3
  /**
4
4
  * Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
5
5
  * Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
@@ -1,5 +1,5 @@
1
- import { ErrorMode } from '../error/index.js';
2
- import type { CommonLogger } from '../log/index.js';
1
+ import { ErrorMode } from '../error/errorMode.js';
2
+ import type { CommonLogger } from '../log/commonLogger.js';
3
3
  import { type AbortableAsyncMapper } from '../types.js';
4
4
  export interface PMapOptions {
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { ErrorMode } from '../error/index.js';
1
+ import { ErrorMode } from '../error/errorMode.js';
2
2
  import { END, SKIP } from '../types.js';
3
3
  /**
4
4
  * Forked from https://github.com/sindresorhus/p-map
@@ -1,5 +1,5 @@
1
- import { type ErrorData } from '../error/index.js';
2
- import type { CommonLogger } from '../log/index.js';
1
+ import type { ErrorData } from '../error/error.model.js';
2
+ import type { CommonLogger } from '../log/commonLogger.js';
3
3
  import type { AnyFunction } from '../types.js';
4
4
  export interface PRetryOptions {
5
5
  /**
@@ -1,5 +1,5 @@
1
- import { _since } from '../datetime/index.js';
2
- import { _errorDataAppend } from '../error/index.js';
1
+ import { _since } from '../datetime/time.util.js';
2
+ import { _errorDataAppend } from '../error/error.util.js';
3
3
  import { pDelay } from './pDelay.js';
4
4
  import { pTimeout } from './pTimeout.js';
5
5
  /**
@@ -1,4 +1,3 @@
1
- import type { Primitive } from './types.js';
2
1
  /**
3
2
  Flatten the type output to improve type hints shown in editors.
4
3
  */
@@ -44,54 +43,6 @@ type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ?
44
43
  export type Except<ObjectType, KeysType extends keyof ObjectType> = {
45
44
  [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
46
45
  };
47
- /**
48
- Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
49
-
50
- This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
51
-
52
- Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
53
-
54
- @example
55
- ```
56
- // data.json
57
- {
58
- "foo": ["bar"]
59
- }
60
-
61
- // main.ts
62
- import {ReadonlyDeep} from 'type-fest';
63
- import dataJson = require('./data.json');
64
-
65
- const data: ReadonlyDeep<typeof dataJson> = dataJson;
66
-
67
- export default data;
68
-
69
- // test.ts
70
- import data from './main';
71
-
72
- data.foo.push('bar');
73
- //=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
74
- ```
75
-
76
- @category Utilities
77
- */
78
- export type ReadonlyDeep<T> = T extends Primitive | ((...args: any[]) => unknown) ? T : T extends ReadonlyMap<infer KeyType, infer ValueType> ? ReadonlyMapDeep<KeyType, ValueType> : T extends ReadonlySet<infer ItemType> ? ReadonlySetDeep<ItemType> : T extends object ? ReadonlyObjectDeep<T> : unknown;
79
- /**
80
- Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
81
- */
82
- interface ReadonlyMapDeep<KeyType, ValueType> extends ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>> {
83
- }
84
- /**
85
- Same as `ReadonlyDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `ReadonlyDeep`.
86
- */
87
- interface ReadonlySetDeep<ItemType> extends ReadonlySet<ReadonlyDeep<ItemType>> {
88
- }
89
- /**
90
- Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper for `ReadonlyDeep`.
91
- */
92
- type ReadonlyObjectDeep<ObjectType extends object> = {
93
- readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>;
94
- };
95
46
  /**
96
47
  * Pick only index signatures from the given object type, leaving out all explicitly defined properties.
97
48
  * This is the counterpart of `OmitIndexSignature`.
package/dist/types.d.ts CHANGED
@@ -343,4 +343,52 @@ export type Promisable<T> = T | PromiseLike<T>;
343
343
  Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
344
344
  */
345
345
  export type Class<T = any> = new (...args: any[]) => T;
346
+ /**
347
+ Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
348
+
349
+ This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
350
+
351
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
352
+
353
+ @example
354
+ ```
355
+ // data.json
356
+ {
357
+ "foo": ["bar"]
358
+ }
359
+
360
+ // main.ts
361
+ import {ReadonlyDeep} from 'type-fest';
362
+ import dataJson = require('./data.json');
363
+
364
+ const data: ReadonlyDeep<typeof dataJson> = dataJson;
365
+
366
+ export default data;
367
+
368
+ // test.ts
369
+ import data from './main';
370
+
371
+ data.foo.push('bar');
372
+ //=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
373
+ ```
374
+
375
+ @category Utilities
376
+ */
377
+ export type ReadonlyDeep<T> = T extends Primitive | ((...args: any[]) => unknown) ? T : T extends ReadonlyMap<infer KeyType, infer ValueType> ? ReadonlyMapDeep<KeyType, ValueType> : T extends ReadonlySet<infer ItemType> ? ReadonlySetDeep<ItemType> : T extends object ? ReadonlyObjectDeep<T> : unknown;
378
+ /**
379
+ Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
380
+ */
381
+ interface ReadonlyMapDeep<KeyType, ValueType> extends ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>> {
382
+ }
383
+ /**
384
+ Same as `ReadonlyDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `ReadonlyDeep`.
385
+ */
386
+ interface ReadonlySetDeep<ItemType> extends ReadonlySet<ReadonlyDeep<ItemType>> {
387
+ }
388
+ /**
389
+ Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper for `ReadonlyDeep`.
390
+ */
391
+ type ReadonlyObjectDeep<ObjectType extends object> = {
392
+ readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>;
393
+ };
346
394
  export {};
package/dist/types.js CHANGED
@@ -64,3 +64,4 @@ export function _typeCast(v) { }
64
64
  * Type-safe Object.assign that checks that part is indeed a Partial<T>
65
65
  */
66
66
  export const _objectAssign = Object.assign;
67
+ /* eslint-enable */
@@ -0,0 +1,30 @@
1
+ import type { AppError } from '../error/error.util.js';
2
+ import type { ErrorDataTuple } from '../types.js';
3
+ /**
4
+ * Item to be validated.
5
+ * Can be null or undefined, which allows validation function to produce an error,
6
+ * if undefined/null are not accepted. But they might be accepted too, it depends
7
+ * on the schema (implementation detail of the ValidationFunction).
8
+ *
9
+ * Function returns a tuple of [err, item].
10
+ * In case of error it will be [err, null].
11
+ * In case of success it is [null, item].
12
+ *
13
+ * ValidationFunction may mutate the input item or not,
14
+ * depending on the implementation.
15
+ *
16
+ * @experimental
17
+ */
18
+ export type ValidationFunction<T, ERR extends AppError> = (item: T | null | undefined, opt?: ValidationFunctionOptions) => ErrorDataTuple<T, ERR>;
19
+ export interface ValidationFunctionOptions {
20
+ /**
21
+ * E.g User
22
+ * Used for error message printing.
23
+ */
24
+ itemName?: string;
25
+ /**
26
+ * E.g `12345678` (user id).
27
+ * Used for error message printing.
28
+ */
29
+ itemId?: string;
30
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.16.1",
4
+ "version": "15.17.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "zod": "^4"
@@ -18,19 +18,27 @@
18
18
  "./cfg/frontend/tsconfig.json": "./cfg/frontend/tsconfig.json",
19
19
  ".": "./dist/index.js",
20
20
  "./browser": "./dist/browser/index.js",
21
+ "./browser/*.js": "./dist/browser/*.js",
21
22
  "./datetime": "./dist/datetime/index.js",
23
+ "./datetime/*.js": "./dist/datetime/*.js",
22
24
  "./decorators": "./dist/decorators/index.js",
25
+ "./decorators/*.js": "./dist/decorators/*.js",
23
26
  "./deviceIdService": "./dist/deviceIdService.js",
24
27
  "./error": "./dist/error/index.js",
28
+ "./error/*.js": "./dist/error/*.js",
25
29
  "./http": "./dist/http/index.js",
26
30
  "./json-schema": "./dist/json-schema/index.js",
27
- "./log": "./dist/log/index.js",
31
+ "./log": "./dist/log/commonLogger.js",
28
32
  "./math": "./dist/math/index.js",
33
+ "./math/*.js": "./dist/math/*.js",
29
34
  "./object": "./dist/object/index.js",
35
+ "./object/*.js": "./dist/object/*.js",
30
36
  "./promise": "./dist/promise/index.js",
37
+ "./promise/*.js": "./dist/promise/*.js",
31
38
  "./nanoid": "./dist/nanoid.js",
32
39
  "./semver": "./dist/semver.js",
33
40
  "./string": "./dist/string/index.js",
41
+ "./string/*.js": "./dist/string/*.js",
34
42
  "./types": "./dist/types.js",
35
43
  "./zod": "./dist/zod/index.js"
36
44
  },
@@ -42,7 +50,6 @@
42
50
  "!src/**/*.test.*",
43
51
  "!src/**/__snapshots__"
44
52
  ],
45
- "main": "dist/index.js",
46
53
  "types": "dist/index.d.ts",
47
54
  "sideEffects": false,
48
55
  "engines": {
@@ -1,4 +1,4 @@
1
- import { isServerSide } from '../index.js'
1
+ import { isServerSide } from '../env.js'
2
2
  import { loadScript } from './script.util.js'
3
3
 
4
4
  declare global {
@@ -1,8 +1,8 @@
1
1
  import { _ms } from '../datetime/time.util.js'
2
- import { _assert } from '../error/index.js'
3
- import type { CommonLogger } from '../log/index.js'
4
- import { SimpleMovingAverage } from '../math/index.js'
5
- import { _stringify } from '../string/index.js'
2
+ import { _assert } from '../error/assert.js'
3
+ import type { CommonLogger } from '../log/commonLogger.js'
4
+ import { SimpleMovingAverage } from '../math/sma.js'
5
+ import { _stringify } from '../string/stringify.js'
6
6
  import { _getArgsSignature, _getMethodSignature } from './decorator.util.js'
7
7
 
8
8
  /**
@@ -1,5 +1,5 @@
1
- import type { PRetryOptions } from '../promise/index.js'
2
- import { pRetryFn } from '../promise/index.js'
1
+ import type { PRetryOptions } from '../promise/pRetry.js'
2
+ import { pRetryFn } from '../promise/pRetry.js'
3
3
 
4
4
  // eslint-disable-next-line @typescript-eslint/naming-convention
5
5
  export function _Retry(opt: PRetryOptions = {}): MethodDecorator {
@@ -1,5 +1,5 @@
1
- import { _since } from '../datetime/index.js'
2
- import type { CommonLogger } from '../log/index.js'
1
+ import { _since } from '../datetime/time.util.js'
2
+ import type { CommonLogger } from '../log/commonLogger.js'
3
3
  import type { AnyFunction, UnixTimestampMillis } from '../types.js'
4
4
  import { _anyToError } from './error.util.js'
5
5
 
package/src/index.ts CHANGED
@@ -14,4 +14,5 @@ export * from './number/createDeterministicRandom.js'
14
14
  export * from './number/number.util.js'
15
15
  export * from './polyfill.js'
16
16
  export * from './unit/size.util.js'
17
+ export type * from './validation/validation.js'
17
18
  export * from './web.js'
@@ -1,4 +1,4 @@
1
- import { _uniq } from '../index.js'
1
+ import { _uniq } from '../array/array.util.js'
2
2
  import { _filterNullishValues } from '../object/object.util.js'
3
3
  import type { AnyObject } from '../types.js'
4
4
  import type { JsonSchemaObject } from './jsonSchema.model.js'
@@ -1,26 +1,25 @@
1
1
  import { _uniq } from '../array/array.util.js'
2
- import { _deepCopy, _sortObject } from '../object/index.js'
2
+ import { _deepCopy } from '../object/object.util.js'
3
+ import { _sortObject } from '../object/sortObject.js'
3
4
  import type { AnyObject, BaseDBEntity } from '../types.js'
4
- import {
5
- type JsonSchemaAllOf,
6
- type JsonSchemaArray,
7
- type JsonSchemaOneOf,
8
- type JsonSchemaTuple,
9
- mergeJsonSchemaObjects,
10
- } from './index.js'
11
5
  import { JSON_SCHEMA_ORDER } from './jsonSchema.cnst.js'
12
6
  import type {
13
7
  JsonSchema,
8
+ JsonSchemaAllOf,
14
9
  JsonSchemaAny,
10
+ JsonSchemaArray,
15
11
  JsonSchemaBoolean,
16
12
  JsonSchemaConst,
17
13
  JsonSchemaEnum,
18
14
  JsonSchemaNull,
19
15
  JsonSchemaNumber,
20
16
  JsonSchemaObject,
17
+ JsonSchemaOneOf,
21
18
  JsonSchemaRef,
22
19
  JsonSchemaString,
20
+ JsonSchemaTuple,
23
21
  } from './jsonSchema.model.js'
22
+ import { mergeJsonSchemaObjects } from './jsonSchema.util.js'
24
23
 
25
24
  /* eslint-disable id-blacklist, @typescript-eslint/explicit-module-boundary-types */
26
25
 
@@ -1,5 +1,5 @@
1
1
  import { _range } from '../array/range.js'
2
- import { _average, _percentile, _percentiles } from '../math/index.js'
2
+ import { _average, _percentile, _percentiles } from './math.util.js'
3
3
 
4
4
  /**
5
5
  * Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
@@ -1,5 +1,5 @@
1
- import { ErrorMode } from '../error/index.js'
2
- import type { CommonLogger } from '../log/index.js'
1
+ import { ErrorMode } from '../error/errorMode.js'
2
+ import type { CommonLogger } from '../log/commonLogger.js'
3
3
  import { type AbortableAsyncMapper, END, SKIP } from '../types.js'
4
4
 
5
5
  export interface PMapOptions {
@@ -1,6 +1,7 @@
1
- import { _since } from '../datetime/index.js'
2
- import { _errorDataAppend, type ErrorData } from '../error/index.js'
3
- import type { CommonLogger } from '../log/index.js'
1
+ import { _since } from '../datetime/time.util.js'
2
+ import type { ErrorData } from '../error/error.model.js'
3
+ import { _errorDataAppend } from '../error/error.util.js'
4
+ import type { CommonLogger } from '../log/commonLogger.js'
4
5
  import type { AnyFunction, UnixTimestampMillis } from '../types.js'
5
6
  import { pDelay } from './pDelay.js'
6
7
  import { pTimeout } from './pTimeout.js'
package/src/typeFest.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  /* eslint-disable */
2
2
 
3
- import type { Primitive } from './types.js'
4
-
5
3
  /**
6
4
  Flatten the type output to improve type hints shown in editors.
7
5
  */
@@ -51,65 +49,6 @@ export type Except<ObjectType, KeysType extends keyof ObjectType> = {
51
49
  [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType]
52
50
  }
53
51
 
54
- /**
55
- Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
56
-
57
- This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
58
-
59
- Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
60
-
61
- @example
62
- ```
63
- // data.json
64
- {
65
- "foo": ["bar"]
66
- }
67
-
68
- // main.ts
69
- import {ReadonlyDeep} from 'type-fest';
70
- import dataJson = require('./data.json');
71
-
72
- const data: ReadonlyDeep<typeof dataJson> = dataJson;
73
-
74
- export default data;
75
-
76
- // test.ts
77
- import data from './main';
78
-
79
- data.foo.push('bar');
80
- //=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
81
- ```
82
-
83
- @category Utilities
84
- */
85
- export type ReadonlyDeep<T> = T extends Primitive | ((...args: any[]) => unknown)
86
- ? T
87
- : T extends ReadonlyMap<infer KeyType, infer ValueType>
88
- ? ReadonlyMapDeep<KeyType, ValueType>
89
- : T extends ReadonlySet<infer ItemType>
90
- ? ReadonlySetDeep<ItemType>
91
- : T extends object
92
- ? ReadonlyObjectDeep<T>
93
- : unknown
94
-
95
- /**
96
- Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
97
- */
98
- interface ReadonlyMapDeep<KeyType, ValueType>
99
- extends ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>> {}
100
-
101
- /**
102
- Same as `ReadonlyDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `ReadonlyDeep`.
103
- */
104
- interface ReadonlySetDeep<ItemType> extends ReadonlySet<ReadonlyDeep<ItemType>> {}
105
-
106
- /**
107
- Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper for `ReadonlyDeep`.
108
- */
109
- type ReadonlyObjectDeep<ObjectType extends object> = {
110
- readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>
111
- }
112
-
113
52
  /**
114
53
  * Pick only index signatures from the given object type, leaving out all explicitly defined properties.
115
54
  * This is the counterpart of `OmitIndexSignature`.
package/src/types.ts CHANGED
@@ -443,3 +443,65 @@ export type Promisable<T> = T | PromiseLike<T>
443
443
  Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
444
444
  */
445
445
  export type Class<T = any> = new (...args: any[]) => T
446
+
447
+ /**
448
+ Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their keys/elements into immutable structures recursively.
449
+
450
+ This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
451
+
452
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
453
+
454
+ @example
455
+ ```
456
+ // data.json
457
+ {
458
+ "foo": ["bar"]
459
+ }
460
+
461
+ // main.ts
462
+ import {ReadonlyDeep} from 'type-fest';
463
+ import dataJson = require('./data.json');
464
+
465
+ const data: ReadonlyDeep<typeof dataJson> = dataJson;
466
+
467
+ export default data;
468
+
469
+ // test.ts
470
+ import data from './main';
471
+
472
+ data.foo.push('bar');
473
+ //=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
474
+ ```
475
+
476
+ @category Utilities
477
+ */
478
+ /* eslint-disable @typescript-eslint/no-restricted-types */
479
+ export type ReadonlyDeep<T> = T extends Primitive | ((...args: any[]) => unknown)
480
+ ? T
481
+ : T extends ReadonlyMap<infer KeyType, infer ValueType>
482
+ ? ReadonlyMapDeep<KeyType, ValueType>
483
+ : T extends ReadonlySet<infer ItemType>
484
+ ? ReadonlySetDeep<ItemType>
485
+ : T extends object
486
+ ? ReadonlyObjectDeep<T>
487
+ : unknown
488
+
489
+ /**
490
+ Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
491
+ */
492
+ interface ReadonlyMapDeep<KeyType, ValueType>
493
+ extends ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>> {}
494
+
495
+ /**
496
+ Same as `ReadonlyDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `ReadonlyDeep`.
497
+ */
498
+ interface ReadonlySetDeep<ItemType> extends ReadonlySet<ReadonlyDeep<ItemType>> {}
499
+
500
+ /**
501
+ Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper for `ReadonlyDeep`.
502
+ */
503
+ type ReadonlyObjectDeep<ObjectType extends object> = {
504
+ readonly [KeyType in keyof ObjectType]: ReadonlyDeep<ObjectType[KeyType]>
505
+ }
506
+
507
+ /* eslint-enable */
@@ -0,0 +1,35 @@
1
+ import type { AppError } from '../error/error.util.js'
2
+ import type { ErrorDataTuple } from '../types.js'
3
+
4
+ /**
5
+ * Item to be validated.
6
+ * Can be null or undefined, which allows validation function to produce an error,
7
+ * if undefined/null are not accepted. But they might be accepted too, it depends
8
+ * on the schema (implementation detail of the ValidationFunction).
9
+ *
10
+ * Function returns a tuple of [err, item].
11
+ * In case of error it will be [err, null].
12
+ * In case of success it is [null, item].
13
+ *
14
+ * ValidationFunction may mutate the input item or not,
15
+ * depending on the implementation.
16
+ *
17
+ * @experimental
18
+ */
19
+ export type ValidationFunction<T, ERR extends AppError> = (
20
+ item: T | null | undefined,
21
+ opt?: ValidationFunctionOptions,
22
+ ) => ErrorDataTuple<T, ERR>
23
+
24
+ export interface ValidationFunctionOptions {
25
+ /**
26
+ * E.g User
27
+ * Used for error message printing.
28
+ */
29
+ itemName?: string
30
+ /**
31
+ * E.g `12345678` (user id).
32
+ * Used for error message printing.
33
+ */
34
+ itemId?: string
35
+ }
@@ -1 +0,0 @@
1
- export * from './commonLogger.js';
package/dist/log/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './commonLogger.js';
package/src/log/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './commonLogger.js'