chain-simple 1.0.1 → 1.1.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.
- package/built/index.js +14 -54
- package/lib/index.ts +16 -57
- 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,80 +57,41 @@ 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
|
-
return proxed;
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
else if ((0, sat_utils_1.isAsyncFunction)(item[p]) && !(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
86
|
-
logger_1.logger.info('In to async function and resulter is a promise');
|
|
87
|
-
return function (...arguments_) {
|
|
88
|
-
async function handler() {
|
|
89
|
-
return item[p](...arguments_);
|
|
90
|
-
}
|
|
91
|
-
proxifiedResult = handler();
|
|
92
|
-
return proxed;
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
else if ((0, sat_utils_1.isFunction)(item[p]) && !(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
96
|
-
logger_1.logger.info('In to function and resulter is not a promise');
|
|
97
|
-
return function (...arguments_) {
|
|
98
|
-
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_);
|
|
99
76
|
return proxed;
|
|
100
77
|
};
|
|
101
78
|
}
|
|
102
79
|
else if ((p === 'then' || p === 'catch') && (0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
103
|
-
logger_1.logger.info('In then catch');
|
|
104
|
-
/** @info logging */
|
|
105
|
-
logger_1.logger.info('start call promise: ', p);
|
|
106
80
|
if (!(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
107
81
|
return proxifiedResult;
|
|
108
82
|
}
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
proxifiedResult = await proxifiedResult.catch(error => {
|
|
112
|
-
return { error, ____proxed____error: true };
|
|
113
|
-
});
|
|
114
|
-
if (proxifiedResult && proxifiedResult.____proxed____error && (0, sat_utils_1.isFunction)(catcher)) {
|
|
115
|
-
return catcher(proxifiedResult.error);
|
|
116
|
-
}
|
|
117
|
-
if (proxifiedResult && proxifiedResult.____proxed____error) {
|
|
118
|
-
const promised = Promise.reject(proxifiedResult.error);
|
|
119
|
-
return promised[p].call(promised, onRes, onRej);
|
|
120
|
-
}
|
|
121
|
-
const promised = Promise.resolve(proxifiedResult);
|
|
122
|
-
return promised[p].call(promised, onRes, onRej);
|
|
83
|
+
return function (onRes, onRej) {
|
|
84
|
+
return proxifiedResult[p].call(proxifiedResult, onRes, onRej);
|
|
123
85
|
};
|
|
124
86
|
}
|
|
125
87
|
else if (proxifiedResult[p]) {
|
|
126
|
-
logger_1.logger.info('In resulter has prop');
|
|
127
88
|
return proxifiedResult[p];
|
|
128
89
|
}
|
|
129
90
|
if (!(p in item) && p in proxifiedResult) {
|
|
130
|
-
logger_1.logger.info('In target does not have prop but resulter has prop');
|
|
131
91
|
return proxifiedResult[p];
|
|
132
92
|
}
|
|
133
93
|
},
|
|
134
|
-
/** @info
|
|
94
|
+
/** @info base */
|
|
135
95
|
getPrototypeOf(_t) {
|
|
136
96
|
return Object.getPrototypeOf(proxifiedResult);
|
|
137
97
|
},
|
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,82 +70,42 @@ 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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
item[p] &&
|
|
84
|
-
!proxifiedResult[p]
|
|
85
|
-
) {
|
|
77
|
+
|
|
78
|
+
const isCallable = isFunction(Reflect.get(item, p, r)) || isAsyncFunction(Reflect.get(item, p, r));
|
|
79
|
+
|
|
80
|
+
if (!isCallable && !isPromise(proxifiedResult) && item[p] && !proxifiedResult[p]) {
|
|
86
81
|
logger.info('In to not function, not async function, resulter is not a promise and target has prop');
|
|
87
82
|
return item[p];
|
|
88
|
-
} else if (
|
|
89
|
-
logger.info('In to function or async function and resulter is a promise');
|
|
90
|
-
return function (...arguments_) {
|
|
91
|
-
async function handler() {
|
|
92
|
-
await proxifiedResult;
|
|
93
|
-
return item[p](...arguments_);
|
|
94
|
-
}
|
|
95
|
-
proxifiedResult = handler();
|
|
96
|
-
return proxed;
|
|
97
|
-
};
|
|
98
|
-
} else if (isAsyncFunction(item[p]) && !isPromise(proxifiedResult)) {
|
|
99
|
-
logger.info('In to async function and resulter is a promise');
|
|
83
|
+
} else if (isCallable) {
|
|
100
84
|
return function (...arguments_) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
};
|
|
107
|
-
} else if (isFunction(item[p]) && !isPromise(proxifiedResult)) {
|
|
108
|
-
logger.info('In to function and resulter is not a promise');
|
|
109
|
-
return function (...arguments_) {
|
|
110
|
-
proxifiedResult = item[p](...arguments_);
|
|
85
|
+
proxifiedResult = isPromise(proxifiedResult)
|
|
86
|
+
? proxifiedResult.then(function () {
|
|
87
|
+
return item[p].call(item, ...arguments_);
|
|
88
|
+
})
|
|
89
|
+
: item[p].call(item, ...arguments_);
|
|
111
90
|
return proxed;
|
|
112
91
|
};
|
|
113
92
|
} else if ((p === 'then' || p === 'catch') && isPromise(proxifiedResult)) {
|
|
114
|
-
logger.info('In then catch');
|
|
115
|
-
/** @info logging */
|
|
116
|
-
logger.info('start call promise: ', p);
|
|
117
93
|
if (!isPromise(proxifiedResult)) {
|
|
118
94
|
return proxifiedResult;
|
|
119
95
|
}
|
|
120
|
-
return async function (onRes, onRej) {
|
|
121
|
-
const catcher = p === 'catch' ? onRes : onRej;
|
|
122
|
-
|
|
123
|
-
proxifiedResult = await proxifiedResult.catch(error => {
|
|
124
|
-
return { error, ____proxed____error: true };
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
if (proxifiedResult && proxifiedResult.____proxed____error && isFunction(catcher)) {
|
|
128
|
-
return catcher(proxifiedResult.error);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (proxifiedResult && proxifiedResult.____proxed____error) {
|
|
132
|
-
const promised = Promise.reject(proxifiedResult.error);
|
|
133
|
-
return promised[p].call(promised, onRes, onRej);
|
|
134
|
-
}
|
|
135
96
|
|
|
136
|
-
|
|
137
|
-
return
|
|
97
|
+
return function (onRes, onRej) {
|
|
98
|
+
return proxifiedResult[p].call(proxifiedResult, onRes, onRej);
|
|
138
99
|
};
|
|
139
100
|
} else if (proxifiedResult[p]) {
|
|
140
|
-
logger.info('In resulter has prop');
|
|
141
101
|
return proxifiedResult[p];
|
|
142
102
|
}
|
|
143
103
|
if (!(p in item) && p in proxifiedResult) {
|
|
144
|
-
logger.info('In target does not have prop but resulter has prop');
|
|
145
104
|
return proxifiedResult[p];
|
|
146
105
|
}
|
|
147
106
|
},
|
|
148
|
-
/** @info basics */
|
|
149
107
|
|
|
108
|
+
/** @info base */
|
|
150
109
|
getPrototypeOf(_t) {
|
|
151
110
|
return Object.getPrototypeOf(proxifiedResult);
|
|
152
111
|
},
|