@naturalcycles/js-lib 14.159.0 → 14.161.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 (53) hide show
  1. package/dist/{lazy.d.ts → define.d.ts} +26 -0
  2. package/dist/define.js +118 -0
  3. package/dist/error/app.error.d.ts +16 -3
  4. package/dist/error/app.error.js +22 -19
  5. package/dist/error/assert.d.ts +1 -1
  6. package/dist/error/assert.js +2 -2
  7. package/dist/error/error.model.d.ts +7 -0
  8. package/dist/error/error.util.js +29 -22
  9. package/dist/error/httpRequestError.d.ts +2 -2
  10. package/dist/error/httpRequestError.js +7 -2
  11. package/dist/error/jsonParseError.d.ts +2 -2
  12. package/dist/error/jsonParseError.js +5 -2
  13. package/dist/error/try.js +3 -1
  14. package/dist/http/fetcher.js +6 -3
  15. package/dist/http/fetcher.model.d.ts +1 -0
  16. package/dist/http/fetcher.model.js +1 -0
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/object/object.util.d.ts +16 -11
  20. package/dist/object/object.util.js +29 -12
  21. package/dist/promise/pTimeout.d.ts +3 -3
  22. package/dist/promise/pTimeout.js +2 -2
  23. package/dist/types.d.ts +9 -2
  24. package/dist/types.js +9 -2
  25. package/dist-esm/{lazy.js → define.js} +38 -0
  26. package/dist-esm/error/app.error.js +22 -19
  27. package/dist-esm/error/assert.js +2 -2
  28. package/dist-esm/error/error.util.js +30 -23
  29. package/dist-esm/error/httpRequestError.js +7 -2
  30. package/dist-esm/error/jsonParseError.js +5 -2
  31. package/dist-esm/error/try.js +3 -1
  32. package/dist-esm/http/fetcher.js +6 -3
  33. package/dist-esm/http/fetcher.model.js +1 -0
  34. package/dist-esm/index.js +1 -1
  35. package/dist-esm/object/object.util.js +27 -11
  36. package/dist-esm/promise/pTimeout.js +2 -2
  37. package/dist-esm/types.js +8 -1
  38. package/package.json +1 -1
  39. package/src/{lazy.ts → define.ts} +68 -0
  40. package/src/error/app.error.ts +39 -22
  41. package/src/error/assert.ts +2 -2
  42. package/src/error/error.model.ts +8 -0
  43. package/src/error/error.util.ts +31 -25
  44. package/src/error/httpRequestError.ts +9 -3
  45. package/src/error/jsonParseError.ts +7 -8
  46. package/src/error/try.ts +7 -1
  47. package/src/http/fetcher.model.ts +2 -0
  48. package/src/http/fetcher.ts +6 -3
  49. package/src/index.ts +1 -1
  50. package/src/object/object.util.ts +51 -30
  51. package/src/promise/pTimeout.ts +4 -4
  52. package/src/types.ts +12 -2
  53. package/dist/lazy.js +0 -65
