@hz-9/a5-core 0.2.0-alpha.5 → 0.2.0-alpha.50

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/dist/all.d.ts CHANGED
@@ -1,13 +1,17 @@
1
+ /// <reference types="node" />
2
+
1
3
  import { FastifyReply as FastifyReply_2 } from 'fastify';
2
4
  import { FastifyRequest as FastifyRequest_2 } from 'fastify';
5
+ import { IncomingHttpHeaders } from 'node:http';
3
6
  import { Logger } from '@nestjs/common';
4
7
  import { LoggerService } from '@nestjs/common/services/logger.service';
5
8
  import { LoggerService as LoggerService_2 } from '@nestjs/common';
6
9
  import { LogLevel } from '@nestjs/common/services/logger.service';
7
- import { nanoid } from 'nanoid';
8
10
  import { NestFactoryStatic } from '@nestjs/core/nest-factory';
9
11
  import { NestFastifyApplication } from '@nestjs/platform-fastify';
10
12
  import { NestMiddleware } from '@nestjs/common';
13
+ import { V4Options } from 'uuid';
14
+ import { V7Options } from 'uuid';
11
15
 
12
16
  /**
13
17
  * @public
@@ -391,6 +395,27 @@ declare interface A5FormatMessageOptions {
391
395
  */
392
396
  export declare type A5GlobalProvideToken = `Global.A5.${string}`;
393
397
 
398
+ /**
399
+ * A5 加载包错误
400
+ * 当动态加载可选依赖包失败时抛出此错误
401
+ * @public
402
+ */
403
+ export declare class A5LoadPackageError extends Error {
404
+ /**
405
+ * 错误名称
406
+ */
407
+ readonly name: string;
408
+ /**
409
+ * 尝试加载的包名
410
+ */
411
+ readonly packageName: string;
412
+ /**
413
+ * 原始错误(如果有)
414
+ */
415
+ readonly cause?: Error;
416
+ constructor(packageName: string, message?: string, cause?: Error);
417
+ }
418
+
394
419
  /**
395
420
  * @public
396
421
  *
@@ -399,6 +424,33 @@ export declare type A5GlobalProvideToken = `Global.A5.${string}`;
399
424
  */
400
425
  export declare type A5ModuleConfigPath = `A5.${string}`;
401
426
 
427
+ /**
428
+ * A5 随机工具类
429
+ *
430
+ * 提供生成随机值和 UUID 的静态方法
431
+ *
432
+ * @public
433
+ */
434
+ export declare class A5RandomUtil {
435
+ static nanoid(size?: number): string;
436
+ static uuidV4(options?: V4Options): string;
437
+ static uuidV7(options?: V7Options): string;
438
+ /**
439
+ * 生成指定位数的随机数字
440
+ *
441
+ * @param digits - 位数,默认 6
442
+ * @returns 随机数字字符串
443
+ */
444
+ static randomDigits(digits?: number): string;
445
+ /**
446
+ * 生成指定长度的随机字符串(a-zA-Z0-9)
447
+ *
448
+ * @param length - 长度,默认 8
449
+ * @returns 随机字符串
450
+ */
451
+ static randomString(length?: number, chars?: string): string;
452
+ }
453
+
402
454
  /**
403
455
  * @public
404
456
  *
@@ -433,8 +485,21 @@ export declare class A5Util {
433
485
  * @returns 资源路径的绝对路径。
434
486
  */
435
487
  static tryWithAbsolutePath(sourcePath: string, basePath: string): string;
488
+ static getReqIdFromRequest(req: FastifyRequest): string;
489
+ static getReqHeadersFromRequest(req: FastifyRequest): IncomingHttpHeaders;
490
+ static getReqIdAndHeadersFromRequest(req: FastifyRequest): {
491
+ reqId: string;
492
+ reqHeaders: IncomingHttpHeaders;
493
+ };
436
494
  }
437
495
 
496
+ /**
497
+ * 类型断言工具 - 判断两个类型结构相似
498
+ *
499
+ * @public
500
+ */
501
+ export declare type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>;
502
+
438
503
  /**
439
504
  * @public
440
505
  */
