@kevisual/router 0.0.70 → 0.0.72

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.70",
4
+ "version": "0.0.72",
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.29.3",
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.40",
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,10 +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
- import { filter } from '@kevisual/js-filter'
7
-
5
+ import { randomId } from './utils/random.ts';
8
6
  export type RouterContextT = { code?: number;[key: string]: any };
9
7
  export type RouteContext<T = { code?: number }, S = any> = {
10
8
  /**
@@ -14,6 +12,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
14
12
  appId?: string;
15
13
  // run first
16
14
  query?: { [key: string]: any };
15
+ args?: { [key: string]: any };
17
16
  // response body
18
17
  /** return body */
19
18
  body?: number | string | Object;
@@ -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
@@ -405,15 +405,15 @@ export class QueryRouter {
405
405
  console.error('=====debug====:middlerware error');
406
406
  console.error('=====debug====:', e);
407
407
  console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
408
- console.error('=====debug====:', e.message);
409
408
  }
410
409
  if (e instanceof CustomError || e?.code) {
411
410
  ctx.code = e.code;
412
411
  ctx.message = e.message;
413
412
  ctx.body = null;
414
413
  } else {
415
- console.error(`fn:${route.path}-${route.key}:${route.id}`);
416
- console.error(`middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
414
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
415
+ console.error(`[router error] middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
416
+ console.error(e)
417
417
  ctx.code = 500;
418
418
  ctx.message = 'Internal Server Error';
419
419
  ctx.body = null;
@@ -432,14 +432,16 @@ export class QueryRouter {
432
432
  await route.run(ctx as Required<RouteContext>);
433
433
  } catch (e) {
434
434
  if (route?.isDebug) {
435
- console.error('=====debug====:', 'router run error:', e.message);
435
+ console.error('=====debug====:route error');
436
+ console.error('=====debug====:', e);
437
+ console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
436
438
  }
437
439
  if (e instanceof CustomError) {
438
440
  ctx.code = e.code;
439
441
  ctx.message = e.message;
440
442
  } else {
441
- console.error(`[error]fn:${route.path}-${route.key}:${route.id}`);
442
- console.error('error', e.message);
443
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
444
+ console.error(`[router error] error`, e);
443
445
  ctx.code = 500;
444
446
  ctx.message = 'Internal Server Error';
445
447
  }
@@ -469,6 +471,7 @@ export class QueryRouter {
469
471
  return ctx;
470
472
  }
471
473
  ctx.query = { ...ctx.query, ...ctx.nextQuery };
474
+ ctx.args = ctx.query;
472
475
  ctx.nextQuery = {};
473
476
  return await this.runRoute(path, key, ctx);
474
477
  }
@@ -496,6 +499,7 @@ export class QueryRouter {
496
499
  const { path, key = '', payload = {}, ...query } = message;
497
500
  ctx = ctx || {};
498
501
  ctx.query = { ...ctx.query, ...query, ...payload };
502
+ ctx.args = ctx.query;
499
503
  ctx.state = { ...ctx?.state };
500
504
  ctx.throw = this.throw;
501
505
  ctx.app = this;
@@ -720,7 +724,7 @@ export class QueryRouterServer extends QueryRouter {
720
724
  if (opts?.appId) {
721
725
  this.appId = opts.appId;
722
726
  } else {
723
- this.appId = nanoid(16);
727
+ this.appId = randomId(16);
724
728
  }
725
729
  }
726
730
  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 };