@kevisual/router 0.0.83 → 0.0.84

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.d.ts CHANGED
@@ -80,9 +80,20 @@ type RouterContextT = {
80
80
  code?: number;
81
81
  [key: string]: any;
82
82
  };
83
+ type BuildRouteContext<M, U> = M extends {
84
+ args?: infer A;
85
+ } ? A extends z.ZodObject<any> ? RouteContext<{
86
+ args?: z.infer<A>;
87
+ }, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
88
+ args?: {
89
+ [K in keyof A]: z.infer<A[K]>;
90
+ };
91
+ }, U> : RouteContext<U> : RouteContext<U>;
83
92
  type RouteContext<T = {
84
93
  code?: number;
85
- }, S = any> = {
94
+ }, U extends SimpleObject$1 = {}, S = {
95
+ [key: string]: any;
96
+ }> = {
86
97
  /**
87
98
  * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
88
99
  * 或者不需要登录的,直接调用
@@ -105,7 +116,14 @@ type RouteContext<T = {
105
116
  code?: number;
106
117
  /** return msg */
107
118
  message?: string;
119
+ /**
120
+ * 传递状态
121
+ */
108
122
  state?: S;
123
+ /**
124
+ * 当前routerId
125
+ */
126
+ currentId?: string;
109
127
  /**
110
128
  * 当前路径
111
129
  */
@@ -146,14 +164,12 @@ type RouteContext<T = {
146
164
  path: string;
147
165
  key?: string;
148
166
  payload?: any;
149
- }, ctx?: RouteContext & {
150
- [key: string]: any;
151
- }) => Promise<any>;
167
+ }, ctx?: RouteContext) => Promise<any>;
152
168
  index?: number;
153
169
  throw?: throwError['throw'];
154
170
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
155
171
  needSerialize?: boolean;
156
- } & T;
172
+ } & T & U;
157
173
  type SimpleObject$1 = Record<string, any>;
158
174
  type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
