@kevisual/router 0.0.33 → 0.0.35

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.33",
4
+ "version": "0.0.35",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -21,8 +21,8 @@
21
21
  "author": "abearxiong",
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@kevisual/local-proxy": "^0.0.6",
25
- "@kevisual/query": "^0.0.29",
24
+ "@kevisual/local-proxy": "^0.0.8",
25
+ "@kevisual/query": "^0.0.31",
26
26
  "@rollup/plugin-alias": "^6.0.0",
27
27
  "@rollup/plugin-commonjs": "29.0.0",
28
28
  "@rollup/plugin-node-resolve": "^16.0.3",
@@ -32,18 +32,18 @@
32
32
  "@types/send": "^1.2.1",
33
33
  "@types/ws": "^8.18.1",
34
34
  "@types/xml2js": "^0.4.14",
35
- "cookie": "^1.0.2",
35
+ "cookie": "^1.1.1",
36
36
  "lodash-es": "^4.17.21",
37
37
  "nanoid": "^5.1.6",
38
- "rollup": "^4.53.2",
39
- "rollup-plugin-dts": "^6.2.3",
38
+ "rollup": "^4.53.3",
39
+ "rollup-plugin-dts": "^6.3.0",
40
40
  "ts-loader": "^9.5.4",
41
41
  "ts-node": "^10.9.2",
42
42
  "tslib": "^2.8.1",
43
43
  "typescript": "^5.9.3",
44
44
  "ws": "npm:@kevisual/ws",
45
45
  "xml2js": "^0.6.2",
46
- "zod": "^4.1.12"
46
+ "zod": "^4.1.13"
47
47
  },
48
48
  "repository": {
49
49
  "type": "git",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "path-to-regexp": "^8.3.0",
54
- "selfsigned": "^4.0.0",
54
+ "selfsigned": "^5.2.0",
55
55
  "send": "^1.2.0"
56
56
  },
