@kevisual/router 0.0.35 → 0.0.36

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.
@@ -48,7 +48,7 @@ declare class QueryChain {
48
48
  * @param queryData
49
49
  * @returns
50
50
  */
51
- getKey(queryData?: SimpleObject): Pick<RouteOpts, 'path' | 'key' | 'metadata' | 'description' | 'validator'>;
51
+ getKey(queryData?: SimpleObject): Pick<RouteOpts, 'path' | 'key' | 'metadata' | 'description'>;
52
52
  post<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
53
53
  get<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
54
54
  }
package/dist/router.d.ts CHANGED
@@ -1,50 +1,13 @@
1
- import { z } from 'zod';
2
1
  import http, { IncomingMessage, ServerResponse } from 'node:http';
3
2
  import https from 'node:https';
4
3
  import http2 from 'node:http2';
5
4
  import * as cookie from 'cookie';
5
+ import { z } from 'zod';
6
6
  import { WebSocketServer, WebSocket } from 'ws';
7
7
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
8
8
  import { RouteOpts as RouteOpts$1, QueryRouterServer as QueryRouterServer$1, RouteMiddleware as RouteMiddleware$1, Run as Run$1 } from '@kevisual/router';
9
9
  import { Query, DataOpts, Result } from '@kevisual/query/query';
10
10
 