159
175
  type RunMessage = {
@@ -164,7 +180,7 @@ type RunMessage = {
164
180
  };
165
181
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
166
182
  type RouteMiddleware = {
167
- path: string;
183
+ path?: string;
168
184
  key?: string;
169
185
  id?: string;
170
186
  } | string;
@@ -205,9 +221,11 @@ declare const tool: {
205
221
  /** */
206
222
  declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
207
223
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
208
- declare class Route<U = {
209
- [key: string]: any;
210
- }, T extends SimpleObject$1 = SimpleObject$1> implements throwError {
224
+ /**
225
+ * @M 是 route的 metadate的类型,默认是 SimpleObject
226
+ * @U RouteContext state的类型
227
+ */
228
+ declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
211
229
  /**
212
230
  * 一级路径
213
231
  */
@@ -217,10 +235,10 @@ declare class Route<U = {
217
235
  */
218
236
  key?: string;
219
237
  id?: string;
220
- run?: Run;
238
+ run?: Run<BuildRouteContext<M, U>>;
221
239
  nextRoute?: NextRoute;
222
240
  description?: string;
223
- metadata?: T;
241
+ metadata?: M;
224
242
  middleware?: RouteMiddleware[];
225
243
  type?: string;
226
244
  /**
@@ -235,13 +253,13 @@ declare class Route<U = {
235
253
  } = RouterContextT>(opts: DefineRouteOpts): this;
236
254
  define<T extends {
237
255
  [key: string]: any;
238
- } = RouterContextT>(fn: Run<T & U>): this;
256
+ } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
239
257
  define<T extends {
240
258
  [key: string]: any;
241
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
259
+ } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
242
260
  define<T extends {
243
261
  [key: string]: any;
244
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
262
+ } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
245
263
  update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
246
264
  addTo(router: QueryRouter | {
247
265
  add: (route: Route) => void;
@@ -272,11 +290,11 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
272
290
  type AddOpts = {
273
291
  overwrite?: boolean;
274
292
  };
275
- declare class QueryRouter implements throwError {
293
+ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
276
294
  appId: string;
277
295
  routes: Route[];
278
296
  maxNextRoute: number;
279
- context?: RouteContext;
297
+ context?: RouteContext<T>;
280
298
  constructor();
281
299
  /**
282
300
  * add route
@@ -304,7 +322,7 @@ declare class QueryRouter implements throwError {
304
322
  * @param ctx
305
323
  * @returns
306
324
  */
307
- runRoute(path: string, key: string, ctx?: RouteContext): any;
325
+ runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
308
326
  /**
309
327
  * 第一次执行
310
328
  * @param message
@@ -315,9 +333,11 @@ declare class QueryRouter implements throwError {
315
333
  path: string;
316
334
  key?: string;
317
335
  payload?: any;
318
- }, ctx?: RouteContext & {
336
+ }, ctx?: RouteContext<T> & {
319
337
  [key: string]: any;
320
- }): Promise<any>;
338
+ }): Promise<RouteContext<T, {}, {
339
+ [key: string]: any;
340
+ }>>;
321
341
  /**
322
342
  * 返回的数据包含所有的context的请求返回的内容,可做其他处理
323
343
  * @param message
@@ -329,9 +349,15 @@ declare class QueryRouter implements throwError {
329
349
  path?: string;
330
350
  key?: string;
331
351
  payload?: any;
332
- }, ctx?: RouteContext & {
352
+ }, ctx?: RouteContext<T> & {
353
+ [key: string]: any;
354
+ }): Promise<RouteContext<T, {}, {
333
355
  [key: string]: any;
334
- }): Promise<any>;
356
+ }> | {
357
+ code: number;
358
+ body: any;
359
+ message: string;
360
+ }>;
335
361
  /**
336
362
  * 请求 result 的数据
337
363
  * @param message
@@ -347,9 +373,9 @@ declare class QueryRouter implements throwError {
347
373
  }, ctx?: RouteContext & {
348
374
  [key: string]: any;
349
375
  }): Promise<{
350
- code: any;
376
+ code: number;
351
377
  data: any;
352
- message: any;
378
+ message: string;
353
379
  }>;
354
380
  /**
355
381
  * Router Run获取数据
@@ -362,12 +388,12 @@ declare class QueryRouter implements throwError {
362
388
  path?: string;
363
389
  key?: string;
364
390
  payload?: any;
365
- }, ctx?: RouteContext & {
391
+ }, ctx?: RouteContext<T> & {
366
392
  [key: string]: any;
367
393
  }): Promise<{
368
- code: any;
394
+ code: number;
369
395
  data: any;
370
- message: any;
396
+ message: string;
371
397
  }>;
372
398
  /**
373
399
  * 设置上下文
@@ -379,12 +405,12 @@ declare class QueryRouter implements throwError {
379
405
  /**
380
406
  * 获取handle函数, 这里会去执行parse函数
381
407
  */
382
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
408
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
383
409
  id?: string;
384
410
  path?: string;
385
411
  key?: string;
386
412
  [key: string]: any;
387
- }, handleContext?: RouteContext) => Promise<{
413
+ }, handleContext?: RouteContext<T>) => Promise<{
388
414
  [key: string]: any;
389
415
  code: string;
390
416
  data?: any;
@@ -398,22 +424,16 @@ declare class QueryRouter implements throwError {
398
424
  message: any;
399
425
  data?: undefined;
400
426
  }>;
401
- exportRoutes(): Route<{
402
- [key: string]: any;
403
- }, SimpleObject$1>[];
427
+ exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
404
428
  importRoutes(routes: Route[]): void;
405
429
  importRouter(router: QueryRouter): void;
406
430
  throw(...args: any[]): void;
407
- hasRoute(path: string, key?: string): Route<{
408
- [key: string]: any;
409
- }, SimpleObject$1>;
431
+ hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
410
432
  findRoute(opts?: {
411
433
  path?: string;
412
434
  key?: string;
413
435
  id?: string;
414
- }): Route<{
415
- [key: string]: any;
416
- }, SimpleObject$1>;
436
+ }): Route<SimpleObject$1, SimpleObject$1>;
417
437
  createRouteList(opts?: {
418
438
  force?: boolean;
419
439
  filter?: (route: Route) => boolean;
@@ -455,10 +475,11 @@ declare class QueryRouter implements throwError {
455
475
  [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
456
476
  };
457
477
  }
458
- type QueryRouterServerOpts = {
478
+ type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
459
479
  handleFn?: HandleFn;
460
- context?: RouteContext;
480
+ context?: RouteContext<C>;
461
481
  appId?: string;
482
+ initHandle?: boolean;
462
483
  };
463
484
  interface HandleFn<T = any> {
464
485
  (msg: {
@@ -475,20 +496,27 @@ interface HandleFn<T = any> {
475
496
  /**
476
497
  * QueryRouterServer
477
498
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
499
+ * @template C 自定义 RouteContext 类型
478
500
  */
479
- declare class QueryRouterServer extends QueryRouter {
501
+ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
480
502
  appId: string;
481
503
  handle: any;
482
- constructor(opts?: QueryRouterServerOpts);
504
+ context: RouteContext<C>;
505
+ constructor(opts?: QueryRouterServerOpts<C>);
483
506
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
484
507
  addRoute(route: Route, opts?: AddOpts): void;
485
508
  Route: typeof Route;
486
- route(opts: RouteOpts): Route<Required<RouteContext>>;
487
- route(path: string, key?: string): Route<Required<RouteContext>>;
488
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
489
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
490
- prompt(description: string): Route<Required<RouteContext>>;
491
- prompt(description: Function): Route<Required<RouteContext>>;
509
+ route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
510
+ metadata?: M;
511
+ }): Route<M, Required<RouteContext<C>>>;
512
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
513
+ metadata?: M;
514
+ }): Route<M, Required<RouteContext<C>>>;
515
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
516
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
517
+ metadata?: M;
518
+ }): Route<M, Required<RouteContext<C>>>;
519
+ prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
492
520
  /**
493
521
  * 调用了handle
494
522
  * @param param0
@@ -499,9 +527,7 @@ declare class QueryRouterServer extends QueryRouter {
499
527
  path?: string;
500
528
  key?: string;
501
529
  payload?: any;
502
- }, ctx?: RouteContext & {
503
- [key: string]: any;
504
- }): Promise<any>;
530
+ }, ctx?: Partial<RouteContext<C>>): Promise<any>;
505
531
  }
506
532
  declare class Mini extends QueryRouterServer {
507
533
  }
@@ -957,7 +983,7 @@ type RouterHandle = (msg: {
957
983
  [key: string]: any;
958
984
  };
959
985
  type AppOptions<T = {}> = {
960
- router?: QueryRouter;
986
+ router?: QueryRouterServer;
961
987
  server?: ServerType;
962
988
  /** handle msg 关联 */
963
989
  routerHandle?: RouterHandle;
@@ -965,17 +991,18 @@ type AppOptions<T = {}> = {
965
991
  serverOptions?: ServerNodeOpts;
966
992
  appId?: string;
967
993
  };
968
- type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
994
+ type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
969
995
  app: App<T>;
970
996
  };
