@hz-9/a5-core 0.2.0-alpha.52 → 0.2.0-alpha.54

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 ADDED
@@ -0,0 +1,920 @@
1
+ /// <reference types="node" />
2
+
3
+ import { FastifyReply as FastifyReply_2 } from 'fastify';
4
+ import { FastifyRequest as FastifyRequest_2 } from 'fastify';
5
+ import { IncomingHttpHeaders } from 'node:http';
6
+ import { Logger } from '@nestjs/common';
7
+ import { LoggerService } from '@nestjs/common/services/logger.service';
8
+ import { LoggerService as LoggerService_2 } from '@nestjs/common';
9
+ import { LogLevel } from '@nestjs/common/services/logger.service';
10
+ import ms from 'ms';
11
+ import { NestFactoryStatic } from '@nestjs/core/nest-factory';
12
+ import { NestFastifyApplication } from '@nestjs/platform-fastify';
13
+ import { NestMiddleware } from '@nestjs/common';
14
+ import { V4Options } from 'uuid';
15
+ import { V7Options } from 'uuid';
16
+
17
+ /**
18
+ * @public
19
+ *
20
+ * `A5App.prototype.listen` 参数默认值。
21
+ *
22
+ */
23
+ export declare const A5_APP_LISTEN_OPTIONS_DEFAULT: {
24
+ /**
25
+ * 是否自动退出。
26
+ */
27
+ readonly AUTO_EXIT: true;
28
+ /**
29
+ * 重试间隔。
30
+ */
31
+ readonly TRY_INTERVAL: 300;
32
+ /**
33
+ * 最大重试次数。
34
+ */
35
+ readonly TRY_TIMES: 20;
36
+ };
37
+
38
+ /**
39
+ * @public
40
+ *
41
+ * 默认监听端口。
42
+ */
43
+ export declare const A5_APP_LISTEN_PORT_DEFAULT: 16100;
44
+
45
+ /**
46
+ * @public
47
+ *
48
+ * A5App Constructor Options
49
+ *
50
+ */
51
+ export declare interface A5AppConstructorOptions {
52
+ /**
53
+ * Logger 对象。
54
+ *
55
+ * 显性设置 Logger 对象。默认情况下,将会加载 A5LoggerModule 的 Logger 实例。
56
+ *
57
+ * 如果加载失败,将会使用 A5ConsoleLogger 的实例。
58
+ *
59
+ */
60
+ readonly logger?: LoggerService_2;
61
+ }
62
+
63
+ /**
64
+ * @public
65
+ *
66
+ * `A5` for `NestApplication`。服务应用类。
67
+ *
68
+ */
69
+ export declare class A5Application {
70
+ protected readonly initTime: Date;
71
+ protected readonly logger: Logger;
72
+ /**
73
+ * setExtraUrl 及 getUrls 函数用于保存数据的数组
74
+ */
75
+ protected readonly extraUrls: string[];
76
+ /**
77
+ *
78
+ * 由 [nanoid](https://github.com/ai/nanoid) 生成的实例 ID。21 位长的字符串。
79
+ *
80
+ */
81
+ readonly instanceId: string;
82
+ readonly nestApp: NestFastifyApplication;
83
+ readonly options: A5AppOptions;
84
+ constructor(nestApp: NestFastifyApplication, options?: A5AppConstructorOptions);
85
+ init(): Promise<void>;
86
+ /**
87
+ *
88
+ * @public
89
+ *
90
+ * `A5` 应用,对网络进行监听。
91
+ *
92
+ * `A5` 增加了对端口获取失败的多次尝试。
93
+ *
94
+ * 如果想关闭自动重试,请传入 `listen({ tryTimes: 0 })`
95
+ *
96
+ * TODO 未考虑采用哪种方案,进行多次尝试。
97
+ *
98
+ */
99
+ listen(options?: A5AppListenOptions): Promise<void>;
100
+ listen(port: number | string, options?: A5AppListenOptions): Promise<void>;
101
+ /**
102
+ *
103
+ * @public
104
+ *
105
+ * 添加额外的 urls, 便于 printAddress 进行输出
106
+ *
107
+ */
108
+ setExtraUrl(urlOrUrls: string | string[]): Promise<void>;
109
+ /**
110
+ *
111
+ * @public
112
+ *
113
+ * 输出可以访问当前服务的地址。
114
+ *
115
+ * TODO 需要精准判断 IPv4 和 IPv6 等各项情况。
116
+ *
117
+ */
118
+ getUrls(): Promise<string[]>;
119
+ /**
120
+ *
121
+ * @public
122
+ *
123
+ * 在正常获取 IP 与端口后,可通过此函数输出日志信息。
124
+ *
125
+ */
126
+ printAddress(): Promise<void>;
127
+ /**
128
+ *
129
+ * @public
130
+ *
131
+ * 关闭应用。
132
+ * 是 `NestApplication.prototype.close()` 的语法糖。
133
+ *
134
+ */
135
+ close(): Promise<void>;
136
+ /**
137
+ *
138
+ * @public
139
+ *
140
+ * 应用是否处于关闭状态
141
+ *
142
+ */
143
+ isClose(): boolean;
144
+ /**
145
+ * @internal
146
+ *
147
+ * 将会优先尝试 A5 内部的日志模块。如果没有的话,才会使用 A5ConsoleLogger
148
+ *
149
+ */
150
+ private _getA5LoggerService;
151
+ }
152
+
153
+ /**
154
+ *
155
+ * @public
156
+ *
157
+ * `A5App.prototype.listen` 参数
158
+ *
159
+ */
160
+ export declare interface A5AppListenOptions {
161
+ /**
162
+ *
163
+ * 若多次尝试失败,是否退出 NestApplication。
164
+ *
165
+ * 可选。默认值为 @see A5_APP_LISTEN_OPTIONS_DEFAULT.AUTO_EXIT
166
+ *
167
+ */
168
+ readonly autoExit?: boolean;
169
+ /**
170
+ *
171
+ * 若端口被占用,则重新获取端口的时间间隔。(单位 ms)
172
+ *
173
+ * 数值是正整数。
174
+ *
175
+ * 可选。默认值为 @see A5_APP_LISTEN_OPTIONS_DEFAULT.TRY_INTERVAL
176
+ *
177
+ * TODO 后续可以添加合适的时间范围。
178
+ *
179
+ */
180
+ readonly tryInterval?: number;
181
+ /**
182
+ *
183
+ * 若端口被占用,则重新获取端口的最大尝试次数。
184
+ *
185
+ * 数值是正整数。
186
+ *
187
+ * 可选。默认值为 @see A5_APP_LISTEN_OPTIONS_DEFAULT.TRY_TIMES
188
+ *
189
+ */
190
+ readonly tryTimes?: number;
191
+ }
192
+
193
+ /**
194
+ * @public
195
+ *
196
+ * A5App Options
197
+ *
198
+ */
199
+ export declare type A5AppOptions = Required<A5AppConstructorOptions>;
200
+
201
+ /**
202
+ * @public
203
+ */
204
+ export declare class A5ConsoleLogger implements LoggerService {
205
+ protected static lastTimestampAt?: number;
206
+ protected readonly originalContext: string;
207
+ protected context: string;
208
+ protected options: A5ConsoleLoggerOptions;
209
+ constructor(options?: A5ConsoleLoggerConstructorOptions);
210
+ /**
211
+ * Write a 'log' level log, if the configured level allows for it.
212
+ * Prints to `stdout` with newline.
213
+ */
214
+ log(message: unknown, context?: string): void;
215
+ log(message: unknown, ...optionalParams: [...unknown[], string | unknown]): void;
216
+ /**
217
+ * Write an 'error' level log, if the configured level allows for it.
218
+ * Prints to `stderr` with newline.
219
+ */
220
+ error(message: unknown, stackOrContext?: string): void;
221
+ error(message: unknown, stack?: string, context?: string): void;
222
+ error(message: unknown, ...optionalParams: [...unknown[], string | unknown, string | unknown]): void;
223
+ /**
224
+ * Write a 'warn' level log, if the configured level allows for it.
225
+ * Prints to `stdout` with newline.
226
+ */
227
+ warn(message: unknown, context?: string): void;
228
+ warn(message: unknown, ...optionalParams: [...unknown[], string | unknown]): void;
229
+ /**
230
+ * Write a 'debug' level log, if the configured level allows for it.
231
+ * Prints to `stdout` with newline.
232
+ */
233
+ debug(message: unknown, context?: string): void;
234
+ debug(message: unknown, ...optionalParams: [...unknown[], string | unknown]): void;
235
+ /**
236
+ * Write a 'verbose' level log, if the configured level allows for it.
237
+ * Prints to `stdout` with newline.
238
+ */
239
+ verbose(message: unknown, context?: string): void;
240
+ verbose(message: unknown, ...optionalParams: [...unknown[], string | unknown]): void;
241
+ /**
242
+ * TODO 需要思考,是否需要让 fatal 与 error 保持一致!!!
243
+ */
244
+ /**
245
+ * Write a 'fatal' level log, if the configured level allows for it.
246
+ * Prints to `stdout` with newline.
247
+ */
248
+ fatal(message: unknown, context?: string): void;
249
+ fatal(message: unknown, ...optionalParams: [...unknown[], string | unknown]): void;
250
+ /**
251
+ * Set log levels
252
+ * @param levels - log levels
253
+ */
254
+ setLogLevels(levels: LogLevel[]): void;
255
+ /**
256
+ * Set logger context
257
+ * @param context - context
258
+ */
259
+ setContext(context: string): void;
260
+ /**
261
+ * Get logger context
262
+ * @param context - context
263
+ */
264
+ getContext(): string;
265
+ /**
266
+ * Resets the logger context to the value that was passed in the constructor.
267
+ */
268
+ resetContext(): void;
269
+ isLevelEnabled(level: LogLevel): boolean;
270
+ protected printMessages(messages: unknown[], context_: string, logLevel?: LogLevel, writeStreamType?: 'stdout' | 'stderr'): void;
271
+ protected formatMessage(options: A5FormatMessageOptions): string;
272
+ protected formatMessageStructure(options: A5FormatMessageOptions): A5FormatMessageInfo;
273
+ protected formatPrefix(): string;
274
+ protected formatPid(): string;
275
+ protected formatLogLevel(logLevel: LogLevel): string;
276
+ protected formatTimestamp(dateMilliseconds: number): string;
277
+ protected formatContext(context: string): string;
278
+ protected stringifyMessage(message: unknown, logLevel: LogLevel): string;
279
+ protected colorize(message: string, logLevel: LogLevel): string;
280
+ protected printStackTrace(stack: string | undefined): void;
281
+ protected updateAndGetTimestampDiff(): number | null;
282
+ protected formatTimestampDiff(timestampDiff: number | null): string;
283
+ protected getContextAndMessagesToPrint(args: unknown[]): {
284
+ context: string;
285
+ messages: unknown[];
286
+ };
287
+ protected getContextAndStackAndMessagesToPrint(args: unknown[]): {
288
+ messages: unknown[];
289
+ context: string;
290
+ stack?: string;
291
+ };
292
+ protected isStackFormat(stack: unknown): stack is string;
293
+ /**
294
+ * 移除最后 多个 undefined
295
+ */
296
+ protected removeLastUndefined(args: unknown[]): unknown[];
297
+ protected getColorByLogLevel(level: LogLevel): (text: string) => string;
298
+ /**
299
+ * 保证日志数据的统一格式,对 Nest 默认的日志输出进行格式化。
300
+ */
301
+ protected _contextNeedApologeticReplace(context: string): context is 'NestFactory' | 'NestApplication';
302
+ /**
303
+ * 保证日志数据的统一格式,对 Nest 默认的日志输出进行格式化。
304
+ */
305
+ protected _contextApologeticReplace(context: 'NestFactory' | 'NestApplication'): string;
306
+ /**
307
+ * 保证日志数据的统一格式,对 Nest 默认的日志输出进行格式化。
308
+ */
309
+ protected _messageApologeticReplace(message: unknown): unknown;
310
+ }
311
+
312
+ /**
313
+ * @public
314
+ */
315
+ export declare interface A5ConsoleLoggerConstructorOptions {
316
+ /**
317
+ * Context name for logs
318
+ *
319
+ * Default is ''
320
+ */
321
+ context?: string;
322
+ /**
323
+ * Enabled log levels.
324
+ *
325
+ * Default is @see DEFAULT_LOG_LEVELS
326
+ */
327
+ logLevels?: LogLevel[];
328
+ /**
329
+ * If enabled, will print timestamp (time difference) between current and previous log message.
330
+ *
331
+ * Default is `false`
332
+ */
333
+ timestamp?: boolean;
334
+ }
335
+
336
+ /**
337
+ * @public
338
+ */
339
+ export declare class A5ConsoleLoggerMiddleware implements NestMiddleware {
340
+ protected logger: Logger;
341
+ use(req: FastifyRequestRaw, res: FastifyReplyRaw, next: () => void): void;
342
+ }
343
+
344
+ /**
345
+ * @public
346
+ */
347
+ export declare type A5ConsoleLoggerOptions = Omit<Required<A5ConsoleLoggerConstructorOptions>, 'context'>;
348
+
349
+ /**
350
+ * @public
351
+ */
352
+ export declare const A5Factory: A5FactoryStatic;
353
+
354
+ /**
355
+ * @public
356
+ */
357
+ export declare interface A5FactoryCreateOptions {
358
+ /**
359
+ * 是否输出日志信息。可选。默认为 true
360
+ */
361
+ readonly printLogo?: boolean;
362
+ }
363
+
364
+ declare class A5FactoryStatic {
365
+ protected nestFactoryStatic: NestFactoryStatic;
366
+ constructor();
367
+ create(module: unknown, options?: A5FactoryCreateOptions): Promise<A5Application>;
368
+ }
369
+
370
+ /**
371
+ * @public
372
+ */
373
+ export declare interface A5FormatMessageInfo {
374
+ prefix: string;
375
+ pid: string;
376
+ timestamp: number;
377
+ timestampStr: string;
378
+ logLevel: string;
379
+ context: string;
380
+ output: string;
381
+ timestampDiff: number | null;
382
+ timestampDiffStr: string;
383
+ }
384
+
385
+ declare interface A5FormatMessageOptions {
386
+ logLevel: LogLevel;
387
+ context: string;
388
+ message: unknown;
389
+ }
390
+
391
+ /**
392
+ * @public
393
+ *
394
+ * `A5` 全局提供者 Token。
395
+ *
396
+ */
397
+ export declare type A5GlobalProvideToken = `Global.A5.${string}`;
398
+
399
+ /**
400
+ * A5 加载包错误
401
+ * 当动态加载可选依赖包失败时抛出此错误
402
+ * @public
403
+ */
404
+ export declare class A5LoadPackageError extends Error {
405
+ /**
406
+ * 错误名称
407
+ */
408
+ readonly name: string;
409
+ /**
410
+ * 尝试加载的包名
411
+ */
412
+ readonly packageName: string;
413
+ /**
414
+ * 原始错误(如果有)
415
+ */
416
+ readonly cause?: Error;
417
+ constructor(packageName: string, message?: string, cause?: Error);
418
+ }
419
+
420
+ /**
421
+ * @public
422
+ *
423
+ * `A5` 配置项路径。
424
+ *
425
+ */
426
+ export declare type A5ModuleConfigPath = `A5.${string}`;
427
+
428
+ /**
429
+ * A5 随机工具类
430
+ *
431
+ * 提供生成随机值和 UUID 的静态方法
432
+ *
433
+ * @public
434
+ */
435
+ export declare class A5RandomUtil {
436
+ static nanoid(size?: number): string;
437
+ static uuidV4(options?: V4Options): string;
438
+ static uuidV7(options?: V7Options): string;
439
+ /**
440
+ * 生成指定位数的随机数字
441
+ *
442
+ * @param digits - 位数,默认 6
443
+ * @returns 随机数字字符串
444
+ */
445
+ static randomDigits(digits?: number): string;
446
+ /**
447
+ * 生成指定长度的随机字符串(a-zA-Z0-9)
448
+ *
449
+ * @param length - 长度,默认 8
450
+ * @returns 随机字符串
451
+ */
452
+ static randomString(length?: number, chars?: string): string;
453
+ }
454
+
455
+ /**
456
+ * @public
457
+ *
458
+ * `A5` 作用域提供者 Token。
459
+ *
460
+ */
461
+ export declare type A5ScopeProvideToken = `Scope.A5.${string}`;
462
+
463
+ /**
464
+ *
465
+ * @public
466
+ *
467
+ * 通用工具类
468
+ *
469
+ */
470
+ export declare class A5Util {
471
+ /**
472
+ *
473
+ * 睡眠。
474
+ *
475
+ * @param t - 时间。单位:毫秒
476
+ */
477
+ static sleep(t: number): Promise<void>;
478
+ /**
479
+ * @public
480
+ *
481
+ * 如果不是绝对路径,则进行拼接
482
+ *
483
+ * @param sourcePath - 资源路径
484
+ * @param basePath - 基础路径
485
+ *
486
+ * @returns 资源路径的绝对路径。
487
+ */
488
+ static tryWithAbsolutePath(sourcePath: string, basePath: string): string;
489
+ static getReqIdFromRequest(req: FastifyRequest): string;
490
+ static getReqHeadersFromRequest(req: FastifyRequest): IncomingHttpHeaders;
491
+ static getReqIdAndHeadersFromRequest(req: FastifyRequest): {
492
+ reqId: string;
493
+ reqHeaders: IncomingHttpHeaders;
494
+ };
495
+ }
496
+
497
+ /**
498
+ * 类型断言工具 - 判断两个类型结构相似
499
+ *
500
+ * @public
501
+ */
502
+ export declare type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>;
503
+
504
+ /**
505
+ * @public
506
+ */
507
+ export declare class ColorUtil {
508
+ static get isColorAllowed(): boolean;
509
+ static bold(text: string): string;
510
+ static red(text: string): string;
511
+ static green(text: string): string;
512
+ static yellow(text: string): string;
513
+ static blue(text: string): string;
514
+ static magenta(text: string): string;
515
+ static cyan(text: string): string;
516
+ static redBright(text: string): string;
517
+ static greenBright(text: string): string;
518
+ static yellowBright(text: string): string;
519
+ static blueBright(text: string): string;
520
+ static magentaBright(text: string): string;
521
+ static cyanBright(text: string): string;
522
+ /**
523
+ * 移除 ANSI 颜色代码
524
+ *
525
+ * @param text - text to clear
526
+ * @returns cleaned text
527
+ */
528
+ static clear(text: string): string;
529
+ }
530
+
531
+ /**
532
+ * 类型调试工具 - 展开类型定义
533
+ *
534
+ * @public
535
+ */
536
+ export declare type Debug<T> = {
537
+ [K in keyof T]: T[K];
538
+ };
539
+
540
+ /**
541
+ * @public
542
+ */
543
+ export declare const DEFAULT_LOG_LEVELS: LogLevel[];
544
+
545
+ /**
546
+ * 类型断言工具 - 判断两个类型相等
547
+ *
548
+ * @public
549
+ */
550
+ export declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
551
+
552
+ /**
553
+ * @public
554
+ */
555
+ export declare const ERROR_WELCOME_MSG: string;
556
+
557
+ /**
558
+ * 类型断言工具 - 期望类型为 true
559
+ *
560
+ * @public
561
+ */
562
+ export declare type Expect<T extends true> = T;
563
+
564
+ /**
565
+ * 类型断言工具 - 期望类型扩展自目标类型
566
+ *
567
+ * @public
568
+ */
569
+ export declare type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false;
570
+
571
+ /**
572
+ * 类型断言工具 - 期望值为 false
573
+ *
574
+ * @public
575
+ */
576
+ export declare type ExpectFalse<T extends false> = T;
577
+
578
+ /**
579
+ * 类型断言工具 - 期望值为 true
580
+ *
581
+ * @public
582
+ */
583
+ export declare type ExpectTrue<T extends true> = T;
584
+
585
+ /**
586
+ * 类型断言工具 - 验证函数参数类型
587
+ *
588
+ * @public
589
+ */
590
+ export declare type ExpectValidArgs<FUNC extends (...args: any[]) => any, ARGS extends any[]> = ARGS extends Parameters<FUNC> ? true : false;
591
+
592
+ /**
593
+ * @public
594
+ */
595
+ export declare type FastifyReply = FastifyReply_2;
596
+
597
+ /**
598
+ * @public
599
+ */
600
+ export declare type FastifyReplyRaw = FastifyReply_2['raw'];
601
+
602
+ /**
603
+ * @public
604
+ */
605
+ export declare type FastifyRequest = FastifyRequest_2;
606
+
607
+ /**
608
+ * @public
609
+ */
610
+ export declare type FastifyRequestRaw = FastifyRequest_2['raw'];
611
+
612
+ /**
613
+ *
614
+ * @public
615
+ *
616
+ * `A5 Log Module` Global Provide Token
617
+ *
618
+ */
619
+ export declare const GLOBAL_PROVIDE_TOKEN_A5_CONFIG: A5GlobalProvideToken;
620
+
621
+ /**
622
+ *
623
+ * @public
624
+ *
625
+ * `A5 Log Module` Global Provide Token
626
+ *
627
+ */
628
+ export declare const GLOBAL_PROVIDE_TOKEN_A5_LOG_LOGGER: A5GlobalProvideToken;
629
+
630
+ /**
631
+ * @public
632
+ */
633
+ export declare const HOME_STATIC_PATH: string;
634
+
635
+ /**
636
+ *
637
+ * @public
638
+ *
639
+ * `A5 Config Module` 抽象类。
640
+ *
641
+ */
642
+ export declare abstract class IA5ConfigModule {
643
+ }
644
+
645
+ /**
646
+ *
647
+ * @public
648
+ *
649
+ * `A5 Log Module` 抽象类。
650
+ *
651
+ */
652
+ export declare abstract class IA5LogModule {
653
+ }
654
+
655
+ /**
656
+ * 类型断言工具 - 判断是否为 any 类型
657
+ *
658
+ * @public
659
+ */
660
+ export declare type IsAny<T> = 0 extends 1 & T ? true : false;
661
+
662
+ /**
663
+ * 类型断言工具 - 判断是否为 false
664
+ *
665
+ * @public
666
+ */
667
+ export declare type IsFalse<T extends false> = T;
668
+
669
+ /**
670
+ * 类型断言工具 - 判断是否为 true
671
+ *
672
+ * @public
673
+ */
674
+ export declare type IsTrue<T extends true> = T;
675
+
676
+ /**
677
+ * JSON 数组类型
678
+ *
679
+ * 表示 JSON 数组结构,元素为任意 JSON 值
680
+ *
681
+ * @public
682
+ */
683
+ export declare interface JsonArray extends Array<JsonValue> {
684
+ }
685
+
686
+ /**
687
+ * JSON 对象类型
688
+ *
689
+ * 表示 JSON 对象结构,键为字符串,值为任意 JSON 值
690
+ *
691
+ * @public
692
+ */
693
+ export declare interface JsonObject {
694
+ [key: string]: JsonValue;
695
+ }
696
+
697
+ /**
698
+ * JSON 值类型
699
+ *
700
+ * 表示所有合法的 JSON 值类型,包括基础类型、对象和数组
701
+ *
702
+ * @public
703
+ */
704
+ export declare type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
705
+
706
+ /**
707
+ * 包加载工具类
708
+ * 用于统一处理可选依赖的动态导入
709
+ * @public
710
+ */
711
+ export declare class LoadPackageUtil {
712
+ /**
713
+ * 动态加载包
714
+ * @param packageName - 包名
715
+ * @returns 加载的包模块
716
+ * @throws 当包加载失败时抛出错误
717
+ */
718
+ static loadPackage<T = unknown>(packageName: string): T;
719
+ /**
720
+ * 安全地动态加载包
721
+ * @param packageName - 包名
722
+ * @returns 加载的包模块,如果失败则返回 null
723
+ */
724
+ static loadPackageSafe<T = unknown>(packageName: string): T | null;
725
+ /**
726
+ * 带重试的动态加载包
727
+ * @param packageName - 包名
728
+ * @param retries - 重试次数,默认为 3
729
+ * @param retryDelay - 重试间隔(毫秒),默认为 100
730
+ * @returns 加载的包模块
731
+ * @throws 当所有重试都失败时抛出错误
732
+ */
733
+ static loadPackageWithRetry<T = unknown>(packageName: string, retries?: number, retryDelay?: number): Promise<T>;
734
+ }
735
+
736
+ /**
737
+ * 因 `logo` 字符串特殊字符过多,不应直接存放当前文件。
738
+ *
739
+ * 进行 base64 编码并存放。
740
+ */
741
+ /**
742
+ *
743
+ * @public
744
+ *
745
+ * 打印 Logo。
746
+ *
747
+ * logo 是在 http://patorjk.com/software/taag/#p=display&f=Big&t=a5 进行制作的。
748
+ *
749
+ */
750
+ export declare class LogoUtil {
751
+ private static _content;
752
+ /**
753
+ * Logo 的文本信息。
754
+ */
755
+ static get content(): string;
756
+ /**
757
+ *
758
+ * 打印 A5 的 Logo 及版本信息。
759
+ *
760
+ * 使用 `console.log` 输出信息,不会写入到日志文件中。
761
+ *
762
+ * @param onlyLogo - 仅打印 Logo,不包含版本信息。可选。默认为 false
763
+ *
764
+ */
765
+ static print(onlyLogo?: boolean): void;
766
+ }
767
+
768
+ /**
769
+ * @public
770
+ */
771
+ export declare const MAIN_STATIC_PATH: string;
772
+
773
+ /**
774
+ * 类型合并工具 - 合并嵌套类型
775
+ *
776
+ * @public
777
+ */
778
+ export declare type MergeInsertions<T> = T extends object ? {
779
+ [K in keyof T]: MergeInsertions<T[K]>;
780
+ } : T;
781
+
782
+ /**
783
+ *
784
+ * @public
785
+ *
786
+ * `A5 Config Module` Config Path
787
+ *
788
+ */
789
+ export declare const MODULE_CONFIG_PATH_A5_CONFIG: <T extends string>(str: T) => `A5.config.${T}`;
790
+
791
+ /**
792
+ *
793
+ * @public
794
+ *
795
+ * `A5 Log Module` Config Path
796
+ *
797
+ */
798
+ export declare const MODULE_CONFIG_PATH_A5_LOG: <T extends string>(str: T) => `A5.log.${T}`;
799
+
800
+ /**
801
+ * @public
802
+ */
803
+ export declare type MSStringValue = ms.StringValue;
804
+
805
+ /**
806
+ * @public
807
+ */
808
+ export declare class MSUtil {
809
+ /**
810
+ * 将 ms 时间字符串转换为毫秒数
811
+ *
812
+ * @param value - ms 时间字符串
813
+ * @returns 毫秒数
814
+ *
815
+ * @example
816
+ * ```typescript
817
+ * MSUtil.ms('123') // 123
818
+ * MSUtil.ms('2h') // 7200000
819
+ * MSUtil.ms('30s') // 30000
820
+ * MSUtil.ms('123 ms') // 123
821
+ * ```
822
+ */
823
+ static ms(value: ms.StringValue): number;
824
+ /**
825
+ * 判断字符串是否是有效的 ms 时间字符串格式
826
+ * 可以作为类型守卫使用,在运行时验证并断言为 ms.StringValue 类型
827
+ *
828
+ * @param value - 要检查的字符串值
829
+ * @returns 如果是有效的 ms 字符串返回 true,否则返回 false
830
+ *
831
+ * @example
832
+ * ```typescript
833
+ * // 有效格式
834
+ * MSUtil.isMsStringValue('123') // true
835
+ * MSUtil.isMsStringValue('2h') // true
836
+ * MSUtil.isMsStringValue('30s') // true
837
+ * MSUtil.isMsStringValue('123 ms') // true
838
+ *
839
+ * // 无效格式
840
+ * MSUtil.isMsStringValue('abc') // false
841
+ * MSUtil.isMsStringValue('') // false
842
+ * MSUtil.isMsStringValue('123xx') // false
843
+ * ```
844
+ */
845
+ static isMsStringValue(value: string): value is ms.StringValue;
846
+ }
847
+
848
+ /**
849
+ * 类型断言工具 - 判断不是 any 类型
850
+ *
851
+ * @public
852
+ */
853
+ export declare type NotAny<T> = true extends IsAny<T> ? false : true;
854
+
855
+ /**
856
+ * 类型断言工具 - 判断两个类型不相等
857
+ *
858
+ * @public
859
+ */
860
+ export declare type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
861
+
862
+ /**
863
+ * @public
864
+ */
865
+ export declare type RequiredButOmit<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;
866
+
867
+ /**
868
+ * @public
869
+ */
870
+ export declare type RequiredButPick<T, K extends keyof T> = Required<Omit<T, K>> & Pick<T, K>;
871
+
872
+ /**
873
+ * @public
874
+ *
875
+ * 当前运行环境。
876
+ *
877
+ */
878
+ export declare class RunEnvUtil {
879
+ /**
880
+ * 是否处于开发环境.
881
+ */
882
+ static get isDev(): boolean;
883
+ /**
884
+ * 是否处于生产环境.
885
+ */
886
+ static get isProd(): boolean;
887
+ /**
888
+ * 是否处于 PKG 打包后的环境。
889
+ */
890
+ static get inPKG(): boolean;
891
+ /**
892
+ * 是否处于 `VS Code` 的 `Debugger` 模式下。
893
+ */
894
+ static get inVSCodeDebugger(): boolean;
895
+ /**
896
+ * 是否处于 `Jest` 的单元测试环境下。
897
+ */
898
+ static get inJest(): boolean;
899
+ }
900
+
901
+ /**
902
+ * 类型转换工具 - 联合类型转交叉类型
903
+ *
904
+ * @public
905
+ */
906
+ export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
907
+
908
+ /**
909
+ * @public
910
+ */
911
+ export declare type Writable<T> = {
912
+ -readonly [P in keyof T]: T[P];
913
+ };
914
+
915
+ /**
916
+ * @public
917
+ */
918
+ export declare type WritableRequired<T> = Writable<Required<T>>;
919
+
920
+ export { }
@@ -2,5 +2,6 @@ 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 './ms.util';
5
6
  export * from './random.util';
