@kevisual/router 0.0.15 → 0.0.17

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.
@@ -1,393 +1,5 @@
1
- import * as zod from 'zod';
2
- import { Schema } from 'zod';
3
-
4
- type BaseRule = {
5
- value?: any;
6
- required?: boolean;
7
- message?: string;
8
- };
9
- type RuleString = {
10
- type: 'string';
11
- minLength?: number;
12
- maxLength?: number;
13
- regex?: string;
14
- } & BaseRule;
15
- type RuleNumber = {
16
- type: 'number';
17
- min?: number;
18
- max?: number;
19
- } & BaseRule;
20
- type RuleBoolean = {
21
- type: 'boolean';
22
- } & BaseRule;
23
- type RuleArray = {
24
- type: 'array';
25
- items: Rule;
26
- minItems?: number;
27
- maxItems?: number;
28
- } & BaseRule;
29
- type RuleObject = {
30
- type: 'object';
31
- properties: {
32
- [key: string]: Rule;
33
- };
34
- } & BaseRule;
35
- type RuleAny = {
36
- type: 'any';
37
- } & BaseRule;
38
- type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
39
-
40
- type RouterContextT = {
41
- code?: number;
42
- [key: string]: any;
43
- };
44
- type RouteContext<T = {
45
- code?: number;
46
- }, S = any> = {
47
- query?: {
48
- [key: string]: any;
49
- };
50
- /** return body */
51
- body?: number | string | Object;
52
- /** return code */
53
- code?: number;
54
- /** return msg */
55
- message?: string;
56
- state?: S;
57
- /**
58
- * 当前路径
59
- */
60
- currentPath?: string;
61
- /**
62
- * 当前key
63
- */
64
- currentKey?: string;
65
- /**
66
- * 当前route
67
- */
68
- currentRoute?: Route;
69
- /**
70
- * 进度
71
- */
72
- progress?: [string, string][];
73
- nextQuery?: {
74
- [key: string]: any;
75
- };
76
- end?: boolean;
77
- /**
78
- * 请求 route的返回结果,包函ctx
79
- */
80
- queryRouter?: QueryRouter;
81
- error?: any;
82
- /** 请求 route的返回结果,包函ctx */
83
- call?: (message: {
84
- path: string;
85
- key?: string;
86
- payload?: any;
87
- [key: string]: any;
88
- } | {
89
- id: string;
90
- apyload?: any;
91
- [key: string]: any;
92
- }, ctx?: RouteContext & {
93
- [key: string]: any;
94
- }) => Promise<any>;
95
- /** 请求 route的返回结果,不包函ctx */
96
- queryRoute?: (message: {
97
- path: string;
98
- key?: string;
99
- payload?: any;
100
- }, ctx?: RouteContext & {
101
- [key: string]: any;
102
- }) => Promise<any>;
103
- index?: number;
104
- throw?: (code?: number | string, message?: string, tips?: string) => void;
105
- /** 是否需要序列化 */
106
- needSerialize?: boolean;
107
- } & T;
108
- type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
109
- type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
110
- type RouteOpts = {
111
- path?: string;
112
- key?: string;
113
- id?: string;
114
- run?: Run;
115
- nextRoute?: NextRoute;
116
- description?: string;
117
- metadata?: {
118
- [key: string]: any;
119
- };
120
- middleware?: Route[] | string[];
121
- type?: 'route' | 'middleware';
122
- /**
123
- * validator: {
124
- * packageName: {
125
- * type: 'string',
126
- * required: true,
127
- * },
128
- * }
129
- */
130
- validator?: {
131
- [key: string]: Rule;
132
- };
133
- schema?: {
134
- [key: string]: Schema<any>;
135
- };
136
- isVerify?: boolean;
137
- verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
138
- verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
139
- /**
140
- * $#$ will be used to split path and key
141
- */
142
- idUsePath?: boolean;
143
- /**
144
- * id 合并的分隔符,默认为 $#$
145
- */
146
- delimiter?: string;
147
- isDebug?: boolean;
148
- };
149
- type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
150
- declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
151
- type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
152
- declare class Route<U = {
153
- [key: string]: any;
154
- }> {
155
- /**
156
- * 一级路径
157
- */
158
- path?: string;
159
- /**
160
- * 二级路径
161
- */
162
- key?: string;
163
- id?: string;
164
- share?: boolean;
165
- run?: Run;
166
- nextRoute?: NextRoute;
167
- description?: string;
168
- metadata?: {
169
- [key: string]: any;
170
- };
171
- middleware?: (Route | string)[];
172
- type?: string;
173
- private _validator?;
174
- schema?: {
175
- [key: string]: Schema<any>;
176
- };
177
- data?: any;
178
- /**
179
- * 是否需要验证
180
- */
181
- isVerify?: boolean;
182
- /**
183
- * 是否开启debug,开启后会打印错误信息
184
- */
185
- isDebug?: boolean;
186
- constructor(path: string, key?: string, opts?: RouteOpts);
187
- private createSchema;
188
- /**
189
- * set validator and create schema
190
- * @param validator
191
- */
192
- set validator(validator: {
193
- [key: string]: Rule;
194
- });
195
- get validator(): {
196
- [key: string]: Rule;
197
- };
198
- /**
199
- * has code, body, message in ctx, return ctx if has error
200
- * @param ctx
201
- * @param dev
202
- * @returns
203
- */
204
- verify(ctx: RouteContext, dev?: boolean): void;
205
- /**
206
- * Need to manully call return ctx fn and configure body, code, message
207
- * @param key
208
- * @param ctx
209
- * @param dev
210
- * @returns
211
- */
212
- verifyKey(key: string, ctx: RouteContext, dev?: boolean): {
213
- message: string;
214
- path: string;
215
- key: string;
216
- error: any;
217
- } | {
218
- message: string;
219
- path: string;
220
- key: string;
221
- error?: undefined;
222
- };
223
- setValidator(validator: {
224
- [key: string]: Rule;
225
- }): this;
226
- define<T extends {
227
- [key: string]: any;
228
- } = RouterContextT>(opts: DefineRouteOpts): this;
229
- define<T extends {
230
- [key: string]: any;
231
- } = RouterContextT>(fn: Run<T & U>): this;
232
- define<T extends {
233
- [key: string]: any;
234
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
235
- define<T extends {
236
- [key: string]: any;
237
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
238
- addTo(router: QueryRouter | {
239
- add: (route: Route) => void;
240
- [key: string]: any;
241
- }): void;
242
- setData(data: any): this;
243
- throw(code?: number | string, message?: string, tips?: string): void;
244
- }
245
- declare class QueryRouter {
246
- routes: Route[];
247
- maxNextRoute: number;
248
- context?: RouteContext;
249
- constructor();
250
- add(route: Route): void;
251
- /**
252
- * remove route by path and key
253
- * @param route
254
- */
255
- remove(route: Route | {
256
- path: string;
257
- key?: string;
258
- }): void;
259
- /**
260
- * remove route by id
261
- * @param uniqueId
262
- */
263
- removeById(unique: string): void;
264
- /**
265
- * 执行route
266
- * @param path
267
- * @param key
268
- * @param ctx
269
- * @returns
270
- */
271
- runRoute(path: string, key: string, ctx?: RouteContext): any;
272
- /**
273
- * 第一次执行
274
- * @param message
275
- * @param ctx
276
- * @returns
277
- */
278
- parse(message: {
279
- path: string;
280
- key?: string;
281
- payload?: any;
282
- }, ctx?: RouteContext & {
283
- [key: string]: any;
284
- }): Promise<any>;
285
- /**
286
- * 返回的数据包含所有的context的请求返回的内容,可做其他处理
287
- * @param message
288
- * @param ctx
289
- * @returns
290
- */
291
- call(message: {
292
- id?: string;
293
- path?: string;
294
- key?: string;
295
- payload?: any;
296
- }, ctx?: RouteContext & {
297
- [key: string]: any;
298
- }): Promise<any>;
299
- /**
300
- * 请求 result 的数据
301
- * @param message
302
- * @param ctx
303
- * @returns
304
- */
305
- queryRoute(message: {
306
- path: string;
307
- key?: string;
308
- payload?: any;
309
- }, ctx?: RouteContext & {
310
- [key: string]: any;
311
- }): Promise<{
312
- code: any;
313
- data: any;
314
- message: any;
315
- }>;
316
- setContext(ctx: RouteContext): Promise<void>;
317
- getList(): RouteInfo[];
318
- /**
319
- * 获取handle函数, 这里会去执行parse函数
320
- */
321
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
322
- path: string;
323
- key?: string;
324
- [key: string]: any;
325
- }, handleContext?: RouteContext) => Promise<{
326
- [key: string]: any;
327
- code: string;
328
- data?: any;
329
- message?: string;
330
- } | {
331
- code: any;
332
- data: any;
333
- message: any;
334
- } | {
335
- code: number;
336
- message: any;
337
- data?: undefined;
338
- }>;
339
- exportRoutes(): Route<{
340
- [key: string]: any;
341
- }>[];
342
- importRoutes(routes: Route[]): void;
343
- importRouter(router: QueryRouter): void;
344
- throw(code?: number | string, message?: string, tips?: string): void;
345
- hasRoute(path: string, key?: string): Route<{
346
- [key: string]: any;
347
- }>;
348
- }
349
- type QueryRouterServerOpts = {
350
- handleFn?: HandleFn;
351
- context?: RouteContext;
352
- };
353
- interface HandleFn<T = any> {
354
- (msg: {
355
- path: string;
356
- [key: string]: any;
357
- }, ctx?: any): {
358
- code: string;
359
- data?: any;
360
- message?: string;
361
- [key: string]: any;
362
- };
363
- (res: RouteContext<T>): any;
364
- }
365
- /**
366
- * QueryRouterServer
367
- * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
368
- */
369
- declare class QueryRouterServer extends QueryRouter {
370
- handle: any;
371
- constructor(opts?: QueryRouterServerOpts);
372
- setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
373
- use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
374
- addRoute(route: Route): void;
375
- Route: typeof Route;
376
- route(opts: RouteOpts): Route<Required<RouteContext>>;
377
- route(path: string, key?: string): Route<Required<RouteContext>>;
378
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
379
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
380
- /**
381
- * 等于queryRoute,但是调用了handle
382
- * @param param0
383
- * @returns
384
- */
385
- run({ path, key, payload }: {
386
- path: string;
387
- key?: string;
388
- payload?: any;
389
- }): Promise<any>;
390
- }
1
+ import { RouteOpts, QueryRouterServer, RouteMiddleware, Run } from '@kevisual/router';
2
+ export { RouteOpts } from '@kevisual/router';
391
3
 