971
997
  /**
972
998
  * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
973
999
  * U - Route Context的扩展类型
974
1000
  */
975
- declare class App<U = {}> extends QueryRouter {
1001
+ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
976
1002
  appId: string;
977
- router: QueryRouter;
1003
+ router: QueryRouterServer;
978
1004
  server: ServerType;
1005
+ context: AppRouteContext<U>;
979
1006
  constructor(opts?: AppOptions<U>);
980
1007
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
981
1008
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
@@ -985,34 +1012,7 @@ declare class App<U = {}> extends QueryRouter {
985
1012
  listen(path: string, listeningListener?: () => void): void;
986
1013
  listen(handle: any, backlog?: number, listeningListener?: () => void): void;
987
1014
  listen(handle: any, listeningListener?: () => void): void;
988
- addRoute(route: Route, opts?: AddOpts): void;
989
1015
  Route: typeof Route;
990
- route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
991
- route(path: string, key?: string): Route<AppRouteContext<U>>;
992
- route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
993
- route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
994
- prompt(description: string): Route<AppRouteContext<U>>;
995
- prompt(description: Function): Route<AppRouteContext<U>>;
996
- call(message: {
997
- id?: string;
998
- path?: string;
999
- key?: string;
1000
- payload?: any;
1001
- }, ctx?: AppRouteContext<U> & {
1002
- [key: string]: any;
1003
- }): Promise<any>;
1004
- run(msg: {
1005
- id?: string;
1006
- path?: string;
1007
- key?: string;
1008
- payload?: any;
1009
- }, ctx?: Partial<AppRouteContext<U>> & {
1010
- [key: string]: any;
1011
- }): Promise<{
1012
- code: any;
1013
- data: any;
1014
- message: any;
1015
- }>;
1016
1016
  static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
1017
1017
  cookies: Record<string, string>;
1018
1018
  token: string;
package/dist/router.js CHANGED
@@ -17052,6 +17052,7 @@ class QueryRouter {
17052
17052
  const maxNextRoute = this.maxNextRoute;
17053
17053
  ctx = ctx || {};
17054
17054
  ctx.currentPath = path;
17055
+ ctx.currentId = route?.id;
17055
17056
  ctx.currentKey = key;
17056
17057
  ctx.currentRoute = route;
17057
17058
  ctx.index = (ctx.index || 0) + 1;
@@ -17065,7 +17066,7 @@ class QueryRouter {
17065
17066
  ctx.code = 500;
17066
17067
  ctx.message = "Too many nextRoute";
17067
17068
  ctx.body = null;
17068
- return;
17069
+ return ctx;
17069
17070
  }
17070
17071
  if (route && route.middleware && route.middleware.length > 0) {
17071
17072
  const errorMiddleware = [];
@@ -17140,7 +17141,9 @@ class QueryRouter {
17140
17141
  }
17141
17142
  return ctx;
17142
17143
  }
17143
- if (ctx.end) {}
17144
+ if (ctx.end) {
17145
+ return ctx;
17146
+ }
17144
17147
  }
17145
17148
  }
17146
17149
  }