11
- type BaseRule = {
12
- value?: any;
13
- required?: boolean;
14
- message?: string;
15
- };
16
- type RuleString = {
17
- type: 'string';
18
- min?: number;
19
- max?: number;
20
- regex?: string;
21
- } & BaseRule;
22
- type RuleNumber = {
23
- type: 'number';
24
- min?: number;
25
- max?: number;
26
- } & BaseRule;
27
- type RuleBoolean = {
28
- type: 'boolean';
29
- } & BaseRule;
30
- type RuleArray = {
31
- type: 'array';
32
- items: Rule;
33
- } & BaseRule;
34
- type RuleObject = {
35
- type: 'object';
36
- properties: {
37
- [key: string]: Rule;
38
- };
39
- } & BaseRule;
40
- type RuleAny = {
41
- type: 'any';
42
- } & BaseRule;
43
- type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
44
- declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
45
-
46
- type Schema = z.ZodType<any, any, any>;
47
-
48
11
  type RouterContextT = {
49
12
  code?: number;
50
13
  [key: string]: any;
@@ -57,6 +20,11 @@ type RouteContext<T = {
57
20
  };
58
21
  /** return body */
59
22
  body?: number | string | Object;
23
+ forward?: (response: {
24
+ code: number;
25
+ data?: any;
26
+ message?: any;
27
+ }) => void;
60
28
  /** return code */
61
29
  code?: number;
62
30
  /** return msg */
@@ -82,12 +50,9 @@ type RouteContext<T = {
82
50
  [key: string]: any;
83
51
  };
84
52
  end?: boolean;
85
- /**
86
- * 请求 route的返回结果,包函ctx
87
- */
88
- queryRouter?: QueryRouter;
53
+ app?: QueryRouter;
89
54
  error?: any;
90
- /** 请求 route的返回结果,包函ctx */
55
+ /** 请求 route的返回结果,不解析body为data */
91
56
  call?: (message: {
92
57
  path: string;
93
58
  key?: string;
@@ -100,8 +65,8 @@ type RouteContext<T = {
100
65
  }, ctx?: RouteContext & {
101
66
  [key: string]: any;
102
67
  }) => Promise<any>;
103
- /** 请求 route的返回结果,不包函ctx */
104
- queryRoute?: (message: {
68
+ /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
69
+ run?: (message: {
105
70
  path: string;
106
71
  key?: string;
107
72
  payload?: any;
@@ -121,11 +86,11 @@ type RouteMiddleware = {
121
86
  key?: string;
122
87
  id?: string;
123
88
  } | string;
124
- type RouteOpts = {
89
+ type RouteOpts<T = {}> = {
125
90
  path?: string;
126
91
  key?: string;
127
92
  id?: string;
128
- run?: Run;
93
+ run?: Run<T>;
129
94
  nextRoute?: NextRoute;
130
95
  description?: string;
131
96
  metadata?: {
@@ -133,23 +98,6 @@ type RouteOpts = {
133
98
  };
134
99
  middleware?: RouteMiddleware[];
135
100
  type?: 'route' | 'middleware';
136
- /**
137
- * validator: {
138
- * packageName: {
139
- * type: 'string',
140
- * required: true,
141
- * },
142
- * }
143
- */
144
- validator?: {
145
- [key: string]: Rule;
146
- };
147
- schema?: {
148
- [key: string]: any;
149
- };
150
- isVerify?: boolean;
151
- verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
152
- verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
153
101
  /**
154
102
  * $#$ will be used to split path and key
155
103
  */
@@ -160,8 +108,8 @@ type RouteOpts = {
160
108
  delimiter?: string;
161
109
  isDebug?: boolean;
162
110
  };
163
- type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
164
- declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware", "metadata"];
111
+ type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
112
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
165
113
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
166
114
  declare class Route<U = {
167
115
  [key: string]: any;
@@ -183,59 +131,12 @@ declare class Route<U = {
183
131
  };
184
132
  middleware?: RouteMiddleware[];
185
133
  type?: string;
186
- private _validator?;
187
- schema?: {
188
- [key: string]: any;
189
- };
190
134
  data?: any;
191
- /**
192
- * 是否需要验证
193
- */
194
- isVerify?: boolean;
195
135
  /**
196
136
  * 是否开启debug,开启后会打印错误信息
197
137
  */
198
138
  isDebug?: boolean;
199
139
  constructor(path?: string, key?: string, opts?: RouteOpts);
200
- private createSchema;
201
- /**
202
- * set validator and create schema
203
- * @param validator
204
- */
205
- set validator(validator: {
206
- [key: string]: Rule;
207
- });
208
- get validator(): {
209
- [key: string]: Rule;
210
- };
211
- /**
212
- * has code, body, message in ctx, return ctx if has error
213
- * @param ctx
214
- * @param dev
215
- * @returns
216
- */
217
- verify(ctx: RouteContext, dev?: boolean): void;
218
- /**
219
- * Need to manully call return ctx fn and configure body, code, message
220
- * @param key
221
- * @param ctx
222
- * @param dev
223
- * @returns
224
- */
225
- verifyKey(key: string, ctx: RouteContext, dev?: boolean): {
226
- message: string;
227
- path: string;
228
- key: string;
229
- error: any;
230
- } | {
231
- message: string;
232
- path: string;
233
- key: string;
234
- error?: undefined;
235
- };
236
- setValidator(validator: {
237
- [key: string]: Rule;
238
- }): this;
239
140
  prompt(description: string): this;
240
141
  prompt(description: Function): this;
241
142
  define<T extends {
@@ -553,7 +454,7 @@ declare class Server {
553
454
  * @description 主要是为了兼容其他的监听
554
455
  * @param listener
555
456
  */
556
- on(listener: Listener | Listener[]): void;
457
+ on(listener: Listener | Listener[]): () => void;
557
458
  get callback(): any;
558
459
  get server(): http.Server<typeof IncomingMessage, typeof ServerResponse> | https.Server<typeof IncomingMessage, typeof ServerResponse> | http2.Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof http2.Http2ServerRequest, typeof http2.Http2ServerResponse>;
559
460
  }
@@ -602,6 +503,43 @@ declare class CustomError extends Error {
602
503
  };
603
504
  }
604
505
 
506
+ type BaseRule = {
507
+ value?: any;
508
+ required?: boolean;
509
+ message?: string;
510
+ };
511
+ type RuleString = {
512
+ type: 'string';
513
+ min?: number;
514
+ max?: number;
515
+ regex?: string;
516
+ } & BaseRule;
517
+ type RuleNumber = {
518
+ type: 'number';
519
+ min?: number;
520
+ max?: number;
521
+ } & BaseRule;
522
+ type RuleBoolean = {
523
+ type: 'boolean';
524
+ } & BaseRule;
525
+ type RuleArray = {
526
+ type: 'array';
527
+ items: Rule;
528
+ } & BaseRule;
529
+ type RuleObject = {
530
+ type: 'object';
531
+ properties: {
532
+ [key: string]: Rule;
533
+ };
534
+ } & BaseRule;
535
+ type RuleAny = {
536
+ type: 'any';
537
+ } & BaseRule;
538
+ type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
539
+ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
540
+
541
+ type Schema = z.ZodType<any, any, any>;
542
+
605
543
  type WsServerBaseOpts = {
606
544
  wss?: WebSocketServer;
607
545
  path?: string;
@@ -678,7 +616,7 @@ declare class QueryChain {
678
616
  * @param queryData
679
617
  * @returns
680
618
  */
681
- getKey(queryData?: SimpleObject): Pick<RouteOpts$1, 'path' | 'key' | 'metadata' | 'description' | 'validator'>;
619
+ getKey(queryData?: SimpleObject): Pick<RouteOpts$1, 'path' | 'key' | 'metadata' | 'description'>;
682
620
  post<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
683
621
  get<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
684
622
  }
@@ -723,15 +661,18 @@ type AppOptions<T = {}> = {
723
661
  path?: string;
724
662
  };
725
663
  };
726
- type AppReqRes = HandleCtx;
664
+ type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
665
+ app: App<T>;
666
+ };
727
667
  /**
728
668
  * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
669
+ * U - Route Context的扩展类型
729
670
  */
730
- declare class App<T = {}, U = AppReqRes> {
671
+ declare class App<U = {}> {
731
672
  router: QueryRouter;
732
673
  server: Server;
733
674
  io: WsServer;
734
- constructor(opts?: AppOptions<T>);
675
+ constructor(opts?: AppOptions<U>);
735
676
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
736
677
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
737
678
  listen(port: number, backlog?: number, listeningListener?: () => void): void;
@@ -744,21 +685,31 @@ declare class App<T = {}, U = AppReqRes> {
744
685
  addRoute(route: Route): void;
745
686
  add: (route: Route) => void;
746
687
  Route: typeof Route;
747
- route(opts: RouteOpts): Route<U>;
748
- route(path: string, key?: string): Route<U>;
749
- route(path: string, opts?: RouteOpts): Route<U>;
750
- route(path: string, key?: string, opts?: RouteOpts): Route<U>;
751
- prompt(description: string): Route<Required<RouteContext>>;
752
- prompt(description: Function): Route<Required<RouteContext>>;
688
+ route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
689
+ route(path: string, key?: string): Route<AppRouteContext<U>>;
690
+ route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
691
+ route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
692
+ prompt(description: string): Route<AppRouteContext<U>>;
693
+ prompt(description: Function): Route<AppRouteContext<U>>;
753
694
  call(message: {
754
695
  id?: string;
755
696
  path?: string;
756
697
  key?: string;
757
698
  payload?: any;
758
- }, ctx?: RouteContext & {
699
+ }, ctx?: AppRouteContext<U> & {
759
700
  [key: string]: any;
760
701
  }): Promise<any>;
761
- queryRoute(path: string, key?: string, payload?: any, ctx?: RouteContext & {
702
+ /**
703
+ * @deprecated
704
+ */
705
+ queryRoute(path: string, key?: string, payload?: any, ctx?: AppRouteContext<U> & {
706
+ [key: string]: any;
707
+ }): Promise<{
708
+ code: any;
709
+ data: any;
710
+ message: any;
711
+ }>;
712
+ run(path: string, key?: string, payload?: any, ctx?: AppRouteContext<U> & {
762
713
  [key: string]: any;
763
714
  }): Promise<{
764
715
  code: any;