klubok 0.3.3 → 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 CHANGED
@@ -1,4 +1,5 @@
1
1
  type KeyedFunction<K extends string, F extends Function> = F & {
2
+ exitable: boolean;
2
3
  key: K;
3
4
  mutable: boolean;
4
5
  onError: boolean;
@@ -6,6 +7,7 @@ type KeyedFunction<K extends string, F extends Function> = F & {
6
7
  export declare const pure: <K extends string, C, R>(key: K, fn: (ctx: C) => R) => KeyedFunction<K, (ctx: C) => R>;
7
8
  export declare const eff: <K extends string, C, R>(key: K, fn: (ctx: C) => Promise<R>) => KeyedFunction<K, (ctx: C) => Promise<R>>;
8
9
  export declare const mut: <K extends string, C, R>(fn: KeyedFunction<K, (ctx: C) => R>) => KeyedFunction<K, (ctx: C) => R>;
10
+ export declare const exitIf: <K extends string, C, R>(fn: KeyedFunction<K, (ctx: C) => R>) => KeyedFunction<K, (ctx: C) => R>;
9
11
  export declare const onError: <K extends string, C, R>(key: K, fn: (ctx: C) => R) => KeyedFunction<K, (ctx: C) => R>;
10
12
  export declare function klubok<K1 extends string, C extends object, R1>(fn1: KeyedFunction<K1, (ctx: C) => Promise<R1> | R1>): (ctx: C, mock?: {
11
13
  [k in K1]?: R1 | ((ctx: C) => R1 | Promise<R1>);
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 onError = (key, fn) => Object.assign(fn, { key, mutable: false, onError: true });
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) => funcs.reduce(mock == null && only == null
18
- ? (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key)
19
- ? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
20
- : (async () => {
21
- try {
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
- return await Promise.resolve(mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) === 'function'
40
- ? Reflect.get(mock, fn.key)(ctx)
41
- : fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }));
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
- })()), Promise.resolve({ ...rootCtx, ...mock }));
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "klubok",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "description": "Do notation pipes for Promise-based or pure functions which easy to mock",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",