@@ -17334,7 +17337,7 @@ class QueryRouter {
17334
17337
  description: "列出当前应用下的所有的路由信息",
17335
17338
  middleware: opts?.middleware || [],
17336
17339
  run: async (ctx) => {
17337
- const tokenUser = ctx.state.tokenUser;
17340
+ const tokenUser = ctx.state;
17338
17341
  let isUser = !!tokenUser;
17339
17342
  const list = this.getList(opts?.filter).filter((item) => {
17340
17343
  if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
@@ -17369,7 +17372,10 @@ class QueryRouterServer extends QueryRouter {
17369
17372
  handle;
17370
17373
  constructor(opts) {
17371
17374
  super();
17372
- this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
17375
+ const initHandle = opts?.initHandle ?? true;
17376
+ if (initHandle || opts?.handleFn) {
17377
+ this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
17378
+ }
17373
17379
  this.setContext({ needSerialize: false, ...opts?.context });
17374
17380
  if (opts?.appId) {
17375
17381
  this.appId = opts.appId;
@@ -17400,15 +17406,8 @@ class QueryRouterServer extends QueryRouter {
17400
17406
  }
17401
17407
  return new Route(path, key, opts);
17402
17408
  }
17403
- prompt(...args) {
17404
- const [desc] = args;
17405
- let description = "";
17406
- if (typeof desc === "string") {
17407
- description = desc;
17408
- } else if (typeof desc === "function") {
17409
- description = desc() || "";
17410
- }
17411
- return new Route("", "", { description });
17409
+ prompt(description) {
17410
+ return new Route(undefined, undefined, { description });
17412
17411
  }
17413
17412
  async run(msg, ctx) {
17414
17413
  const handle = this.handle;
@@ -18484,11 +18483,11 @@ class BunServer extends ServerBase {
18484
18483
  }
18485
18484
  }
18486
18485
  // src/app.ts
18487
- class App extends QueryRouter {
18486
+ class App extends QueryRouterServer {
18488
18487
  router;
18489
18488
  server;
18490
18489
  constructor(opts) {
18491
- super();
18490
+ super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } });
18492
18491
  const router = this;
18493
18492
  let server = opts?.server;
18494
18493
  if (!server) {
@@ -18500,7 +18499,6 @@ class App extends QueryRouter {
18500
18499
  }
18501
18500
  }
18502
18501
  server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
18503
- router.setContext({ needSerialize: true, ...opts?.routerContext });
18504
18502
  this.router = router;
18505
18503
  this.server = server;
18506
18504
  if (opts?.appId) {
@@ -18513,42 +18511,7 @@ class App extends QueryRouter {
18513
18511
  listen(...args) {
18514
18512
  this.server.listen(...args);
18515
18513
  }
18516
- addRoute(route, opts) {
18517
- super.add(route, opts);
18518
- }
18519
18514
  Route = Route;
18520
- route(...args) {
18521
- const [path, key, opts] = args;
18522
- if (typeof path === "object") {
18523
- return new Route(path.path, path.key, path);
18524
- }
18525
- if (typeof path === "string") {
18526
- if (opts) {
18527
- return new Route(path, key, opts);
18528
- }
18529
- if (key && typeof key === "object") {
18530
- return new Route(path, key?.key || "", key);
18531
- }
18532
- return new Route(path, key);
18533
- }
18534
- return new Route(path, key, opts);
18535
- }
18536
- prompt(...args) {
18537
- const [desc] = args;
18538
- let description = "";
18539
- if (typeof desc === "string") {
18540
- description = desc;
18541
- } else if (typeof desc === "function") {
18542
- description = desc() || "";
18543
- }
18544
- return new Route("", "", { description });
18545
- }
18546
- async call(message, ctx) {
18547
- return await super.call(message, ctx);
18548
- }
18549
- async run(msg, ctx) {
18550
- return await super.run(msg, ctx);
18551
- }
18552
18515
  static handleRequest(req, res) {
18553
18516
  return handleServer(req, res);
18554
18517
  }