392
4
  type RouteObject = {
393
5
  [key: string]: RouteOpts;
@@ -410,7 +22,7 @@ declare class Chain {
410
22
  [key: string]: any;
411
23
  }): this;
412
24
  setPath(path: string): this;
413
- setMiddleware(middleware: string[] | Route[]): this;
25
+ setMiddleware(middleware: RouteMiddleware[]): this;
414
26
  setKey(key: string): this;
415
27
  setId(key: string): this;
416
28
  setRun(run: Run): this;
@@ -428,32 +40,8 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
428
40
  static create<U extends Record<string, RouteOpts>>(value: U, opts?: ChainOptions): QueryUtil<U>;
429
41
  get<K extends keyof T>(key: K): RouteOpts;
430
42
  chain<K extends keyof T>(key: K, opts?: ChainOptions): Chain;
431
- queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => {
432
- path?: string;
433
- key?: string;
434
- id?: string;
435
- run?: Run;
436
- nextRoute?: NextRoute;
437
- description?: string;
438
- metadata?: {
439
- [key: string]: any;
440
- };
441
- middleware?: Route[] | string[];
442
- type?: "route" | "middleware";
443
- validator?: {
444
- [key: string]: Rule;
445
- };
446
- schema?: {
447
- [key: string]: zod.ZodType<any>;
448
- };
449
- isVerify?: boolean;
450
- verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
451
- verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
452
- idUsePath?: boolean;
453
- delimiter?: string;
454
- isDebug?: boolean;
455
- };
43
+ queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts;
456
44
  }