6
7
  export * from './run-env.util';
@@ -18,6 +18,7 @@ __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("./ms.util"), exports);
21
22
  __exportStar(require("./random.util"), exports);
22
23
  __exportStar(require("./run-env.util"), exports);
23
24
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,47 @@
1
+ import ms from 'ms';
2
+ /**
3
+ * @public
4
+ */
5
+ export type MSStringValue = ms.StringValue;
6
+ /**
7
+ * @public
8
+ */
9
+ export declare class MSUtil {
10
+ /**
11
+ * 将 ms 时间字符串转换为毫秒数
12
+ *
13
+ * @param value - ms 时间字符串
14
+ * @returns 毫秒数
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * MSUtil.ms('123') // 123
19
+ * MSUtil.ms('2h') // 7200000
20
+ * MSUtil.ms('30s') // 30000
21
+ * MSUtil.ms('123 ms') // 123
22
+ * ```
23
+ */
24
+ static ms(value: ms.StringValue): number;
25
+ /**
26
+ * 判断字符串是否是有效的 ms 时间字符串格式
27
+ * 可以作为类型守卫使用,在运行时验证并断言为 ms.StringValue 类型
28
+ *
29
+ * @param value - 要检查的字符串值
30
+ * @returns 如果是有效的 ms 字符串返回 true,否则返回 false
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * // 有效格式
35
+ * MSUtil.isMsStringValue('123') // true
36
+ * MSUtil.isMsStringValue('2h') // true
37
+ * MSUtil.isMsStringValue('30s') // true
38
+ * MSUtil.isMsStringValue('123 ms') // true
39
+ *
40
+ * // 无效格式
41
+ * MSUtil.isMsStringValue('abc') // false
42
+ * MSUtil.isMsStringValue('') // false
43
+ * MSUtil.isMsStringValue('123xx') // false
44
+ * ```
45
+ */
46
+ static isMsStringValue(value: string): value is ms.StringValue;
47
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.MSUtil = void 0;
7
+ const ms_1 = __importDefault(require("ms"));
8
+ /**
9
+ * @public
10
+ */
11
+ class MSUtil {
12
+ /**
13
+ * 将 ms 时间字符串转换为毫秒数
14
+ *
15
+ * @param value - ms 时间字符串
16
+ * @returns 毫秒数
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * MSUtil.ms('123') // 123
21
+ * MSUtil.ms('2h') // 7200000
22
+ * MSUtil.ms('30s') // 30000
23
+ * MSUtil.ms('123 ms') // 123
24
+ * ```
25
+ */
26
+ static ms(value) {
27
+ return (0, ms_1.default)(value);
28
+ }
29
+ /**
30
+ * 判断字符串是否是有效的 ms 时间字符串格式
31
+ * 可以作为类型守卫使用,在运行时验证并断言为 ms.StringValue 类型
32
+ *
33
+ * @param value - 要检查的字符串值
34
+ * @returns 如果是有效的 ms 字符串返回 true,否则返回 false
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * // 有效格式
39
+ * MSUtil.isMsStringValue('123') // true
40
+ * MSUtil.isMsStringValue('2h') // true
41
+ * MSUtil.isMsStringValue('30s') // true
42
+ * MSUtil.isMsStringValue('123 ms') // true
43
+ *
44
+ * // 无效格式
45
+ * MSUtil.isMsStringValue('abc') // false
46
+ * MSUtil.isMsStringValue('') // false
47
+ * MSUtil.isMsStringValue('123xx') // false
48
+ * ```
49
+ */
50
+ static isMsStringValue(value) {
51
+ if (typeof value !== 'string' || value.length === 0) {
52
+ return false;
53
+ }
54
+ try {
55
+ // 尝试转换,如果 ms() 函数能成功解析则说明格式正确
56
+ // 使用类型断言因为我们正在验证这个值
57
+ const result = (0, ms_1.default)(value);
58
+ return typeof result === 'number' && Number.isNaN(result) === false;
59
+ }
60
+ catch (error) {
61
+ return false;
62
+ }
63
+ }
64
+ }
65
+ exports.MSUtil = MSUtil;
66
+ //# sourceMappingURL=ms.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.52",
3
+ "version": "0.2.0-alpha.54",
4
4
  "description": "The core library for the `@hz-9/a5-*` series of repositories.",
5
5
  "keywords": [
6
6
  "nest",
@@ -53,7 +53,9 @@
53
53
  "type-fest": "~4.20.0",
54
54
  "upath": "~2.0.1",
55
55
  "ua-parser-js": "~1.0.38",
56
- "uuid": "^10.0.0"
56
+ "uuid": "^10.0.0",
57
+ "ms": "~2.1.3",
58
+ "@types/ms": "~2.1.0"
57
59
  },
58
60
  "devDependencies": {
59
61
  "@hz-9/eslint-config-airbnb-ts": "~0.6.0",