klubok 0.2.1 → 0.3.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 +2 -0
- package/dist/index.js +18 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
type KeyedFunction<K extends string, F extends Function> = F & {
|
2
2
|
key: K;
|
3
3
|
mutable: boolean;
|
4
|
+
onError: boolean;
|
4
5
|
};
|
5
6
|
export declare const pure: <K extends string, C, R>(key: K, fn: (ctx: C) => R) => KeyedFunction<K, (ctx: C) => R>;
|
6
7
|
export declare const eff: <K extends string, C, R>(key: K, fn: (ctx: C) => Promise<R>) => KeyedFunction<K, (ctx: C) => Promise<R>>;
|
7
8
|
export declare const mut: <K extends string, C, R>(fn: KeyedFunction<K, (ctx: C) => R>) => KeyedFunction<K, (ctx: C) => R>;
|
9
|
+
export declare const onError: <K extends string, C, R>(key: K, fn: (ctx: C) => R) => KeyedFunction<K, (ctx: C) => R>;
|
8
10
|
export declare function klubok<K1 extends string, C extends object, R1>(fn1: KeyedFunction<K1, (ctx: C) => Promise<R1> | R1>): (ctx: C, mock?: {
|
9
11
|
[k in K1]?: R1 | ((ctx: C) => R1 | Promise<R1>);
|
10
12
|
}, only?: K1[]) => Promise<C & {
|
package/dist/index.js
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.mut = exports.eff = exports.pure = void 0;
|
3
|
+
exports.onError = 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 });
|
6
|
+
const pure = (key, fn) => Object.assign(fn, { key, mutable: false, onError: false });
|
7
7
|
exports.pure = pure;
|
8
|
-
const eff = (key, fn) => Object.assign(fn, { key, mutable: false });
|
8
|
+
const eff = (key, fn) => Object.assign(fn, { 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 onError = (key, fn) => Object.assign(fn, { key, mutable: false, onError: true });
|
13
|
+
exports.onError = onError;
|
12
14
|
function klubok(...fns) {
|
13
|
-
|
15
|
+
const funcs = fns.filter(f => !f.onError);
|
16
|
+
const onError = fns.find(f => f.onError);
|
17
|
+
return (rootCtx = {}, mock, only) => funcs.reduce(mock == null && only == null
|
14
18
|
? (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key)
|
15
19
|
? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
|
16
20
|
: (async () => {
|
@@ -18,10 +22,11 @@ function klubok(...fns) {
|
|
18
22
|
return await Promise.resolve(fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }));
|
19
23
|
}
|
20
24
|
catch (error) {
|
21
|
-
|
22
|
-
|
23
|
-
ctx
|
24
|
-
}
|
25
|
+
await onError?.(error);
|
26
|
+
if (error instanceof Error) {
|
27
|
+
error.stack += '\ncontext: ' + (0, node_util_1.inspect)(ctx);
|
28
|
+
}
|
29
|
+
throw error;
|
25
30
|
}
|
26
31
|
})())
|
27
32
|
: (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key) && !Reflect.has(mock ?? {}, fn.key)
|
@@ -36,10 +41,11 @@ function klubok(...fns) {
|
|
36
41
|
: fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }));
|
37
42
|
}
|
38
43
|
catch (error) {
|
39
|
-
|
40
|
-
|
41
|
-
ctx
|
42
|
-
}
|
44
|
+
await onError?.(error);
|
45
|
+
if (error instanceof Error) {
|
46
|
+
error.stack += '\ncontext: ' + (0, node_util_1.inspect)(ctx);
|
47
|
+
}
|
48
|
+
throw error;
|
43
49
|
}
|
44
50
|
})()), Promise.resolve({ ...rootCtx, ...mock }));
|
45
51
|
}
|