@ybgnb/utils 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 hzhilong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @ybgnb/utils
2
+
3
+ 自用工具库
@@ -0,0 +1,4 @@
1
+ export * from './types/error';
2
+ export * from './types/extended-type';
3
+ export * from './utils/base-utils';
4
+ export * from './utils/biz-result';
package/dist/index.mjs ADDED
@@ -0,0 +1,117 @@
1
+ import dayjs from "dayjs";
2
+ class CommonError {
3
+ message;
4
+ constructor(message) {
5
+ this.message = message;
6
+ }
7
+ }
8
+ class AbortedError extends CommonError {
9
+ constructor() {
10
+ super("操作已取消");
11
+ }
12
+ }
13
+ class BaseUtils {
14
+ /**
15
+ * 判断是否为通用异常
16
+ * @param error 需要判断的异常
17
+ */
18
+ static isCommonError(error) {
19
+ return error instanceof CommonError;
20
+ }
21
+ /**
22
+ * 获取错误信息
23
+ * @param error
24
+ */
25
+ static getErrorMessage(error) {
26
+ return this.convertToCommonError(error).message;
27
+ }
28
+ /**
29
+ * 设置异常信息的前置提示
30
+ * @param error 异常
31
+ * @param preMsg 前置信息 `${preMsg}${error.message}`
32
+ */
33
+ static prependErrorMessage(error, preMsg) {
34
+ if (preMsg) {
35
+ error.message = `${preMsg}${error.message}`;
36
+ }
37
+ return error;
38
+ }
39
+ /**
40
+ * 转换到通用异常
41
+ * @param error 异常
42
+ * @param preMsg 前置提示 `${preMsg}${error.message}`
43
+ */
44
+ static convertToCommonError(error, preMsg) {
45
+ if (this.isCommonError(error)) {
46
+ return this.prependErrorMessage(error, preMsg);
47
+ }
48
+ if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
49
+ const newError = error;
50
+ return this.prependErrorMessage(new CommonError(newError.message), preMsg);
51
+ } else {
52
+ return this.prependErrorMessage(new CommonError(String(error)), preMsg);
53
+ }
54
+ }
55
+ /**
56
+ * 格式化时间为 2020-02-02 20:20:20 的字符串
57
+ * @param date 需要格式化的时间,为空则获取当前时间
58
+ */
59
+ static getFormatedDateTime(date) {
60
+ return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
61
+ }
62
+ /**
63
+ * 格式化时间为 2020-02-02 的字符串
64
+ * @param date 需要格式化的时间,为空则获取当前时间
65
+ */
66
+ static getFormatedDate(date) {
67
+ return dayjs(date).format("YYYY-MM-DD");
68
+ }
69
+ /**
70
+ * 格式化时间为 20:20:20 的字符串
71
+ * @param date 需要格式化的时间,为空则获取当前时间
72
+ */
73
+ static getFormatedTime(date) {
74
+ return dayjs(date).format("HH:mm:ss");
75
+ }
76
+ }
77
+ class BizResult {
78
+ success;
79
+ msg;
80
+ data;
81
+ constructor(success, msg, data) {
82
+ this.success = success;
83
+ this.msg = msg;
84
+ this.data = data;
85
+ }
86
+ static createSuccess(data) {
87
+ return new BizResult(true, "操作成功", data);
88
+ }
89
+ static createFail(msg = "操作失败", data) {
90
+ return new BizResult(false, msg, data);
91
+ }
92
+ static createError(e) {
93
+ return new BizResult(false, BaseUtils.getErrorMessage(e));
94
+ }
95
+ async toPromise() {
96
+ if (this.success) {
97
+ return this.data;
98
+ } else {
99
+ throw new CommonError(this.msg);
100
+ }
101
+ }
102
+ }
103
+ const execBiz = async (run) => {
104
+ try {
105
+ return BizResult.createSuccess(await run());
106
+ } catch (e) {
107
+ return BizResult.createError(e);
108
+ }
109
+ };
110
+ export {
111
+ AbortedError,
112
+ BaseUtils,
113
+ BizResult,
114
+ CommonError,
115
+ execBiz
116
+ };
117
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/types/error.ts","../src/utils/base-utils.ts","../src/utils/biz-result.ts"],"sourcesContent":["/**\n * 通用异常\n */\nexport class CommonError {\n message: string;\n\n constructor(message: string) {\n this.message = message;\n }\n}\n\n/**\n * 终止异常\n */\nexport class AbortedError extends CommonError {\n constructor() {\n super(\"操作已取消\");\n }\n}\n","import dayjs from 'dayjs';\nimport { CommonError } from '../types/error';\n\n/**\n * 基础工具集\n */\nexport class BaseUtils {\n /**\n * 判断是否为通用异常\n * @param error 需要判断的异常\n */\n static isCommonError(error: unknown): error is CommonError {\n return error instanceof CommonError\n }\n\n /**\n * 获取错误信息\n * @param error\n */\n static getErrorMessage(error: unknown): string {\n return this.convertToCommonError(error).message\n }\n\n /**\n * 设置异常信息的前置提示\n * @param error 异常\n * @param preMsg 前置信息 `${preMsg}${error.message}`\n */\n static prependErrorMessage(error: CommonError, preMsg?: string): CommonError {\n if (preMsg) {\n error.message = `${preMsg}${error.message}`\n }\n return error\n }\n\n /**\n * 转换到通用异常\n * @param error 异常\n * @param preMsg 前置提示 `${preMsg}${error.message}`\n */\n static convertToCommonError(error: unknown, preMsg?: string): CommonError {\n if (this.isCommonError(error)) {\n return this.prependErrorMessage(error, preMsg)\n }\n if (\n typeof error === 'object' &&\n error !== null &&\n 'message' in error &&\n typeof (error as Record<string, unknown>).message === 'string'\n ) {\n const newError = error as { message: string }\n return this.prependErrorMessage(new CommonError(newError.message), preMsg)\n } else {\n // 如果抛出的异常不是object\n return this.prependErrorMessage(new CommonError(String(error)), preMsg)\n }\n }\n\n /**\n * 格式化时间为 2020-02-02 20:20:20 的字符串\n * @param date 需要格式化的时间,为空则获取当前时间\n */\n static getFormatedDateTime(date?: Date): string {\n return dayjs(date).format('YYYY-MM-DD HH:mm:ss')\n }\n\n /**\n * 格式化时间为 2020-02-02 的字符串\n * @param date 需要格式化的时间,为空则获取当前时间\n */\n static getFormatedDate(date?: Date): string {\n return dayjs(date).format('YYYY-MM-DD')\n }\n\n /**\n * 格式化时间为 20:20:20 的字符串\n * @param date 需要格式化的时间,为空则获取当前时间\n */\n static getFormatedTime(date?: Date): string {\n return dayjs(date).format('HH:mm:ss')\n }\n}\n","import { CommonError } from '../types/error'\nimport { BaseUtils } from './base-utils'\n\n/**\n * 业务执行结果\n */\nexport class BizResult<T> {\n success: boolean\n msg: string\n data?: T\n\n constructor(success: boolean, msg: string, data?: T) {\n this.success = success\n this.msg = msg\n this.data = data\n }\n\n static createSuccess<T>(data: T) {\n return new BizResult(true, '操作成功', data)\n }\n\n static createFail<T>(msg: string = '操作失败', data?: T) {\n return new BizResult(false, msg, data)\n }\n\n static createError<T>(e: unknown) {\n return new BizResult<T>(false, BaseUtils.getErrorMessage(e))\n }\n\n /**\n * 展开成Promise\n */\n async toPromise(): Promise<void>\n async toPromise<T>(): Promise<T>\n async toPromise<T = void>(): Promise<T> {\n if (this.success) {\n return this.data as T\n } else {\n throw new CommonError(this.msg)\n }\n }\n}\n\n/**\n * 执行业务(自动包裹BuResult)\n * @param run 执行方法\n */\nexport const execBiz = async <T>(run: () => Promise<T>): Promise<BizResult<T>> => {\n try {\n return BizResult.createSuccess<T>(await run())\n } catch (e) {\n return BizResult.createError(e)\n }\n}\n"],"names":[],"mappings":";AAGO,MAAM,YAAY;AAAA,EACvB;AAAA,EAEA,YAAY,SAAiB;AAC3B,SAAK,UAAU;AAAA,EACjB;AACF;AAKO,MAAM,qBAAqB,YAAY;AAAA,EAC5C,cAAc;AACZ,UAAM,OAAO;AAAA,EACf;AACF;ACZO,MAAM,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA,EAKrB,OAAO,cAAc,OAAsC;AACzD,WAAO,iBAAiB;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,gBAAgB,OAAwB;AAC7C,WAAO,KAAK,qBAAqB,KAAK,EAAE;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,oBAAoB,OAAoB,QAA8B;AAC3E,QAAI,QAAQ;AACV,YAAM,UAAU,GAAG,MAAM,GAAG,MAAM,OAAO;AAAA,IAC3C;AACA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,qBAAqB,OAAgB,QAA8B;AACxE,QAAI,KAAK,cAAc,KAAK,GAAG;AAC7B,aAAO,KAAK,oBAAoB,OAAO,MAAM;AAAA,IAC/C;AACA,QACE,OAAO,UAAU,YACjB,UAAU,QACV,aAAa,SACb,OAAQ,MAAkC,YAAY,UACtD;AACA,YAAM,WAAW;AACjB,aAAO,KAAK,oBAAoB,IAAI,YAAY,SAAS,OAAO,GAAG,MAAM;AAAA,IAC3E,OAAO;AAEL,aAAO,KAAK,oBAAoB,IAAI,YAAY,OAAO,KAAK,CAAC,GAAG,MAAM;AAAA,IACxE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,oBAAoB,MAAqB;AAC9C,WAAO,MAAM,IAAI,EAAE,OAAO,qBAAqB;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,gBAAgB,MAAqB;AAC1C,WAAO,MAAM,IAAI,EAAE,OAAO,YAAY;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,gBAAgB,MAAqB;AAC1C,WAAO,MAAM,IAAI,EAAE,OAAO,UAAU;AAAA,EACtC;AACF;AC3EO,MAAM,UAAa;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,SAAkB,KAAa,MAAU;AACnD,SAAK,UAAU;AACf,SAAK,MAAM;AACX,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,OAAO,cAAiB,MAAS;AAC/B,WAAO,IAAI,UAAU,MAAM,QAAQ,IAAI;AAAA,EACzC;AAAA,EAEA,OAAO,WAAc,MAAc,QAAQ,MAAU;AACnD,WAAO,IAAI,UAAU,OAAO,KAAK,IAAI;AAAA,EACvC;AAAA,EAEA,OAAO,YAAe,GAAY;AAChC,WAAO,IAAI,UAAa,OAAO,UAAU,gBAAgB,CAAC,CAAC;AAAA,EAC7D;AAAA,EAOA,MAAM,YAAkC;AACtC,QAAI,KAAK,SAAS;AAChB,aAAO,KAAK;AAAA,IACd,OAAO;AACL,YAAM,IAAI,YAAY,KAAK,GAAG;AAAA,IAChC;AAAA,EACF;AACF;AAMO,MAAM,UAAU,OAAU,QAAiD;AAChF,MAAI;AACF,WAAO,UAAU,cAAiB,MAAM,KAAK;AAAA,EAC/C,SAAS,GAAG;AACV,WAAO,UAAU,YAAY,CAAC;AAAA,EAChC;AACF;"}
@@ -0,0 +1,120 @@
1
+ (function(global, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("dayjs")) : typeof define === "function" && define.amd ? define(["exports", "dayjs"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["ybgnb-js-utils"] = {}, global.dayjs));
3
+ })(this, (function(exports2, dayjs) {
4
+ "use strict";
5
+ class CommonError {
6
+ message;
7
+ constructor(message) {
8
+ this.message = message;
9
+ }
10
+ }
11
+ class AbortedError extends CommonError {
12
+ constructor() {
13
+ super("操作已取消");
14
+ }
15
+ }
16
+ class BaseUtils {
17
+ /**
18
+ * 判断是否为通用异常
19
+ * @param error 需要判断的异常
20
+ */
21
+ static isCommonError(error) {
22
+ return error instanceof CommonError;
23
+ }
24
+ /**
25
+ * 获取错误信息
26
+ * @param error
27
+ */
28
+ static getErrorMessage(error) {
29
+ return this.convertToCommonError(error).message;
30
+ }
31
+ /**
32
+ * 设置异常信息的前置提示
33
+ * @param error 异常
34
+ * @param preMsg 前置信息 `${preMsg}${error.message}`
35
+ */
36
+ static prependErrorMessage(error, preMsg) {
37
+ if (preMsg) {
38
+ error.message = `${preMsg}${error.message}`;
39
+ }
40
+ return error;
41
+ }
42
+ /**
43
+ * 转换到通用异常
44
+ * @param error 异常
45
+ * @param preMsg 前置提示 `${preMsg}${error.message}`
46
+ */
47
+ static convertToCommonError(error, preMsg) {
48
+ if (this.isCommonError(error)) {
49
+ return this.prependErrorMessage(error, preMsg);
50
+ }
51
+ if (typeof error === "object" && error !== null && "message" in error && typeof error.message === "string") {
52
+ const newError = error;
53
+ return this.prependErrorMessage(new CommonError(newError.message), preMsg);
54
+ } else {
55
+ return this.prependErrorMessage(new CommonError(String(error)), preMsg);
56
+ }
57
+ }
58
+ /**
59
+ * 格式化时间为 2020-02-02 20:20:20 的字符串
60
+ * @param date 需要格式化的时间,为空则获取当前时间
61
+ */
62
+ static getFormatedDateTime(date) {
63
+ return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
64
+ }
65
+ /**
66
+ * 格式化时间为 2020-02-02 的字符串
67
+ * @param date 需要格式化的时间,为空则获取当前时间
68
+ */
69
+ static getFormatedDate(date) {
70
+ return dayjs(date).format("YYYY-MM-DD");
71
+ }
72
+ /**
73
+ * 格式化时间为 20:20:20 的字符串
74
+ * @param date 需要格式化的时间,为空则获取当前时间
75
+ */
76
+ static getFormatedTime(date) {
77
+ return dayjs(date).format("HH:mm:ss");
78
+ }
79
+ }
80
+ class BizResult {
81
+ success;
82
+ msg;
83
+ data;
84
+ constructor(success, msg, data) {
85
+ this.success = success;
86
+ this.msg = msg;
87
+ this.data = data;
88
+ }
89
+ static createSuccess(data) {
90
+ return new BizResult(true, "操作成功", data);
91
+ }
92
+ static createFail(msg = "操作失败", data) {
93
+ return new BizResult(false, msg, data);
94
+ }
95
+ static createError(e) {
96
+ return new BizResult(false, BaseUtils.getErrorMessage(e));
97
+ }
98
+ async toPromise() {
99
+ if (this.success) {
100
+ return this.data;
101
+ } else {
102
+ throw new CommonError(this.msg);
103
+ }
104
+ }
105
+ }
106
+ const execBiz = async (run) => {
107
+ try {
108
+ return BizResult.createSuccess(await run());
109
+ } catch (e) {
110
+ return BizResult.createError(e);
111
+ }
112
+ };
113
+ exports2.AbortedError = AbortedError;
114
+ exports2.BaseUtils = BaseUtils;
115
+ exports2.BizResult = BizResult;
116
+ exports2.CommonError = CommonError;
117
+ exports2.execBiz = execBiz;
118
+ Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
119
+ }));
120
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../src/types/error.ts","../src/utils/base-utils.ts","../src/utils/biz-result.ts"],"sourcesContent":["/**\n * 通用异常\n */\nexport class CommonError {\n message: string;\n\n constructor(message: string) {\n this.message = message;\n }\n}\n\n/**\n * 终止异常\n */\nexport class AbortedError extends CommonError {\n constructor() {\n super(\"操作已取消\");\n }\n}\n","import dayjs from 'dayjs';\nimport { CommonError } from '../types/error';\n\n/**\n * 基础工具集\n */\nexport class BaseUtils {\n /**\n * 判断是否为通用异常\n * @param error 需要判断的异常\n */\n static isCommonError(error: unknown): error is CommonError {\n return error instanceof CommonError\n }\n\n /**\n * 获取错误信息\n * @param error\n */\n static getErrorMessage(error: unknown): string {\n return this.convertToCommonError(error).message\n }\n\n /**\n * 设置异常信息的前置提示\n * @param error 异常\n * @param preMsg 前置信息 `${preMsg}${error.message}`\n */\n static prependErrorMessage(error: CommonError, preMsg?: string): CommonError {\n if (preMsg) {\n error.message = `${preMsg}${error.message}`\n }\n return error\n }\n\n /**\n * 转换到通用异常\n * @param error 异常\n * @param preMsg 前置提示 `${preMsg}${error.message}`\n */\n static convertToCommonError(error: unknown, preMsg?: string): CommonError {\n if (this.isCommonError(error)) {\n return this.prependErrorMessage(error, preMsg)\n }\n if (\n typeof error === 'object' &&\n error !== null &&\n 'message' in error &&\n typeof (error as Record<string, unknown>).message === 'string'\n ) {\n const newError = error as { message: string }\n return this.prependErrorMessage(new CommonError(newError.message), preMsg)\n } else {\n // 如果抛出的异常不是object\n return this.prependErrorMessage(new CommonError(String(error)), preMsg)\n }\n }\n\n /**\n * 格式化时间为 2020-02-02 20:20:20 的字符串\n * @param date 需要格式化的时间,为空则获取当前时间\n */\n static getFormatedDateTime(date?: Date): string {\n return dayjs(date).format('YYYY-MM-DD HH:mm:ss')\n }\n\n /**\n * 格式化时间为 2020-02-02 的字符串\n * @param date 需要格式化的时间,为空则获取当前时间\n */\n static getFormatedDate(date?: Date): string {\n return dayjs(date).format('YYYY-MM-DD')\n }\n\n /**\n * 格式化时间为 20:20:20 的字符串\n * @param date 需要格式化的时间,为空则获取当前时间\n */\n static getFormatedTime(date?: Date): string {\n return dayjs(date).format('HH:mm:ss')\n }\n}\n","import { CommonError } from '../types/error'\nimport { BaseUtils } from './base-utils'\n\n/**\n * 业务执行结果\n */\nexport class BizResult<T> {\n success: boolean\n msg: string\n data?: T\n\n constructor(success: boolean, msg: string, data?: T) {\n this.success = success\n this.msg = msg\n this.data = data\n }\n\n static createSuccess<T>(data: T) {\n return new BizResult(true, '操作成功', data)\n }\n\n static createFail<T>(msg: string = '操作失败', data?: T) {\n return new BizResult(false, msg, data)\n }\n\n static createError<T>(e: unknown) {\n return new BizResult<T>(false, BaseUtils.getErrorMessage(e))\n }\n\n /**\n * 展开成Promise\n */\n async toPromise(): Promise<void>\n async toPromise<T>(): Promise<T>\n async toPromise<T = void>(): Promise<T> {\n if (this.success) {\n return this.data as T\n } else {\n throw new CommonError(this.msg)\n }\n }\n}\n\n/**\n * 执行业务(自动包裹BuResult)\n * @param run 执行方法\n */\nexport const execBiz = async <T>(run: () => Promise<T>): Promise<BizResult<T>> => {\n try {\n return BizResult.createSuccess<T>(await run())\n } catch (e) {\n return BizResult.createError(e)\n }\n}\n"],"names":[],"mappings":";;;;EAGO,MAAM,YAAY;AAAA,IACvB;AAAA,IAEA,YAAY,SAAiB;AAC3B,WAAK,UAAU;AAAA,IACjB;AAAA,EACF;AAAA,EAKO,MAAM,qBAAqB,YAAY;AAAA,IAC5C,cAAc;AACZ,YAAM,OAAO;AAAA,IACf;AAAA,EACF;AAAA,ECZO,MAAM,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA,IAKrB,OAAO,cAAc,OAAsC;AACzD,aAAO,iBAAiB;AAAA,IAC1B;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,OAAO,gBAAgB,OAAwB;AAC7C,aAAO,KAAK,qBAAqB,KAAK,EAAE;AAAA,IAC1C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOA,OAAO,oBAAoB,OAAoB,QAA8B;AAC3E,UAAI,QAAQ;AACV,cAAM,UAAU,GAAG,MAAM,GAAG,MAAM,OAAO;AAAA,MAC3C;AACA,aAAO;AAAA,IACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOA,OAAO,qBAAqB,OAAgB,QAA8B;AACxE,UAAI,KAAK,cAAc,KAAK,GAAG;AAC7B,eAAO,KAAK,oBAAoB,OAAO,MAAM;AAAA,MAC/C;AACA,UACE,OAAO,UAAU,YACjB,UAAU,QACV,aAAa,SACb,OAAQ,MAAkC,YAAY,UACtD;AACA,cAAM,WAAW;AACjB,eAAO,KAAK,oBAAoB,IAAI,YAAY,SAAS,OAAO,GAAG,MAAM;AAAA,MAC3E,OAAO;AAEL,eAAO,KAAK,oBAAoB,IAAI,YAAY,OAAO,KAAK,CAAC,GAAG,MAAM;AAAA,MACxE;AAAA,IACF;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,OAAO,oBAAoB,MAAqB;AAC9C,aAAO,MAAM,IAAI,EAAE,OAAO,qBAAqB;AAAA,IACjD;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,OAAO,gBAAgB,MAAqB;AAC1C,aAAO,MAAM,IAAI,EAAE,OAAO,YAAY;AAAA,IACxC;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,OAAO,gBAAgB,MAAqB;AAC1C,aAAO,MAAM,IAAI,EAAE,OAAO,UAAU;AAAA,IACtC;AAAA,EACF;AAAA,EC3EO,MAAM,UAAa;AAAA,IACxB;AAAA,IACA;AAAA,IACA;AAAA,IAEA,YAAY,SAAkB,KAAa,MAAU;AACnD,WAAK,UAAU;AACf,WAAK,MAAM;AACX,WAAK,OAAO;AAAA,IACd;AAAA,IAEA,OAAO,cAAiB,MAAS;AAC/B,aAAO,IAAI,UAAU,MAAM,QAAQ,IAAI;AAAA,IACzC;AAAA,IAEA,OAAO,WAAc,MAAc,QAAQ,MAAU;AACnD,aAAO,IAAI,UAAU,OAAO,KAAK,IAAI;AAAA,IACvC;AAAA,IAEA,OAAO,YAAe,GAAY;AAChC,aAAO,IAAI,UAAa,OAAO,UAAU,gBAAgB,CAAC,CAAC;AAAA,IAC7D;AAAA,IAOA,MAAM,YAAkC;AACtC,UAAI,KAAK,SAAS;AAChB,eAAO,KAAK;AAAA,MACd,OAAO;AACL,cAAM,IAAI,YAAY,KAAK,GAAG;AAAA,MAChC;AAAA,IACF;AAAA,EACF;AAMO,QAAM,UAAU,OAAU,QAAiD;AAChF,QAAI;AACF,aAAO,UAAU,cAAiB,MAAM,KAAK;AAAA,IAC/C,SAAS,GAAG;AACV,aAAO,UAAU,YAAY,CAAC;AAAA,IAChC;AAAA,EACF;;;;;;;;"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * 通用异常
3
+ */
4
+ export declare class CommonError {
5
+ message: string;
6
+ constructor(message: string);
7
+ }
8
+ /**
9
+ * 终止异常
10
+ */
11
+ export declare class AbortedError extends CommonError {
12
+ constructor();
13
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 提取getter属性
3
+ */
4
+ export type ExtractGetterProperties<T> = {
5
+ [K in keyof T as K extends `get${infer Rest}` ? Uncapitalize<Rest> : never]: T[K] extends (...args: any[]) => Promise<infer R> ? R : never;
6
+ };
@@ -0,0 +1,43 @@
1
+ import { CommonError } from '../types/error';
2
+ /**
3
+ * 基础工具集
4
+ */
5
+ export declare class BaseUtils {
6
+ /**
7
+ * 判断是否为通用异常
8
+ * @param error 需要判断的异常
9
+ */
10
+ static isCommonError(error: unknown): error is CommonError;
11
+ /**
12
+ * 获取错误信息
13
+ * @param error
14
+ */
15
+ static getErrorMessage(error: unknown): string;
16
+ /**
17
+ * 设置异常信息的前置提示
18
+ * @param error 异常
19
+ * @param preMsg 前置信息 `${preMsg}${error.message}`
20
+ */
21
+ static prependErrorMessage(error: CommonError, preMsg?: string): CommonError;
22
+ /**
23
+ * 转换到通用异常
24
+ * @param error 异常
25
+ * @param preMsg 前置提示 `${preMsg}${error.message}`
26
+ */
27
+ static convertToCommonError(error: unknown, preMsg?: string): CommonError;
28
+ /**
29
+ * 格式化时间为 2020-02-02 20:20:20 的字符串
30
+ * @param date 需要格式化的时间,为空则获取当前时间
31
+ */
32
+ static getFormatedDateTime(date?: Date): string;
33
+ /**
34
+ * 格式化时间为 2020-02-02 的字符串
35
+ * @param date 需要格式化的时间,为空则获取当前时间
36
+ */
37
+ static getFormatedDate(date?: Date): string;
38
+ /**
39
+ * 格式化时间为 20:20:20 的字符串
40
+ * @param date 需要格式化的时间,为空则获取当前时间
41
+ */
42
+ static getFormatedTime(date?: Date): string;
43
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 业务执行结果
3
+ */
4
+ export declare class BizResult<T> {
5
+ success: boolean;
6
+ msg: string;
7
+ data?: T;
8
+ constructor(success: boolean, msg: string, data?: T);
9
+ static createSuccess<T>(data: T): BizResult<T>;
10
+ static createFail<T>(msg?: string, data?: T): BizResult<T>;
11
+ static createError<T>(e: unknown): BizResult<T>;
12
+ /**
13
+ * 展开成Promise
14
+ */
15
+ toPromise(): Promise<void>;
16
+ toPromise<T>(): Promise<T>;
17
+ }
18
+ /**
19
+ * 执行业务(自动包裹BuResult)
20
+ * @param run 执行方法
21
+ */
22
+ export declare const execBiz: <T>(run: () => Promise<T>) => Promise<BizResult<T>>;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@ybgnb/utils",
3
+ "version": "0.0.1",
4
+ "author": "hzhilong",
5
+ "private": false,
6
+ "description": "自用工具库",
7
+ "main": "dist/index.umd.js",
8
+ "module": "dist/index.mjs",
9
+ "types": "dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.umd.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "build": "barrelsby -c barrelsby.json --delete --noHeader && vite build",
22
+ "npm login": " npm login",
23
+ "npm publish": " npm publish --access public"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/hzhilong/ybgnb-js",
28
+ "directory": "utils"
29
+ },
30
+ "keywords": [
31
+ "ybgnb"
32
+ ],
33
+ "license": "MIT",
34
+ "bugs": {
35
+ "url": "https://github.com/hzhilong/ybgnb-js/issues"
36
+ },
37
+ "homepage": "https://github.com/hzhilong/ybgnb-js#readme",
38
+ "devDependencies": {
39
+ "barrelsby": "^2.8.1",
40
+ "rimraf": "^5.0.5",
41
+ "typescript": "^5.8.3",
42
+ "vite": "^7.1.5",
43
+ "vite-plugin-dts": "^4.5.4"
44
+ },
45
+ "peerDependencies": {
46
+ "dayjs": "^1.11.13",
47
+ "nanoid": "^5.1.5"
48
+ }
49
+ }