@kevisual/router 0.0.16 → 0.0.18

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,402 +1,10 @@
1
- import * as node_modules_zod_lib_types_js from 'node_modules/zod/lib/types.js';
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 RouteMiddleware = {
111
- path: string;
112
- key?: string;
113
- id?: string;
114
- } | string;
115
- type RouteOpts = {
116
- path?: string;
117
- key?: string;
118
- id?: string;
119
- run?: Run;
120
- nextRoute?: NextRoute;
121
- description?: string;
122
- metadata?: {
123
- [key: string]: any;
124
- };
125
- middleware?: RouteMiddleware[];
126
- type?: 'route' | 'middleware';
127
- /**
128
- * validator: {
129
- * packageName: {
130
- * type: 'string',
131
- * required: true,
132
- * },
133
- * }
134
- */
135
- validator?: {
136
- [key: string]: Rule;
137
- };
138
- schema?: {
139
- [key: string]: Schema<any>;
140
- };
141
- isVerify?: boolean;
142
- verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
143
- verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
144
- /**
145
- * $#$ will be used to split path and key
146
- */
147
- idUsePath?: boolean;
148
- /**
149
- * id 合并的分隔符,默认为 $#$
150
- */
151
- delimiter?: string;
152
- isDebug?: boolean;
153
- };
154
- type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
155
- declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
156
- type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
157
- declare class Route<U = {
158
- [key: string]: any;
159
- }> {
160
- /**
161
- * 一级路径
162
- */
163
- path?: string;
164
- /**
165
- * 二级路径
166
- */
167
- key?: string;
168
- id?: string;
169
- share?: boolean;
170
- run?: Run;
171
- nextRoute?: NextRoute;
172
- description?: string;
173
- metadata?: {
174
- [key: string]: any;
175
- };
176
- middleware?: RouteMiddleware[];
177
- type?: string;
178
- private _validator?;
179
- schema?: {
180
- [key: string]: Schema<any>;
181
- };
182
- data?: any;
183
- /**
184
- * 是否需要验证
185
- */
186
- isVerify?: boolean;
187
- /**
188
- * 是否开启debug,开启后会打印错误信息
189
- */
190
- isDebug?: boolean;
191
- constructor(path: string, key?: string, opts?: RouteOpts);
192
- private createSchema;
193
- /**
194
- * set validator and create schema
195
- * @param validator
196
- */
197
- set validator(validator: {
198
- [key: string]: Rule;
199
- });
200
- get validator(): {
201
- [key: string]: Rule;
202
- };
203
- /**
204
- * has code, body, message in ctx, return ctx if has error
205
- * @param ctx
206
- * @param dev
207
- * @returns
208
- */
209
- verify(ctx: RouteContext, dev?: boolean): void;
210
- /**
211
- * Need to manully call return ctx fn and configure body, code, message
212
- * @param key
213
- * @param ctx
214
- * @param dev
215
- * @returns
216
- */
217
- verifyKey(key: string, ctx: RouteContext, dev?: boolean): {
218
- message: string;
219
- path: string;
220
- key: string;
221
- error: any;
222
- } | {
223
- message: string;
224
- path: string;
225
- key: string;
226
- error?: undefined;
227
- };
228
- setValidator(validator: {
229
- [key: string]: Rule;
230
- }): this;
231
- define<T extends {
232
- [key: string]: any;
233
- } = RouterContextT>(opts: DefineRouteOpts): this;
234
- define<T extends {
235
- [key: string]: any;
236
- } = RouterContextT>(fn: Run<T & U>): this;
237
- define<T extends {
238
- [key: string]: any;
239
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
240
- define<T extends {
241
- [key: string]: any;
242
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
243
- addTo(router: QueryRouter | {
244
- add: (route: Route) => void;
245
- [key: string]: any;
246
- }): void;
247
- setData(data: any): this;
248
- throw(code?: number | string, message?: string, tips?: string): void;
249
- }
250
- declare class QueryRouter {
251
- routes: Route[];
252
- maxNextRoute: number;
253
- context?: RouteContext;
254
- constructor();
255
- add(route: Route): void;
256
- /**
257
- * remove route by path and key
258
- * @param route
259
- */
260
- remove(route: Route | {
261
- path: string;
262
- key?: string;
263
- }): void;
264
- /**
265
- * remove route by id
266
- * @param uniqueId
267
- */
268
- removeById(unique: string): void;
269
- /**
270
- * 执行route
271
- * @param path
272
- * @param key
273
- * @param ctx
274
- * @returns
275
- */
276
- runRoute(path: string, key: string, ctx?: RouteContext): any;
277
- /**
278
- * 第一次执行
279
- * @param message
280
- * @param ctx
281
- * @returns
282
- */
283
- parse(message: {
284
- path: string;
285
- key?: string;
286
- payload?: any;
287
- }, ctx?: RouteContext & {
288
- [key: string]: any;
289
- }): Promise<any>;
290
- /**
291
- * 返回的数据包含所有的context的请求返回的内容,可做其他处理
292
- * @param message
293
- * @param ctx
294
- * @returns
295
- */
296
- call(message: {
297
- id?: string;
298
- path?: string;
299
- key?: string;
300
- payload?: any;
301
- }, ctx?: RouteContext & {
302
- [key: string]: any;
303
- }): Promise<any>;
304
- /**
305
- * 请求 result 的数据
306
- * @param message
307
- * @param ctx
308
- * @returns
309
- */
310
- queryRoute(message: {
311
- path: string;
312
- key?: string;
313
- payload?: any;
314
- }, ctx?: RouteContext & {
315
- [key: string]: any;
316
- }): Promise<{
317
- code: any;
318
- data: any;
319
- message: any;
320
- }>;
321
- setContext(ctx: RouteContext): Promise<void>;
322
- getList(): RouteInfo[];
323
- /**
324
- * 获取handle函数, 这里会去执行parse函数
325
- */
326
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
327
- path: string;
328
- key?: string;
329
- [key: string]: any;
330
- }, handleContext?: RouteContext) => Promise<{
331
- [key: string]: any;
332
- code: string;
333
- data?: any;
334
- message?: string;
335
- } | {
336
- code: any;
337
- data: any;
338
- message: any;
339
- } | {
340
- code: number;
341
- message: any;
342
- data?: undefined;
343
- }>;
344
- exportRoutes(): Route<{
345
- [key: string]: any;
346
- }>[];
347
- importRoutes(routes: Route[]): void;
348
- importRouter(router: QueryRouter): void;
349
- throw(code?: number | string, message?: string, tips?: string): void;
350
- hasRoute(path: string, key?: string): Route<{
351
- [key: string]: any;
352
- }>;
353
- }
354
- type QueryRouterServerOpts = {
355
- handleFn?: HandleFn;
356
- context?: RouteContext;
357
- };
358
- interface HandleFn<T = any> {
359
- (msg: {
360
- path: string;
361
- [key: string]: any;
362
- }, ctx?: any): {
363
- code: string;
364
- data?: any;
365
- message?: string;
366
- [key: string]: any;
367
- };
368
- (res: RouteContext<T>): any;
369
- }
370
- /**
371
- * QueryRouterServer
372
- * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
373
- */
374
- declare class QueryRouterServer extends QueryRouter {
375
- handle: any;
376
- constructor(opts?: QueryRouterServerOpts);
377
- setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
378
- use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
379
- addRoute(route: Route): void;
380
- Route: typeof Route;
381
- route(opts: RouteOpts): Route<Required<RouteContext>>;
382
- route(path: string, key?: string): Route<Required<RouteContext>>;
383
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
384
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
385
- /**
386
- * 等于queryRoute,但是调用了handle
387
- * @param param0
388
- * @returns
389
- */
390
- run({ path, key, payload }: {
391
- path: string;
392
- key?: string;
393
- payload?: any;
394
- }): Promise<any>;
395
- }
1
+ import { RouteOpts, QueryRouterServer, RouteMiddleware, Run } from '@kevisual/router';
2
+ export { RouteOpts } from '@kevisual/router';
396
3
 
