klubok 0.1.3 → 0.2.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.
Files changed (2) hide show
  1. package/dist/index.js +24 -4
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -12,13 +12,33 @@ function klubok(...fns) {
12
12
  return (rootCtx = {}, mock, only) => fns.reduce(mock == null && only == null
13
13
  ? (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key)
14
14
  ? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
15
- : Promise.resolve(fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp })))
15
+ : (async () => {
16
+ try {
17
+ return await Promise.resolve(fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }));
18
+ }
19
+ catch (error) {
20
+ throw {
21
+ error: error.stack ?? error.message ?? error,
22
+ ctx
23
+ };
24
+ }
25
+ })())
16
26
  : (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key) && !Reflect.has(mock ?? {}, fn.key)
17
27
  ? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
18
28
  : (mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) !== 'function') ||
19
29
  (only && only.length && !only.includes(fn.key))
20
30
  ? ctx
21
- : Promise.resolve(mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) === 'function'
22
- ? Reflect.get(mock, fn.key)(ctx)
23
- : fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }))), Promise.resolve({ ...rootCtx, ...mock }));
31
+ : (async () => {
32
+ try {
33
+ return await Promise.resolve(mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) === 'function'
34
+ ? Reflect.get(mock, fn.key)(ctx)
35
+ : fn(ctx)).then(resp => ({ ...ctx, [fn.key]: resp }));
36
+ }
37
+ catch (error) {
38
+ throw {
39
+ error: error.stack ?? error.message ?? error,
40
+ ctx
41
+ };
42
+ }
43
+ })()), Promise.resolve({ ...rootCtx, ...mock }));
24
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "klubok",
3
- "version": "0.1.3",
3
+ "version": "0.2.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",