@kevisual/router 0.0.82 → 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.
@@ -26,12 +26,20 @@ declare class CustomError extends Error {
26
26
  * @returns
27
27
  */
28
28
  static isError(error: unknown): error is CustomError;
29
+ static throw(code?: number | string, message?: string): void;
30
+ static throw(code?: number | string, opts?: CustomErrorOptions): void;
31
+ static throw(opts?: CustomErrorOptions): void;
29
32
  parse(e?: CustomError): {
30
33
  code: number;
31
34
  data: any;
32
35
  message: string;
33
36
  };
34
37
  }
38
+ interface throwError {
39
+ throw(code?: number | string, message?: string): void;
40
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
41
+ throw(opts?: CustomErrorOptions): void;
42
+ }
35
43
 
36
44
  declare class MockProcess {
37
45
  emitter?: EventEmitter;
@@ -66,9 +74,20 @@ type RouterContextT = {
66
74
  code?: number;
67
75
  [key: string]: any;
68
76
  };
77
+ type BuildRouteContext<M, U> = M extends {
78
+ args?: infer A;
79
+ } ? A extends z.ZodObject<any> ? RouteContext<{
80
+ args?: z.infer<A>;
81
+ }, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
82
+ args?: {
83
+ [K in keyof A]: z.infer<A[K]>;
84
+ };
85
+ }, U> : RouteContext<U> : RouteContext<U>;
69
86
  type RouteContext<T = {
70
87
  code?: number;
71
- }, S = any> = {
88
+ }, U extends SimpleObject$1 = {}, S = {
89
+ [key: string]: any;
90
+ }> = {
72
91
  /**
73
92
  * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
74
93
  * 或者不需要登录的,直接调用
@@ -91,7 +110,14 @@ type RouteContext<T = {
91
110
  code?: number;
92
111
  /** return msg */
93
112
  message?: string;
113
+ /**
114
+ * 传递状态
115
+ */
94
116
  state?: S;
117
+ /**
118
+ * 当前routerId
119
+ */
120
+ currentId?: string;
95
121
  /**
96
122
  * 当前路径
97
123
  */
@@ -132,14 +158,12 @@ type RouteContext<T = {
132
158
  path: string;
133
159
  key?: string;
134
160
  payload?: any;
135
- }, ctx?: RouteContext & {
136
- [key: string]: any;
137
- }) => Promise<any>;
161
+ }, ctx?: RouteContext) => Promise<any>;
138
162
  index?: number;
139
- throw?: (code?: number | string, message?: string, tips?: string) => void;
163
+ throw?: throwError['throw'];
140
164
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
141
165
  needSerialize?: boolean;
142
- } & T;
166
+ } & T & U;
143
167
  type SimpleObject$1 = Record<string, any>;
144
168
  type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
