klubok 0.4.2 → 0.4.3

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 +20 -15
  2. package/package.json +8 -5
package/dist/index.js CHANGED
@@ -19,23 +19,28 @@ function klubok(...fns) {
19
19
  return (rootCtx = {}, mock, only) => {
20
20
  let isExit = false;
21
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`))
24
- : (async () => {
25
- try {
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 }));
22
+ ? (acc, fn) => acc.then(async (ctx) => {
23
+ if (!fn.mutable && ctx[fn.key]) {
24
+ return Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`));
25
+ }
26
+ if (isExit) {
27
+ return ctx;
28
+ }
29
+ try {
30
+ const resp = await fn(ctx);
31
+ if (fn.exitable && resp) {
32
+ isExit = true;
30
33
  }
31
- catch (error) {
32
- await onError?.({ ...ctx, $error: error });
33
- if (error instanceof Error) {
34
- error.stack += '\ncontext: ' + (0, node_util_1.inspect)(ctx);
35
- }
36
- throw error;
34
+ return { ...ctx, [fn.key]: resp };
35
+ }
36
+ catch (error) {
37
+ await onError?.({ ...ctx, $error: error });
38
+ if (error instanceof Error) {
39
+ error.stack += '\ncontext: ' + (0, node_util_1.inspect)(ctx);
37
40
  }
38
- })())
41
+ throw error;
42
+ }
43
+ })
39
44
  : (acc, fn) => acc.then(ctx => !fn.mutable && Reflect.has(ctx, fn.key) && !Reflect.has(mock ?? {}, fn.key)
40
45
  ? Promise.reject(new Error(`Try to override existing alias "${fn.key}". Let's rename alias or use "mut" wrapper`))
41
46
  : (mock && Reflect.has(mock, fn.key) && typeof Reflect.get(mock, fn.key) !== 'function') ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "klubok",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
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",
@@ -13,8 +13,9 @@
13
13
  "dist/index.d.ts"
14
14
  ],
15
15
  "scripts": {
16
+ "bench": "node ./bench.ts",
16
17
  "build": "rm -rf dist && tsc",
17
- "test": "node --experimental-strip-types --test ./test.ts",
18
+ "test": "node --test ./test.ts",
18
19
  "test:types": "cp index.test-d.ts ./dist/ && npx tsd",
19
20
  "prepublishOnly": "npm run build"
20
21
  },
@@ -33,8 +34,10 @@
33
34
  "author": "Vladislav Botvin",
34
35
  "license": "MIT",
35
36
  "devDependencies": {
36
- "@types/node": "^22.10.6",
37
- "tsd": "^0.31.2",
38
- "typescript": "^5.7.3"
37
+ "@types/node": "^22.15.3",
38
+ "klubok": "0.4.2",
39
+ "tinybench": "^4.0.1",
40
+ "tsd": "^0.32.0",
41
+ "typescript": "^5.8.3"
39
42
  }
40
43
  }