@kevisual/router 0.2.11 → 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.
@@ -77,6 +77,57 @@ type ListenProcessOptions = {
77
77
  };
78
78
  declare const listenProcess: ({ app, mockProcess, params, timeout }: ListenProcessOptions) => Promise<void>;
79
79
 
80
+ /** JSON Schema 基本类型映射到 TypeScript 类型 */
81
+ type JsonSchemaTypeToTS<T> = T extends {
82
+ type: "string";
83
+ } ? string : T extends {
84
+ type: "boolean";
85
+ } ? boolean : T extends {
86
+ type: "number";
87
+ } ? number : T extends {
88
+ type: "integer";
89
+ } ? number : T extends {
90
+ type: "object";
91
+ } ? object : T extends {
92
+ type: "array";
93
+ } ? any[] : any;
94
+ /** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
95
+ type ArgsShapeToPayload<T> = {
96
+ [K in keyof T as T[K] extends {
97
+ optional: true;
98
+ } ? never : K]: JsonSchemaTypeToTS<T[K]>;
99
+ } & {
100
+ [K in keyof T as T[K] extends {
101
+ optional: true;
102
+ } ? K : never]?: JsonSchemaTypeToTS<T[K]>;
103
+ };
104
+ /** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
105
+ type ArgsToPayload<T> = T extends {
106
+ type: "object";
107
+ properties: infer P;
108
+ } ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
109
+ /** 从 API 定义中提取 metadata.args */
110
+ type ExtractArgs<T> = T extends {
111
+ metadata: {
112
+ args: infer A;
113
+ };
114
+ } ? A : {};
115
+ /** 从 API 定义中提取 metadata.returns */
116
+ type ExtractReturns<T> = T extends {
117
+ metadata: {
118
+ returns: infer R;
119
+ };
120
+ } ? R : unknown;
121
+ /** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
122
+ type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
123
+ /** runAction 的返回类型,根据 API 定义中的 metadata.returns 推断 data 字段类型 */
124
+ type RunActionReturns<T> = {
125
+ code: number | string;
126
+ data?: unknown extends ExtractReturns<T> ? any : ArgsToPayload<ExtractReturns<T>>;
127
+ message?: string;
128
+ [key: string]: any;
129
+ };
130
+
80
131
  type RouterContextT = {
81
132
  code?: number;
82
133
  [key: string]: any;
@@ -548,8 +599,9 @@ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> exten
548
599
  key?: string;
549
600
  metadata?: {
550
601
  args?: any;
602
+ returns?: any;
551
603
  };
552
- } = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<any>;
604
+ } = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<RunActionReturns<T>>;
553
605
  /**
554
606
  * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
555
607
  * @param fun 认证函数,接收 RouteContext 和认证类型
@@ -560,43 +612,6 @@ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> exten
560
612
  }
561
613
  declare class Mini extends QueryRouterServer {
562
614
  }
563
- /** JSON Schema 基本类型映射到 TypeScript 类型 */
564
- type JsonSchemaTypeToTS<T> = T extends {
565
- type: "string";
566
- } ? string : T extends {
567
- type: "boolean";
568
- } ? boolean : T extends {
569
- type: "number";
570
- } ? number : T extends {
571
- type: "integer";
572
- } ? number : T extends {
573
- type: "object";
574
- } ? object : T extends {
575
- type: "array";
576
- } ? any[] : any;
577
- /** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
578
- type ArgsShapeToPayload<T> = {
579
- [K in keyof T as T[K] extends {
580
- optional: true;
581
- } ? never : K]: JsonSchemaTypeToTS<T[K]>;
582
- } & {
583
- [K in keyof T as T[K] extends {
584
- optional: true;
585
- } ? K : never]?: JsonSchemaTypeToTS<T[K]>;
586
- };
587
- /** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
588
- type ArgsToPayload<T> = T extends {
589
- type: "object";
590
- properties: infer P;
591
- } ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
592
- /** 从 API 定义中提取 metadata.args */
593
- type ExtractArgs<T> = T extends {
594
- metadata: {
595
- args: infer A;
596
- };
597
- } ? A : {};
598
- /** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
599
- type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
600
615
 
601
616
  type BaseRule = {
602
617
  value?: any;