397
4
  type RouteObject = {
398
5
  [key: string]: RouteOpts;
399
6
  };
7
+ type SimpleObject = Record<string, any>;
400
8
  declare function define<T extends Record<string, RouteOpts>>(value: T): {
401
9
  [K in keyof T]: T[K] & RouteOpts;
402
10
  };
@@ -418,8 +26,8 @@ declare class Chain {
418
26
  setMiddleware(middleware: RouteMiddleware[]): this;
419
27
  setKey(key: string): this;
420
28
  setId(key: string): this;
421
- setRun(run: Run): this;
422
- define(run: Run): this;
29
+ setRun<U extends SimpleObject = {}>(run: Run<U>): this;
30
+ define<U extends SimpleObject = {}>(run: Run<U>): this;
423
31
  createRoute(): this;
424
32
  }
425
33
  declare const util: {
@@ -433,32 +41,8 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
433
41
  static create<U extends Record<string, RouteOpts>>(value: U, opts?: ChainOptions): QueryUtil<U>;
434
42
  get<K extends keyof T>(key: K): RouteOpts;
435
43
  chain<K extends keyof T>(key: K, opts?: ChainOptions): Chain;
436
- queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => {
437
- path?: string;
438
- key?: string;
439
- id?: string;
440
- run?: Run;
441
- nextRoute?: NextRoute;
442
- description?: string;
443
- metadata?: {
444
- [key: string]: any;
445
- };
446
- middleware?: RouteMiddleware[];
447
- type?: "route" | "middleware";
448
- validator?: {
449
- [key: string]: Rule;
450
- };
451
- schema?: {
452
- [key: string]: node_modules_zod_lib_types_js.ZodType<any>;
453
- };
454
- isVerify?: boolean;
455
- verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
456
- verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
457
- idUsePath?: boolean;
458
- delimiter?: string;
459
- isDebug?: boolean;
460
- };
44
+ queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts;
461
45
  }
