@kevisual/router 0.0.35 → 0.0.37
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/router-browser.d.ts +54 -115
- package/dist/router-browser.js +3458 -4669
- package/dist/router-define.d.ts +1 -1
- package/dist/router.d.ts +85 -128
- package/dist/router.js +4506 -5699
- package/package.json +2 -2
- package/src/app.ts +20 -12
- package/src/route.ts +31 -181
- package/src/router-define.ts +3 -3
- package/src/server/server.ts +12 -2
- package/src/test/app-type.ts +13 -0
package/dist/router-define.d.ts
CHANGED
|
@@ -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'
|
|
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
|
|
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
|
|
104
|
-
|
|
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' | '
|
|
164
|
-
declare const pickValue: readonly ["path", "key", "id", "description", "type", "
|
|
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 {
|
|
@@ -444,8 +345,9 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
444
345
|
* @param param0
|
|
445
346
|
* @returns
|
|
446
347
|
*/
|
|
447
|
-
run(
|
|
448
|
-
|
|
348
|
+
run(msg: {
|
|
349
|
+
id?: string;
|
|
350
|
+
path?: string;
|
|
449
351
|
key?: string;
|
|
450
352
|
payload?: any;
|
|
451
353
|
}, ctx?: RouteContext & {
|
|
@@ -553,7 +455,7 @@ declare class Server {
|
|
|
553
455
|
* @description 主要是为了兼容其他的监听
|
|
554
456
|
* @param listener
|
|
555
457
|
*/
|
|
556
|
-
on(listener: Listener | Listener[]): void;
|
|
458
|
+
on(listener: Listener | Listener[]): () => void;
|
|
557
459
|
get callback(): any;
|
|
558
460
|
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
461
|
}
|
|
@@ -602,6 +504,43 @@ declare class CustomError extends Error {
|
|
|
602
504
|
};
|
|
603
505
|
}
|
|
604
506
|
|
|
507
|
+
type BaseRule = {
|
|
508
|
+
value?: any;
|
|
509
|
+
required?: boolean;
|
|
510
|
+
message?: string;
|
|
511
|
+
};
|
|
512
|
+
type RuleString = {
|
|
513
|
+
type: 'string';
|
|
514
|
+
min?: number;
|
|
515
|
+
max?: number;
|
|
516
|
+
regex?: string;
|
|
517
|
+
} & BaseRule;
|
|
518
|
+
type RuleNumber = {
|
|
519
|
+
type: 'number';
|
|
520
|
+
min?: number;
|
|
521
|
+
max?: number;
|
|
522
|
+
} & BaseRule;
|
|
523
|
+
type RuleBoolean = {
|
|
524
|
+
type: 'boolean';
|
|
525
|
+
} & BaseRule;
|
|
526
|
+
type RuleArray = {
|
|
527
|
+
type: 'array';
|
|
528
|
+
items: Rule;
|
|
529
|
+
} & BaseRule;
|
|
530
|
+
type RuleObject = {
|
|
531
|
+
type: 'object';
|
|
532
|
+
properties: {
|
|
533
|
+
[key: string]: Rule;
|
|
534
|
+
};
|
|
535
|
+
} & BaseRule;
|
|
536
|
+
type RuleAny = {
|
|
537
|
+
type: 'any';
|
|
538
|
+
} & BaseRule;
|
|
539
|
+
type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
|
|
540
|
+
declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
|
|
541
|
+
|
|
542
|
+
type Schema = z.ZodType<any, any, any>;
|
|
543
|
+
|
|
605
544
|
type WsServerBaseOpts = {
|
|
606
545
|
wss?: WebSocketServer;
|
|
607
546
|
path?: string;
|
|
@@ -678,7 +617,7 @@ declare class QueryChain {
|
|
|
678
617
|
* @param queryData
|
|
679
618
|
* @returns
|
|
680
619
|
*/
|
|
681
|
-
getKey(queryData?: SimpleObject): Pick<RouteOpts$1, 'path' | 'key' | 'metadata' | 'description'
|
|
620
|
+
getKey(queryData?: SimpleObject): Pick<RouteOpts$1, 'path' | 'key' | 'metadata' | 'description'>;
|
|
682
621
|
post<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
683
622
|
get<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
684
623
|
}
|
|
@@ -723,15 +662,18 @@ type AppOptions<T = {}> = {
|
|
|
723
662
|
path?: string;
|
|
724
663
|
};
|
|
725
664
|
};
|
|
726
|
-
type
|
|
665
|
+
type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
|
|
666
|
+
app: App<T>;
|
|
667
|
+
};
|
|
727
668
|
/**
|
|
728
669
|
* 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
|
|
670
|
+
* U - Route Context的扩展类型
|
|
729
671
|
*/
|
|
730
|
-
declare class App<
|
|
672
|
+
declare class App<U = {}> {
|
|
731
673
|
router: QueryRouter;
|
|
732
674
|
server: Server;
|
|
733
675
|
io: WsServer;
|
|
734
|
-
constructor(opts?: AppOptions<
|
|
676
|
+
constructor(opts?: AppOptions<U>);
|
|
735
677
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
736
678
|
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
737
679
|
listen(port: number, backlog?: number, listeningListener?: () => void): void;
|
|
@@ -744,21 +686,36 @@ declare class App<T = {}, U = AppReqRes> {
|
|
|
744
686
|
addRoute(route: Route): void;
|
|
745
687
|
add: (route: Route) => void;
|
|
746
688
|
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<
|
|
752
|
-
prompt(description: Function): Route<
|
|
689
|
+
route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
690
|
+
route(path: string, key?: string): Route<AppRouteContext<U>>;
|
|
691
|
+
route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
692
|
+
route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
693
|
+
prompt(description: string): Route<AppRouteContext<U>>;
|
|
694
|
+
prompt(description: Function): Route<AppRouteContext<U>>;
|
|
753
695
|
call(message: {
|
|
754
696
|
id?: string;
|
|
755
697
|
path?: string;
|
|
756
698
|
key?: string;
|
|
757
699
|
payload?: any;
|
|
758
|
-
}, ctx?:
|
|
700
|
+
}, ctx?: AppRouteContext<U> & {
|
|
759
701
|
[key: string]: any;
|
|
760
702
|
}): Promise<any>;
|
|
761
|
-
|
|
703
|
+
/**
|
|
704
|
+
* @deprecated
|
|
705
|
+
*/
|
|
706
|
+
queryRoute(path: string, key?: string, payload?: any, ctx?: AppRouteContext<U> & {
|
|
707
|
+
[key: string]: any;
|
|
708
|
+
}): Promise<{
|
|
709
|
+
code: any;
|
|
710
|
+
data: any;
|
|
711
|
+
message: any;
|
|
712
|
+
}>;
|
|
713
|
+
run(msg: {
|
|
714
|
+
id?: string;
|
|
715
|
+
path?: string;
|
|
716
|
+
key?: string;
|
|
717
|
+
payload?: any;
|
|
718
|
+
}, ctx?: AppRouteContext<U> & {
|
|
762
719
|
[key: string]: any;
|
|
763
720
|
}): Promise<{
|
|
764
721
|
code: any;
|