@kevisual/router 0.0.71 → 0.0.73

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/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.71",
4
+ "version": "0.0.73",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
8
8
  "types": "./dist/router.d.ts",
9
9
  "scripts": {
10
- "build": "npm run clean && rollup -c",
11
- "watch": "rollup -c -w",
10
+ "build": "npm run clean && bun bun.config.ts",
11
+ "watch": "bun bun.config.ts --watch",
12
12
  "clean": "rm -rf dist"
13
13
  },
14
14
  "files": [
@@ -21,36 +21,27 @@
21
21
  "keywords": [],
22
22
  "author": "abearxiong",
23
23
  "license": "MIT",
24
- "packageManager": "pnpm@10.28.2",
24
+ "packageManager": "pnpm@10.30.0",
25
25
  "devDependencies": {
26
- "@kevisual/context": "^0.0.4",
27
- "@kevisual/dts": "^0.0.3",
26
+ "@kevisual/code-builder": "^0.0.6",
27
+ "@kevisual/context": "^0.0.6",
28
+ "@kevisual/dts": "^0.0.4",
28
29
  "@kevisual/js-filter": "^0.0.5",
29
30
  "@kevisual/local-proxy": "^0.0.8",
30
- "@kevisual/query": "^0.0.39",
31
+ "@kevisual/query": "^0.0.43",
31
32
  "@kevisual/use-config": "^1.0.30",
32
- "@opencode-ai/plugin": "^1.1.49",
33
- "@rollup/plugin-alias": "^6.0.0",
34
- "@rollup/plugin-commonjs": "29.0.0",
35
- "@rollup/plugin-node-resolve": "^16.0.3",
36
- "@rollup/plugin-typescript": "^12.3.0",
37
- "@types/bun": "^1.3.8",
38
- "@types/node": "^25.2.0",
33
+ "@opencode-ai/plugin": "^1.2.6",
34
+ "@types/bun": "^1.3.9",
35
+ "@types/node": "^25.2.3",
39
36
  "@types/send": "^1.2.1",
40
37
  "@types/ws": "^8.18.1",
41
38
  "@types/xml2js": "^0.4.14",
42
39
  "eventemitter3": "^5.0.4",
43
40
  "fast-glob": "^3.3.3",
44
- "hono": "^4.11.7",
41
+ "hono": "^4.11.9",
45
42
  "nanoid": "^5.1.6",
46
43
  "path-to-regexp": "^8.3.0",
47
- "rollup": "^4.57.1",
48
- "rollup-plugin-dts": "^6.3.0",
49
44
  "send": "^1.2.1",
50
- "ts-loader": "^9.5.4",
51
- "ts-node": "^10.9.2",
52
- "tslib": "^2.8.1",
53
- "tsx": "^4.21.0",
54
45
  "typescript": "^5.9.3",
55
46
  "ws": "npm:@kevisual/ws",
56
47
  "xml2js": "^0.6.2",
@@ -67,41 +58,15 @@
67
58
  "access": "public"
68
59
  },
69
60
  "exports": {
70
- ".": {
71
- "import": "./dist/router.js",
72
- "require": "./dist/router.js",
73
- "types": "./dist/router.d.ts"
74
- },
75
- "./browser": {
76
- "import": "./dist/router-browser.js",
77
- "require": "./dist/router-browser.js",
78
- "types": "./dist/router-browser.d.ts"
79
- },
80
- "./simple": {
81
- "import": "./dist/router-simple.js",
82
- "require": "./dist/router-simple.js",
83
- "types": "./dist/router-simple.d.ts"
84
- },
61
+ ".": "./dist/router.js",
62
+ "./browser": "./dist/router-browser.js",
63
+ "./simple": "./dist/router-simple.js",
85
64
  "./opencode": "./dist/opencode.js",
86
65
  "./skill": "./dist/app.js",
87
- "./define": {
88
- "import": "./dist/router-define.js",
89
- "require": "./dist/router-define.js",
90
- "types": "./dist/router-define.d.ts"
91
- },
66
+ "./define": "./dist/router-define.js",
92
67
  "./ws": "./dist/ws.js",
93
- "./mod.ts": {
94
- "import": "./mod.ts",
95
- "require": "./mod.ts",
96
- "types": "./mod.d.ts"
97
- },
98
- "./src/*": {
99
- "import": "./src/*",
100
- "require": "./src/*"
101
- },
102
- "./modules/*": {
103
- "import": "./src/modules/*",
104
- "require": "./src/modules/*"
105
- }
68
+ "./mod.ts": "./mod.ts",
69
+ "./src/*": "./src/*",
70
+ "./modules/*": "./src/modules/*"
106
71
  }
107
72
  }
package/src/app.ts CHANGED
@@ -6,7 +6,7 @@ import { handleServer } from './server/handle-server.ts';
6
6
  import { IncomingMessage, ServerResponse } from 'http';
7
7
  import { isBun } from './utils/is-engine.ts';
8
8
  import { BunServer } from './server/server-bun.ts';
9
- import { nanoid } from 'nanoid';
9
+ import { randomId } from './utils/random.ts';
10
10
 
11
11
  type RouterHandle = (msg: { path: string;[key: string]: any }) => { code: string; data?: any; message?: string;[key: string]: any };
12
12
  type AppOptions<T = {}> = {
@@ -48,7 +48,7 @@ export class App<U = {}> extends QueryRouter {
48
48
  if (opts?.appId) {
49
49
  this.appId = opts.appId;
50
50
  } else {
51
- this.appId = nanoid(16);
51
+ this.appId = randomId(16, 'rand-');
52
52
  }
53
53
  router.appId = this.appId;
54
54
  }
package/src/browser.ts CHANGED
@@ -4,7 +4,7 @@ export type { Rule, Schema, } from './validator/index.ts';
4
4
 
5
5
  export { createSchema } from './validator/index.ts';
6
6
 
7
- export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
7
+ export type { RouteContext, RouteOpts, RouteInfo, RouteMiddleware } from './route.ts';
8
8
 
9
9
  export type { Run, Skill } from './route.ts';
10
10
 
package/src/index.ts CHANGED
@@ -4,7 +4,7 @@ export type { Rule, Schema, } from './validator/index.ts';
4
4
 
5
5
  export { createSchema } from './validator/index.ts';
6
6
 
7
- export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
7
+ export type { RouteContext, RouteOpts, RouteInfo, RouteMiddleware } from './route.ts';
8
8
 
9
9
  export type { Run, Skill } from './route.ts';
10
10
 
package/src/route.ts CHANGED
@@ -1,9 +1,8 @@
1
- import { nanoid } from 'nanoid';
2
1
  import { CustomError } from './result/error.ts';
3
2
  import { pick } from './utils/pick.ts';
4
3
  import { listenProcess, MockProcess } from './utils/listen-process.ts';
5
4
  import { z } from 'zod';
6
-
5
+ import { randomId } from './utils/random.ts';
7
6
  export type RouterContextT = { code?: number;[key: string]: any };
8
7
  export type RouteContext<T = { code?: number }, S = any> = {
9
8
  /**
@@ -114,6 +113,7 @@ export const createSkill = <T = SimpleObject>(skill: Skill<T>): Skill<T> => {
114
113
  }
115
114
 
116
115
  export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
116
+
117
117
  export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleObject> {
118
118
  /**
119
119
  * 一级路径
@@ -137,14 +137,14 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
137
137
  isDebug?: boolean;
138
138
  constructor(path: string = '', key: string = '', opts?: RouteOpts) {
139
139
  if (!path) {
140
- path = nanoid(8)
140
+ path = randomId(8, 'rand-');
141
141
  }
142
142
  path = path.trim();
143
143
  key = key.trim();
144
144
  this.path = path;
145
145
  this.key = key;
146
146
  if (opts) {
147
- this.id = opts.id || nanoid();
147
+ this.id = opts.id || randomId(12, 'rand-');
148
148
  if (!opts.id && opts.idUsePath) {
149
149
  const delimiter = opts.delimiter ?? '$#$';
150
150
  this.id = path + delimiter + key;
@@ -159,7 +159,7 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
159
159
  this.path = opts.path || path;
160
160
  } else {
161
161
  this.middleware = [];
162
- this.id = nanoid();
162
+ this.id = randomId(12, 'rand-');
163
163
  }
164
164
  this.isDebug = opts?.isDebug ?? false;
165
165
  }
@@ -215,10 +215,10 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
215
215
  return this;
216
216
  }
217
217
 
218
- update(opts: DefineRouteOpts, checkList?: string[]): this {
218
+ update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this {
219
219
  const keys = Object.keys(opts);
220
220
  const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'isDebug'];
221
- checkList = checkList || defaultCheckList;
221
+ const checkList = onlyUpdateList || defaultCheckList;
222
222
  for (let item of keys) {
223
223
  if (!checkList.includes(item)) {
224
224
  continue;
@@ -316,8 +316,8 @@ export class QueryRouter {
316
316
  * remove route by id
317
317
  * @param uniqueId
318
318
  */
319
- removeById(unique: string) {
320
- this.routes = this.routes.filter((r) => r.id !== unique);
319
+ removeById(uniqueId: string) {
320
+ this.routes = this.routes.filter((r) => r.id !== uniqueId);
321
321
  }
322
322
  /**
323
323
  * 执行route
@@ -663,14 +663,28 @@ export class QueryRouter {
663
663
  return false;
664
664
  });
665
665
  }
666
- createRouteList(force: boolean = false, filter?: (route: Route) => boolean) {
666
+ createRouteList(opts?: { force?: boolean, filter?: (route: Route) => boolean, middleware?: string[] }) {
667
667
  const hasListRoute = this.hasRoute('router', 'list');
668
- if (!hasListRoute || force) {
668
+ if (!hasListRoute || opts?.force) {
669
669
  const listRoute = new Route('router', 'list', {
670
670
  description: '列出当前应用下的所有的路由信息',
671
+ middleware: opts?.middleware || [],
671
672
  run: async (ctx: RouteContext) => {
672
- const list = this.getList(filter);
673
- ctx.body = { list };
673
+ const tokenUser = ctx.state.tokenUser;
674
+ let isUser = !!tokenUser;
675
+ const list = this.getList(opts?.filter).filter((item) => {
676
+ if (item.id === 'auth' || item.id === 'auth-can' || item.id === 'check-auth-admin' || item.id === 'auth-admin') {
677
+ return false;
678
+ }
679
+ return true;
680
+ });
681
+ ctx.body = {
682
+ list: list.map((item) => {
683
+ const route = pick(item, ['id', 'path', 'key', 'description', 'middleware'] as const);
684
+ return toJSONSchema(route);
685
+ }),
686
+ isUser
687
+ };
674
688
  },
675
689
  });
676
690
  this.add(listRoute);
@@ -690,10 +704,11 @@ export class QueryRouter {
690
704
  getList?: boolean
691
705
  force?: boolean
692
706
  filter?: (route: Route) => boolean
707
+ routeListMiddleware?: string[]
693
708
  }) {
694
709
  const getList = opts?.getList ?? true;
695
710
  if (getList) {
696
- this.createRouteList(opts?.force ?? false, opts?.filter);
711
+ this.createRouteList({ force: opts?.force, filter: opts?.filter, middleware: opts?.routeListMiddleware });
697
712
  }
698
713
  return listenProcess({ app: this as any, params, ...opts });
699
714
  }
@@ -724,7 +739,7 @@ export class QueryRouterServer extends QueryRouter {
724
739
  if (opts?.appId) {
725
740
  this.appId = opts.appId;
726
741
  } else {
727
- this.appId = nanoid(16);
742
+ this.appId = randomId(16);
728
743
  }
729
744
  }
730
745
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
@@ -1,4 +1,4 @@
1
- import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from '@kevisual/router';
1
+ import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from './route.ts';
2
2
  import type { DataOpts, Query, Result } from '@kevisual/query/query';
3
3
  // export type RouteObject<T extends readonly string[]> = {
4
4
  // [K in T[number]]: RouteOpts;
@@ -0,0 +1,8 @@
1
+ import { customAlphabet } from 'nanoid';
2
+
3
+ const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 16);
4
+
5
+ export const randomId = (length: number = 8, affix: string = '') => {
6
+ return affix + nanoid(length);
7
+ }
8
+ export { nanoid };