462
46
 
463
47
  export { QueryUtil, define, util };
464
- export type { RouteArray, RouteObject, RouteOpts };
48
+ 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;
@@ -111,7 +112,8 @@ type RouteContext<T = {
111
112
  /** 是否需要序列化 */
112
113
  needSerialize?: boolean;
113
114
  } & T;
114
- type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
115
+ type SimpleObject$1 = Record<string, any>;
116
+ type Run<T extends SimpleObject$1 = {}> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
115
117
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
116
118
  type RouteMiddleware = {
117
119
  path: string;
@@ -577,6 +579,49 @@ declare class WsServer extends WsServerBase {
577
579
  listen(): void;
578
580
  }
579
581
 
582
+ type RouteObject = {
583
+ [key: string]: RouteOpts$1;
584
+ };
585
+ type SimpleObject = Record<string, any>;
586
+ declare function define<T extends Record<string, RouteOpts$1>>(value: T): {
587
+ [K in keyof T]: T[K] & RouteOpts$1;
588
+ };
589
+ type RouteArray = RouteOpts$1[];
590
+ type ChainOptions = {
591
+ app: QueryRouterServer$1;
592
+ };
593
+ declare class Chain {
594
+ object: RouteOpts$1;
595
+ app?: QueryRouterServer$1;
596
+ constructor(object: RouteOpts$1, opts?: ChainOptions);
597
+ get key(): string;
598
+ get path(): string;
599
+ setDescription(desc: string): this;
600
+ setMeta(metadata: {
601
+ [key: string]: any;
602
+ }): this;
603
+ setPath(path: string): this;
604
+ setMiddleware(middleware: RouteMiddleware$1[]): this;
605
+ setKey(key: string): this;
606
+ setId(key: string): this;
607
+ setRun<U extends SimpleObject = {}>(run: Run$1<U>): this;
608
+ define<U extends SimpleObject = {}>(run: Run$1<U>): this;
609
+ createRoute(): this;
610
+ }
611
+ declare const util: {
612
+ getChain: (obj: RouteOpts$1, opts?: ChainOptions) => Chain;
613
+ };
614
+ declare class QueryUtil<T extends RouteObject = RouteObject> {
615
+ routeObject: T;
616
+ app: QueryRouterServer$1;
617
+ constructor(object: T, opts?: ChainOptions);
618
+ static createFormObj<U extends RouteObject>(object: U, opts?: ChainOptions): QueryUtil<U>;
619
+ static create<U extends Record<string, RouteOpts$1>>(value: U, opts?: ChainOptions): QueryUtil<U>;
620
+ get<K extends keyof T>(key: K): RouteOpts$1;
621
+ chain<K extends keyof T>(key: K, opts?: ChainOptions): Chain;
622
+ queryChain<K extends keyof T>(key: K): (queryData?: Record<string, any>) => RouteOpts$1;
623
+ }
624
+
580
625
  type RouterHandle = (msg: {
581
626
  path: string;
582
627
  [key: string]: any;
@@ -647,5 +692,5 @@ declare class App<T = {}, U = AppReqRes> {
647
692
  throw(code?: number | string, message?: string, tips?: string): void;
648
693
  }
649
694
 
650
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, Server, createSchema, handleServer };
651
- export type { RouteContext, RouteMiddleware, RouteOpts, Rule, Run };
695
+ export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
696
+ export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run };