chain-simple 1.0.2 → 1.1.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.js +15 -55
- package/lib/index.ts +17 -61
- package/package.json +1 -1
package/built/index.js
CHANGED
|
@@ -37,18 +37,17 @@ logger_1.logger.setLogLevel(process.env.CHAIN_SIMPLE_LOG_LEVEL);
|
|
|
37
37
|
*/
|
|
38
38
|
function makePropertiesChainable(item, config) {
|
|
39
39
|
if (!(0, sat_utils_1.canBeProxed)(item)) {
|
|
40
|
-
throw new TypeError('first argument should be an entity that can be proxed');
|
|
40
|
+
throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
|
|
41
41
|
}
|
|
42
42
|
if (!(0, sat_utils_1.isUndefined)(config) && !(0, sat_utils_1.isObject)(config)) {
|
|
43
|
-
throw new TypeError('second argument should be an object');
|
|
43
|
+
throw new TypeError('makePropertiesChainable(): second argument should be an object');
|
|
44
44
|
}
|
|
45
45
|
let proxifiedResult = item;
|
|
46
46
|
const proxed = new Proxy(item, {
|
|
47
|
-
get(_t, p) {
|
|
47
|
+
get(_t, p, r) {
|
|
48
48
|
if (config && config.getEntity === p) {
|
|
49
49
|
return item;
|
|
50
50
|
}
|
|
51
|
-
logger_1.logger.info(p);
|
|
52
51
|
if (p === Symbol.toStringTag) {
|
|
53
52
|
return proxifiedResult[Symbol.toStringTag];
|
|
54
53
|
}
|
|
@@ -58,82 +57,43 @@ function makePropertiesChainable(item, config) {
|
|
|
58
57
|
};
|
|
59
58
|
}
|
|
60
59
|
if (p === 'toJSON') {
|
|
61
|
-
logger_1.logger.info('In to JSON');
|
|
62
60
|
return function () {
|
|
63
61
|
return proxifiedResult;
|
|
64
62
|
};
|
|
65
63
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
!(0, sat_utils_1.isPromise)(proxifiedResult) &&
|
|
69
|
-
item[p] &&
|
|
70
|
-
!proxifiedResult[p]) {
|
|
64
|
+
const isCallable = (0, sat_utils_1.isFunction)(Reflect.get(item, p, r)) || (0, sat_utils_1.isAsyncFunction)(Reflect.get(item, p, r));
|
|
65
|
+
if (!isCallable && !(0, sat_utils_1.isPromise)(proxifiedResult) && item[p] && !proxifiedResult[p]) {
|
|
71
66
|
logger_1.logger.info('In to not function, not async function, resulter is not a promise and target has prop');
|
|
72
67
|
return item[p];
|
|
73
68
|
}
|
|
74
|
-
else if (
|
|
75
|
-
logger_1.logger.info('In to function or async function and resulter is a promise');
|
|
69
|
+
else if (isCallable) {
|
|
76
70
|
return function (...arguments_) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
proxifiedResult = handler();
|
|
83
|
-
return proxed;
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
else if ((0, sat_utils_1.isAsyncFunction)(item[p]) && !(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
87
|
-
logger_1.logger.info('In to async function and resulter is a promise');
|
|
88
|
-
return function (...arguments_) {
|
|
89
|
-
const handler = async function () {
|
|
90
|
-
return item[p](...arguments_);
|
|
91
|
-
};
|
|
92
|
-
Object.defineProperty(handler, 'name', { value: p });
|
|
93
|
-
proxifiedResult = handler();
|
|
94
|
-
return proxed;
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
else if ((0, sat_utils_1.isFunction)(item[p]) && !(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
98
|
-
logger_1.logger.info('In to function and resulter is not a promise');
|
|
99
|
-
return function (...arguments_) {
|
|
100
|
-
proxifiedResult = item[p](...arguments_);
|
|
71
|
+
proxifiedResult = (0, sat_utils_1.isPromise)(proxifiedResult)
|
|
72
|
+
? proxifiedResult.then(function () {
|
|
73
|
+
return item[p].call(item, ...arguments_);
|
|
74
|
+
})
|
|
75
|
+
: item[p].call(item, ...arguments_);
|
|
101
76
|
return proxed;
|
|
102
77
|
};
|
|
103
78
|
}
|
|
104
79
|
else if ((p === 'then' || p === 'catch') && (0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
105
|
-
logger_1.logger.info('In then catch');
|
|
106
|
-
/** @info logging */
|
|
107
|
-
logger_1.logger.info('start call promise: ', p);
|
|
108
80
|
if (!(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
109
81
|
return proxifiedResult;
|
|
110
82
|
}
|
|
111
|
-
return
|
|
112
|
-
const
|
|
113
|
-
proxifiedResult =
|
|
114
|
-
return { error, ____proxed____error: true };
|
|
115
|
-
});
|
|
116
|
-
if (proxifiedResult && proxifiedResult.____proxed____error && (0, sat_utils_1.isFunction)(catcher)) {
|
|
117
|
-
return catcher(proxifiedResult.error);
|
|
118
|
-
}
|
|
119
|
-
if (proxifiedResult && proxifiedResult.____proxed____error) {
|
|
120
|
-
const promised = Promise.reject(proxifiedResult.error);
|
|
121
|
-
return promised[p].call(promised, onRes, onRej);
|
|
122
|
-
}
|
|
123
|
-
const promised = Promise.resolve(proxifiedResult);
|
|
83
|
+
return function (onRes, onRej) {
|
|
84
|
+
const promised = proxifiedResult;
|
|
85
|
+
proxifiedResult = item;
|
|
124
86
|
return promised[p].call(promised, onRes, onRej);
|
|
125
87
|
};
|
|
126
88
|
}
|
|
127
89
|
else if (proxifiedResult[p]) {
|
|
128
|
-
logger_1.logger.info('In resulter has prop');
|
|
129
90
|
return proxifiedResult[p];
|
|
130
91
|
}
|
|
131
92
|
if (!(p in item) && p in proxifiedResult) {
|
|
132
|
-
logger_1.logger.info('In target does not have prop but resulter has prop');
|
|
133
93
|
return proxifiedResult[p];
|
|
134
94
|
}
|
|
135
95
|
},
|
|
136
|
-
/** @info
|
|
96
|
+
/** @info base */
|
|
137
97
|
getPrototypeOf(_t) {
|
|
138
98
|
return Object.getPrototypeOf(proxifiedResult);
|
|
139
99
|
},
|
package/lib/index.ts
CHANGED
|
@@ -45,21 +45,20 @@ export type TChainable<T extends Record<string, TFn>> = {
|
|
|
45
45
|
*/
|
|
46
46
|
function makePropertiesChainable(item, config?: { getEntity: string }) {
|
|
47
47
|
if (!canBeProxed(item)) {
|
|
48
|
-
throw new TypeError('first argument should be an entity that can be proxed');
|
|
48
|
+
throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
if (!isUndefined(config) && !isObject(config)) {
|
|
52
|
-
throw new TypeError('second argument should be an object');
|
|
52
|
+
throw new TypeError('makePropertiesChainable(): second argument should be an object');
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
let proxifiedResult = item;
|
|
56
56
|
const proxed = new Proxy(item, {
|
|
57
|
-
get(_t, p) {
|
|
57
|
+
get(_t, p, r) {
|
|
58
58
|
if (config && config.getEntity === p) {
|
|
59
59
|
return item;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
logger.info(p);
|
|
63
62
|
if (p === Symbol.toStringTag) {
|
|
64
63
|
return proxifiedResult[Symbol.toStringTag];
|
|
65
64
|
}
|
|
@@ -71,88 +70,45 @@ function makePropertiesChainable(item, config?: { getEntity: string }) {
|
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
if (p === 'toJSON') {
|
|
74
|
-
logger.info('In to JSON');
|
|
75
73
|
return function () {
|
|
76
74
|
return proxifiedResult;
|
|
77
75
|
};
|
|
78
76
|
}
|
|
79
|
-
if (
|
|
80
|
-
!isFunction(item[p]) &&
|
|
81
|
-
!isAsyncFunction(item[p]) &&
|
|
82
|
-
!isPromise(proxifiedResult) &&
|
|
83
|
-
item[p] &&
|
|
84
|
-
!proxifiedResult[p]
|
|
85
|
-
) {
|
|
86
|
-
logger.info('In to not function, not async function, resulter is not a promise and target has prop');
|
|
87
|
-
return item[p];
|
|
88
|
-
} else if ((isFunction(item[p]) || isAsyncFunction(item[p])) && isPromise(proxifiedResult)) {
|
|
89
|
-
logger.info('In to function or async function and resulter is a promise');
|
|
90
|
-
return function (...arguments_) {
|
|
91
|
-
const handler = async function () {
|
|
92
|
-
await proxifiedResult;
|
|
93
|
-
return item[p](...arguments_);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
Object.defineProperty(handler, 'name', { value: p });
|
|
97
|
-
|
|
98
|
-
proxifiedResult = handler();
|
|
99
|
-
return proxed;
|
|
100
|
-
};
|
|
101
|
-
} else if (isAsyncFunction(item[p]) && !isPromise(proxifiedResult)) {
|
|
102
|
-
logger.info('In to async function and resulter is a promise');
|
|
103
|
-
return function (...arguments_) {
|
|
104
|
-
const handler = async function () {
|
|
105
|
-
return item[p](...arguments_);
|
|
106
|
-
};
|
|
107
77
|
|
|
108
|
-
|
|
78
|
+
const isCallable = isFunction(Reflect.get(item, p, r)) || isAsyncFunction(Reflect.get(item, p, r));
|
|
109
79
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
} else if (
|
|
114
|
-
logger.info('In to function and resulter is not a promise');
|
|
80
|
+
if (!isCallable && !isPromise(proxifiedResult) && item[p] && !proxifiedResult[p]) {
|
|
81
|
+
logger.info('In to not function, not async function, resulter is not a promise and target has prop');
|
|
82
|
+
return item[p];
|
|
83
|
+
} else if (isCallable) {
|
|
115
84
|
return function (...arguments_) {
|
|
116
|
-
proxifiedResult =
|
|
85
|
+
proxifiedResult = isPromise(proxifiedResult)
|
|
86
|
+
? proxifiedResult.then(function () {
|
|
87
|
+
return item[p].call(item, ...arguments_);
|
|
88
|
+
})
|
|
89
|
+
: item[p].call(item, ...arguments_);
|
|
117
90
|
return proxed;
|
|
118
91
|
};
|
|
119
92
|
} else if ((p === 'then' || p === 'catch') && isPromise(proxifiedResult)) {
|
|
120
|
-
logger.info('In then catch');
|
|
121
|
-
/** @info logging */
|
|
122
|
-
logger.info('start call promise: ', p);
|
|
123
93
|
if (!isPromise(proxifiedResult)) {
|
|
124
94
|
return proxifiedResult;
|
|
125
95
|
}
|
|
126
|
-
return async function (onRes, onRej) {
|
|
127
|
-
const catcher = p === 'catch' ? onRes : onRej;
|
|
128
|
-
|
|
129
|
-
proxifiedResult = await proxifiedResult.catch(error => {
|
|
130
|
-
return { error, ____proxed____error: true };
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (proxifiedResult && proxifiedResult.____proxed____error && isFunction(catcher)) {
|
|
134
|
-
return catcher(proxifiedResult.error);
|
|
135
|
-
}
|
|
136
96
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
97
|
+
return function (onRes, onRej) {
|
|
98
|
+
const promised = proxifiedResult;
|
|
99
|
+
proxifiedResult = item;
|
|
141
100
|
|
|
142
|
-
const promised = Promise.resolve(proxifiedResult);
|
|
143
101
|
return promised[p].call(promised, onRes, onRej);
|
|
144
102
|
};
|
|
145
103
|
} else if (proxifiedResult[p]) {
|
|
146
|
-
logger.info('In resulter has prop');
|
|
147
104
|
return proxifiedResult[p];
|
|
148
105
|
}
|
|
149
106
|
if (!(p in item) && p in proxifiedResult) {
|
|
150
|
-
logger.info('In target does not have prop but resulter has prop');
|
|
151
107
|
return proxifiedResult[p];
|
|
152
108
|
}
|
|
153
109
|
},
|
|
154
|
-
/** @info basics */
|
|
155
110
|
|
|
111
|
+
/** @info base */
|
|
156
112
|
getPrototypeOf(_t) {
|
|
157
113
|
return Object.getPrototypeOf(proxifiedResult);
|
|
158
114
|
},
|