@@ -462,16 +527,67 @@ export declare class ColorUtil {
462
527
  static clear(text: string): string;
463
528
  }
464
529
 
530
+ /**
531
+ * 类型调试工具 - 展开类型定义
532
+ *
533
+ * @public
534
+ */
535
+ export declare type Debug<T> = {
536
+ [K in keyof T]: T[K];
537
+ };
538
+
465
539
  /**
466
540
  * @public
467
541
  */
468
542
  export declare const DEFAULT_LOG_LEVELS: LogLevel[];
469
543
 
544
+ /**
545
+ * 类型断言工具 - 判断两个类型相等
546
+ *
547
+ * @public
548
+ */
549
+ export declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
550
+
470
551
  /**
471
552
  * @public
472
553
  */
473
554
  export declare const ERROR_WELCOME_MSG: string;
474
555
 
556
+ /**
557
+ * 类型断言工具 - 期望类型为 true
558
+ *
559
+ * @public
560
+ */
561
+ export declare type Expect<T extends true> = T;
562
+
563
+ /**
564
+ * 类型断言工具 - 期望类型扩展自目标类型
565
+ *
566
+ * @public
567
+ */
568
+ export declare type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false;
569
+
570
+ /**
571
+ * 类型断言工具 - 期望值为 false
572
+ *
573
+ * @public
574
+ */
575
+ export declare type ExpectFalse<T extends false> = T;
576
+
577
+ /**
578
+ * 类型断言工具 - 期望值为 true
579
+ *
580
+ * @public
581
+ */
582
+ export declare type ExpectTrue<T extends true> = T;
583
+
584
+ /**
585
+ * 类型断言工具 - 验证函数参数类型
586
+ *
587
+ * @public
588
+ */
589
+ export declare type ExpectValidArgs<FUNC extends (...args: any[]) => any, ARGS extends any[]> = ARGS extends Parameters<FUNC> ? true : false;
590
+
475
591
  /**
476
592
  * @public
477
593
  */
