@kevisual/router 0.0.26 → 0.0.27

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,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.26",
4
+ "version": "0.0.27",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -10,15 +10,12 @@
10
10
  "build": "npm run clean && rollup -c",
11
11
  "build:app": "npm run build && rsync dist/*browser* ../deploy/dist",
12
12
  "watch": "rollup -c -w",
13
- "clean": "rm -rf dist",
14
- "auto:bun": "bun test/auto/app.ts",
15
- "auto:deno": "bun test/auto/app.ts"
13
+ "clean": "rm -rf dist"
16
14
  },
17
15
  "files": [
18
16
  "dist",
19
17
  "src",
20
- "mod.ts",
21
- "auto.ts"
18
+ "mod.ts"
22
19
  ],
23
20
  "keywords": [],
24
21
  "author": "abearxiong",
@@ -26,33 +23,34 @@
26
23
  "devDependencies": {
27
24
  "@kevisual/local-proxy": "^0.0.6",
28
25
  "@kevisual/query": "^0.0.29",
29
- "@kevisual/use-config": "^1.0.19",
30
26
  "@rollup/plugin-alias": "^5.1.1",
31
27
  "@rollup/plugin-commonjs": "^28.0.6",
32
- "@rollup/plugin-node-resolve": "^16.0.1",
28
+ "@rollup/plugin-node-resolve": "^16.0.3",
33
29
  "@rollup/plugin-typescript": "^12.1.4",
34
- "@types/bun": "^1.2.10",
35
- "@types/node": "^24.2.1",
36
- "@types/send": "^0.17.5",
30
+ "@types/lodash-es": "^4.17.12",
31
+ "@types/node": "^24.7.2",
32
+ "@types/send": "^1.2.0",
37
33
  "@types/ws": "^8.18.1",
38
34
  "@types/xml2js": "^0.4.14",
39
35
  "cookie": "^1.0.2",
40
- "deno": "^2.4.3",
41
- "es-toolkit": "^1.39.8",
42
- "nanoid": "^5.1.5",
43
- "rollup": "^4.46.2",
44
- "rollup-plugin-dts": "^6.2.1",
45
- "typescript": "^5.9.2",
36
+ "lodash-es": "^4.17.21",
37
+ "nanoid": "^5.1.6",
38
+ "rollup": "^4.52.4",
39
+ "rollup-plugin-dts": "^6.2.3",
40
+ "ts-loader": "^9.5.4",
41
+ "ts-node": "^10.9.2",
42
+ "tslib": "^2.8.1",
43
+ "typescript": "^5.9.3",
46
44
  "ws": "npm:@kevisual/ws",
47
45
  "xml2js": "^0.6.2",
48
- "zod": "^4.0.17"
46
+ "zod": "^4.1.12"
49
47
  },
50
48
  "repository": {
51
49
  "type": "git",
52
50
  "url": "git+https://github.com/abearxiong/kevisual-router.git"
53
51
  },
54
52
  "dependencies": {
55
- "path-to-regexp": "^8.2.0",
53
+ "path-to-regexp": "^8.3.0",
56
54
  "selfsigned": "^3.0.1",
57
55
  "send": "^1.2.0"
58
56
  },
@@ -70,6 +68,11 @@
70
68
  "require": "./dist/router-browser.js",
71
69
  "types": "./dist/router-browser.d.ts"
72
70
  },
71
+ "./sign": {
72
+ "import": "./dist/router-sign.js",
73
+ "require": "./dist/router-sign.js",
74
+ "types": "./dist/router-sign.d.ts"
75
+ },
73
76
  "./simple": {
74
77
  "import": "./dist/router-simple.js",
75
78
  "require": "./dist/router-simple.js",
@@ -80,20 +83,16 @@
80
83
  "require": "./dist/router-define.js",
81
84
  "types": "./dist/router-define.d.ts"
82
85
  },
