chain-simple 1.1.0 → 1.2.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/built/index.d.ts +8 -6
- package/built/index.js +16 -1
- package/lib/index.ts +22 -3
- package/package.json +1 -1
package/built/index.d.ts
CHANGED
|
@@ -3,6 +3,12 @@ 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
|
+
};
|
|
11
|
+
};
|
|
6
12
|
/**
|
|
7
13
|
* @example
|
|
8
14
|
* const {makePropertiesChainable} = require('chain-simple');
|
|
@@ -34,10 +40,6 @@ export type TChainable<T extends Record<string, TFn>> = {
|
|
|
34
40
|
* @param {{getEntity: string}} [config] config to describe how to get original not project object
|
|
35
41
|
* @returns {object} object with chainable properties
|
|
36
42
|
*/
|
|
37
|
-
declare function makePropertiesChainable(item: any, config?:
|
|
38
|
-
|
|
39
|
-
}): any;
|
|
40
|
-
declare function makeConstructorInstancePropertiesChainable(constructorFunction: any, config?: {
|
|
41
|
-
getEntity: string;
|
|
42
|
-
}): any;
|
|
43
|
+
declare function makePropertiesChainable(item: any, config?: TConfig): any;
|
|
44
|
+
declare function makeConstructorInstancePropertiesChainable(constructorFunction: any, config?: TConfig): any;
|
|
43
45
|
export { makePropertiesChainable, makeConstructorInstancePropertiesChainable };
|
package/built/index.js
CHANGED
|
@@ -61,6 +61,19 @@ 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
|
+
Object.assign(item, extension);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
console.error(error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
64
77
|
const isCallable = (0, sat_utils_1.isFunction)(Reflect.get(item, p, r)) || (0, sat_utils_1.isAsyncFunction)(Reflect.get(item, p, r));
|
|
65
78
|
if (!isCallable && !(0, sat_utils_1.isPromise)(proxifiedResult) && item[p] && !proxifiedResult[p]) {
|
|
66
79
|
logger_1.logger.info('In to not function, not async function, resulter is not a promise and target has prop');
|
|
@@ -81,7 +94,9 @@ function makePropertiesChainable(item, config) {
|
|
|
81
94
|
return proxifiedResult;
|
|
82
95
|
}
|
|
83
96
|
return function (onRes, onRej) {
|
|
84
|
-
|
|
97
|
+
const promised = proxifiedResult;
|
|
98
|
+
proxifiedResult = item;
|
|
99
|
+
return promised[p].call(promised, onRes, onRej);
|
|
85
100
|
};
|
|
86
101
|
}
|
|
87
102
|
else if (proxifiedResult[p]) {
|
package/lib/index.ts
CHANGED
|
@@ -12,6 +12,8 @@ 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 = { getEntity?: string; extendProxed?: (propName) => { [k: string]: any } };
|
|
16
|
+
|
|
15
17
|
/**
|
|
16
18
|
* @example
|
|
17
19
|
* const {makePropertiesChainable} = require('chain-simple');
|
|
@@ -43,7 +45,7 @@ export type TChainable<T extends Record<string, TFn>> = {
|
|
|
43
45
|
* @param {{getEntity: string}} [config] config to describe how to get original not project object
|
|
44
46
|
* @returns {object} object with chainable properties
|
|
45
47
|
*/
|
|
46
|
-
function makePropertiesChainable(item, config?:
|
|
48
|
+
function makePropertiesChainable(item, config?: TConfig) {
|
|
47
49
|
if (!canBeProxed(item)) {
|
|
48
50
|
throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
|
|
49
51
|
}
|
|
@@ -74,6 +76,20 @@ function makePropertiesChainable(item, config?: { getEntity: string }) {
|
|
|
74
76
|
return proxifiedResult;
|
|
75
77
|
};
|
|
76
78
|
}
|
|
79
|
+
if (
|
|
80
|
+
p !== 'then' &&
|
|
81
|
+
p !== 'catch' &&
|
|
82
|
+
isUndefined(Reflect.get(item, p, r)) &&
|
|
83
|
+
isObject(config) &&
|
|
84
|
+
isFunction(config.extendProxed)
|
|
85
|
+
) {
|
|
86
|
+
try {
|
|
87
|
+
const extension = config.extendProxed(p);
|
|
88
|
+
Object.assign(item, extension);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
77
93
|
|
|
78
94
|
const isCallable = isFunction(Reflect.get(item, p, r)) || isAsyncFunction(Reflect.get(item, p, r));
|
|
79
95
|
|
|
@@ -95,7 +111,10 @@ function makePropertiesChainable(item, config?: { getEntity: string }) {
|
|
|
95
111
|
}
|
|
96
112
|
|
|
97
113
|
return function (onRes, onRej) {
|
|
98
|
-
|
|
114
|
+
const promised = proxifiedResult;
|
|
115
|
+
proxifiedResult = item;
|
|
116
|
+
|
|
117
|
+
return promised[p].call(promised, onRes, onRej);
|
|
99
118
|
};
|
|
100
119
|
} else if (proxifiedResult[p]) {
|
|
101
120
|
return proxifiedResult[p];
|
|
@@ -128,7 +147,7 @@ function handlerConstructor(config) {
|
|
|
128
147
|
};
|
|
129
148
|
}
|
|
130
149
|
|
|
131
|
-
function makeConstructorInstancePropertiesChainable(constructorFunction, config?:
|
|
150
|
+
function makeConstructorInstancePropertiesChainable(constructorFunction, config?: TConfig) {
|
|
132
151
|
return new Proxy(constructorFunction, handlerConstructor(config));
|
|
133
152
|
}
|
|
134
153
|
|