457
45
 
458
46
  export { QueryUtil, define, util };
459
- export type { RouteArray, RouteObject, RouteOpts };
47
+ export type { RouteArray, RouteObject };
package/dist/router.d.ts CHANGED
@@ -5,6 +5,7 @@ import https from 'node:https';
5
5
  import http2 from 'node:http2';
6
6
  import * as cookie from 'cookie';
7
7
  import { WebSocketServer, WebSocket } from 'ws';
8
+ import { RouteOpts as RouteOpts$1, QueryRouterServer as QueryRouterServer$1, RouteMiddleware as RouteMiddleware$1, Run as Run$1 } from '@kevisual/router';
8
9
 
9
10
  type BaseRule = {
10
11
  value?: any;
@@ -113,6 +114,11 @@ type RouteContext<T = {
113
114
  } & T;
114
115
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
115
116
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
117
+ type RouteMiddleware = {
118
+ path: string;
119
+ key?: string;
120
+ id?: string;
121
+ } | string;
116
122
  type RouteOpts = {
117
123
  path?: string;
118
124
  key?: string;
@@ -123,7 +129,7 @@ type RouteOpts = {
123
129
  metadata?: {
124
130
  [key: string]: any;
125
131
  };
126
- middleware?: Route[] | string[];
132
+ middleware?: RouteMiddleware[];
127
133
  type?: 'route' | 'middleware';
128
134
  /**
129
135
  * validator: {
@@ -174,7 +180,7 @@ declare class Route<U = {
174
180
  metadata?: {
175
181
  [key: string]: any;
176
182
  };
177
- middleware?: (Route | string)[];
183
+ middleware?: RouteMiddleware[];
178
184
  type?: string;
179
185
  private _validator?;
180
186
  schema?: {
@@ -572,6 +578,48 @@ declare class WsServer extends WsServerBase {
572
578
  listen(): void;
573
579
  }
574
580
 
581
+ type RouteObject = {
582
+ [key: string]: RouteOpts$1;
583
+ };
584
+ declare function define<T extends Record<string, RouteOpts$1>>(value: T): {
585
+ [K in keyof T]: T[K] & RouteOpts$1;
586
+ };
587
+ type RouteArray = RouteOpts$1[];
588
+ type ChainOptions = {
589
+ app: QueryRouterServer$1;
590
+ };
591
+ declare class Chain {
592
+ object: RouteOpts$1;
593
+ app?: QueryRouterServer$1;
594
+ constructor(object: RouteOpts$1, opts?: ChainOptions);
595
+ get key(): string;
596
+ get path(): string;
597
+ setDescription(desc: string): this;
598
+ setMeta(metadata: {
599
+ [key: string]: any;
600
+ }): this;
601
+ setPath(path: string): this;
602
+ setMiddleware(middleware: RouteMiddleware$1[]): this;
603
+ setKey(key: string): this;
604
+ setId(key: string): this;
605
+ setRun(run: Run$1): this;
606
+ define(run: Run$1): this;
607
+ createRoute(): this;
608
+ }
609
+ declare const util: {
610
+ getChain: (obj: RouteOpts$1, opts?: ChainOptions) => Chain;
611
+ };
612
+ declare class QueryUtil<T extends RouteObject = RouteObject> {
613
+ routeObject: T;
614
+ app: QueryRouterServer$1;
615
+ constructor(object: T, opts?: ChainOptions);
616
+ static createFormObj<U extends RouteObject>(object: U, opts?: ChainOptions): QueryUtil<U>;
617
+ static create<U extends Record<string, RouteOpts$1>>(value: U, opts?: ChainOptions): QueryUtil<U>;
618
+ get<K extends keyof T>(key: K): RouteOpts$1;
619
+ chain<K extends keyof T>(key: K, opts?: ChainOptions): Chain;
620
+ queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts$1;
621
+ }
622
+
575
623
  type RouterHandle = (msg: {
576
624
  path: string;
577
625
  [key: string]: any;
@@ -642,5 +690,5 @@ declare class App<T = {}, U = AppReqRes> {
642
690
  throw(code?: number | string, message?: string, tips?: string): void;
643
691
  }
644
692
 
645
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, Server, createSchema, handleServer };
646
- export type { RouteContext, RouteOpts, Rule, Run };
693
+ export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
694
+ export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run };