@@ -28,3 +28,29 @@ export declare function _defineLazyProperty<OBJ extends AnyObject>(obj: OBJ, pro
28
28
  * Like _defineLazyProperty, but allows to define multiple props at once.
29
29
  */
30
30
  export declare function _defineLazyProps<OBJ extends AnyObject>(obj: OBJ, props: Partial<Record<keyof OBJ, AnyFunction>>): OBJ;
31
+ /**
32
+ * Same as Object.defineProperty, but with better (least restricting) defaults.
33
+ *
34
+ * Defaults are:
35
+ * writable: true
36
+ * configurable: true
37
+ * enumerable: true
38
+ * value: existing obj[prop] value
39
+ *
40
+ * Original defaults:
41
+ * writable: false
42
+ * configurable: false
43
+ * enumerable: false
44
+ * value: existing obj[prop] value
45
+ *
46
+ */
47
+ export declare function _defineProperty<T extends AnyObject>(obj: T, prop: keyof T, pd: PropertyDescriptor): T;
48
+ /**
49
+ * Object.defineProperties with better defaults.
50
+ * See _defineProperty for exact defaults definition.
51
+ */
52
+ export declare function _defineProps<T extends AnyObject>(obj: T, props: Partial<Record<keyof T, PropertyDescriptor>>): T;
53
+ /**
54
+ * Like _defineProps, but skips props with nullish values.
55
+ */
56
+ export declare function _defineNonNullishProps<T extends AnyObject>(obj: T, props: Partial<Record<keyof T, PropertyDescriptor>>): T;
package/dist/define.js ADDED
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._defineNonNullishProps = exports._defineProps = exports._defineProperty = exports._defineLazyProps = exports._defineLazyProperty = exports._lazyValue = void 0;
4
+ const object_util_1 = require("./object/object.util");
5
+ const types_1 = require("./types");
6
+ /**
7
+ * const value = lazyValue(() => expensiveComputation())
8
+ *
9
+ * value() // calls expensiveComputation() once
10
+ * value() // returns cached result
11
+ * value() // returns cached result
12
+ *
13
+ * Based on: https://github.com/sindresorhus/lazy-value
14
+ */
15
+ function _lazyValue(fn) {
16
+ let isCalled = false;
17
+ let result;
18
+ return (() => {
19
+ if (!isCalled) {
20
+ isCalled = true;
21
+ result = fn();
22
+ }
23
+ return result;
24
+ });
25
+ }
26
+ exports._lazyValue = _lazyValue;
27
+ /**
28
+ * interface Obj {
29
+ * v: number
30
+ * }
31
+ *
32
+ * const obj = {} as Obj
33
+ *
34
+ * _defineLazyProperty(obj, 'v', () => expensiveComputation())
35
+ * obj.v // runs expensiveComputation() once
36
+ * obj.v // cached value
37
+ * obj.v // cached value
38
+ *
39
+ * Based on: https://github.com/sindresorhus/define-lazy-prop
40
+ */
41
+ function _defineLazyProperty(obj, propertyName, fn) {
42
+ const define = (value) => {
43
+ Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true });
44
+ };
45
+ Object.defineProperty(obj, propertyName, {
46
+ configurable: true,
47
+ enumerable: true,
48
+ get() {
49
+ const result = fn();
50
+ define(result);
51
+ return result;
52
+ },
53
+ set(value) {
54
+ define(value);
55
+ },
56
+ });
57
+ return obj;
58
+ }
59
+ exports._defineLazyProperty = _defineLazyProperty;
60
+ /**
61
+ * Like _defineLazyProperty, but allows to define multiple props at once.
62
+ */
63
+ function _defineLazyProps(obj, props) {
64
+ Object.entries(props).forEach(([k, fn]) => _defineLazyProperty(obj, k, fn));
65
+ return obj;
66
+ }
67
+ exports._defineLazyProps = _defineLazyProps;
68
+ /**
69
+ * Same as Object.defineProperty, but with better (least restricting) defaults.
70
+ *
71
+ * Defaults are:
72
+ * writable: true
73
+ * configurable: true
74
+ * enumerable: true
75
+ * value: existing obj[prop] value
76
+ *
77
+ * Original defaults:
78
+ * writable: false
79
+ * configurable: false
80
+ * enumerable: false
81
+ * value: existing obj[prop] value
82
+ *
83
+ */
84
+ function _defineProperty(obj, prop, pd) {
85
+ return Object.defineProperty(obj, prop, {
86
+ writable: true,
87
+ configurable: true,
88
+ enumerable: true,
89
+ // value: obj[prop], // existing value is already kept by default
90
+ ...pd,
91
+ });
92
+ }
93
+ exports._defineProperty = _defineProperty;
94
+ /**
95
+ * Object.defineProperties with better defaults.
96
+ * See _defineProperty for exact defaults definition.
97
+ */
98
+ function _defineProps(obj, props) {
99
+ return Object.defineProperties(obj, (0, object_util_1._mapValues)(props, (k, pd) => ({
100
+ writable: true,
101
+ configurable: true,
102
+ enumerable: true,
103
+ // value: obj[k], // existing value is already kept by default
104
+ ...pd,
105
+ })));
106
+ }
107
+ exports._defineProps = _defineProps;
108
+ /**
109
+ * Like _defineProps, but skips props with nullish values.
110
+ */
111
+ function _defineNonNullishProps(obj, props) {
112
+ return _defineProps(obj, (0, object_util_1._mapObject)(props, (k, pd) => {
113
+ if (pd.value === null || pd.value === undefined)
114
+ return types_1.SKIP;
115
+ return [k, pd];
116
+ }));
117
+ }
118
+ exports._defineNonNullishProps = _defineNonNullishProps;
@@ -4,15 +4,28 @@ import type { ErrorData, ErrorObject } from './error.model';
4
4
  *