@@ -535,6 +651,57 @@ export declare abstract class IA5ConfigModule {
535
651
  export declare abstract class IA5LogModule {
536
652
  }
537
653
 
654
+ /**
655
+ * 类型断言工具 - 判断是否为 any 类型
656
+ *
657
+ * @public
658
+ */
659
+ export declare type IsAny<T> = 0 extends 1 & T ? true : false;
660
+
661
+ /**
662
+ * 类型断言工具 - 判断是否为 false
663
+ *
664
+ * @public
665
+ */
666
+ export declare type IsFalse<T extends false> = T;
667
+
668
+ /**
669
+ * 类型断言工具 - 判断是否为 true
670
+ *
671
+ * @public
672
+ */
673
+ export declare type IsTrue<T extends true> = T;
674
+
675
+ /**
676
+ * JSON 数组类型
677
+ *
678
+ * 表示 JSON 数组结构,元素为任意 JSON 值
679
+ *
680
+ * @public
681
+ */
682
+ export declare interface JsonArray extends Array<JsonValue> {
683
+ }
684
+
685
+ /**
686
+ * JSON 对象类型
687
+ *
688
+ * 表示 JSON 对象结构,键为字符串,值为任意 JSON 值
689
+ *
690
+ * @public
691
+ */
692
+ export declare interface JsonObject {
693
+ [key: string]: JsonValue;
694
+ }
695
+
696
+ /**
697
+ * JSON 值类型
698
+ *
699
+ * 表示所有合法的 JSON 值类型,包括基础类型、对象和数组
700
+ *
701
+ * @public
702
+ */
703
+ export declare type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
704
+
538
705
  /**
539
706
  * 包加载工具类
540
707
  * 用于统一处理可选依赖的动态导入
@@ -547,13 +714,13 @@ export declare class LoadPackageUtil {
547
714
  * @returns 加载的包模块
548
715
  * @throws 当包加载失败时抛出错误
549
716
  */
550
- static loadPackage<T = unknown>(packageName: string): Promise<T>;
717
+ static loadPackage<T = unknown>(packageName: string): T;
551
718
  /**
552
719
  * 安全地动态加载包
553
720
  * @param packageName - 包名
554
721
  * @returns 加载的包模块,如果失败则返回 null
555
722
  */
556
- static loadPackageSafe<T = unknown>(packageName: string): Promise<T | null>;
723
+ static loadPackageSafe<T = unknown>(packageName: string): T | null;
557
724
  /**
558
725
  * 带重试的动态加载包
559
726
  * @param packageName - 包名
@@ -602,6 +769,15 @@ export declare class LogoUtil {
602
769
  */
603
770
  export declare const MAIN_STATIC_PATH: string;
604
771
 
772
+ /**
773
+ * 类型合并工具 - 合并嵌套类型
774
+ *
775
+ * @public
776
+ */
777
+ export declare type MergeInsertions<T> = T extends object ? {
778
+ [K in keyof T]: MergeInsertions<T[K]>;
779
+ } : T;
780
+
605
781
  /**
606
782
  *
607
783
  * @public
@@ -620,7 +796,19 @@ export declare const MODULE_CONFIG_PATH_A5_CONFIG: <T extends string>(str: T) =>
620
796
  */
621
797
  export declare const MODULE_CONFIG_PATH_A5_LOG: <T extends string>(str: T) => `A5.log.${T}`;
622
798
 
623
- export { nanoid }
799
+ /**
800
+ * 类型断言工具 - 判断不是 any 类型
801
+ *
802
+ * @public
803
+ */
804
+ export declare type NotAny<T> = true extends IsAny<T> ? false : true;
805
+
806
+ /**
807
+ * 类型断言工具 - 判断两个类型不相等
808
+ *
809
+ * @public
810
+ */
811
+ export declare type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
624
812
 
625
813
  /**
626
814
  * @public
@@ -661,6 +849,13 @@ export declare class RunEnvUtil {
661
849
  static get inJest(): boolean;
662
850
  }
663
851
 
852
+ /**
853
+ * 类型转换工具 - 联合类型转交叉类型
854
+ *
855
+ * @public
856
+ */
857
+ export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
858
+
664
859
  /**
665
860
  * @public
666
861
  */
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.A5Application = void 0;
4
4
  const common_1 = require("@nestjs/common");
5
5
  const core_1 = require("@nestjs/core");
6
- const nanoid_1 = require("nanoid");
7
6
  const index_1 = require("../const/index");
8
7
  const log_1 = require("../module/log");
9
8
  const util_1 = require("../util");
@@ -75,7 +74,7 @@ class A5Application {
75
74
  * setExtraUrl 及 getUrls 函数用于保存数据的数组
76
75
  */
77
76
  this.extraUrls = [];
78
- this.instanceId = (0, nanoid_1.nanoid)();
77
+ this.instanceId = util_1.A5RandomUtil.nanoid();
79
78
  this.nestApp = nestApp;
80
79
  this.options = {
81
80
  logger: options.logger ?? this._getA5LoggerService(),
@@ -11,6 +11,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  var __param = (this && this.__param) || function (paramIndex, decorator) {
12
12
  return function (target, key) { decorator(target, key, paramIndex); }
13
13
  };
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
14
17
  var A5ConsoleLogger_1;
15
18
  Object.defineProperty(exports, "__esModule", { value: true });
16
19
  exports.A5ConsoleLogger = exports.DEFAULT_LOG_LEVELS = void 0;
@@ -20,19 +23,11 @@ const optional_decorator_1 = require("@nestjs/common/decorators/core/optional.de
20
23
  const is_log_level_enabled_util_1 = require("@nestjs/common/services/utils/is-log-level-enabled.util");
21
24
  const cli_colors_util_1 = require("@nestjs/common/utils/cli-colors.util");
22
25
  const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
26
+ const dayjs_1 = __importDefault(require("dayjs"));
23
27
  /**
24
28
  * @public
25
29
  */
26
30
  exports.DEFAULT_LOG_LEVELS = ['log', 'error', 'warn', 'debug', 'verbose', 'fatal'];
27
- const dateTimeFormatter = new Intl.DateTimeFormat(undefined, {
28
- year: 'numeric',
29
- month: '2-digit',
30
- day: '2-digit',
31
- hour: 'numeric',
32
- minute: 'numeric',
33
- second: 'numeric',
34
- fractionalSecondDigits: 3,
35
- });
36
31
  /**
37
32
  * @public
38
33
  */
@@ -185,7 +180,7 @@ let A5ConsoleLogger = A5ConsoleLogger_1 = class A5ConsoleLogger {
185
180
  return logLevel.toUpperCase().padStart(7, ' ');
186
181
  }
187
182
  formatTimestamp(dateMilliseconds) {
188
- return dateTimeFormatter.format(dateMilliseconds).replace(/ /g, 'T').replace(/\//g, '-');
183
+ return (0, dayjs_1.default)(dateMilliseconds).format('YYYY-MM-DDTHH:mm:ss.SSS');
189
184
  }
190
185
  formatContext(context) {
191
186
  if (context.length === 0)
@@ -35,7 +35,10 @@ class A5FactoryStatic {
35
35
  const nestOptions = {
36
36
  bufferLogs: true,
37
37
  };
38
- const nestApplication = await this.nestFactoryStatic.create(module, new platform_fastify_1.FastifyAdapter(), nestOptions);
38
+ const nestApplication = await this.nestFactoryStatic.create(module, new platform_fastify_1.FastifyAdapter({
39
+ // 生成 Req Id
40
+ genReqId: (req) => util_1.A5RandomUtil.uuidV7(),
41
+ }), nestOptions);
39
42
  // this.initBodyParser(nestApplication, options)
40
43
  const app = new a5_application_1.A5Application(nestApplication);
41
44
  await app.init();
@@ -0,0 +1,20 @@
1
+ /**
2
+ * A5 加载包错误
3
+ * 当动态加载可选依赖包失败时抛出此错误
4
+ * @public
5
+ */
6
+ export declare class A5LoadPackageError extends Error {
7
+ /**
8
+ * 错误名称
9
+ */
10
+ readonly name: string;
11
+ /**
12
+ * 尝试加载的包名
13
+ */
14
+ readonly packageName: string;
15
+ /**
16
+ * 原始错误(如果有)
17
+ */
18
+ readonly cause?: Error;
19
+ constructor(packageName: string, message?: string, cause?: Error);
20
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.A5LoadPackageError = void 0;
4
+ /**
5
+ * A5 加载包错误
6
+ * 当动态加载可选依赖包失败时抛出此错误
7
+ * @public
8
+ */
9
+ class A5LoadPackageError extends Error {
10
+ constructor(packageName, message, cause) {
11
+ super(message || `Failed to load package: ${packageName}`);
12
+ /**
13
+ * 错误名称
14
+ */
15
+ this.name = 'A5LoadPackageError';
16
+ this.packageName = packageName;
17
+ this.cause = cause;
18
+ // 设置原型链,确保 instanceof 正常工作
19
+ Object.setPrototypeOf(this, A5LoadPackageError.prototype);
20
+ }
21
+ }
22
+ exports.A5LoadPackageError = A5LoadPackageError;
23
+ //# sourceMappingURL=a5-load-package.error.js.map
@@ -0,0 +1 @@
1
+ export * from './a5-load-package.error';
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./nanoid"), exports);
17
+ __exportStar(require("./a5-load-package.error"), exports);
18
18
  //# sourceMappingURL=index.js.map
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './const';
2
2
  export * from './core';
3
+ export * from './errors';
3
4
  export * from './interface';
4
5
  export * from './module';
5
6
  export * from './middleware';
6
- export * from './plugins';
7
7
  export * from './util';
package/dist/index.js CHANGED
@@ -16,9 +16,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./const"), exports);
18
18
  __exportStar(require("./core"), exports);
19
+ __exportStar(require("./errors"), exports);
19
20
  __exportStar(require("./interface"), exports);
20
21
  __exportStar(require("./module"), exports);
21
22
  __exportStar(require("./middleware"), exports);
22
- __exportStar(require("./plugins"), exports);
23
23
  __exportStar(require("./util"), exports);
24
24
  //# sourceMappingURL=index.js.map
@@ -1,3 +1,5 @@
1
1
  export * from './base';
2
2
  export * from './http';
3
+ export * from './json';
3
4
  export * from './provide-token';
5
+ export * from './type-challenges.utils';
@@ -16,5 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./base"), exports);
18
18
  __exportStar(require("./http"), exports);
19
+ __exportStar(require("./json"), exports);
19
20
  __exportStar(require("./provide-token"), exports);
21
+ __exportStar(require("./type-challenges.utils"), exports);
20
22
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,27 @@
1
+ /**
2
+ * JSON 值类型
3
+ *
4
+ * 表示所有合法的 JSON 值类型,包括基础类型、对象和数组
5
+ *
6
+ * @public
7
+ */
8
+ export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
9
+ /**
10
+ * JSON 对象类型
11
+ *
12
+ * 表示 JSON 对象结构,键为字符串,值为任意 JSON 值
13
+ *
14
+ * @public
15
+ */
16
+ export interface JsonObject {
17
+ [key: string]: JsonValue;
18
+ }
19
+ /**
20
+ * JSON 数组类型
21
+ *
22
+ * 表示 JSON 数组结构,元素为任意 JSON 值
23
+ *
24
+ * @public
25
+ */
26
+ export interface JsonArray extends Array<JsonValue> {
27
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1,94 @@
1
+ /**
2
+ * 类型断言工具 - 期望类型为 true
3
+ *
4
+ * @public
5
+ */
6
+ export type Expect<T extends true> = T;
7
+ /**
8
+ * 类型断言工具 - 期望值为 true
9
+ *
10
+ * @public
11
+ */
12
+ export type ExpectTrue<T extends true> = T;
13
+ /**
14
+ * 类型断言工具 - 期望值为 false
15
+ *
16
+ * @public
17
+ */
18
+ export type ExpectFalse<T extends false> = T;
19
+ /**
20
+ * 类型断言工具 - 判断是否为 true
21
+ *
22
+ * @public
23
+ */
24
+ export type IsTrue<T extends true> = T;
25
+ /**
26
+ * 类型断言工具 - 判断是否为 false
27
+ *
28
+ * @public
29
+ */
30
+ export type IsFalse<T extends false> = T;
31
+ /**
32
+ * 类型断言工具 - 判断两个类型不相等
33
+ *
34
+ * @public
35
+ */
36
+ export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
37
+ /**
38
+ * 类型断言工具 - 判断两个类型相等
39
+ *
40
+ * @public
41
+ */
42
+ export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
43
+ /**
44
+ * 类型断言工具 - 判断是否为 any 类型
45
+ *
46
+ * @public
47
+ */
48
+ export type IsAny<T> = 0 extends 1 & T ? true : false;
49
+ /**
50
+ * 类型断言工具 - 判断不是 any 类型
51
+ *
52
+ * @public
53
+ */
54
+ export type NotAny<T> = true extends IsAny<T> ? false : true;
55
+ /**
56
+ * 类型调试工具 - 展开类型定义
57
+ *
58
+ * @public
59
+ */
60
+ export type Debug<T> = {
61
+ [K in keyof T]: T[K];
62
+ };
63
+ /**
64
+ * 类型合并工具 - 合并嵌套类型
65
+ *
66
+ * @public
67
+ */
68
+ export type MergeInsertions<T> = T extends object ? {
69
+ [K in keyof T]: MergeInsertions<T[K]>;
70
+ } : T;
71
+ /**
72
+ * 类型断言工具 - 判断两个类型结构相似
73
+ *
74
+ * @public
75
+ */
76
+ export type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>;
77
+ /**
78
+ * 类型断言工具 - 期望类型扩展自目标类型
79
+ *
80
+ * @public
81
+ */
82
+ export type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false;
83
+ /**
84
+ * 类型断言工具 - 验证函数参数类型
85
+ *
86
+ * @public
87
+ */
88
+ export type ExpectValidArgs<FUNC extends (...args: any[]) => any, ARGS extends any[]> = ARGS extends Parameters<FUNC> ? true : false;
89
+ /**
90
+ * 类型转换工具 - 联合类型转交叉类型
91
+ *
92
+ * @public
93
+ */
94
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=type-challenges.utils.js.map
@@ -10,10 +10,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  /* eslint-disable @typescript-eslint/consistent-type-definitions */
13
- const core_1 = require("../../../core");
14
- const util_1 = require("../../../util");
15
13
  const node_console_1 = __importDefault(require("node:console"));
16
14
  const common_1 = require("@nestjs/common");
15
+ const core_1 = require("../../../core");
16
+ const util_1 = require("../../../util");
17
17
  describe('A5Factory', () => {
18
18
  describe('create', () => {
19
19
  describe('printLogo', () => {
@@ -1,3 +1,6 @@
1
+ /// <reference types="node" />
2
+ import { IncomingHttpHeaders } from 'node:http';
3
+ import { FastifyRequest } from '../interface/http';
1
4
  /**
2
5
  *
3
6
  * @public
@@ -24,4 +27,10 @@ export declare class A5Util {
24
27
  * @returns 资源路径的绝对路径。
25
28
  */
26
29
  static tryWithAbsolutePath(sourcePath: string, basePath: string): string;
30
+ static getReqIdFromRequest(req: FastifyRequest): string;
31
+ static getReqHeadersFromRequest(req: FastifyRequest): IncomingHttpHeaders;
32
+ static getReqIdAndHeadersFromRequest(req: FastifyRequest): {
33
+ reqId: string;
34
+ reqHeaders: IncomingHttpHeaders;
35
+ };
27
36
  }
@@ -36,6 +36,19 @@ class A5Util {
36
36
  ? (0, upath_1.normalize)(sourcePath)
37
37
  : (0, upath_1.resolve)((0, upath_1.normalize)(basePath), (0, upath_1.normalize)(sourcePath));
38
38
  }
39
+ static getReqIdFromRequest(req) {
40
+ return req.id;
41
+ }
42
+ static getReqHeadersFromRequest(req) {
43
+ // TODO 支持 HTTP2
44
+ return req.headers;
45
+ }
46
+ static getReqIdAndHeadersFromRequest(req) {
47
+ return {
48
+ reqId: req.id,
49
+ reqHeaders: req.headers,
50
+ };
51
+ }
39
52
  }
40
53
  exports.A5Util = A5Util;
41
54
  //# sourceMappingURL=a5.util.js.map
@@ -2,4 +2,5 @@ export * from './a5.util';
2
2
  export * from './color.util';
3
3
  export * from './load-package.util';
4
4
  export * from './logo.util';
5
+ export * from './random.util';
5
6
  export * from './run-env.util';
@@ -18,5 +18,6 @@ __exportStar(require("./a5.util"), exports);
18
18
  __exportStar(require("./color.util"), exports);
19
19
  __exportStar(require("./load-package.util"), exports);
20
20
  __exportStar(require("./logo.util"), exports);
21
+ __exportStar(require("./random.util"), exports);
21
22
  __exportStar(require("./run-env.util"), exports);
22
23
  //# sourceMappingURL=index.js.map
@@ -10,13 +10,13 @@ export declare class LoadPackageUtil {
10
10
  * @returns 加载的包模块
11
11
  * @throws 当包加载失败时抛出错误
12
12
  */
13
- static loadPackage<T = unknown>(packageName: string): Promise<T>;
13
+ static loadPackage<T = unknown>(packageName: string): T;
14
14
  /**
15
15
  * 安全地动态加载包
16
16
  * @param packageName - 包名
17
17
  * @returns 加载的包模块,如果失败则返回 null
18
18
  */
19
- static loadPackageSafe<T = unknown>(packageName: string): Promise<T | null>;
19
+ static loadPackageSafe<T = unknown>(packageName: string): T | null;
20
20
  /**
21
21
  * 带重试的动态加载包
22
22
  * @param packageName - 包名
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LoadPackageUtil = void 0;
4
+ const errors_1 = require("../errors");
4
5
  const a5_util_1 = require("./a5.util");
5
6
  // TODO 当前文件,并未编写单元测试
6
7
  /**
@@ -15,7 +16,7 @@ class LoadPackageUtil {
15
16
  * @returns 加载的包模块
16
17
  * @throws 当包加载失败时抛出错误
17
18
  */
18
- static async loadPackage(packageName) {
19
+ static loadPackage(packageName) {
19
20
  try {
20
21
  // 使用 Dynimic_import 引入模块,仍会显示 ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG 报错,
21
22
  // 使用 require 进行动态引入;
@@ -25,8 +26,10 @@ class LoadPackageUtil {
25
26
  return module;
26
27
  }
27
28
  catch (error) {
28
- // TODO 优化报错显示
29
- throw new Error(`Failed to load package "${packageName}": ${error instanceof Error ? error.message : String(error)}`);
29
+ // 抛出 A5LoadPackageError 错误
30
+ const cause = error instanceof Error ? error : undefined;
31
+ const message = error instanceof Error ? error.message : String(error);
32
+ throw new errors_1.A5LoadPackageError(packageName, `Failed to load package "${packageName}": ${message}`, cause);
30
33
  }
31
34
  }
32
35
  /**
@@ -34,9 +37,9 @@ class LoadPackageUtil {
34
37
  * @param packageName - 包名
35
38
  * @returns 加载的包模块,如果失败则返回 null
36
39
  */
37
- static async loadPackageSafe(packageName) {
40
+ static loadPackageSafe(packageName) {
38
41
  try {
39
- return await LoadPackageUtil.loadPackage(packageName);
42
+ return LoadPackageUtil.loadPackage(packageName);
40
43
  }
41
44
  catch {
42
45
  return null;
@@ -54,7 +57,7 @@ class LoadPackageUtil {
54
57
  let lastError = null;
55
58
  for (let i = 0; i <= retries; i += 1) {
56
59
  try {
57
- return await LoadPackageUtil.loadPackage(packageName);
60
+ return LoadPackageUtil.loadPackage(packageName);
58
61
  }
59
62
  catch (error) {
60
63
  lastError = error instanceof Error ? error : new Error(String(error));
@@ -64,7 +67,8 @@ class LoadPackageUtil {
64
67
  }
65
68
  }
66
69
  }
67
- throw lastError || new Error(`Failed to load package "${packageName}" after ${retries + 1} attempts`);
70
+ throw (lastError ||
71
+ new errors_1.A5LoadPackageError(packageName, `Failed to load package "${packageName}" after ${retries + 1} attempts`));
68
72
  }
69
73
  }
70
74
  exports.LoadPackageUtil = LoadPackageUtil;
@@ -0,0 +1,27 @@
1
+ import { V4Options, V7Options } from 'uuid';
2
+ /**
3
+ * A5 随机工具类
4
+ *
5
+ * 提供生成随机值和 UUID 的静态方法
6
+ *
7
+ * @public
8
+ */
9
+ export declare class A5RandomUtil {
10
+ static nanoid(size?: number): string;
11
+ static uuidV4(options?: V4Options): string;
12
+ static uuidV7(options?: V7Options): string;
13
+ /**
14
+ * 生成指定位数的随机数字
15
+ *
16
+ * @param digits - 位数,默认 6
17
+ * @returns 随机数字字符串
18
+ */
19
+ static randomDigits(digits?: number): string;
20
+ /**
21
+ * 生成指定长度的随机字符串(a-zA-Z0-9)
22
+ *
23
+ * @param length - 长度,默认 8
24
+ * @returns 随机字符串
25
+ */
26
+ static randomString(length?: number, chars?: string): string;
27
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.A5RandomUtil = void 0;
4
+ const nanoid_1 = require("nanoid");
5
+ const uuid_1 = require("uuid");
6
+ /**
7
+ * A5 随机工具类
8
+ *
9
+ * 提供生成随机值和 UUID 的静态方法
10
+ *
11
+ * @public
12
+ */
13
+ class A5RandomUtil {
14
+ static nanoid(size) {
15
+ return (0, nanoid_1.nanoid)(size);
16
+ }
17
+ static uuidV4(options) {
18
+ return (0, uuid_1.v4)(options);
19
+ }
20
+ static uuidV7(options) {
21
+ return (0, uuid_1.v7)(options);
22
+ }
23
+ /**
24
+ * 生成指定位数的随机数字
25
+ *
26
+ * @param digits - 位数,默认 6
27
+ * @returns 随机数字字符串
28
+ */
29
+ static randomDigits(digits = 6) {
30
+ const min = 10 ** (digits - 1);
31
+ const max = 10 ** digits - 1;
32
+ const randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
33
+ return randomNum.toString();
34
+ }
35
+ /**
36
+ * 生成指定长度的随机字符串(a-zA-Z0-9)
37
+ *
38
+ * @param length - 长度,默认 8
39
+ * @returns 随机字符串
40
+ */
41
+ static randomString(length = 8, chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
42
+ return (0, nanoid_1.customAlphabet)(chars)(length);
43
+ }
44
+ }
45
+ exports.A5RandomUtil = A5RandomUtil;
46
+ //# sourceMappingURL=random.util.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hz-9/a5-core",
3
- "version": "0.2.0-alpha.5",
3
+ "version": "0.2.0-alpha.50",
4
4
  "description": "The core library for the `@hz-9/a5-*` series of repositories.",
5
5
  "keywords": [
6
6
  "nest",
@@ -38,31 +38,28 @@
38
38
  ],
39
39
  "dependencies": {
40
40
  "@nestjs/platform-fastify": "^10.0.0",
41
- "fastify": "4.28.1",
42
41
  "@types/fs-extra": "~11.0.3",
43
42
  "@types/heft-jest": "~1.0.3",
44
- "@types/lodash": "~4.17.4",
45
43
  "@types/node": "~20.3.1",
46
44
  "@types/pug": "~2.0.10",
47
- "@types/uuid": "~9.0.8",
48
- "@type-challenges/utils": "~0.1.1",
45
+ "@types/uuid": "^10.0.0",
49
46
  "axios": "~1.7.2",
50
47
  "body-parser": "~1.20.2",
51
48
  "dayjs": "~1.11.10",
49
+ "fastify": "4.28.1",
52
50
  "fs-extra": "~11.1.1",
53
- "lodash": "~4.17.21",
54
51
  "nanoid": "^3.3.7",
55
52
  "pug": "~3.0.3",
56
53
  "type-fest": "~4.20.0",
57
54
  "upath": "~2.0.1",
58
- "ua-parser-js": "~1.0.38"
55
+ "ua-parser-js": "~1.0.38",
56
+ "uuid": "^10.0.0"
59
57
  },
60
58
  "devDependencies": {
61
59
  "@hz-9/eslint-config-airbnb-ts": "~0.6.0",
62
60
  "@nestjs/cli": "^10.0.0",
63
61
  "@nestjs/common": "^10.0.0",
64
62
  "@nestjs/core": "^10.0.0",
65
- "@nestjs/swagger": "~7.1.14",
66
63
  "@nestjs/testing": "^10.0.0",
67
64
  "@rushstack/heft": "0.66.1",
68
65
  "@types/body-parser": "~1.19.5",
@@ -1 +0,0 @@
1
- export * from './nanoid';
@@ -1 +0,0 @@
1
- export { nanoid } from 'nanoid';
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nanoid = void 0;
4
- var nanoid_1 = require("nanoid");
5
- Object.defineProperty(exports, "nanoid", { enumerable: true, get: function () { return nanoid_1.nanoid; } });
6
- //# sourceMappingURL=nanoid.js.map