@kevisual/router 0.0.32 → 0.0.34

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
@@ -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
  }
@@ -633,7 +631,7 @@ export class QueryRouter {
633
631
  * @returns
634
632
  */
635
633
  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 });
634
+ const res = await this.call(message, { ...this.context, ...ctx });
637
635
  return {
638
636
  code: res.code,
639
637
  data: res.body,
@@ -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
+