@kevisual/router 0.0.34 → 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.
- package/dist/router-browser.d.ts +75 -115
- package/dist/router-browser.js +3320 -4543
- package/dist/router-define.d.ts +1 -1
- package/dist/router.d.ts +101 -128
- package/dist/router.js +4514 -5719
- package/package.json +1 -1
- package/src/app.ts +20 -12
- package/src/route.ts +50 -210
- 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 {
|
|
@@ -316,6 +217,7 @@ declare class QueryRouter {
|
|
|
316
217
|
* 请求 result 的数据
|
|
317
218
|
* @param message
|
|
318
219
|
* @param ctx
|
|
220
|
+
* @deprecated use run or call instead
|
|
319
221
|
* @returns
|
|
320
222
|
*/
|
|
321
223
|
queryRoute(message: {
|
|
@@ -330,6 +232,24 @@ declare class QueryRouter {
|
|
|
330
232
|
data: any;
|
|
331
233
|
message: any;
|
|
332
234
|
}>;
|
|
235
|
+
/**
|
|
236
|
+
* Router Run获取数据
|
|
237
|
+
* @param message
|
|
238
|
+
* @param ctx
|
|
239
|
+
* @returns
|
|
240
|
+
*/
|
|
241
|
+
run(message: {
|
|
242
|
+
id?: string;
|
|
243
|
+
path?: string;
|
|
244
|
+
key?: string;
|
|
245
|
+
payload?: any;
|
|
246
|
+
}, ctx?: RouteContext & {
|
|
247
|
+
[key: string]: any;
|
|
248
|
+
}): Promise<{
|
|
249
|
+
code: any;
|
|
250
|
+
data: any;
|
|
251
|
+
message: any;
|
|
252
|
+
}>;
|
|
333
253
|
/**
|
|
334
254
|
* 设置上下文
|
|
335
255
|
* @description 这里的上下文是为了在handle函数中使用
|
|
@@ -421,7 +341,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
421
341
|
prompt(description: string): Route<Required<RouteContext>>;
|
|
422
342
|
prompt(description: Function): Route<Required<RouteContext>>;
|
|
423
343
|
/**
|
|
424
|
-
*
|
|
344
|
+
* 调用了handle
|
|
425
345
|
* @param param0
|
|
426
346
|
* @returns
|
|
427
347
|
*/
|
|
@@ -429,9 +349,12 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
429
349
|
path: string;
|
|
430
350
|
key?: string;
|
|
431
351
|
payload?: any;
|
|
352
|
+
}, ctx?: RouteContext & {
|
|
353
|
+
[key: string]: any;
|
|
432
354
|
}): Promise<any>;
|
|
433
355
|
}
|
|
434
|
-
declare
|
|
356
|
+
declare class Mini extends QueryRouterServer {
|
|
357
|
+
}
|
|
435
358
|
|
|
436
359
|
declare class Connect {
|
|
437
360
|
path: string;
|
|
@@ -531,7 +454,7 @@ declare class Server {
|
|
|
531
454
|
* @description 主要是为了兼容其他的监听
|
|
532
455
|
* @param listener
|
|
533
456
|
*/
|
|
534
|
-
on(listener: Listener | Listener[]): void;
|
|
457
|
+
on(listener: Listener | Listener[]): () => void;
|
|
535
458
|
get callback(): any;
|
|
536
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>;
|
|
537
460
|
}
|
|
@@ -580,6 +503,43 @@ declare class CustomError extends Error {
|
|
|
580
503
|
};
|
|
581
504
|
}
|
|
582
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
|
+
|
|
583
543
|
type WsServerBaseOpts = {
|
|
584
544
|
wss?: WebSocketServer;
|
|
585
545
|
path?: string;
|
|
@@ -656,7 +616,7 @@ declare class QueryChain {
|
|
|
656
616
|
* @param queryData
|
|
657
617
|
* @returns
|
|
658
618
|
*/
|
|
659
|
-
getKey(queryData?: SimpleObject): Pick<RouteOpts$1, 'path' | 'key' | 'metadata' | 'description'
|
|
619
|
+
getKey(queryData?: SimpleObject): Pick<RouteOpts$1, 'path' | 'key' | 'metadata' | 'description'>;
|
|
660
620
|
post<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
661
621
|
get<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
662
622
|
}
|
|
@@ -701,15 +661,18 @@ type AppOptions<T = {}> = {
|
|
|
701
661
|
path?: string;
|
|
702
662
|
};
|
|
703
663
|
};
|
|
704
|
-
type
|
|
664
|
+
type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
|
|
665
|
+
app: App<T>;
|
|
666
|
+
};
|
|
705
667
|
/**
|
|
706
668
|
* 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
|
|
669
|
+
* U - Route Context的扩展类型
|
|
707
670
|
*/
|
|
708
|
-
declare class App<
|
|
671
|
+
declare class App<U = {}> {
|
|
709
672
|
router: QueryRouter;
|
|
710
673
|
server: Server;
|
|
711
674
|
io: WsServer;
|
|
712
|
-
constructor(opts?: AppOptions<
|
|
675
|
+
constructor(opts?: AppOptions<U>);
|
|
713
676
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
714
677
|
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
715
678
|
listen(port: number, backlog?: number, listeningListener?: () => void): void;
|
|
@@ -722,21 +685,31 @@ declare class App<T = {}, U = AppReqRes> {
|
|
|
722
685
|
addRoute(route: Route): void;
|
|
723
686
|
add: (route: Route) => void;
|
|
724
687
|
Route: typeof Route;
|
|
725
|
-
route(opts: RouteOpts): Route<U
|
|
726
|
-
route(path: string, key?: string): Route<U
|
|
727
|
-
route(path: string, opts?: RouteOpts): Route<U
|
|
728
|
-
route(path: string, key?: string, opts?: RouteOpts): Route<U
|
|
729
|
-
prompt(description: string): Route<
|
|
730
|
-
prompt(description: Function): Route<
|
|
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>>;
|
|
731
694
|
call(message: {
|
|
732
695
|
id?: string;
|
|
733
696
|
path?: string;
|
|
734
697
|
key?: string;
|
|
735
698
|
payload?: any;
|
|
736
|
-
}, ctx?:
|
|
699
|
+
}, ctx?: AppRouteContext<U> & {
|
|
737
700
|
[key: string]: any;
|
|
738
701
|
}): Promise<any>;
|
|
739
|
-
|
|
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> & {
|
|
740
713
|
[key: string]: any;
|
|
741
714
|
}): Promise<{
|
|
742
715
|
code: any;
|