@kevisual/router 0.0.83 → 0.0.85

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.
@@ -30,9 +30,20 @@ type RouterContextT = {
30
30
  code?: number;
31
31
  [key: string]: any;
32
32
  };
33
+ type BuildRouteContext<M, U> = M extends {
34
+ args?: infer A;
35
+ } ? A extends z.ZodObject<any> ? RouteContext<{
36
+ args?: z.infer<A>;
37
+ }, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
38
+ args?: {
39
+ [K in keyof A]: z.infer<A[K]>;
40
+ };
41
+ }, U> : RouteContext<U> : RouteContext<U>;
33
42
  type RouteContext<T = {
34
43
  code?: number;
35
- }, S = any> = {
44
+ }, U extends SimpleObject$1 = {}, S = {
45
+ [key: string]: any;
46
+ }> = {
36
47
  /**
37
48
  * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
38
49
  * 或者不需要登录的,直接调用
@@ -55,7 +66,14 @@ type RouteContext<T = {
55
66
  code?: number;
56
67
  /** return msg */
57
68
  message?: string;
69
+ /**
70
+ * 传递状态
71
+ */
58
72
  state?: S;
73
+ /**
74
+ * 当前routerId
75
+ */
76
+ currentId?: string;
59
77
  /**
60
78
  * 当前路径
61
79
  */
@@ -96,14 +114,12 @@ type RouteContext<T = {
96
114
  path: string;
97
115
  key?: string;
98
116
  payload?: any;
99
- }, ctx?: RouteContext & {
100
- [key: string]: any;
101
- }) => Promise<any>;
117
+ }, ctx?: RouteContext) => Promise<any>;
102
118
  index?: number;
103
119
  throw?: throwError['throw'];
104
120
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
105
121
  needSerialize?: boolean;
106
- } & T;
122
+ } & T & U;
107
123
  type SimpleObject$1 = Record<string, any>;
108
124
  type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
