chain-simple 1.1.1 → 1.2.2

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/built/index.d.ts CHANGED
@@ -3,6 +3,14 @@ type TReplaceReturnType<T extends TFn, TNewReturnType> = (...args: Parameters<T>
3
3
  export type TChainable<T extends Record<string, TFn>> = {
4
4
  [K in keyof T]: TReplaceReturnType<T[K], ReturnType<T[K]> & TChainable<T>>;
5
5
  };
6
+ type TConfig = {
7
+ getEntity?: string;
8
+ extendProxed?: (propName: any) => {
9
+ [k: string]: any;
10
+ } | ((item: any) => {
11
+ [k: string]: any;
12
+ });
13
+ };
6
14
  /**
7
15
  * @example
8
16
  * const {makePropertiesChainable} = require('chain-simple');
@@ -34,10 +42,6 @@ export type TChainable<T extends Record<string, TFn>> = {
34
42
  * @param {{getEntity: string}} [config] config to describe how to get original not project object
35
43
  * @returns {object} object with chainable properties
36
44
  */
37
- declare function makePropertiesChainable(item: any, config?: {
38
- getEntity: string;
39
- }): any;
40
- declare function makeConstructorInstancePropertiesChainable(constructorFunction: any, config?: {
41
- getEntity: string;
42
- }): any;
45
+ declare function makePropertiesChainable(item: any, config?: TConfig): any;
46
+ declare function makeConstructorInstancePropertiesChainable(constructorFunction: any, config?: TConfig): any;
43
47
  export { makePropertiesChainable, makeConstructorInstancePropertiesChainable };
package/built/index.js CHANGED
@@ -61,6 +61,26 @@ function makePropertiesChainable(item, config) {
61
61
  return proxifiedResult;
62
62
  };
63
63
  }
64
+ if (p !== 'then' &&
65
+ p !== 'catch' &&
66
+ (0, sat_utils_1.isUndefined)(Reflect.get(item, p, r)) &&
67
+ (0, sat_utils_1.isObject)(config) &&
68
+ (0, sat_utils_1.isFunction)(config.extendProxed)) {
69
+ try {
70
+ const extension = config.extendProxed(p);
71
+ if ((0, sat_utils_1.isObject)(extension)) {
72
+ Object.assign(item, extension);
73
+ }
74
+ else if ((0, sat_utils_1.isFunction)(extension)) {
75
+ // @ts-ignore
76
+ const result = extension(item);
77
+ Object.assign(item, result);
78
+ }
79
+ }
80
+ catch (error) {
81
+ console.error(error);
82
+ }
83
+ }
64
84
  const isCallable = (0, sat_utils_1.isFunction)(Reflect.get(item, p, r)) || (0, sat_utils_1.isAsyncFunction)(Reflect.get(item, p, r));
65
85
  if (!isCallable && !(0, sat_utils_1.isPromise)(proxifiedResult) && item[p] && !proxifiedResult[p]) {
66
86
  logger_1.logger.info('In to not function, not async function, resulter is not a promise and target has prop');
package/lib/index.ts CHANGED
@@ -12,6 +12,11 @@ export type TChainable<T extends Record<string, TFn>> = {
12
12
  [K in keyof T]: TReplaceReturnType<T[K], ReturnType<T[K]> & TChainable<T>>;
13
13
  };
14
14
 
15
+ type TConfig = {
16
+ getEntity?: string;
17
+ extendProxed?: (propName) => { [k: string]: any } | ((item: any) => { [k: string]: any });
18
+ };
19
+
15
20
  /**
16
21
  * @example
17
22
  * const {makePropertiesChainable} = require('chain-simple');
@@ -43,7 +48,7 @@ export type TChainable<T extends Record<string, TFn>> = {
43
48
  * @param {{getEntity: string}} [config] config to describe how to get original not project object
44
49
  * @returns {object} object with chainable properties
45
50
  */
46
- function makePropertiesChainable(item, config?: { getEntity: string }) {
51
+ function makePropertiesChainable(item, config?: TConfig) {
47
52
  if (!canBeProxed(item)) {
48
53
  throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
49
54
  }
@@ -74,6 +79,26 @@ function makePropertiesChainable(item, config?: { getEntity: string }) {
74
79
  return proxifiedResult;
75
80
  };
76
81
  }
82
+ if (
83
+ p !== 'then' &&
84
+ p !== 'catch' &&
85
+ isUndefined(Reflect.get(item, p, r)) &&
86
+ isObject(config) &&
87
+ isFunction(config.extendProxed)
88
+ ) {
89
+ try {
90
+ const extension = config.extendProxed(p);
91
+ if (isObject(extension)) {
92
+ Object.assign(item, extension);
93
+ } else if (isFunction(extension)) {
94
+ // @ts-ignore
95
+ const result = extension(item);
96
+ Object.assign(item, result);
97
+ }
98
+ } catch (error) {
99
+ console.error(error);
100
+ }
101
+ }
77
102
 
78
103
  const isCallable = isFunction(Reflect.get(item, p, r)) || isAsyncFunction(Reflect.get(item, p, r));
79
104
 
@@ -131,7 +156,7 @@ function handlerConstructor(config) {
131
156
  };
132
157
  }
133
158
 
134
- function makeConstructorInstancePropertiesChainable(constructorFunction, config?: { getEntity: string }) {
159
+ function makeConstructorInstancePropertiesChainable(constructorFunction, config?: TConfig) {
135
160
  return new Proxy(constructorFunction, handlerConstructor(config));
136
161
  }
137
162
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chain-simple",
3
- "version": "1.1.1",
3
+ "version": "1.2.2",
4
4
  "description": "Main purpose of this package is - provide simple way to build chain between any item methods",
5
5
  "main": "built/index.js",
6
6
  "directories": {