145
169
  type RunMessage = {
@@ -150,7 +174,7 @@ type RunMessage = {
150
174
  };
151
175
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
152
176
  type RouteMiddleware = {
153
- path: string;
177
+ path?: string;
154
178
  key?: string;
155
179
  id?: string;
156
180
  } | string;
@@ -191,9 +215,11 @@ declare const tool: {
191
215
  /** */
192
216
  declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
193
217
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
194
- declare class Route<U = {
195
- [key: string]: any;
196
- }, T extends SimpleObject$1 = SimpleObject$1> {
218
+ /**
219
+ * @M 是 route的 metadate的类型,默认是 SimpleObject
220
+ * @U RouteContext state的类型
221
+ */
222
+ declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
197
223
  /**
198
224
  * 一级路径
199
225
  */
@@ -203,10 +229,10 @@ declare class Route<U = {
203
229
  */
204
230
  key?: string;
205
231
  id?: string;
206
- run?: Run;
232
+ run?: Run<BuildRouteContext<M, U>>;
207
233
  nextRoute?: NextRoute;
208
234
  description?: string;
209
- metadata?: T;
235
+ metadata?: M;
210
236
  middleware?: RouteMiddleware[];
211
237
  type?: string;
212
238
  /**
@@ -221,19 +247,19 @@ declare class Route<U = {
221
247
  } = RouterContextT>(opts: DefineRouteOpts): this;
222
248
  define<T extends {
223
249
  [key: string]: any;
224
- } = RouterContextT>(fn: Run<T & U>): this;
250
+ } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
225
251
  define<T extends {
226
252
  [key: string]: any;
227
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
253
+ } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
228
254
  define<T extends {
229
255
  [key: string]: any;
230
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
256
+ } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
231
257
  update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
232
258
  addTo(router: QueryRouter | {
233
259
  add: (route: Route) => void;
234
260
  [key: string]: any;
235
261
  }, opts?: AddOpts): void;
236
- throw(code?: number | string, message?: string, tips?: string): void;
262
+ throw(...args: any[]): void;
237
263
  }
238
264
  declare const toJSONSchema: (args: any, opts?: {
239
265
  mergeObject?: boolean;
@@ -258,11 +284,11 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
258
284
  type AddOpts = {
259
285
  overwrite?: boolean;
260
286
  };
261
- declare class QueryRouter {
287
+ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
262
288
  appId: string;
263
289
  routes: Route[];
264
290
  maxNextRoute: number;
265
- context?: RouteContext;
291
+ context?: RouteContext<T>;
266
292
  constructor();
267
293
  /**
268
294
  * add route
@@ -290,7 +316,7 @@ declare class QueryRouter {
290
316
  * @param ctx
291
317
  * @returns
292
318
  */
293
- runRoute(path: string, key: string, ctx?: RouteContext): any;
319
+ runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
294
320
  /**
295
321
  * 第一次执行
296
322
  * @param message
@@ -301,9 +327,11 @@ declare class QueryRouter {
301
327
  path: string;
302
328
  key?: string;
303
329
  payload?: any;
304
- }, ctx?: RouteContext & {
330
+ }, ctx?: RouteContext<T> & {
331
+ [key: string]: any;
332
+ }): Promise<RouteContext<T, {}, {
305
333
  [key: string]: any;
306
- }): Promise<any>;
334
+ }>>;
307
335
  /**
308
336
  * 返回的数据包含所有的context的请求返回的内容,可做其他处理
309
337
  * @param message
@@ -315,9 +343,15 @@ declare class QueryRouter {
315
343
  path?: string;
316
344
  key?: string;
317
345
  payload?: any;
318
- }, ctx?: RouteContext & {
346
+ }, ctx?: RouteContext<T> & {
347
+ [key: string]: any;
348
+ }): Promise<RouteContext<T, {}, {
319
349
  [key: string]: any;
320
- }): Promise<any>;
350
+ }> | {
351
+ code: number;
352
+ body: any;
353
+ message: string;
354
+ }>;
321
355
  /**
322
356
  * 请求 result 的数据
323
357
  * @param message
@@ -333,9 +367,9 @@ declare class QueryRouter {
333
367
  }, ctx?: RouteContext & {
334
368
  [key: string]: any;
335
369
  }): Promise<{
336
- code: any;
370
+ code: number;
337
371
  data: any;
338
- message: any;
372
+ message: string;
339
373
  }>;
340
374
  /**
341
375
  * Router Run获取数据
@@ -348,12 +382,12 @@ declare class QueryRouter {
348
382
  path?: string;
349
383
  key?: string;
350
384
  payload?: any;
351
- }, ctx?: RouteContext & {
385
+ }, ctx?: RouteContext<T> & {
352
386
  [key: string]: any;
353
387
  }): Promise<{
354
- code: any;
388
+ code: number;
355
389
  data: any;
356
- message: any;
390
+ message: string;
357
391
  }>;
358
392
  /**
359
393
  * 设置上下文
@@ -365,12 +399,12 @@ declare class QueryRouter {
365
399
  /**
366
400
  * 获取handle函数, 这里会去执行parse函数
367
401
  */
368
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
402
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
369
403
  id?: string;
370
404
  path?: string;
371
405
  key?: string;
372
406
  [key: string]: any;
373
- }, handleContext?: RouteContext) => Promise<{
407
+ }, handleContext?: RouteContext<T>) => Promise<{
374
408
  [key: string]: any;
375
409
  code: string;
376
410
  data?: any;
@@ -384,24 +418,16 @@ declare class QueryRouter {
384
418
  message: any;
385
419
  data?: undefined;
386
420
  }>;
387
- exportRoutes(): Route<{
388
- [key: string]: any;
389
- }, SimpleObject$1>[];
421
+ exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
390
422
  importRoutes(routes: Route[]): void;
391
423
  importRouter(router: QueryRouter): void;
392
- throw(code?: number | string, message?: string): void;
393
- throw(code?: number | string, opts?: CustomErrorOptions): void;
394
- throw(opts?: CustomErrorOptions): void;
395
- hasRoute(path: string, key?: string): Route<{
396
- [key: string]: any;
397
- }, SimpleObject$1>;
424
+ throw(...args: any[]): void;
425
+ hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
398
426
  findRoute(opts?: {
399
427
  path?: string;
400
428
  key?: string;
401
429
  id?: string;
402
- }): Route<{
403
- [key: string]: any;
404
- }, SimpleObject$1>;
430
+ }): Route<SimpleObject$1, SimpleObject$1>;
405
431
  createRouteList(opts?: {
406
432
  force?: boolean;
407
433
  filter?: (route: Route) => boolean;
@@ -443,10 +469,11 @@ declare class QueryRouter {
443
469
  [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
444
470
  };
445
471
  }
446
- type QueryRouterServerOpts = {
472
+ type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
447
473
  handleFn?: HandleFn;
448
- context?: RouteContext;
474
+ context?: RouteContext<C>;
449
475
  appId?: string;
476
+ initHandle?: boolean;
450
477
  };
451
478
  interface HandleFn<T = any> {
452
479
  (msg: {
@@ -463,20 +490,27 @@ interface HandleFn<T = any> {
463
490
  /**
464
491
  * QueryRouterServer
465
492
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
493
+ * @template C 自定义 RouteContext 类型
466
494
  */
467
- declare class QueryRouterServer extends QueryRouter {
495
+ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
468
496
  appId: string;
469
497
  handle: any;
470
- constructor(opts?: QueryRouterServerOpts);
498
+ context: RouteContext<C>;
499
+ constructor(opts?: QueryRouterServerOpts<C>);
471
500
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
472
501
  addRoute(route: Route, opts?: AddOpts): void;
473
502
  Route: typeof Route;
474
- route(opts: RouteOpts): Route<Required<RouteContext>>;
475
- route(path: string, key?: string): Route<Required<RouteContext>>;
476
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
477
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
478
- prompt(description: string): Route<Required<RouteContext>>;
479
- prompt(description: Function): Route<Required<RouteContext>>;
503
+ route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
504
+ metadata?: M;
505
+ }): Route<M, Required<RouteContext<C>>>;
506
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
507
+ metadata?: M;
508
+ }): Route<M, Required<RouteContext<C>>>;
509
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
510
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
511
+ metadata?: M;
512
+ }): Route<M, Required<RouteContext<C>>>;
513
+ prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
480
514
  /**
481
515
  * 调用了handle
482
516
  * @param param0
@@ -487,9 +521,7 @@ declare class QueryRouterServer extends QueryRouter {
487
521
  path?: string;
488
522
  key?: string;
489
523
  payload?: any;
490
- }, ctx?: RouteContext & {
491
- [key: string]: any;
492
- }): Promise<any>;
524
+ }, ctx?: Partial<RouteContext<C>>): Promise<any>;
493
525
  }
494
526
  declare class Mini extends QueryRouterServer {
495
527
  }
@@ -240,6 +240,18 @@ class CustomError extends Error {
240
240
  static isError(error) {
241
241
  return error instanceof CustomError || typeof error === "object" && error !== null && "code" in error;
242
242
  }
243
+ static throw(...args) {
244
+ const [args0, args1] = args;
245
+ if (args0 && typeof args0 === "object") {
246
+ throw new CustomError(args0);
247
+ }
248
+ if (args1 && typeof args1 === "object") {
249
+ throw new CustomError(args0, args1);
250
+ } else if (args1) {
251
+ throw new CustomError(args0, { message: args1 });
252
+ }
253
+ throw new CustomError(args0);
254
+ }
243
255
  parse(e) {
244
256
  if (e) {
245
257
  return CustomError.parseError(e);
@@ -14164,7 +14176,7 @@ class Route {
14164
14176
  router.add(this, opts);
14165
14177
  }
14166
14178
  throw(...args) {
14167
- throw new CustomError(...args);
14179
+ CustomError.throw(...args);
14168
14180
  }
14169
14181
  }
14170
14182
  var toJSONSchemaRoute = (route) => {
@@ -14207,6 +14219,7 @@ class QueryRouter {
14207
14219
  const maxNextRoute = this.maxNextRoute;
14208
14220
  ctx = ctx || {};
14209
14221
  ctx.currentPath = path;
14222
+ ctx.currentId = route?.id;
14210
14223
  ctx.currentKey = key;
14211
14224
  ctx.currentRoute = route;
14212
14225
  ctx.index = (ctx.index || 0) + 1;
@@ -14220,7 +14233,7 @@ class QueryRouter {
14220
14233
  ctx.code = 500;
14221
14234
  ctx.message = "Too many nextRoute";
14222
14235
  ctx.body = null;
14223
- return;
14236
+ return ctx;
14224
14237
  }
14225
14238
  if (route && route.middleware && route.middleware.length > 0) {
14226
14239
  const errorMiddleware = [];
@@ -14295,7 +14308,9 @@ class QueryRouter {
14295
14308
  }
14296
14309
  return ctx;
14297
14310
  }
14298
- if (ctx.end) {}
14311
+ if (ctx.end) {
14312
+ return ctx;
14313
+ }
14299
14314
  }
14300
14315
  }
14301
14316
  }
@@ -14462,16 +14477,7 @@ class QueryRouter {
14462
14477
  this.importRoutes(router.routes);
14463
14478
  }
14464
14479
  throw(...args) {
14465
- const [args0, args1] = args;
14466
- if (args0 && typeof args0 === "object") {
14467
- throw new CustomError(args0);
14468
- }
14469
- if (args1 && typeof args1 === "object") {
14470
- throw new CustomError(args0, args1);
14471
- } else if (args1) {
14472
- throw new CustomError(args0, { message: args1 });
14473
- }
14474
- throw new CustomError(args0);
14480
+ CustomError.throw(...args);
14475
14481
  }
14476
14482
  hasRoute(path, key = "") {
14477
14483
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -14498,7 +14504,7 @@ class QueryRouter {
14498
14504
  description: "列出当前应用下的所有的路由信息",
14499
14505
  middleware: opts?.middleware || [],
14500
14506
  run: async (ctx) => {
14501
- const tokenUser = ctx.state.tokenUser;
14507
+ const tokenUser = ctx.state;
14502
14508
  let isUser = !!tokenUser;
14503
14509
  const list = this.getList(opts?.filter).filter((item) => {
14504
14510
  if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
@@ -14533,7 +14539,10 @@ class QueryRouterServer extends QueryRouter {
14533
14539
  handle;
14534
14540
  constructor(opts) {
14535
14541
  super();
14536
- this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
14542
+ const initHandle = opts?.initHandle ?? true;
14543
+ if (initHandle || opts?.handleFn) {
14544
+ this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
14545
+ }
14537
14546
  this.setContext({ needSerialize: false, ...opts?.context });
14538
14547
  if (opts?.appId) {
14539
14548
  this.appId = opts.appId;
@@ -14564,15 +14573,8 @@ class QueryRouterServer extends QueryRouter {
14564
14573
  }
14565
14574
  return new Route(path, key, opts);
14566
14575
  }
14567
- prompt(...args) {
14568
- const [desc] = args;
14569
- let description = "";
14570
- if (typeof desc === "string") {
14571
- description = desc;
14572
- } else if (typeof desc === "function") {
14573
- description = desc() || "";
14574
- }
14575
- return new Route("", "", { description });
14576
+ prompt(description) {
14577
+ return new Route(undefined, undefined, { description });
14576
14578
  }
14577
14579
  async run(msg, ctx) {
14578
14580
  const handle = this.handle;