57
57
  "publishConfig": {
package/src/route.ts CHANGED
@@ -115,7 +115,6 @@ export class Route<U = { [key: string]: any }> {
115
115
  */
116
116
  key?: string;
117
117
  id?: string;
118
- share? = false;
119
118
  run?: Run;
120
119
  nextRoute?: NextRoute; // route to run after this route
121
120
  description?: string;
@@ -587,9 +586,7 @@ export class QueryRouter {
587
586
  ctx.query = { ...ctx.query, ...query, ...payload };
588
587
  ctx.state = { ...ctx?.state };
589
588
  ctx.throw = this.throw;
590
- // put queryRouter to ctx
591
- // TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
592
- ctx.queryRouter = this;
589
+ ctx.app = this;
593
590
  ctx.call = this.call.bind(this);
594
591
  ctx.queryRoute = this.queryRoute.bind(this);
595
592
  ctx.index = 0;
@@ -610,7 +607,10 @@ export class QueryRouter {
610
607
  async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
611
608
  let path = message.path;
612
609
  let key = message.key;
613
- if (message.id) {
610
+ // 优先 path + key
611
+ if (path) {
612
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
613
+ } else if (message.id) {
614
614
  const route = this.routes.find((r) => r.id === message.id);
615
615
  if (route) {
616
616
  path = route.path;
@@ -619,8 +619,6 @@ export class QueryRouter {
619
619
  return { code: 404, body: null, message: 'Not found route' };
620
620
  }
621
621
  return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
622
- } else if (path) {
623
- return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
624
622
  } else {
625
623
  return { code: 404, body: null, message: 'Not found path' };
626
624
  }
@@ -630,10 +628,25 @@ export class QueryRouter {
630
628
  * 请求 result 的数据
631
629
  * @param message
632
630
  * @param ctx
631
+ * @deprecated use run or call instead
633
632
  * @returns
634
633
  */
635
634
  async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
636
- const res = await this.parse(message, { ...this.context, ...ctx });
635
+ const res = await this.call(message, { ...this.context, ...ctx });
636
+ return {
637
+ code: res.code,
638
+ data: res.body,
639
+ message: res.message,
640
+ };
641
+ }
642
+ /**
643
+ * Router Run获取数据
644
+ * @param message
645
+ * @param ctx
646
+ * @returns
647
+ */
648
+ async run(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
649
+ const res = await this.call(message, { ...this.context, ...ctx });
637
650
  return {
638
651
  code: res.code,
639
652
  data: res.body,
@@ -660,17 +673,7 @@ export class QueryRouter {
660
673
  return async (msg: { id?: string; path?: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
661
674
  try {
662
675
  const context = { ...ctx, ...handleContext };
663
- if (msg.id) {
664
- const route = router.routes.find((r) => r.id === msg.id);
665
- if (route) {
666
- msg.path = route.path;
667
- msg.key = route.key;
668
- } else {
669
- return { code: 404, message: 'Not found route' };
670
- }
671
- }
672
- // @ts-ignore
673
- const res = await router.parse(msg, context);
676
+ const res = await router.call(msg, context);
674
677
  if (wrapperFn) {
675
678
  res.data = res.body;
676
679
  return wrapperFn(res, context);
@@ -798,34 +801,19 @@ export class QueryRouterServer extends QueryRouter {
798
801
  }
799
802
 
800
803
  /**
801
- * 等于queryRoute,但是调用了handle
804
+ * 调用了handle
802
805
  * @param param0
803
806
  * @returns
804
807
  */
805
- async run({ path, key, payload }: { path: string; key?: string; payload?: any }) {
808
+ async run({ path, key, payload }: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
806
809
  const handle = this.handle;
807
- const resultError = (error: string, code = 500) => {
808
- const r = {
809
- code: code,
810
- message: error,
811
- };
812
- return r;
813
- };
814
- try {
815
- const end = handle({ path, key, ...payload });
816
- return end;
817
- } catch (e) {
818
- if (e.code && typeof e.code === 'number') {
819
- return {
820
- code: e.code,
821
- message: e.message,
822
- };
823
- } else {
824
- return resultError('Router Server error');
825
- }
810
+ if (handle) {
811
+ const result = await this.call({ path, key, payload }, ctx);
812
+ return handle(result);
826
813
  }
814
+ return super.run({ path, key, payload }, ctx);
827
815
  }
828
816
  }
829
817
 
830
818
 
831
- export const Mini = QueryRouterServer
819
+ export class Mini extends QueryRouterServer {}
@@ -122,17 +122,33 @@ type HttpChainOpts = {
122
122
  req?: Req;
123
123
  res?: ServerResponse;
124
124
  simpleRouter?: SimpleRouter;
125
+ server?: Server;
125
126
  };
127
+
128
+ /**
129
+ * HttpChain 类, 用于链式调用,router.get内部使用
130
+ */
131
+
126
132
  export class HttpChain {
133
+ /**
134
+ * 请求对象, 每一次请求都是不一样的
135
+ */
127
136
  req: Req;
137
+ /**
138
+ * 响应对象, 每一次请求响应都是不一样的
139
+ */
128
140
  res: ServerResponse;
129
141
  simpleRouter: SimpleRouter;
130
142
  server: Server;
131
143
  hasSetHeader: boolean = false;
132
144
  isSseSet: boolean = false;
133
145
  constructor(opts?: HttpChainOpts) {
134
- this.req = opts?.req;
135
- this.res = opts?.res;
146
+ if (opts?.res) {
147
+ this.res = opts.res;
148
+ }
149
+ if (opts?.req) {
150
+ this.req = opts.req;
151
+ }
136
152
  this.simpleRouter = opts?.simpleRouter;
137
153
  }
138
154
  setReq(req: Req) {
@@ -200,7 +216,13 @@ export class HttpChain {
200
216
  this.server.listen(opts, callback);
201
217
  return this;
202
218
  }
203
- parse() {
219
+ /**
220
+ * 外部 parse 方法
221
+ * @returns
222
+ */
223
+ parse(opts?: { listenOptions?: ListenOptions, listenCallBack?: () => void }) {
224
+ const { listenOptions, listenCallBack } = opts || {};
225
+
204
226
  if (!this.server || !this.simpleRouter) {
205
227
  throw new Error('Server and SimpleRouter must be set before calling parse');
206
228
  }
@@ -216,6 +238,9 @@ export class HttpChain {
216
238
  }
217
239
  }
218
240
  };
241
+ if (listenOptions) {
242
+ this.server.listen(listenOptions, listenCallBack);
243
+ }
219
244
  this.server.on('request', listener);
220
245
  return () => {
221
246
  that.server.removeListener('request', listener);
@@ -0,0 +1,18 @@
1
+ // import { Server } from 'node:http';
2
+ import { Server } from '../server/server.ts'
3
+
4
+ const server = new Server({
5
+ path: '/',
6
+ handle: async (data, ctx) => {
7
+ console.log('ctx', ctx.req.url)
8
+ console.log('Received data:', data);
9
+
10
+ ctx.res.writeHead(200, { 'Content-Type': 'application/json' });
11
+ return JSON.stringify({ code: 200, message: 'Success', data });
12
+ }
13
+ });
14
+
15
+ server.listen(51015, '0.0.0.0', () => {
16
+ console.log('Server is listening on http://localhost:3000');
17
+ });
18
+