109
125
  type RunMessage = {
@@ -114,7 +130,7 @@ type RunMessage = {
114
130
  };
115
131
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
116
132
  type RouteMiddleware = {
117
- path: string;
133
+ path?: string;
118
134
  key?: string;
119
135
  id?: string;
120
136
  } | string;
@@ -127,7 +143,7 @@ type RouteOpts<U = {}, T = SimpleObject$1> = {
127
143
  description?: string;
128
144
  metadata?: T;
129
145
  middleware?: RouteMiddleware[];
130
- type?: 'route' | 'middleware';
146
+ type?: 'route' | 'middleware' | 'compound';
131
147
  /**
132
148
  * $#$ will be used to split path and key
133
149
  */
@@ -141,9 +157,11 @@ type RouteOpts<U = {}, T = SimpleObject$1> = {
141
157
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
142
158
  declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
143
159
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
144
- declare class Route<U = {
145
- [key: string]: any;
146
- }, T extends SimpleObject$1 = SimpleObject$1> implements throwError {
160
+ /**
161
+ * @M 是 route的 metadate的类型,默认是 SimpleObject
162
+ * @U RouteContext state的类型
163
+ */
164
+ declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
147
165
  /**
148
166
  * 一级路径
149
167
  */
@@ -153,10 +171,10 @@ declare class Route<U = {
153
171
  */
154
172
  key?: string;
155
173
  id?: string;
156
- run?: Run;
174
+ run?: Run<BuildRouteContext<M, U>>;
157
175
  nextRoute?: NextRoute;
158
176
  description?: string;
159
- metadata?: T;
177
+ metadata?: M;
160
178
  middleware?: RouteMiddleware[];
161
179
  type?: string;
162
180
  /**
@@ -171,13 +189,13 @@ declare class Route<U = {
171
189
  } = RouterContextT>(opts: DefineRouteOpts): this;
172
190
  define<T extends {
173
191
  [key: string]: any;
174
- } = RouterContextT>(fn: Run<T & U>): this;
192
+ } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
175
193
  define<T extends {
176
194
  [key: string]: any;
177
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
195
+ } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
178
196
  define<T extends {
179
197
  [key: string]: any;
180
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
198
+ } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
181
199
  update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
182
200
  addTo(router: QueryRouter | {
183
201
  add: (route: Route) => void;
@@ -191,11 +209,11 @@ declare class Route<U = {
191
209
  type AddOpts = {
192
210
  overwrite?: boolean;
193
211
  };
194
- declare class QueryRouter implements throwError {
212
+ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
195
213
  appId: string;
196
214
  routes: Route[];
197
215
  maxNextRoute: number;
198
- context?: RouteContext;
216
+ context?: RouteContext<T>;
199
217
  constructor();
200
218
  /**
201
219
  * add route
@@ -223,7 +241,7 @@ declare class QueryRouter implements throwError {
223
241
  * @param ctx
224
242
  * @returns
225
243
  */
226
- runRoute(path: string, key: string, ctx?: RouteContext): any;
244
+ runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
227
245
  /**
228
246
  * 第一次执行
229
247
  * @param message
@@ -234,9 +252,11 @@ declare class QueryRouter implements throwError {
234
252
  path: string;
235
253
  key?: string;
236
254
  payload?: any;
237
- }, ctx?: RouteContext & {
255
+ }, ctx?: RouteContext<T> & {
256
+ [key: string]: any;
257
+ }): Promise<RouteContext<T, {}, {
238
258
  [key: string]: any;
239
- }): Promise<any>;
259
+ }>>;
240
260
  /**
241
261
  * 返回的数据包含所有的context的请求返回的内容,可做其他处理
242
262
  * @param message
@@ -248,9 +268,15 @@ declare class QueryRouter implements throwError {
248
268
  path?: string;
249
269
  key?: string;
250
270
  payload?: any;
251
- }, ctx?: RouteContext & {
271
+ }, ctx?: RouteContext<T> & {
272
+ [key: string]: any;
273
+ }): Promise<RouteContext<T, {}, {
252
274
  [key: string]: any;
253
- }): Promise<any>;
275
+ }> | {
276
+ code: number;
277
+ body: any;
278
+ message: string;
279
+ }>;
254
280
  /**
255
281
  * 请求 result 的数据
256
282
  * @param message
@@ -266,9 +292,9 @@ declare class QueryRouter implements throwError {
266
292
  }, ctx?: RouteContext & {
267
293
  [key: string]: any;
268
294
  }): Promise<{
269
- code: any;
295
+ code: number;
270
296
  data: any;
271
- message: any;
297
+ message: string;
272
298
  }>;
273
299
  /**
274
300
  * Router Run获取数据
@@ -281,12 +307,12 @@ declare class QueryRouter implements throwError {
281
307
  path?: string;
282
308
  key?: string;
283
309
  payload?: any;
284
- }, ctx?: RouteContext & {
310
+ }, ctx?: RouteContext<T> & {
285
311
  [key: string]: any;
286
312
  }): Promise<{
287
- code: any;
313
+ code: number;
288
314
  data: any;
289
- message: any;
315
+ message: string;
290
316
  }>;
291
317
  /**
292
318
  * 设置上下文
@@ -298,12 +324,12 @@ declare class QueryRouter implements throwError {
298
324
  /**
299
325
  * 获取handle函数, 这里会去执行parse函数
300
326
  */
301
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
327
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
302
328
  id?: string;
303
329
  path?: string;
304
330
  key?: string;
305
331
  [key: string]: any;
306
- }, handleContext?: RouteContext) => Promise<{
332
+ }, handleContext?: RouteContext<T>) => Promise<{
307
333
  [key: string]: any;
308
334
  code: string;
309
335
  data?: any;
@@ -317,22 +343,16 @@ declare class QueryRouter implements throwError {
317
343
  message: any;
318
344
  data?: undefined;
319
345
  }>;
320
- exportRoutes(): Route<{
321
- [key: string]: any;
322
- }, SimpleObject$1>[];
346
+ exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
323
347
  importRoutes(routes: Route[]): void;
324
348
  importRouter(router: QueryRouter): void;
325
349
  throw(...args: any[]): void;
326
- hasRoute(path: string, key?: string): Route<{
327
- [key: string]: any;
328
- }, SimpleObject$1>;
350
+ hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
329
351
  findRoute(opts?: {
330
352
  path?: string;
331
353
  key?: string;
332
354
  id?: string;
333
- }): Route<{
334
- [key: string]: any;
335
- }, SimpleObject$1>;
355
+ }): Route<SimpleObject$1, SimpleObject$1>;
336
356
  createRouteList(opts?: {
337
357
  force?: boolean;
338
358
  filter?: (route: Route) => boolean;
@@ -374,10 +394,11 @@ declare class QueryRouter implements throwError {
374
394
  [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
375
395
  };
376
396
  }
377
- type QueryRouterServerOpts = {
397
+ type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
378
398
  handleFn?: HandleFn;
379
- context?: RouteContext;
399
+ context?: RouteContext<C>;
380
400
  appId?: string;
401
+ initHandle?: boolean;
381
402
  };
382
403
  interface HandleFn<T = any> {
383
404
  (msg: {
@@ -394,20 +415,27 @@ interface HandleFn<T = any> {
394
415
  /**
395
416
  * QueryRouterServer
396
417
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
418
+ * @template C 自定义 RouteContext 类型
397
419
  */
398
- declare class QueryRouterServer extends QueryRouter {
420
+ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
399
421
  appId: string;
400
422
  handle: any;
401
- constructor(opts?: QueryRouterServerOpts);
423
+ context: RouteContext<C>;
424
+ constructor(opts?: QueryRouterServerOpts<C>);
402
425
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
403
426
  addRoute(route: Route, opts?: AddOpts): void;
404
427
  Route: typeof Route;
405
- route(opts: RouteOpts): Route<Required<RouteContext>>;
406
- route(path: string, key?: string): Route<Required<RouteContext>>;
407
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
408
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
409
- prompt(description: string): Route<Required<RouteContext>>;
410
- prompt(description: Function): Route<Required<RouteContext>>;
428
+ route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
429
+ metadata?: M;
430
+ }): Route<M, Required<RouteContext<C>>>;
431
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
432
+ metadata?: M;
433
+ }): Route<M, Required<RouteContext<C>>>;
434
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
435
+ route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
436
+ metadata?: M;
437
+ }): Route<M, Required<RouteContext<C>>>;
438
+ prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
411
439
  /**
412
440
  * 调用了handle
413
441
  * @param param0
@@ -418,9 +446,7 @@ declare class QueryRouterServer extends QueryRouter {
418
446
  path?: string;
419
447
  key?: string;
420
448
  payload?: any;
421
- }, ctx?: RouteContext & {
422
- [key: string]: any;
423
- }): Promise<any>;
449
+ }, ctx?: Partial<RouteContext<C>>): Promise<any>;
424
450
  }
425
451
 
426
452
  type RouteObject = {