5
5
  * message - "technical" message. Frontend decides to show it or not.
6
6
  * data - optional "any" payload.
7
- * data.userMessage - if present, will be displayed to the User as is.
7
+ * data.userFriendly - if present, will be displayed to the User as is.
8
8
  *
9
9
  * Based on: https://medium.com/@xpl/javascript-deriving-from-error-properly-8d2f8f315801
10
10
  */
11
11
  export declare class AppError<DATA_TYPE extends ErrorData = ErrorData> extends Error {
12
12
  data: DATA_TYPE;
13
13
  /**
14
- * cause here is normalized to be an ErrorObject
14
+ * `cause` here is normalized to be an ErrorObject
15
+ */
16
+ cause?: ErrorObject;
17
+ constructor(message: string, data?: DATA_TYPE, opt?: AppErrorOptions);
18
+ }
19
+ /**
20
+ * Extra options for AppError constructor.
21
+ */
22
+ export interface AppErrorOptions {
23
+ /**
24
+ * Overrides Error.name and Error.constructor.name
25
+ */
26
+ name?: string;
27
+ /**
28
+ * Sets Error.cause
15
29
  */
16
30
  cause?: ErrorObject;
17
- constructor(message: string, data?: DATA_TYPE, cause?: ErrorObject, name?: string);
18
31
  }
@@ -6,29 +6,26 @@ exports.AppError = void 0;
6
6
  *
7
7
  * message - "technical" message. Frontend decides to show it or not.
8
8
  * data - optional "any" payload.