83
- "./auto": {
84
- "import": "./dist/auto.js",
85
- "types": "./dist/auto.d.ts"
86
+ "./simple-lib": {
87
+ "import": "./dist/router-simple-lib.js",
88
+ "require": "./dist/router-simple-lib.js",
89
+ "types": "./dist/router-simple-lib.d.ts"
86
90
  },
87
91
  "./mod.ts": {
88
92
  "import": "./mod.ts",
89
93
  "require": "./mod.ts",
90
94
  "types": "./mod.d.ts"
91
95
  },
92
- "./auto.ts": {
93
- "import": "./auto.ts",
94
- "require": "./auto.ts",
95
- "types": "./auto.ts"
96
- },
97
96
  "./src/*": {
98
97
  "import": "./src/*",
99
98
  "require": "./src/*"
package/src/app.ts CHANGED
@@ -3,7 +3,7 @@ import { Server, ServerOpts, HandleCtx } from './server/server.ts';
3
3
  import { WsServer } from './server/ws-server.ts';
4
4
  import { CustomError } from './result/error.ts';
5
5
  import { handleServer } from './server/handle-server.ts';
6
- import { IncomingMessage, ServerResponse } from 'node:http';
6
+ import { IncomingMessage, ServerResponse } from 'http';
7
7
 
8
8
  type RouterHandle = (msg: { path: string; [key: string]: any }) => { code: string; data?: any; message?: string; [key: string]: any };
9
9
  type AppOptions<T = {}> = {
package/src/connect.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { nanoid } from 'nanoid';
2
+ import { RouteContext } from './route.ts';
3
+
4
+ export class Connect {
5
+ path: string;
6
+ key?: string;
7
+ _fn?: (ctx?: RouteContext) => Promise<RouteContext>;
8
+ description?: string;
9
+ connects: { path: string; key?: string }[];
10
+ share = false;
11
+
12
+ constructor(path: string) {
13
+ this.path = path;
14
+ this.key = nanoid();
15
+ }
16
+ use(path: string) {
17
+ this.connects.push({ path });
18
+ }
19
+ useList(paths: string[]) {
20
+ paths.forEach((path) => {
21
+ this.connects.push({ path });
22
+ });
23
+ }
24
+ useConnect(connect: Connect) {
25
+ this.connects.push({ path: connect.path, key: connect.key });
26
+ }
27
+ useConnectList(connects: Connect[]) {
28
+ connects.forEach((connect) => {
29
+ this.connects.push({ path: connect.path, key: connect.key });
30
+ });
31
+ }
32
+ getPathList() {
33
+ return this.connects.map((c) => c.path).filter(Boolean);
34
+ }
35
+ set fn(fn: (ctx?: RouteContext) => Promise<RouteContext>) {
36
+ this._fn = fn;
37
+ }
38
+ get fn() {
39
+ return this._fn;
40
+ }
41
+ }
42
+ export class QueryConnect {
43
+ connects: Connect[];
44
+ constructor() {
45
+ this.connects = [];
46
+ }
47
+ add(connect: Connect) {
48
+ const has = this.connects.find((c) => c.path === connect.path && c.key === connect.key);
49
+ if (has) {
50
+ // remove the old connect
51
+ console.log('[replace connect]:', connect.path, connect.key);
52
+ this.connects = this.connects.filter((c) => c.path !== connect.path && c.key !== connect.key);
53
+ }
54
+ this.connects.push(connect);
55
+ }
56
+ remove(connect: Connect) {
57
+ this.connects = this.connects.filter((c) => c.path !== connect.path && c.key !== connect.key);
58
+ }
59
+ getList() {
60
+ return this.connects.map((c) => {
61
+ return {
62
+ path: c.path,
63
+ key: c.key,
64
+ };
65
+ });
66
+ }
67
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { Route, QueryRouter, QueryRouterServer } from './route.ts';
2
+ export { Connect, QueryConnect } from './connect.ts';
2
3
 
3
4
  export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
4
5
 
@@ -10,9 +11,8 @@ export { Server, handleServer } from './server/index.ts';
10
11
  */
11
12
  export { CustomError } from './result/error.ts';
12
13
 
13
- export { createSchema } from './validator/index.ts';
14
- export type { Rule } from './validator/rule.ts';
15
- export type { Schema } from 'zod';
14
+ export { Rule, Schema, createSchema } from './validator/index.ts';
15
+
16
16
  export { App } from './app.ts';
17
17
 
18
18
  export * from './router-define.ts';
package/src/io.ts ADDED
@@ -0,0 +1,6 @@
1
+ // TODO: Implement IOApp
2
+ export class IOApp {
3
+ constructor() {
4
+ console.log('IoApp');
5
+ }
6
+ }
package/src/route.ts CHANGED
@@ -2,6 +2,7 @@ import { nanoid } from 'nanoid';
2
2
  import { CustomError } from './result/error.ts';
3
3
  import { Schema, Rule, createSchema } from './validator/index.ts';
4
4
  import { pick } from './utils/pick.ts';
5
+ import { get } from 'lodash-es';
5
6
 
6
7
  export type RouterContextT = { code?: number; [key: string]: any };
7
8
  export type RouteContext<T = { code?: number }, S = any> = {
@@ -86,7 +87,7 @@ export type RouteOpts = {
86
87
  * }
87
88
  */
88
89
  validator?: { [key: string]: Rule };
89
- schema?: { [key: string]: any };
90
+ schema?: { [key: string]: Schema<any> };
90
91
  isVerify?: boolean;
91
92
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
92
93
  verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
@@ -121,7 +122,7 @@ export class Route<U = { [key: string]: any }> {
121
122
  middleware?: RouteMiddleware[]; // middleware
122
123
  type? = 'route';
123
124
  private _validator?: { [key: string]: Rule };
124
- schema?: { [key: string]: any };
125
+ schema?: { [key: string]: Schema<any> };
125
126
  data?: any;
126
127
  /**
127
128
  * 是否需要验证
@@ -205,8 +206,12 @@ export class Route<U = { [key: string]: any }> {
205
206
  if (schema[key]) {
206
207
  const result = schema[key].safeParse(value);
207
208
  if (!result.success) {
208
- // TODO:
209
- const message = 'safe error, TODO:';
209
+ const path = result.error.errors[0]?.path?.join?.('.properties.');
210
+ let message = 'Invalid params';
211
+ if (path) {
212
+ const keyS = `${key}.properties.${path}.message`;
213
+ message = get(validator, keyS, 'Invalid params') as any;
214
+ }
210
215
  throw new CustomError(500, message);
211
216
  }
212
217
  }
@@ -603,7 +608,7 @@ export class QueryRouter {
603
608
  * @description 这里的上下文是为了在handle函数中使用
604
609
  * @param ctx
605
610
  */
606
- setContext(ctx: RouteContext) {
611
+ setContext(ctx: RouteContext) {
607
612
  this.context = ctx;
608
613
  }
609
614
  getList(): RouteInfo[] {
@@ -43,10 +43,11 @@ export class WsServerBase {
43
43
  this.listening = true;
44
44
 
45
45
  this.wss.on('connection', (ws) => {
46
- ws.on('message', async (message: string) => {
46
+ ws.on('message', async (message: string | Buffer) => {
47
47
  const data = parseIfJson(message);
48
48
  if (typeof data === 'string') {
49
- ws.emit('string', data);
49
+ const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
50
+ ws.emit('string', cleanMessage);
50
51
  return;
51
52
  }
52
53
  const { type, data: typeData, ...rest } = data;
@@ -83,7 +84,7 @@ export class WsServerBase {
83
84
  if (message === 'close') {
84
85
  ws.close();
85
86
  }
86
- if (message === 'ping') {
87
+ if (message == 'ping') {
87
88
  ws.send('pong');
88
89
  }
89
90
  });
package/src/sign.ts CHANGED
@@ -30,7 +30,7 @@ export const createCert = (attrs: Attributes[] = [], altNames: AltNames[] = [])
30
30
  );
31
31
 
32
32
  const options = {
33
- days: 365 * 99, // 证书有效期(天)
33
+ days: 365, // 证书有效期(天)
34
34
  extensions: [
35
35
  {
36
36
  name: 'subjectAltName',
package/src/test/ws.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { App } from "../app.ts";
2
+
3
+ const app = new App({
4
+ io: true
5
+ });
6
+
7
+ app
8
+ .route('demo', '03')
9
+ .define(async (ctx) => {
10
+ ctx.body = '03';
11
+ return ctx;
12
+ })
13
+ .addTo(app);
14
+ app
15
+ .route('test', 'test')
16
+ .define(async (ctx) => {
17
+ ctx.body = 'test';
18
+ return ctx;
19
+ })
20
+ .addTo(app);
21
+ console.log(`http://localhost:4002/api/router?path=demo&key=03`);
22
+
23
+ app.listen(4002, () => {
24
+ console.log("Server started on http://localhost:4002");
25
+ });
@@ -1,7 +1,8 @@
1
- export const parseIfJson = (input: string): { [key: string]: any } | string => {
1
+ export const parseIfJson = (input: string|Buffer): { [key: string]: any } | string => {
2
+ const str = typeof input === 'string' ? input : input.toString();
2
3
  try {
3
4
  // 尝试解析 JSON
4
- const parsed = JSON.parse(input);
5
+ const parsed = JSON.parse(str);
5
6
  // 检查解析结果是否为对象(数组或普通对象)
6
7
  if (typeof parsed === 'object' && parsed !== null) {
7
8
  return parsed;
@@ -9,5 +10,5 @@ export const parseIfJson = (input: string): { [key: string]: any } | string => {
9
10
  } catch (e) {
10
11
  // 如果解析失败,直接返回原始字符串
11
12
  }
12
- return input;
13
+ return str;
13
14
  };
@@ -1,6 +1,2 @@
1
- import { z } from 'zod';
1
+ export type { Rule, Schema } from './rule.ts';
2
2
  export { schemaFormRule, createSchema, createSchemaList } from './rule.ts';
3
-
4
- export type { Rule } from './rule.ts';
5
-
6
- export type Schema = z.ZodType<any, any, any>;
@@ -1,4 +1,5 @@
1
- import { z, ZodError } from 'zod';
1
+ import { z, ZodError, Schema } from 'zod';
2
+ export { Schema };
2
3
  type BaseRule = {
3
4
  value?: any;
4
5
  required?: boolean;
@@ -63,7 +64,7 @@ export const schemaFormRule = (rule: Rule): z.ZodType<any, any, any> => {
63
64
  throw new Error(`Unknown rule type: ${(rule as any)?.type}`);
64
65
  }
65
66
  };
66
- export const createSchema = (rule: Rule): z.ZodType<any, any, any> => {
67
+ export const createSchema = (rule: Rule): Schema => {
67
68
  try {
68
69
  rule.required = rule.required ?? false;
69
70
  if (!rule.required) {
package/auto.ts DELETED
@@ -1,20 +0,0 @@
1
- import { loadTS, getMatchFiles } from './src/auto/load-ts.ts';
2
- import { listenSocket } from './src/auto/listen-sock.ts';
3
- import { Route, QueryRouter, QueryRouterServer } from './src/route.ts';
4
-
5
- export { Route, QueryRouter, QueryRouterServer };
6
-
7
- export const App = QueryRouterServer;
8
-
9
- export { createSchema } from './src/validator/index.ts';
10
- export type { Rule } from './src/validator/rule.ts';
11
- export type { Schema } from 'zod';
12
- export type { RouteContext, RouteOpts } from './src/route.ts';
13
-
14
- export type { Run } from './src/route.ts';
15
-
16
- export { CustomError } from './src/result/error.ts';
17
-
18
- export { listenSocket, loadTS, getMatchFiles };
19
-
20
- export { autoCall } from './src/auto/call-sock.ts';