kb-server 0.0.1-beta.17 → 0.0.1-beta.19

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.
@@ -1,3 +1,4 @@
1
+ import { ValidationError } from "class-validator";
1
2
  import { Class } from "utility-types";
2
3
  export interface ServerContext<A = any> {
3
4
  /** 被调用方的 RequestId */
@@ -16,4 +17,8 @@ export type APIs = Record<string, AnyAPIExecution>;
16
17
  export declare function createAPI<P, R, A>(ParamsClass: Class<P>, execution: APIExecution<P, R, A>): API<P, R>;
17
18
  type StubParam<T> = T extends (params: infer P) => any ? P : never;
18
19
  export declare function implementAPI<F extends (params: any) => any, A = Record<string, any>>(_actionStub: F, ParamsClass: Class<StubParam<F>>, execution: APIExecution<StubParam<F>, ReturnType<F>, A>): API<StubParam<F>, ReturnType<F>>;
20
+ /**
21
+ * 解析错误信息
22
+ */
23
+ export declare function getMessage(message: ValidationError, propertyPath?: string): string[];
19
24
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.implementAPI = exports.createAPI = void 0;
3
+ exports.getMessage = exports.implementAPI = exports.createAPI = void 0;
4
4
  const class_transformer_1 = require("class-transformer");
5
5
  const class_validator_1 = require("class-validator");
6
6
  const create_errors_1 = require("./create-errors");
@@ -15,10 +15,8 @@ function createAPI(ParamsClass, execution) {
15
15
  : (0, class_transformer_1.plainToClass)(ParamsClass, params);
16
16
  const errors = await (0, class_validator_1.validate)(paramsToCheck || {});
17
17
  if (errors?.length) {
18
- const messages = errors.map((x, index) => {
19
- return `\n${index}.<${x.property}>:${Object.values(x.constraints || { unknown: "reason unknown" }).join(", ")}`;
20
- });
21
- throw new create_errors_1.CommonErrors.InvalidParameter.ValidationError(messages.join(" "));
18
+ const errorMessages = errors.flatMap((error) => getMessage(error));
19
+ throw new create_errors_1.CommonErrors.InvalidParameter.ValidationError(errorMessages.join("\n"));
22
20
  }
23
21
  // 执行函数
24
22
  return await execution(params, ctx);
@@ -30,3 +28,26 @@ function implementAPI(_actionStub, ParamsClass, execution) {
30
28
  return createAPI(ParamsClass, execution);
31
29
  }
32
30
  exports.implementAPI = implementAPI;
31
+ /**
32
+ * 解析错误信息
33
+ */
34
+ function getMessage(message, propertyPath = "") {
35
+ const messages = [];
36
+ // 构建当前属性的完整路径
37
+ const currentPropertyPath = propertyPath
38
+ ? `${propertyPath}.${message.property}`
39
+ : message.property;
40
+ if (message.constraints) {
41
+ // 将所有约束错误合并为一个字符串
42
+ const constraintErrors = Object.values(message.constraints).join(", ");
43
+ messages.push(`${currentPropertyPath}: ${constraintErrors}`);
44
+ }
45
+ if (message.children && message.children.length > 0) {
46
+ // 递归处理子错误,并传递更新后的属性路径
47
+ for (const [_index, child] of message.children.entries()) {
48
+ messages.push(...getMessage(child, currentPropertyPath));
49
+ }
50
+ }
51
+ return messages;
52
+ }
53
+ exports.getMessage = getMessage;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { createServer } from "./common/create-server";
2
2
  export { createErrors } from "./common/create-errors";
3
- export { implementAPI } from "./common/create-api";
3
+ export { implementAPI, ServerContext } from "./common/create-api";
4
4
  export { callService } from "./common/call-service";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kb-server",
3
- "version": "0.0.1-beta.17",
3
+ "version": "0.0.1-beta.19",
4
4
  "description": "A fast server for Node.JS,made by express.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {