@kevisual/router 0.1.6 → 0.2.2

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/src/route.ts CHANGED
@@ -78,18 +78,18 @@ export type RouteContext<T = { code?: number }, U extends SimpleObject = {}, S =
78
78
  export type SimpleObject = Record<string, any>;
79
79
  export type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
80
80
  export type RunMessage = { path?: string; key?: string; id?: string; payload?: any; };
81
- export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
81
+ export type NextRoute = Pick<Route, 'rid' | 'path' | 'key'>;
82
82
  export type RouteMiddleware =
83
83
  | {
84
84
  path?: string;
85
85
  key?: string;
86
- id?: string;
86
+ rid?: string;
87
87
  }
88
88
  | string;
89
89
  export type RouteOpts<U = {}, T = SimpleObject> = {
90
90
  path?: string;
91
91
  key?: string;
92
- id?: string;
92
+ rid?: string;
93
93
  run?: Run<U>;
94
94
  nextRoute?: NextRoute; // route to run after this route
95
95
  description?: string;
@@ -99,7 +99,7 @@ export type RouteOpts<U = {}, T = SimpleObject> = {
99
99
  isDebug?: boolean;
100
100
  };
101
101
  export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
102
- const pickValue = ['path', 'key', 'id', 'description', 'type', 'middleware', 'metadata'] as const;
102
+ const pickValue = ['path', 'key', 'rid', 'description', 'type', 'middleware', 'metadata'] as const;
103
103
 
104
104
 
105
105
  export type Skill<T = SimpleObject> = {
@@ -143,7 +143,7 @@ export class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject
143
143
  * 二级路径
144
144
  */
145
145
  key?: string;
146
- id?: string;
146
+ rid?: string;
147
147
  run?: Run<BuildRouteContext<M, U>>;
148
148
  nextRoute?: NextRoute; // route to run after this route
149
149
  description?: string;
@@ -164,7 +164,7 @@ export class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject
164
164
  this.key = key;
165
165
  const pathKey = `${path}$$${key}`;
166
166
  if (opts) {
167
- this.id = opts.id || hashIdMd5Sync(pathKey);
167
+ this.rid = opts.rid || hashIdMd5Sync(pathKey);
168
168
  this.run = opts.run as Run<BuildRouteContext<M, U>>;
169
169
  this.nextRoute = opts.nextRoute;
170
170
  this.description = opts.description;
@@ -176,8 +176,8 @@ export class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject
176
176
  } else {
177
177
  this.middleware = [];
178
178
  }
179
- if (!this.id) {
180
- this.id = hashIdMd5Sync(pathKey);
179
+ if (!this.rid) {
180
+ this.rid = hashIdMd5Sync(pathKey);
181
181
  }
182
182
  this.isDebug = opts?.isDebug ?? false;
183
183
  }
@@ -313,7 +313,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
313
313
  * @param uniqueId
314
314
  */
315
315
  removeById(uniqueId: string) {
316
- this.routes = this.routes.filter((r) => r.id !== uniqueId);
316
+ this.routes = this.routes.filter((r) => r.rid !== uniqueId);
317
317
  }
318
318
  /**
319
319
  * 执行route
@@ -327,7 +327,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
327
327
  const maxNextRoute = this.maxNextRoute;
328
328
  ctx = (ctx || {}) as RouteContext<T>;
329
329
  ctx.currentPath = path;
330
- ctx.currentId = route?.id;
330
+ ctx.currentId = route?.rid;
331
331
  ctx.currentKey = key;
332
332
  ctx.currentRoute = route;
333
333
  ctx.index = (ctx.index || 0) + 1;
@@ -354,11 +354,11 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
354
354
  let route: Route | undefined;
355
355
  const isString = typeof item === 'string';
356
356
  if (isString) {
357
- route = this.routes.find((r) => r.id === item);
357
+ route = this.routes.find((r) => r.rid === item);
358
358
  } else {
359
359
  route = this.routes.find((r) => {
360
- if (item.id) {
361
- return r.id === item.id;
360
+ if (item.rid) {
361
+ return r.rid === item.rid;
362
362
  } else {
363
363
  // key 可以是空,所以可以不严格验证
364
364
  return r.path === item.path && r.key == item.key;
@@ -408,8 +408,8 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
408
408
  ctx.message = e.message;
409
409
  ctx.body = null;
410
410
  } else {
411
- console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
412
- console.error(`[router error] middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
411
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.rid}`);
412
+ console.error(`[router error] middleware:${middleware.path}-${middleware.key}:${middleware.rid}`);
413
413
  console.error(e)
414
414
  ctx.code = 500;
415
415
  ctx.message = 'Internal Server Error';
@@ -438,7 +438,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
438
438
  ctx.code = e.code;
439
439
  ctx.message = e.message;
440
440
  } else {
441
- console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
441
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.rid}`);
442
442
  console.error(`[router error] error`, e);
443
443
  ctx.code = 500;
444
444
  ctx.message = 'Internal Server Error';
@@ -455,8 +455,8 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
455
455
  if (route.nextRoute.path || route.nextRoute.key) {
456
456
  path = route.nextRoute.path;
457
457
  key = route.nextRoute.key;
458
- } else if (route.nextRoute.id) {
459
- const nextRoute = this.routes.find((r) => r.id === route.nextRoute.id);
458
+ } else if (route.nextRoute.rid) {
459
+ const nextRoute = this.routes.find((r) => r.rid === route.nextRoute.rid);
460
460
  if (nextRoute) {
461
461
  path = nextRoute.path;
462
462
  key = nextRoute.key;
@@ -529,14 +529,14 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
529
529
  * @param ctx
530
530
  * @returns
531
531
  */
532
- async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
532
+ async call(message: { rid?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
533
533
  let path = message.path;
534
534
  let key = message.key;
535
535
  // 优先 path + key
536
536
  if (path) {
537
537
  return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
538
- } else if (message.id) {
539
- const route = this.routes.find((r) => r.id === message.id);
538
+ } else if (message.rid) {
539
+ const route = this.routes.find((r) => r.rid === message.rid);
540
540
  if (route) {
541
541
  path = route.path;
542
542
  key = route.key;
@@ -630,11 +630,11 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
630
630
  hasRoute(path: string, key: string = '') {
631
631
  return this.routes.find((r) => r.path === path && r.key === key);
632
632
  }
633
- findRoute(opts?: { path?: string; key?: string; id?: string }) {
634
- const { path, key, id } = opts || {};
633
+ findRoute(opts?: { path?: string; key?: string; rid?: string }) {
634
+ const { path, key, rid } = opts || {};
635
635
  return this.routes.find((r) => {
636
- if (id) {
637
- return r.id === id;
636
+ if (rid) {
637
+ return r.rid === rid;
638
638
  }
639
639
  if (path) {
640
640
  if (key !== undefined) {
@@ -655,14 +655,14 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
655
655
  const tokenUser = ctx.state as unknown as { tokenUser?: any };
656
656
  let isUser = !!tokenUser;
657
657
  const list = this.getList(opts?.filter).filter((item) => {
658
- if (item.id === 'auth' || item.id === 'auth-can' || item.id === 'check-auth-admin' || item.id === 'auth-admin') {
658
+ if (item.rid === 'auth' || item.rid === 'auth-can' || item.rid === 'check-auth-admin' || item.rid === 'auth-admin') {
659
659
  return false;
660
660
  }
661
661
  return true;
662
662
  });
663
663
  ctx.body = {
664
664
  list: list.map((item) => {
665
- const route = pick(item, ['id', 'path', 'key', 'description', 'middleware', 'metadata'] as const);
665
+ const route = pick(item, ['rid', 'path', 'key', 'description', 'middleware', 'metadata'] as const);
666
666
  return toJSONSchemaRoute(route);
667
667
  }),
668
668
  isUser
@@ -766,7 +766,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
766
766
  * @param param0
767
767
  * @returns
768
768
  */
769
- async run(msg: { id?: string; path?: string; key?: string; payload?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
769
+ async run(msg: { rid?: string; path?: string; key?: string; payload?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
770
770
  const handle = this.handle;
771
771
  if (handle) {
772
772
  return handle(msg, ctx);
@@ -774,13 +774,13 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
774
774
  return super.run(msg, ctx as RouteContext<C>);
775
775
  }
776
776
 
777
- async runAction<T extends { id?: string; path?: string; key?: string; metadata?: { args?: any } } = {}>(
777
+ async runAction<T extends { rid?: string; path?: string; key?: string; metadata?: { args?: any } } = {}>(
778
778
  api: T,
779
779
  payload: RunActionPayload<T>,
780
780
  ctx?: RouteContext<C>
781
781
  ) {
782
- const { path, key, id } = api as any;
783
- return this.run({ path, key, id, payload }, ctx);
782
+ const { path, key, rid } = api as any;
783
+ return this.run({ path, key, rid, payload }, ctx);
784
784
  }
785
785
  /**
786
786
  * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
@@ -790,7 +790,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
790
790
  this.route({
791
791
  path: 'auth',
792
792
  key: 'auth',
793
- id: 'auth',
793
+ rid: 'auth',
794
794
  description: 'token验证',
795
795
  }).define(async (ctx) => {
796
796
  if (fun) {
@@ -801,7 +801,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
801
801
  this.route({
802
802
  path: 'auth-admin',
803
803
  key: 'auth-admin',
804
- id: 'auth-admin',
804
+ rid: 'auth-admin',
805
805
  description: 'admin token验证',
806
806
  middleware: ['auth']
807
807
  }).define(async (ctx) => {
@@ -813,7 +813,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
813
813
  this.route({
814
814
  path: 'auth-can',
815
815
  key: 'auth-can',
816
- id: 'auth-can',
816
+ rid: 'auth-can',
817
817
  description: '权限验证'
818
818
  }).define(async (ctx) => {
819
819
  if (fun) {
@@ -53,8 +53,8 @@ class Chain {
53
53
  this.object.key = key;
54
54
  return this;
55
55
  }
56
- setId(key: string) {
57
- this.object.id = key;
56
+ setRid(key: string) {
57
+ this.object.rid = key;
58
58
  return this;
59
59
  }
60
60
  setRun<U extends SimpleObject = {}>(run: Run<U>) {