klubok 0.1.3 → 0.2.1

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