@nannygoose-utils/async-handler 1.0.0 → 1.0.4

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/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # @nannygoose-utils/async-handler
2
+
3
+ 一个轻量、高效的异步/同步操作错误处理工具,统一结果返回格式,告别重复 `try/catch` 代码,让错误处理更优雅。
4
+
5
+ ## 📦 安装
6
+
7
+ 使用 npm、yarn 或 pnpm 安装:
8
+
9
+ ```bash
10
+ # npm
11
+ npm install @nannygoose-utils/async-handler --save
12
+
13
+ # yarn
14
+ yarn add @nannygoose-utils/async-handler
15
+
16
+ # pnpm
17
+ pnpm add @nannygoose-utils/async-handler
@@ -0,0 +1 @@
1
+ "use strict";function n(r,e){r-=158;const o=t();let u=o[r];if(void 0===n.TSAFeT){n.cacDmK=function(n){let t="",r="";for(let e,o,u=0,c=0;o=n.charAt(c++);~o&&(e=u%4?64*e+o:o,u++%4)?t+=String.fromCharCode(255&e>>(-2*u&6)):0)o="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=".indexOf(o);for(let e=0,o=t.length;e<o;e++)r+="%"+("00"+t.charCodeAt(e).toString(16)).slice(-2);return decodeURIComponent(r)},n.gYLYRJ={},n.TSAFeT=!0}const c=r+o[0],i=n.gYLYRJ[c];return i?u=i:(u=n.cacDmK(u),n.gYLYRJ[c]=u),u}function t(){const n=["nZy2nZmXD2T6yw1s","C3rYAw5NAwz5","mZaYmdmWsKDAC3z2","mZu0ndaYwwPVuxzx","qNjsuva","nduWmti0EgnUwu9u","CKzns3y","AgPiuw4","m1jvDeXtDa","mJmYnZG3AvHPCvf4","ndqXotu0D1DRy3H4","nu1fExDUuq","ntm4mJmYzKfvC2Lh"];return(t=function(){return n})()}function r(t){const r=n,e=function(n,t){return n(t)};return function(n,t){return n instanceof t}(t,Error)?t:t instanceof Object?new Error(JSON[r(162)](t)):new Error(e(String,t))}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"}),function(){const r=n,e=t();for(;;)try{if(128740===parseInt(r(170))/1+-parseInt(r(163))/2*(-parseInt(r(169))/3)+-parseInt(r(166))/4*(parseInt(r(159))/5)+parseInt(r(158))/6+-parseInt(r(161))/7+-parseInt(r(160))/8+-parseInt(r(164))/9)break;e.push(e.shift())}catch(o){e.push(e.shift())}}(),exports.to=async function(t){const e=n,o={BrRQP:function(n,t){return n(t)}};try{return[null,await t]}catch(u){return[o[e(165)](r,u),void 0]}},exports.toSync=function(t){const e=n,o={hjHQn:function(n){return n()},rFMKv:function(n,t){return n(t)}};try{return[null,o[e(168)](t)]}catch(u){return[o[e(167)](r,u),void 0]}};
@@ -0,0 +1,80 @@
1
+ function _0x4076(_0x4628a2, _0x330993) {
2
+ _0x4628a2 = _0x4628a2 - 158;
3
+ const _0x3af457 = _0x3af4();
4
+ let _0x4076c9 = _0x3af457[_0x4628a2];
5
+ if (_0x4076["TSAFeT"] === void 0) {
6
+ var _0x1361a5 = function(_0xe42547) {
7
+ const _0x236eb2 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";
8
+ let _0xddf801 = "", _0x50a60d = "";
9
+ for (let _0x2767b7 = 0, _0x51d2ac, _0x323d65, _0x1a26fc = 0; _0x323d65 = _0xe42547["charAt"](_0x1a26fc++); ~_0x323d65 && (_0x51d2ac = _0x2767b7 % 4 ? _0x51d2ac * 64 + _0x323d65 : _0x323d65, _0x2767b7++ % 4) ? _0xddf801 += String["fromCharCode"](255 & _0x51d2ac >> (-2 * _0x2767b7 & 6)) : 0) {
10
+ _0x323d65 = _0x236eb2["indexOf"](_0x323d65);
11
+ }
12
+ for (let _0x20a59f = 0, _0x3ae3f8 = _0xddf801["length"]; _0x20a59f < _0x3ae3f8; _0x20a59f++) {
13
+ _0x50a60d += "%" + ("00" + _0xddf801["charCodeAt"](_0x20a59f)["toString"](16))["slice"](-2);
14
+ }
15
+ return decodeURIComponent(_0x50a60d);
16
+ };
17
+ _0x4076["cacDmK"] = _0x1361a5, _0x4076["gYLYRJ"] = {}, _0x4076["TSAFeT"] = !![];
18
+ }
19
+ const _0x452677 = _0x3af457[0], _0x3921a2 = _0x4628a2 + _0x452677, _0x2034a2 = _0x4076["gYLYRJ"][_0x3921a2];
20
+ return !_0x2034a2 ? (_0x4076c9 = _0x4076["cacDmK"](_0x4076c9), _0x4076["gYLYRJ"][_0x3921a2] = _0x4076c9) : _0x4076c9 = _0x2034a2, _0x4076c9;
21
+ }
22
+ (function(_0x467b06, _0x785f42) {
23
+ const _0x106640 = _0x4076, _0x340f79 = _0x467b06();
24
+ while (!![]) {
25
+ try {
26
+ const _0x39d7b2 = parseInt(_0x106640(170)) / 1 + -parseInt(_0x106640(163)) / 2 * (-parseInt(_0x106640(169)) / 3) + -parseInt(_0x106640(166)) / 4 * (parseInt(_0x106640(159)) / 5) + parseInt(_0x106640(158)) / 6 + -parseInt(_0x106640(161)) / 7 + -parseInt(_0x106640(160)) / 8 + -parseInt(_0x106640(164)) / 9;
27
+ if (_0x39d7b2 === _0x785f42) break;
28
+ else _0x340f79["push"](_0x340f79["shift"]());
29
+ } catch (_0x20af1a) {
30
+ _0x340f79["push"](_0x340f79["shift"]());
31
+ }
32
+ }
33
+ })(_0x3af4, 128740);
34
+ function _0x3af4() {
35
+ const _0x16de16 = ["nZy2nZmXD2T6yw1s", "C3rYAw5NAwz5", "mZaYmdmWsKDAC3z2", "mZu0ndaYwwPVuxzx", "qNjsuva", "nduWmti0EgnUwu9u", "CKzns3y", "AgPiuw4", "m1jvDeXtDa", "mJmYnZG3AvHPCvf4", "ndqXotu0D1DRy3H4", "nu1fExDUuq", "ntm4mJmYzKfvC2Lh"];
36
+ _0x3af4 = function() {
37
+ return _0x16de16;
38
+ };
39
+ return _0x3af4();
40
+ }
41
+ function _0x35630b(_0x52118b) {
42
+ const _0x4df0bc = _0x4076, _0x113c85 = { "jiJQY": function(_0x1163bd, _0x408e7f) {
43
+ return _0x1163bd instanceof _0x408e7f;
44
+ }, "tKeXd": function(_0x120b29, _0x1c7513) {
45
+ return _0x120b29(_0x1c7513);
46
+ } };
47
+ if (_0x113c85["jiJQY"](_0x52118b, Error)) return _0x52118b;
48
+ if (_0x52118b instanceof Object) return new Error(JSON[_0x4df0bc(162)](_0x52118b));
49
+ return new Error(_0x113c85["tKeXd"](String, _0x52118b));
50
+ }
51
+ async function to(_0x388758) {
52
+ const _0x1bb0e8 = _0x4076, _0x7f90da = { "BrRQP": function(_0x3d9a8f, _0x563b6e) {
53
+ return _0x3d9a8f(_0x563b6e);
54
+ } };
55
+ try {
56
+ const _0x1e2174 = await _0x388758;
57
+ return [null, _0x1e2174];
58
+ } catch (_0x5bdf37) {
59
+ const _0x5a56a8 = _0x7f90da[_0x1bb0e8(165)](_0x35630b, _0x5bdf37);
60
+ return [_0x5a56a8, void 0];
61
+ }
62
+ }
63
+ function toSync(_0x29734e) {
64
+ const _0x44dbf7 = _0x4076, _0x5b3a59 = { "hjHQn": function(_0x5489b4) {
65
+ return _0x5489b4();
66
+ }, "rFMKv": function(_0x278a73, _0x43185b) {
67
+ return _0x278a73(_0x43185b);
68
+ } };
69
+ try {
70
+ const _0x2be974 = _0x5b3a59[_0x44dbf7(168)](_0x29734e);
71
+ return [null, _0x2be974];
72
+ } catch (_0x18c7ed) {
73
+ const _0x433177 = _0x5b3a59[_0x44dbf7(167)](_0x35630b, _0x18c7ed);
74
+ return [_0x433177, void 0];
75
+ }
76
+ }
77
+ export {
78
+ to,
79
+ toSync
80
+ };
package/package.json CHANGED
@@ -1,23 +1,33 @@
1
1
  {
2
- "name": "@nannygoose-utils/async-handler",
3
- "version": "1.0.0",
2
+ "name": "@nannygoose-utils/async-handler",
3
+ "version": "1.0.4",
4
4
  "description": "异步/同步错误处理工具,统一返回 [error, result] 格式",
5
- "main": "./dist/index.js",
6
- "module": "./dist/index.mjs",
5
+ "main": "./dist/index.cjs.js",
6
+ "module": "./dist/index.es.js",
7
7
  "types": "./dist/index.d.ts",
8
-
9
8
  "files": [
10
9
  "dist"
11
10
  ],
12
11
  "scripts": {
13
- "build": "tsc && node -e \"require('fs').renameSync('dist/index.js', 'dist/index.mjs')\"",
14
- "prepublishOnly": "npm run build"
12
+ "build": "vite build",
13
+ "prepublishOnly": "npm run build",
14
+ "version:patch": "npm version patch",
15
+ "release": "npm run version:patch && npm publish --access public"
15
16
  },
16
- "keywords": [],
17
+ "keywords": [
18
+ "async",
19
+ "error-handler",
20
+ "try-catch",
21
+ "utils"
22
+ ],
17
23
  "author": "<tandongyang@1ceng.com>",
18
24
  "license": "ISC",
19
25
  "devDependencies": {
20
26
  "@types/node": "^25.2.0",
21
- "typescript": "^5.9.3"
27
+ "rollup-plugin-obfuscator": "^1.1.0",
28
+ "terser": "^5.46.0",
29
+ "typescript": "^5.9.3",
30
+ "vite": "^6.0.0",
31
+ "vite-plugin-dts": "^4.0.0"
22
32
  }
23
33
  }
package/dist/index.mjs DELETED
@@ -1,60 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- /**
11
- * 标准化错误对象
12
- * @param error 捕获到的任意类型错误
13
- * @returns 标准 Error 对象
14
- */
15
- function normalizeError(error) {
16
- // 如果已经是 Error 实例,直接返回
17
- if (error instanceof Error) {
18
- return error;
19
- }
20
- // 处理对象类型的错误(如 { message: 'xxx' })
21
- if (error instanceof Object) {
22
- return new Error(JSON.stringify(error));
23
- }
24
- // 处理字符串/数字等基础类型错误
25
- return new Error(String(error));
26
- }
27
- /**
28
- * 处理异步 Promise 操作的错误
29
- * @param promise 要执行的异步 Promise 操作
30
- * @returns 数组 [error, result]:成功时 error 为 null,失败时 result 为 undefined
31
- */
32
- export function to(promise) {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- try {
35
- const result = yield promise;
36
- return [null, result];
37
- }
38
- catch (error) {
39
- const normalizedError = normalizeError(error);
40
- return [normalizedError, undefined];
41
- }
42
- });
43
- }
44
- /**
45
- * 扩展:处理同步函数的错误(保持和 to 函数一致的返回格式)
46
- * @param fn 要执行的同步函数
47
- * @returns 数组 [error, result]
48
- * @example
49
- * const [err, parsedData] = toSync(() => JSON.parse('{"name":"test"}'));
50
- */
51
- export function toSync(fn) {
52
- try {
53
- const result = fn();
54
- return [null, result];
55
- }
56
- catch (error) {
57
- const normalizedError = normalizeError(error);
58
- return [normalizedError, undefined];
59
- }
60
- }