@hz-9/a5-core 0.2.0-alpha.41 → 0.2.0-alpha.43

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
@@ -454,6 +458,15 @@ export declare class A5Util {
454
458
  * @returns 资源路径的绝对路径。
455
459
  */
456
460
  static tryWithAbsolutePath(sourcePath: string, basePath: string): string;
461
+ static nanoid(size?: number): string;
462
+ static uuidV4(options?: V4Options): string;
463
+ static uuidV7(options?: V7Options): string;
464
+ static getReqIdFromRequest(req: FastifyRequest): string;
465
+ static getReqHeadersFromRequest(req: FastifyRequest): IncomingHttpHeaders;
466
+ static getReqIdAndHeadersFromRequest(req: FastifyRequest): {
467
+ reqId: string;
468
+ reqHeaders: IncomingHttpHeaders;
469
+ };
457
470
  }
458
471
 
459
472
  /**
@@ -759,8 +772,6 @@ export declare const MODULE_CONFIG_PATH_A5_CONFIG: <T extends string>(str: T) =>
759
772
  */
760
773
  export declare const MODULE_CONFIG_PATH_A5_LOG: <T extends string>(str: T) => `A5.log.${T}`;
761
774
 
762
- export { nanoid }
763
-
764
775
  /**
765
776
  * 类型断言工具 - 判断不是 any 类型
766
777
  *
@@ -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.A5Util.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.A5Util.uuidV7(),
41
+ }), nestOptions);
39
42
  // this.initBodyParser(nestApplication, options)
40
43
  const app = new a5_application_1.A5Application(nestApplication);
41
44
  await app.init();
package/dist/index.d.ts CHANGED
@@ -4,5 +4,4 @@ export * from './errors';
4
4
  export * from './interface';
5
5
  export * from './module';
6
6
  export * from './middleware';
7
- export * from './plugins';
8
7
  export * from './util';
package/dist/index.js CHANGED
@@ -20,6 +20,5 @@ __exportStar(require("./errors"), exports);
20
20
  __exportStar(require("./interface"), exports);
21
21
  __exportStar(require("./module"), exports);
22
22
  __exportStar(require("./middleware"), exports);
23
- __exportStar(require("./plugins"), exports);
24
23
  __exportStar(require("./util"), exports);
25
24
  //# sourceMappingURL=index.js.map
@@ -1,3 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { IncomingHttpHeaders } from 'node:http';
3
+ import { V4Options, V7Options } from 'uuid';
4
+ import { FastifyRequest } from '../interface/http';
1
5
  /**
2
6
  *
3
7
  * @public
@@ -24,4 +28,13 @@ export declare class A5Util {
24
28
  * @returns 资源路径的绝对路径。
25
29
  */
26
30
  static tryWithAbsolutePath(sourcePath: string, basePath: string): string;
31
+ static nanoid(size?: number): string;
32
+ static uuidV4(options?: V4Options): string;
33
+ static uuidV7(options?: V7Options): string;
34
+ static getReqIdFromRequest(req: FastifyRequest): string;
35
+ static getReqHeadersFromRequest(req: FastifyRequest): IncomingHttpHeaders;
36
+ static getReqIdAndHeadersFromRequest(req: FastifyRequest): {
37
+ reqId: string;
38
+ reqHeaders: IncomingHttpHeaders;
39
+ };
27
40
  }
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.A5Util = void 0;
4
+ const nanoid_1 = require("nanoid");
4
5
  const upath_1 = require("upath");
6
+ const uuid_1 = require("uuid");
5
7
  /**
6
8
  *
7
9
  * @public
@@ -36,6 +38,28 @@ class A5Util {
36
38
  ? (0, upath_1.normalize)(sourcePath)
37
39
  : (0, upath_1.resolve)((0, upath_1.normalize)(basePath), (0, upath_1.normalize)(sourcePath));
38
40
  }
41
+ static nanoid(size) {
42
+ return (0, nanoid_1.nanoid)(size);
43
+ }
44
+ static uuidV4(options) {
45
+ return (0, uuid_1.v4)(options);
46
+ }
47
+ static uuidV7(options) {
48
+ return (0, uuid_1.v7)(options);
49
+ }
50
+ static getReqIdFromRequest(req) {
51
+ return req.id;
52
+ }
53
+ static getReqHeadersFromRequest(req) {
54
+ // TODO 支持 HTTP2
55
+ return req.headers;
56
+ }
57
+ static getReqIdAndHeadersFromRequest(req) {
58
+ return {
59
+ reqId: req.id,
60
+ reqHeaders: req.headers,
61
+ };
62
+ }
39
63
  }
40
64
  exports.A5Util = A5Util;
41
65
  //# sourceMappingURL=a5.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.41",
3
+ "version": "0.2.0-alpha.43",
4
4
  "description": "The core library for the `@hz-9/a5-*` series of repositories.",
5
5
  "keywords": [
6
6
  "nest",
@@ -38,23 +38,24 @@
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
43
  "@types/lodash": "~4.17.4",
45
44
  "@types/node": "~20.3.1",
46
45
  "@types/pug": "~2.0.10",
47
- "@types/uuid": "~9.0.8",
46
+ "@types/uuid": "^10.0.0",
48
47
  "axios": "~1.7.2",
49
48
  "body-parser": "~1.20.2",
50
49
  "dayjs": "~1.11.10",
50
+ "fastify": "4.28.1",
51
51
  "fs-extra": "~11.1.1",
52
52
  "lodash": "~4.17.21",
53
53
  "nanoid": "^3.3.7",
54
54
  "pug": "~3.0.3",
55
55
  "type-fest": "~4.20.0",
56
56
  "upath": "~2.0.1",
57
- "ua-parser-js": "~1.0.38"
57
+ "ua-parser-js": "~1.0.38",
58
+ "uuid": "^10.0.0"
58
59
  },
59
60
  "devDependencies": {
60
61
  "@hz-9/eslint-config-airbnb-ts": "~0.6.0",
@@ -1 +0,0 @@
1
- export * from './nanoid';
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./nanoid"), exports);
18
- //# sourceMappingURL=index.js.map
@@ -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