9
- * data.userMessage - if present, will be displayed to the User as is.
9
+ * data.userFriendly - if present, will be displayed to the User as is.
10
10
  *
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 = {}, cause, name) {
14
+ constructor(message, data = {}, opt = {}) {
15
15
  super(message);
16
- Object.defineProperty(this, 'name', {
17
- value: name || this.constructor.name,
18
- configurable: true,
19
- writable: true,
20
- });
21
- // this is to allow changing this.constuctor.name to a non-minified version
22
- Object.defineProperty(this.constructor, 'name', {
23
- value: name || this.constructor.name,
24
- configurable: true,
25
- writable: true,
26
- });
27
- Object.defineProperty(this, 'data', {
28
- value: data,
29
- writable: true,
30
- configurable: true,
31
- enumerable: false,
16
+ const { name = this.constructor.name, cause } = opt;
17
+ Object.defineProperties(this, {
18
+ name: {
19
+ value: name,
20
+ configurable: true,
21
+ writable: true,
22
+ },
23
+ data: {
24
+ value: data,
25
+ writable: true,
26
+ configurable: true,
27
+ enumerable: false,
28
+ },
32
29
  });
33
30
  if (cause) {
34
31
  Object.defineProperty(this, 'cause', {
@@ -36,9 +33,15 @@ class AppError extends Error {
36
33
  value: cause,
37
34
  writable: true,
38
35
  configurable: true,
39
- enumerable: false,
36
+ enumerable: true, // unlike standard - setting it to true for "visibility"
40
37
  });
41
38
  }
39
+ // this is to allow changing this.constuctor.name to a non-minified version
40
+ Object.defineProperty(this.constructor, 'name', {
41
+ value: name,
42
+ configurable: true,
43
+ writable: true,
44
+ });
42
45
  // todo: check if it's needed at all!
43
46
  // if (Error.captureStackTrace) {
44
47
  // Error.captureStackTrace(this, this.constructor)
@@ -44,5 +44,5 @@ export declare function _assertIsString(v: any, message?: string): asserts v is
44
44
  export declare function _assertIsNumber(v: any, message?: string): asserts v is number;
45
45
  export declare function _assertTypeOf<T>(v: any, expectedType: string, message?: string): asserts v is T;
46
46
  export declare class AssertionError extends AppError {
47
- constructor(message: string, data?: {}, cause?: ErrorObject);
47
+ constructor(message: string, data?: ErrorData);
48
48
  }
@@ -127,8 +127,8 @@ function _assertTypeOf(v, expectedType, message) {
127
127
  }
128
128
  exports._assertTypeOf = _assertTypeOf;
129
129
  class AssertionError extends app_error_1.AppError {
130
- constructor(message, data = {}, cause) {
131
- super(message, data, cause, 'AssertionError');
130
+ constructor(message, data) {
131
+ super(message, data, { name: 'AssertionError' });
132
132
  }
133
133
  }
134
134
  exports.AssertionError = AssertionError;
@@ -69,6 +69,13 @@ export interface ErrorData {
69
69
  [k: string]: any;
70
70
  }
71
71
  export interface HttpRequestErrorData extends ErrorData {
72
+ /**
73
+ * Actual `fetch` Response as it was returned.
74
+ * Note that not every Request has a Response, hence it's optional.
75
+ *
76
+ * Non-enumerable.
77
+ */
78
+ response?: Response;
72
79
  requestUrl: string;
73
80
  requestBaseUrl?: string;
74
81
  requestMethod: HttpMethod;
@@ -87,36 +87,43 @@ exports._errorLikeToErrorObject = _errorLikeToErrorObject;
87
87
  function _errorObjectToError(o, errorClass = Error) {
88
88
  if (o instanceof errorClass)
89
89
  return o;
90
- const err = new errorClass(o.message);
90
+ // Here we pass constructor values assuming it's AppError or sub-class of it
91
+ // If not - will be checked at the next step
92
+ // We cannot check `if (errorClass instanceof AppError)`, only `err instanceof AppError`
93
+ const { name, cause } = o;
94
+ const err = new errorClass(o.message, o.data, { name, cause });
91
95
  // name: err.name, // cannot be assigned to a readonly property like this
92
96
  // stack: o.stack, // also readonly e.g in Firefox
93
- Object.defineProperty(err, 'name', {
94
- value: o.name,
95
- configurable: true,
96
- writable: true,
97
- });
98
- Object.defineProperty(err.constructor, 'name', {
99
- value: o.name,
100
- configurable: true,
101
- writable: true,
102
- });
103
- Object.defineProperty(err, 'data', {
104
- value: o.data,
105
- writable: true,
106
- configurable: true,
107
- enumerable: false,
108
- });
109
97
  if (o.stack) {
110
98
  Object.defineProperty(err, 'stack', {
111
99
  value: o.stack,
112
100
  });
113
101
  }
114
- if (o.cause) {
115
- Object.defineProperty(err, 'cause', {
116
- value: o.cause,
117
- writable: true,
102
+ if (!(err instanceof __1.AppError)) {
103
+ // Following actions are only needed for non-AppError-like errors
104
+ Object.defineProperties(err, {
105
+ name: {
106
+ value: name,
107
+ configurable: true,
108
+ writable: true,
109
+ },
110
+ data: {
111
+ value: o.data,
112
+ writable: true,
113
+ configurable: true,
114
+ enumerable: false,
115
+ },
116
+ cause: {
117
+ value: cause,
118
+ writable: true,
119
+ configurable: true,
120
+ enumerable: true,
121
+ },
122
+ });
123
+ Object.defineProperty(err.constructor, 'name', {
124
+ value: name,
118
125
  configurable: true,
119
- enumerable: false,
126
+ writable: true,
120
127
  });
121
128
  }
122
129
  return err;
@@ -1,4 +1,4 @@
1
- import { AppError } from './app.error';
1
+ import { AppError, AppErrorOptions } from './app.error';
2
2
  import type { ErrorObject, HttpRequestErrorData } from './error.model';
3
3
  /**
4
4
  * Error that is thrown when Http Request was made and returned an error.
@@ -18,7 +18,7 @@ import type { ErrorObject, HttpRequestErrorData } from './error.model';
18
18
  * (by default).
19
19
  */
20
20
  export declare class HttpRequestError extends AppError<HttpRequestErrorData> {
21
- constructor(message: string, data: HttpRequestErrorData, cause: ErrorObject);
21
+ constructor(message: string, data: HttpRequestErrorData, opt?: AppErrorOptions);
22
22
  /**
23
23
  * Cause is strictly-defined for HttpRequestError,
24
24
  * so it always has a cause.
@@ -20,8 +20,13 @@ const app_error_1 = require("./app.error");
20
20
  * (by default).
21
21
  */
22
22
  class HttpRequestError extends app_error_1.AppError {
23
- constructor(message, data, cause) {
24
- super(message, data, cause, 'HttpRequestError');
23
+ constructor(message, data, opt) {
24
+ if (data.response) {
25
+ Object.defineProperty(data, 'response', {
26
+ enumerable: false,
27
+ });
28
+ }
29
+ super(message, data, { ...opt, name: 'HttpRequestError' });
25
30
  }
26
31
  }
27
32
  exports.HttpRequestError = HttpRequestError;
@@ -1,5 +1,5 @@
1
1
  import { AppError } from './app.error';
2
- import { ErrorData, ErrorObject } from './error.model';
2
+ import { ErrorData } from './error.model';
3
3
  export interface JsonParseErrorData extends ErrorData {
4
4
  /**
5
5
  * Original text that failed to get parsed.
@@ -7,5 +7,5 @@ export interface JsonParseErrorData extends ErrorData {
7
7
  text?: string;
8
8
  }
9
9
  export declare class JsonParseError extends AppError<JsonParseErrorData> {
10
- constructor(data: JsonParseErrorData, cause?: ErrorObject);
10
+ constructor(data: JsonParseErrorData);
11
11
  }
@@ -4,8 +4,11 @@ exports.JsonParseError = void 0;
4
4
  const string_util_1 = require("../string/string.util");
5
5
  const app_error_1 = require("./app.error");
6
6
  class JsonParseError extends app_error_1.AppError {
7
- constructor(data, cause) {
8
- super(['Failed to parse', data.text && (0, string_util_1._truncateMiddle)(data.text, 200)].filter(Boolean).join(': '), data, cause, 'JsonParseError');
7
+ constructor(data) {
8
+ const message = ['Failed to parse', data.text && (0, string_util_1._truncateMiddle)(data.text, 200)]
9
+ .filter(Boolean)
10
+ .join(': ');
11
+ super(message, data, { name: 'JsonParseError' });
9
12
  }
10
13
  }
11
14
  exports.JsonParseError = JsonParseError;
package/dist/error/try.js CHANGED
@@ -57,7 +57,9 @@ exports.pTry = pTry;
57
57
  */
58
58
  class UnexpectedPassError extends app_error_1.AppError {
59
59
  constructor() {
60
- super('expected error was not thrown', {}, undefined, 'UnexpectedPassError');
60
+ super('expected error was not thrown', {}, {
61
+ name: 'UnexpectedPassError',
62
+ });
61
63
  }
62
64
  }
63
65
  exports.UnexpectedPassError = UnexpectedPassError;
@@ -309,6 +309,7 @@ class Fetcher {
309
309
  };
310
310
  const message = [res.fetchResponse?.status, res.signature].filter(Boolean).join(' ');
311
311
  res.err = new httpRequestError_1.HttpRequestError(message, (0, object_util_1._filterNullishValues)({
312
+ response: res.fetchResponse,
312
313
  responseStatusCode: res.fetchResponse?.status || 0,
313
314
  // These properties are provided to be used in e.g custom Sentry error grouping
314
315
  // Actually, disabled now, to avoid unnecessary error printing when both msg and data are printed
@@ -316,11 +317,13 @@ class Fetcher {
316
317
  // method: req.method,
317
318
  // tryCount: req.tryCount,
318
319
  requestUrl: res.req.fullUrl,
319
- requestBaseUrl: this.cfg.baseUrl || null,
320
+ requestBaseUrl: this.cfg.baseUrl || undefined,
320
321
  requestMethod: res.req.init.method,
321
322
  requestSignature: res.signature,
322
323
  requestDuration: Date.now() - res.req.started,
323
- }), cause);
324
+ }), {
325
+ cause,
326
+ });
324
327
  await this.processRetry(res);
325
328
  }
326
329
  async processRetry(res) {
@@ -543,7 +546,7 @@ class Fetcher {
543
546
  });
544
547
  if (Object.keys(searchParams).length) {
545
548
  const qs = new URLSearchParams(searchParams).toString();
546
- req.fullUrl += req.fullUrl.includes('?') ? '&' : '?' + qs;
549
+ req.fullUrl += (req.fullUrl.includes('?') ? '&' : '?') + qs;
547
550
  }
548
551
  // setup request body
549
552
  if (opt.json !== undefined) {
@@ -1,3 +1,4 @@
1
+ /// <reference lib="dom" />
1
2
  import type { CommonLogger } from '../log/commonLogger';
2
3
  import type { Promisable } from '../typeFest';
3
4
  import type { AnyObject, NumberOfMilliseconds, Reviver, UnixTimestampMillisNumber } from '../types';
@@ -1,2 +1,3 @@
1
1
  "use strict";
2
+ /// <reference lib="dom"/>
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './array/array.util';
2
- export * from './lazy';
2
+ export * from './define';
3
3
  export * from './string/url.util';
4
4
  export * from './array/range';
5
5
  export * from './decorators/createPromiseDecorator';
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZodError = exports.ZodSchema = exports.z = exports.is = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  tslib_1.__exportStar(require("./array/array.util"), exports);
6
- tslib_1.__exportStar(require("./lazy"), exports);
6
+ tslib_1.__exportStar(require("./define"), exports);
7
7
  tslib_1.__exportStar(require("./string/url.util"), exports);
8
8
  tslib_1.__exportStar(require("./array/range"), exports);
9
9
  tslib_1.__exportStar(require("./decorators/createPromiseDecorator"), exports);
@@ -1,5 +1,6 @@
1
1
  import type { PropertyPath } from '../lodash.types';
2
- import type { AnyObject, ObjectMapper, ObjectPredicate, StringMap, ValueOf } from '../types';
2
+ import { KeyValueTuple, SKIP } from '../types';
3
+ import type { AnyObject, ObjectMapper, ObjectPredicate, ValueOf } from '../types';
3
4
  /**
4
5
  * Returns clone of `obj` with only `props` preserved.
5
6
  * Opposite of Omit.
@@ -44,21 +45,21 @@ export declare function _filterObject<T extends AnyObject>(obj: T, predicate: Ob
44
45
  * 'pebbles': { 'user': 'pebbles', 'age': 1 }
45
46
  * }
46
47
  *
47
- * _.mapValues(users, function(_key, value) { return value.age; });
48
+ * _mapValues(users, (_key, value) => value.age)
48
49
  * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
49
50
  *
50
- * // The `_.property` iteratee shorthand.
51
- * _.mapValues(users, 'age')
52
- * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
51
+ * To skip some key-value pairs - use _mapObject instead.
53
52
  */
54
- export declare function _mapValues<T extends AnyObject, OUT = T>(obj: T, mapper: ObjectMapper<T, any>, mutate?: boolean): OUT;
53
+ export declare function _mapValues<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, any>, mutate?: boolean): T;
55
54
  /**
56
55
  * _.mapKeys({ 'a': 1, 'b': 2 }, (key, value) => key + value)
57
56
  * // => { 'a1': 1, 'b2': 2 }
58
57
  *
59
58
  * Does not support `mutate` flag.
59
+ *
60
+ * To skip some key-value pairs - use _mapObject instead.
60
61
  */
61
- export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>): StringMap<T[keyof T]>;
62
+ export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, string>): Record<string, T[keyof T]>;
62
63
  /**
63
64
  * Maps object through predicate - a function that receives (k, v, obj)
64
65
  * k - key
@@ -71,13 +72,11 @@ export declare function _mapKeys<T extends AnyObject>(obj: T, mapper: ObjectMapp
71
72
  * 0 - key of returned object (string)
72
73
  * 1 - value of returned object (any)
73
74
  *
74
- * If predicate returns falsy value (e.g undefined), or a tuple where key (first item) is falsy - then such key/value pair is ignored (filtered out).
75
+ * If predicate returns SKIP symbol - such key/value pair is ignored (filtered out).
75
76
  *
76
77
  * Non-string keys are passed via String(...)
77
78
  */
78
- export declare function _mapObject<IN extends AnyObject, OUT>(obj: IN, mapper: ObjectMapper<IN, [key: string, value: any]>): {
79
- [P in keyof IN]: OUT;
80
- };
79
+ export declare function _mapObject<T extends AnyObject>(obj: T, mapper: ObjectMapper<T, KeyValueTuple<string, any> | typeof SKIP>): T;
81
80
  export declare function _findKeyByValue<T extends AnyObject>(obj: T, v: ValueOf<T>): keyof T | undefined;
82
81
  export declare function _objectNullValuesToUndefined<T extends AnyObject>(obj: T, mutate?: boolean): T;
83
82
  /**
@@ -184,3 +183,9 @@ export declare function _set<T extends AnyObject>(obj: T, path: PropertyPath, va
184
183
  * // => false
185
184
  */
186
185
  export declare function _has<T extends AnyObject>(obj: T, path: string): boolean;
186
+ /**
187
+ * Does Object.freeze recursively for given object.
188
+ *
189
+ * Based on: https://github.com/substack/deep-freeze/blob/master/index.js
190
+ */
191
+ export declare function _deepFreeze(o: any): void;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._has = exports._set = exports._get = exports._invertMap = exports._invert = exports._unset = exports._deepTrim = exports._merge = exports._filterEmptyValues = exports._undefinedIfEmpty = exports._deepCopy = exports._objectNullValuesToUndefined = exports._findKeyByValue = exports._mapObject = exports._mapKeys = exports._mapValues = exports._filterObject = exports._filterEmptyArrays = exports._filterUndefinedValues = exports._filterNullishValues = exports._filterFalsyValues = exports._mask = exports._omit = exports._pick = void 0;
3
+ exports._deepFreeze = exports._has = exports._set = exports._get = exports._invertMap = exports._invert = exports._unset = exports._deepTrim = exports._merge = exports._filterEmptyValues = exports._undefinedIfEmpty = exports._deepCopy = exports._objectNullValuesToUndefined = exports._findKeyByValue = exports._mapObject = exports._mapKeys = exports._mapValues = exports._filterObject = exports._filterEmptyArrays = exports._filterUndefinedValues = exports._filterNullishValues = exports._filterFalsyValues = exports._mask = exports._omit = exports._pick = void 0;
4
4
  const is_util_1 = require("../is.util");
5
+ const types_1 = require("../types");
5
6
  /**
6
7
  * Returns clone of `obj` with only `props` preserved.
7
8
  * Opposite of Omit.
@@ -93,18 +94,16 @@ exports._filterObject = _filterObject;
93
94
  * 'pebbles': { 'user': 'pebbles', 'age': 1 }
94
95
  * }
95
96
  *
96
- * _.mapValues(users, function(_key, value) { return value.age; });
97
+ * _mapValues(users, (_key, value) => value.age)
97
98
  * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
98
99
  *
99
- * // The `_.property` iteratee shorthand.
100
- * _.mapValues(users, 'age')
101
- * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
100
+ * To skip some key-value pairs - use _mapObject instead.
102
101
  */
103
102
  function _mapValues(obj, mapper, mutate = false) {
104
- return Object.entries(obj).reduce((map, [k, v]) => {
103
+ return (0, types_1._objectEntries)(obj).reduce((map, [k, v]) => {
105
104
  map[k] = mapper(k, v, obj);
106
105
  return map;
107
- }, (mutate ? obj : {}));
106
+ }, mutate ? obj : {});
108
107
  }
109
108
  exports._mapValues = _mapValues;
110
109
  /**
@@ -112,9 +111,11 @@ exports._mapValues = _mapValues;
112
111
  * // => { 'a1': 1, 'b2': 2 }
113
112
  *
114
113
  * Does not support `mutate` flag.
114
+ *
115
+ * To skip some key-value pairs - use _mapObject instead.
115
116
  */
116
117
  function _mapKeys(obj, mapper) {
117
- return Object.entries(obj).reduce((map, [k, v]) => {
118
+ return (0, types_1._objectEntries)(obj).reduce((map, [k, v]) => {
118
119
  map[mapper(k, v, obj)] = v;
119
120
  return map;
120
121
  }, {});
@@ -132,15 +133,14 @@ exports._mapKeys = _mapKeys;
132
133
  * 0 - key of returned object (string)
133
134
  * 1 - value of returned object (any)
134
135
  *
135
- * If predicate returns falsy value (e.g undefined), or a tuple where key (first item) is falsy - then such key/value pair is ignored (filtered out).
136
+ * If predicate returns SKIP symbol - such key/value pair is ignored (filtered out).
136
137
  *
137
138
  * Non-string keys are passed via String(...)
138
139
  */
139
140
  function _mapObject(obj, mapper) {
140
141
  return Object.entries(obj).reduce((map, [k, v]) => {
141
- const r = mapper(k, v, obj) || [];
142
- if (r[0]) {
143
- ;
142
+ const r = mapper(k, v, obj);
143
+ if (r !== types_1.SKIP) {
144
144
  map[r[0]] = r[1];
145
145
  }
146
146
  return map;
@@ -367,3 +367,20 @@ function _has(obj, path) {
367
367
  return v !== undefined && v !== null;
368
368
  }
369
369
  exports._has = _has;
370
+ /**
371
+ * Does Object.freeze recursively for given object.
372
+ *
373
+ * Based on: https://github.com/substack/deep-freeze/blob/master/index.js
374
+ */
375
+ function _deepFreeze(o) {
376
+ Object.freeze(o);
377
+ Object.getOwnPropertyNames(o).forEach(prop => {
378
+ if (o.hasOwnProperty(prop) && // eslint-disable-line no-prototype-builtins
379
+ o[prop] !== null &&
380
+ (typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
381
+ !Object.isFrozen(o[prop])) {
382
+ _deepFreeze(o[prop]);
383
+ }
384
+ });
385
+ }
386
+ exports._deepFreeze = _deepFreeze;
@@ -1,8 +1,8 @@
1
- import { AppError } from '../error/app.error';
2
- import type { ErrorData, ErrorObject } from '../error/error.model';
1
+ import { AppError, AppErrorOptions } from '../error/app.error';
2
+ import type { ErrorData } from '../error/error.model';
3
3
  import type { AnyAsyncFunction, NumberOfMilliseconds } from '../types';
4
4
  export declare class TimeoutError extends AppError {
5
- constructor(message: string, data?: {}, cause?: ErrorObject);
5
+ constructor(message: string, data?: ErrorData, opt?: AppErrorOptions);
6
6
  }
7
7
  export interface PTimeoutOptions {
8
8
  /**
@@ -4,8 +4,8 @@ exports.pTimeout = exports.pTimeoutFn = exports.TimeoutError = void 0;
4
4
  const app_error_1 = require("../error/app.error");
5
5
  const error_util_1 = require("../error/error.util");
6
6
  class TimeoutError extends app_error_1.AppError {
7
- constructor(message, data = {}, cause) {
8
- super(message, data, cause, 'TimeoutError');
7
+ constructor(message, data, opt) {
8
+ super(message, data, { ...opt, name: 'TimeoutError' });
9
9
  }
10
10
  }
11
11
  exports.TimeoutError = TimeoutError;