@kevisual/router 0.2.10 → 0.2.12

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.
@@ -30,6 +30,57 @@ declare class MockProcess {
30
30
  desctroy(): void;
31
31
  }
32
32
 
33
+ /** JSON Schema 基本类型映射到 TypeScript 类型 */
34
+ type JsonSchemaTypeToTS<T> = T extends {
35
+ type: "string";
36
+ } ? string : T extends {
37
+ type: "boolean";
38
+ } ? boolean : T extends {
39
+ type: "number";
40
+ } ? number : T extends {
41
+ type: "integer";
42
+ } ? number : T extends {
43
+ type: "object";
44
+ } ? object : T extends {
45
+ type: "array";
46
+ } ? any[] : any;
47
+ /** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
48
+ type ArgsShapeToPayload<T> = {
49
+ [K in keyof T as T[K] extends {
50
+ optional: true;
51
+ } ? never : K]: JsonSchemaTypeToTS<T[K]>;
52
+ } & {
53
+ [K in keyof T as T[K] extends {
54
+ optional: true;
55
+ } ? K : never]?: JsonSchemaTypeToTS<T[K]>;
56
+ };
57
+ /** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
58
+ type ArgsToPayload<T> = T extends {
59
+ type: "object";
60
+ properties: infer P;
61
+ } ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
62
+ /** 从 API 定义中提取 metadata.args */
63
+ type ExtractArgs<T> = T extends {
64
+ metadata: {
65
+ args: infer A;
66
+ };
67
+ } ? A : {};
68
+ /** 从 API 定义中提取 metadata.returns */
69
+ type ExtractReturns<T> = T extends {
70
+ metadata: {
71
+ returns: infer R;
72
+ };
73
+ } ? R : unknown;
74
+ /** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
75
+ type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
76
+ /** runAction 的返回类型,根据 API 定义中的 metadata.returns 推断 data 字段类型 */
77
+ type RunActionReturns<T> = {
78
+ code: number | string;
79
+ data?: unknown extends ExtractReturns<T> ? any : ArgsToPayload<ExtractReturns<T>>;
80
+ message?: string;
81
+ [key: string]: any;
82
+ };
83
+
33
84
  type RouterContextT = {
34
85
  code?: number;
35
86
  [key: string]: any;
@@ -470,8 +521,9 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
470
521
  key?: string;
471
522
  metadata?: {
472
523
  args?: any;
524
+ returns?: any;
473
525
  };
474
- } = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<any>;
526
+ } = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<RunActionReturns<T>>;
475
527
  /**
476
528
  * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
477
529
  * @param fun 认证函数,接收 RouteContext 和认证类型
@@ -480,43 +532,6 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
480
532
  overwrite?: boolean;
481
533
  }): Promise<void>;
482
534
  }
483
- /** JSON Schema 基本类型映射到 TypeScript 类型 */
484
- type JsonSchemaTypeToTS<T> = T extends {
485
- type: "string";
486
- } ? string : T extends {
487
- type: "boolean";
488
- } ? boolean : T extends {
489
- type: "number";
490
- } ? number : T extends {
491
- type: "integer";
492
- } ? number : T extends {
493
- type: "object";
494
- } ? object : T extends {
495
- type: "array";
496
- } ? any[] : any;
497
- /** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
498
- type ArgsShapeToPayload<T> = {
499
- [K in keyof T as T[K] extends {
500
- optional: true;
501
- } ? never : K]: JsonSchemaTypeToTS<T[K]>;
502
- } & {
503
- [K in keyof T as T[K] extends {
504
- optional: true;
505
- } ? K : never]?: JsonSchemaTypeToTS<T[K]>;
506
- };
507
- /** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
508
- type ArgsToPayload<T> = T extends {
509
- type: "object";
510
- properties: infer P;
511
- } ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
512
- /** 从 API 定义中提取 metadata.args */
513
- type ExtractArgs<T> = T extends {
514
- metadata: {
515
- args: infer A;
516
- };
517
- } ? A : {};
518
- /** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
519
- type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
520
535
 
521
536
  type Cors = {
522
537
  /**