klubok 0.3.2 → 0.4.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/dist/index.d.ts +380 -3040
- package/dist/index.js +39 -28
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -1,44 +1,32 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.onError = exports.mut = exports.eff = exports.pure = void 0;
|
3
|
+
exports.onError = exports.exitIf = exports.mut = exports.eff = exports.pure = void 0;
|
4
4
|
exports.klubok = klubok;
|
5
5
|
const node_util_1 = require("node:util");
|
6
|
-
const pure = (key, fn) => Object.assign(fn, { key, mutable: false, onError: false });
|
6
|
+
const pure = (key, fn) => Object.assign(fn, { exitable: false, key, mutable: false, onError: false });
|
7
7
|
exports.pure = pure;
|
8
|
-
const eff = (key, fn) => Object.assign(fn, { key, mutable: false, onError: false });
|
8
|
+
const eff = (key, fn) => Object.assign(fn, { exitable: false, key, mutable: false, onError: false });
|
9
9
|
exports.eff = eff;
|
10
10
|
const mut = (fn) => Object.assign(fn, { mutable: true });
|
11
11
|
exports.mut = mut;
|
12
|
-
const
|
12
|
+
const exitIf = (fn) => Object.assign(fn, { exitable: true });
|
13
|
+
exports.exitIf = exitIf;
|
14
|
+
const onError = (key, fn) => Object.assign(fn, { exitable: false, key, mutable: false, onError: true });
|
13
15
|
exports.onError = onError;
|
14
16
|
function klubok(...fns) {
|
15
17
|
const funcs = fns.filter(f => !f.onError);
|
16
18
|
const onError = fns.find(f => f.onError);
|
17
|
-
return (rootCtx = {}, mock, only) =>
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
return await Promise.resolve(fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }));
|
23
|
-
}
|
24
|
-
catch (error) {
|
25
|
-
await onError?.({ ...ctx, $error: error });
|
26
|
-
if (error instanceof Error) {
|
27
|
-
error.stack += '\ncontext: ' + (0, node_util_1.inspect)(ctx);
|
28
|
-
}
|
29
|
-
throw error;
|
30
|
-
}
|
31
|
-
})())
|
32
|
-
: (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key) && !Reflect.has(mock ?? {}, fn.key)
|
33
|
-
? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
|
34
|
-
: (mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) !== 'function') ||
|
35
|
-
(only && only.length && !only.includes(fn.key))
|
36
|
-
? ctx
|
19
|
+
return (rootCtx = {}, mock, only) => {
|
20
|
+
let isExit = false;
|
21
|
+
return funcs.reduce(mock == null && only == null
|
22
|
+
? (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key)
|
23
|
+
? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
|
37
24
|
: (async () => {
|
38
25
|
try {
|
39
|
-
|
40
|
-
|
41
|
-
|
26
|
+
if (isExit) {
|
27
|
+
return ctx;
|
28
|
+
}
|
29
|
+
return await Promise.resolve(fn(ctx)).then(resp => (fn.exitable && resp && (isExit = true), { ...ctx, [fn.key]: resp }));
|
42
30
|
}
|
43
31
|
catch (error) {
|
44
32
|
await onError?.({ ...ctx, $error: error });
|
@@ -47,5 +35,28 @@ function klubok(...fns) {
|
|
47
35
|
}
|
48
36
|
throw error;
|
49
37
|
}
|
50
|
-
})())
|
38
|
+
})())
|
39
|
+
: (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key) && !Reflect.has(mock ?? {}, fn.key)
|
40
|
+
? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
|
41
|
+
: (mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) !== 'function') ||
|
42
|
+
(only && only.length && !only.includes(fn.key))
|
43
|
+
? ctx
|
44
|
+
: (async () => {
|
45
|
+
try {
|
46
|
+
if (isExit) {
|
47
|
+
return ctx;
|
48
|
+
}
|
49
|
+
return await Promise.resolve(mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) === 'function'
|
50
|
+
? Reflect.get(mock, fn.key)(ctx)
|
51
|
+
: fn(ctx)).then(resp => (fn.exitable && resp && (isExit = true), { ...ctx, [fn.key]: resp }));
|
52
|
+
}
|
53
|
+
catch (error) {
|
54
|
+
await onError?.({ ...ctx, $error: error });
|
55
|
+
if (error instanceof Error) {
|
56
|
+
error.stack += '\ncontext: ' + (0, node_util_1.inspect)(ctx);
|
57
|
+
}
|
58
|
+
throw error;
|
59
|
+
}
|
60
|
+
})()), Promise.resolve({ ...rootCtx, ...mock }));
|
61
|
+
};
|
51
62
|
}
|