chain-simple 1.2.2 → 1.3.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 +3 -0
- package/built/index.js +37 -8
- package/built/logger.d.ts +2 -8
- package/built/logger.js +1 -1
- package/lib/index.ts +51 -9
- package/lib/logger.ts +1 -1
- package/package.json +2 -2
package/built/index.d.ts
CHANGED
package/built/index.js
CHANGED
|
@@ -36,6 +36,16 @@ logger_1.logger.setLogLevel(process.env.CHAIN_SIMPLE_LOG_LEVEL);
|
|
|
36
36
|
* @returns {object} object with chainable properties
|
|
37
37
|
*/
|
|
38
38
|
function makePropertiesChainable(item, config) {
|
|
39
|
+
const promiseCallableProps = ['then', 'catch', 'finally'];
|
|
40
|
+
const propsList = [];
|
|
41
|
+
if ((0, sat_utils_1.isObject)(config) && config.getEntityPropList) {
|
|
42
|
+
if (!(0, sat_utils_1.isObject)(config.getEntityPropList) && !(0, sat_utils_1.isArray)(config.getEntityPropList)) {
|
|
43
|
+
throw new TypeError('config "getEntityPropList" should be an array or an object');
|
|
44
|
+
}
|
|
45
|
+
propsList.push(...((0, sat_utils_1.isObject)(config.getEntityPropList)
|
|
46
|
+
? Object.keys(config.getEntityPropList)
|
|
47
|
+
: config.getEntityPropList));
|
|
48
|
+
}
|
|
39
49
|
if (!(0, sat_utils_1.canBeProxed)(item)) {
|
|
40
50
|
throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
|
|
41
51
|
}
|
|
@@ -45,7 +55,15 @@ function makePropertiesChainable(item, config) {
|
|
|
45
55
|
let proxifiedResult = item;
|
|
46
56
|
const proxed = new Proxy(item, {
|
|
47
57
|
get(_t, p, r) {
|
|
48
|
-
|
|
58
|
+
var _a;
|
|
59
|
+
if (propsList.length && propsList.includes(p)) {
|
|
60
|
+
const propValue = (_a = Reflect.getOwnPropertyDescriptor(item, p)) === null || _a === void 0 ? void 0 : _a.value;
|
|
61
|
+
if ((0, sat_utils_1.isFunction)(propValue) || (0, sat_utils_1.isAsyncFunction)(propValue)) {
|
|
62
|
+
return item[p].bind(item);
|
|
63
|
+
}
|
|
64
|
+
return item[p];
|
|
65
|
+
}
|
|
66
|
+
if ((0, sat_utils_1.isObject)(config) && config.getEntity === p) {
|
|
49
67
|
return item;
|
|
50
68
|
}
|
|
51
69
|
if (p === Symbol.toStringTag) {
|
|
@@ -61,8 +79,7 @@ function makePropertiesChainable(item, config) {
|
|
|
61
79
|
return proxifiedResult;
|
|
62
80
|
};
|
|
63
81
|
}
|
|
64
|
-
if (p
|
|
65
|
-
p !== 'catch' &&
|
|
82
|
+
if (!promiseCallableProps.includes(p) &&
|
|
66
83
|
(0, sat_utils_1.isUndefined)(Reflect.get(item, p, r)) &&
|
|
67
84
|
(0, sat_utils_1.isObject)(config) &&
|
|
68
85
|
(0, sat_utils_1.isFunction)(config.extendProxed)) {
|
|
@@ -83,20 +100,32 @@ function makePropertiesChainable(item, config) {
|
|
|
83
100
|
}
|
|
84
101
|
const isCallable = (0, sat_utils_1.isFunction)(Reflect.get(item, p, r)) || (0, sat_utils_1.isAsyncFunction)(Reflect.get(item, p, r));
|
|
85
102
|
if (!isCallable && !(0, sat_utils_1.isPromise)(proxifiedResult) && item[p] && !proxifiedResult[p]) {
|
|
86
|
-
logger_1.logger.
|
|
103
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: ${String(p)} is not a callable.`);
|
|
87
104
|
return item[p];
|
|
88
105
|
}
|
|
89
106
|
else if (isCallable) {
|
|
107
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: ${String(p)} is a callable.`);
|
|
90
108
|
return function (...arguments_) {
|
|
91
|
-
|
|
92
|
-
|
|
109
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: ${String(p)} is called with args: `, ...arguments);
|
|
110
|
+
if ((0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
111
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: previous call result is a promise`);
|
|
112
|
+
proxifiedResult = proxifiedResult
|
|
113
|
+
.then(function (r) {
|
|
114
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: previous call result is: `, r);
|
|
93
115
|
return item[p].call(item, ...arguments_);
|
|
94
116
|
})
|
|
95
|
-
|
|
117
|
+
.catch(e => console.log(e));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: previous call result is not a promise`);
|
|
121
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: previous call result is: `, proxifiedResult);
|
|
122
|
+
proxifiedResult = item[p].call(item, ...arguments_);
|
|
123
|
+
}
|
|
96
124
|
return proxed;
|
|
97
125
|
};
|
|
98
126
|
}
|
|
99
|
-
else if ((p
|
|
127
|
+
else if (promiseCallableProps.includes(p) && (0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
128
|
+
logger_1.logger.chainer(`[CHAIN_SIMPLE]: previous call result is a promise and next call is a promise method call`);
|
|
100
129
|
if (!(0, sat_utils_1.isPromise)(proxifiedResult)) {
|
|
101
130
|
return proxifiedResult;
|
|
102
131
|
}
|
package/built/logger.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
declare const logger: {
|
|
2
|
-
|
|
3
|
-
log(...args: any[]): void;
|
|
4
|
-
info(...args: any[]): void;
|
|
5
|
-
warn(...args: any[]): void;
|
|
6
|
-
error(...args: any[]): void;
|
|
7
|
-
setLogLevel(level: string): void;
|
|
8
|
-
addCustomLevel(loggerDescription: any, logLevel: string, description: string, consoleOutput: string, descriptionColor: string, messageColor: any): void;
|
|
1
|
+
declare const logger: import("sat-utils").Tlogger & {
|
|
2
|
+
chainer: (...args: any[]) => void;
|
|
9
3
|
};
|
|
10
4
|
export { logger };
|
package/built/logger.js
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.logger = void 0;
|
|
4
4
|
const sat_utils_1 = require("sat-utils");
|
|
5
|
-
const logger = (0, sat_utils_1.createLogger)();
|
|
5
|
+
const logger = (0, sat_utils_1.createLogger)().addCustomLevel('chainer', 'CHAIN_LOG', 'CHAIN_LOG', 'info', 'BgBlue', 'BgWhite');
|
|
6
6
|
exports.logger = logger;
|
|
7
7
|
//# sourceMappingURL=logger.js.map
|
package/lib/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isObject, isPromise, isFunction, isAsyncFunction, canBeProxed, isUndefined } from 'sat-utils';
|
|
1
|
+
import { isArray, isObject, isPromise, isFunction, isAsyncFunction, canBeProxed, isUndefined } from 'sat-utils';
|
|
2
2
|
|
|
3
3
|
import { logger } from './logger';
|
|
4
4
|
|
|
@@ -15,6 +15,7 @@ export type TChainable<T extends Record<string, TFn>> = {
|
|
|
15
15
|
type TConfig = {
|
|
16
16
|
getEntity?: string;
|
|
17
17
|
extendProxed?: (propName) => { [k: string]: any } | ((item: any) => { [k: string]: any });
|
|
18
|
+
getEntityPropList?: string[] | { [k: string]: any };
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -49,6 +50,21 @@ type TConfig = {
|
|
|
49
50
|
* @returns {object} object with chainable properties
|
|
50
51
|
*/
|
|
51
52
|
function makePropertiesChainable(item, config?: TConfig) {
|
|
53
|
+
const promiseCallableProps: any[] = ['then', 'catch', 'finally'];
|
|
54
|
+
const propsList = [];
|
|
55
|
+
|
|
56
|
+
if (isObject(config) && config.getEntityPropList) {
|
|
57
|
+
if (!isObject(config.getEntityPropList) && !isArray(config.getEntityPropList)) {
|
|
58
|
+
throw new TypeError('config "getEntityPropList" should be an array or an object');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
propsList.push(
|
|
62
|
+
...(isObject(config.getEntityPropList)
|
|
63
|
+
? Object.keys(config.getEntityPropList)
|
|
64
|
+
: (config.getEntityPropList as string[])),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
52
68
|
if (!canBeProxed(item)) {
|
|
53
69
|
throw new TypeError('makePropertiesChainable(): first argument should be an entity that can be proxed');
|
|
54
70
|
}
|
|
@@ -60,7 +76,15 @@ function makePropertiesChainable(item, config?: TConfig) {
|
|
|
60
76
|
let proxifiedResult = item;
|
|
61
77
|
const proxed = new Proxy(item, {
|
|
62
78
|
get(_t, p, r) {
|
|
63
|
-
if (
|
|
79
|
+
if (propsList.length && propsList.includes(p)) {
|
|
80
|
+
const propValue = Reflect.getOwnPropertyDescriptor(item, p)?.value;
|
|
81
|
+
if (isFunction(propValue) || isAsyncFunction(propValue)) {
|
|
82
|
+
return item[p].bind(item);
|
|
83
|
+
}
|
|
84
|
+
return item[p];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (isObject(config) && config.getEntity === p) {
|
|
64
88
|
return item;
|
|
65
89
|
}
|
|
66
90
|
|
|
@@ -79,9 +103,9 @@ function makePropertiesChainable(item, config?: TConfig) {
|
|
|
79
103
|
return proxifiedResult;
|
|
80
104
|
};
|
|
81
105
|
}
|
|
106
|
+
|
|
82
107
|
if (
|
|
83
|
-
p
|
|
84
|
-
p !== 'catch' &&
|
|
108
|
+
!promiseCallableProps.includes(p) &&
|
|
85
109
|
isUndefined(Reflect.get(item, p, r)) &&
|
|
86
110
|
isObject(config) &&
|
|
87
111
|
isFunction(config.extendProxed)
|
|
@@ -103,18 +127,36 @@ function makePropertiesChainable(item, config?: TConfig) {
|
|
|
103
127
|
const isCallable = isFunction(Reflect.get(item, p, r)) || isAsyncFunction(Reflect.get(item, p, r));
|
|
104
128
|
|
|
105
129
|
if (!isCallable && !isPromise(proxifiedResult) && item[p] && !proxifiedResult[p]) {
|
|
106
|
-
logger.
|
|
130
|
+
logger.chainer(`[CHAIN_SIMPLE]: ${String(p)} is not a callable.`);
|
|
131
|
+
|
|
107
132
|
return item[p];
|
|
108
133
|
} else if (isCallable) {
|
|
134
|
+
logger.chainer(`[CHAIN_SIMPLE]: ${String(p)} is a callable.`);
|
|
135
|
+
|
|
109
136
|
return function (...arguments_) {
|
|
110
|
-
|
|
111
|
-
|
|
137
|
+
logger.chainer(`[CHAIN_SIMPLE]: ${String(p)} is called with args: `, ...arguments);
|
|
138
|
+
|
|
139
|
+
if (isPromise(proxifiedResult)) {
|
|
140
|
+
logger.chainer(`[CHAIN_SIMPLE]: previous call result is a promise`);
|
|
141
|
+
proxifiedResult = proxifiedResult
|
|
142
|
+
.then(function (r) {
|
|
143
|
+
logger.chainer(`[CHAIN_SIMPLE]: previous call result is: `, r);
|
|
144
|
+
|
|
112
145
|
return item[p].call(item, ...arguments_);
|
|
113
146
|
})
|
|
114
|
-
|
|
147
|
+
.catch(e => console.log(e));
|
|
148
|
+
} else {
|
|
149
|
+
logger.chainer(`[CHAIN_SIMPLE]: previous call result is not a promise`);
|
|
150
|
+
logger.chainer(`[CHAIN_SIMPLE]: previous call result is: `, proxifiedResult);
|
|
151
|
+
|
|
152
|
+
proxifiedResult = item[p].call(item, ...arguments_);
|
|
153
|
+
}
|
|
154
|
+
|
|
115
155
|
return proxed;
|
|
116
156
|
};
|
|
117
|
-
} else if ((p
|
|
157
|
+
} else if (promiseCallableProps.includes(p) && isPromise(proxifiedResult)) {
|
|
158
|
+
logger.chainer(`[CHAIN_SIMPLE]: previous call result is a promise and next call is a promise method call`);
|
|
159
|
+
|
|
118
160
|
if (!isPromise(proxifiedResult)) {
|
|
119
161
|
return proxifiedResult;
|
|
120
162
|
}
|
package/lib/logger.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chain-simple",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.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": {
|
|
@@ -55,6 +55,6 @@
|
|
|
55
55
|
"node": ">=12.18.3"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"sat-utils": "1.
|
|
58
|
+
"sat-utils": "1.9.0"
|
|
59
59
|
}
|
|
60
60
|
}
|