@naturalcycles/js-lib 14.86.0 → 14.87.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.
|
@@ -2,9 +2,9 @@ export interface PromiseDecoratorCfg<RES = any, PARAMS = any> {
|
|
|
2
2
|
decoratorName: string;
|
|
3
3
|
/**
|
|
4
4
|
* Called BEFORE the original function.
|
|
5
|
-
*
|
|
5
|
+
* If Promise is returned - it will be awaited.
|
|
6
6
|
*/
|
|
7
|
-
beforeFn?: (r: PromiseDecoratorResp<PARAMS>) => void
|
|
7
|
+
beforeFn?: (r: PromiseDecoratorResp<PARAMS>) => void | Promise<void>;
|
|
8
8
|
/**
|
|
9
9
|
* Called just AFTER the original function.
|
|
10
10
|
* The output of this hook will be passed further,
|
|
@@ -24,12 +24,11 @@ function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
24
24
|
pd.value = async function (...args) {
|
|
25
25
|
// console.log(`@${cfg.decoratorName} called inside function`)
|
|
26
26
|
const started = Date.now();
|
|
27
|
-
|
|
27
|
+
try {
|
|
28
28
|
// Before function
|
|
29
|
-
.then(() => {
|
|
30
29
|
// console.log(`@${cfg.decoratorName} Before`)
|
|
31
30
|
if (cfg.beforeFn) {
|
|
32
|
-
|
|
31
|
+
await cfg.beforeFn({
|
|
33
32
|
decoratorParams,
|
|
34
33
|
args,
|
|
35
34
|
key,
|
|
@@ -38,10 +37,8 @@ function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
38
37
|
started,
|
|
39
38
|
});
|
|
40
39
|
}
|
|
41
|
-
})
|
|
42
40
|
// Original function
|
|
43
|
-
|
|
44
|
-
.then(res => {
|
|
41
|
+
let res = await originalMethod.apply(this, args);
|
|
45
42
|
// console.log(`${cfg.decoratorName} After`)
|
|
46
43
|
const resp = {
|
|
47
44
|
decoratorParams,
|
|
@@ -59,8 +56,8 @@ function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
59
56
|
}
|
|
60
57
|
cfg.finallyFn?.(resp);
|
|
61
58
|
return res;
|
|
62
|
-
}
|
|
63
|
-
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
64
61
|
console.error(`@${decoratorName} ${methodSignature} catch:`, err);
|
|
65
62
|
const resp = {
|
|
66
63
|
decoratorParams,
|
|
@@ -82,10 +79,7 @@ function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
82
79
|
if (!handled) {
|
|
83
80
|
throw err; // rethrow
|
|
84
81
|
}
|
|
85
|
-
}
|
|
86
|
-
// es2018 only
|
|
87
|
-
// .finally(() => {})
|
|
88
|
-
);
|
|
82
|
+
}
|
|
89
83
|
};
|
|
90
84
|
return pd;
|
|
91
85
|
};
|
|
@@ -19,14 +19,14 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
19
19
|
const key = String(propertyKey);
|
|
20
20
|
const methodSignature = _getTargetMethodSignature(target, key);
|
|
21
21
|
pd.value = async function (...args) {
|
|
22
|
+
var _a, _b;
|
|
22
23
|
// console.log(`@${cfg.decoratorName} called inside function`)
|
|
23
24
|
const started = Date.now();
|
|
24
|
-
|
|
25
|
+
try {
|
|
25
26
|
// Before function
|
|
26
|
-
.then(() => {
|
|
27
27
|
// console.log(`@${cfg.decoratorName} Before`)
|
|
28
28
|
if (cfg.beforeFn) {
|
|
29
|
-
|
|
29
|
+
await cfg.beforeFn({
|
|
30
30
|
decoratorParams,
|
|
31
31
|
args,
|
|
32
32
|
key,
|
|
@@ -35,11 +35,8 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
35
35
|
started,
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
|
-
})
|
|
39
38
|
// Original function
|
|
40
|
-
|
|
41
|
-
.then(res => {
|
|
42
|
-
var _a;
|
|
39
|
+
let res = await originalMethod.apply(this, args);
|
|
43
40
|
// console.log(`${cfg.decoratorName} After`)
|
|
44
41
|
const resp = {
|
|
45
42
|
decoratorParams,
|
|
@@ -54,9 +51,8 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
54
51
|
}
|
|
55
52
|
(_a = cfg.finallyFn) === null || _a === void 0 ? void 0 : _a.call(cfg, resp);
|
|
56
53
|
return res;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
var _a;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
60
56
|
console.error(`@${decoratorName} ${methodSignature} catch:`, err);
|
|
61
57
|
const resp = {
|
|
62
58
|
decoratorParams,
|
|
@@ -71,14 +67,11 @@ export function _createPromiseDecorator(cfg, decoratorParams = {}) {
|
|
|
71
67
|
cfg.catchFn(Object.assign(Object.assign({}, resp), { err }));
|
|
72
68
|
handled = true;
|
|
73
69
|
}
|
|
74
|
-
(
|
|
70
|
+
(_b = cfg.finallyFn) === null || _b === void 0 ? void 0 : _b.call(cfg, resp);
|
|
75
71
|
if (!handled) {
|
|
76
72
|
throw err; // rethrow
|
|
77
73
|
}
|
|
78
|
-
}
|
|
79
|
-
// es2018 only
|
|
80
|
-
// .finally(() => {})
|
|
81
|
-
);
|
|
74
|
+
}
|
|
82
75
|
};
|
|
83
76
|
return pd;
|
|
84
77
|
};
|
package/package.json
CHANGED
|
@@ -5,9 +5,9 @@ export interface PromiseDecoratorCfg<RES = any, PARAMS = any> {
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Called BEFORE the original function.
|
|
8
|
-
*
|
|
8
|
+
* If Promise is returned - it will be awaited.
|
|
9
9
|
*/
|
|
10
|
-
beforeFn?: (r: PromiseDecoratorResp<PARAMS>) => void
|
|
10
|
+
beforeFn?: (r: PromiseDecoratorResp<PARAMS>) => void | Promise<void>
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Called just AFTER the original function.
|
|
@@ -73,77 +73,71 @@ export function _createPromiseDecorator<RES = any, PARAMS = any>(
|
|
|
73
73
|
// console.log(`@${cfg.decoratorName} called inside function`)
|
|
74
74
|
const started = Date.now()
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
decoratorName,
|
|
88
|
-
started,
|
|
89
|
-
})
|
|
90
|
-
}
|
|
76
|
+
try {
|
|
77
|
+
// Before function
|
|
78
|
+
// console.log(`@${cfg.decoratorName} Before`)
|
|
79
|
+
if (cfg.beforeFn) {
|
|
80
|
+
await cfg.beforeFn({
|
|
81
|
+
decoratorParams,
|
|
82
|
+
args,
|
|
83
|
+
key,
|
|
84
|
+
target,
|
|
85
|
+
decoratorName,
|
|
86
|
+
started,
|
|
91
87
|
})
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
cfg.finallyFn?.(resp)
|
|
113
|
-
|
|
114
|
-
return res
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Original function
|
|
91
|
+
let res = await originalMethod.apply(this, args)
|
|
92
|
+
|
|
93
|
+
// console.log(`${cfg.decoratorName} After`)
|
|
94
|
+
const resp: PromiseDecoratorResp<PARAMS> = {
|
|
95
|
+
decoratorParams,
|
|
96
|
+
args,
|
|
97
|
+
key,
|
|
98
|
+
target,
|
|
99
|
+
decoratorName,
|
|
100
|
+
started,
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (cfg.thenFn) {
|
|
104
|
+
res = cfg.thenFn({
|
|
105
|
+
...resp,
|
|
106
|
+
res,
|
|
115
107
|
})
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (!handled) {
|
|
141
|
-
throw err // rethrow
|
|
142
|
-
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
cfg.finallyFn?.(resp)
|
|
111
|
+
|
|
112
|
+
return res
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error(`@${decoratorName} ${methodSignature} catch:`, err)
|
|
115
|
+
|
|
116
|
+
const resp: PromiseDecoratorResp<PARAMS> = {
|
|
117
|
+
decoratorParams,
|
|
118
|
+
args,
|
|
119
|
+
key,
|
|
120
|
+
target,
|
|
121
|
+
decoratorName,
|
|
122
|
+
started,
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let handled = false
|
|
126
|
+
|
|
127
|
+
if (cfg.catchFn) {
|
|
128
|
+
cfg.catchFn({
|
|
129
|
+
...resp,
|
|
130
|
+
err,
|
|
143
131
|
})
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
132
|
+
handled = true
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
cfg.finallyFn?.(resp)
|
|
136
|
+
|
|
137
|
+
if (!handled) {
|
|
138
|
+
throw err // rethrow
|
|
139
|
+
}
|
|
140
|
+
}
|
|
147
141
|
}
|
|
148
142
|
|
|
149
143
|
return pd
|