@kevisual/router 0.0.6-alpha-4 → 0.0.6

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.
@@ -112,6 +112,9 @@ type RouteOpts = {
112
112
  isVerify?: boolean;
113
113
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
114
114
  verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
115
+ /**
116
+ * $#$ will be used to split path and key
117
+ */
115
118
  idUsePath?: boolean;
116
119
  isDebug?: boolean;
117
120
  };
@@ -208,7 +211,7 @@ declare class QueryRouter {
208
211
  */
209
212
  remove(route: Route | {
210
213
  path: string;
211
- key: string;
214
+ key?: string;
212
215
  }): void;
213
216
  /**
214
217
  * remove route by id
@@ -292,6 +295,9 @@ declare class QueryRouter {
292
295
  importRoutes(routes: Route[]): void;
293
296
  importRouter(router: QueryRouter): void;
294
297
  throw(code?: number | string, message?: string, tips?: string): void;
298
+ hasRoute(path: string, key?: string): Route<{
299
+ [key: string]: any;
300
+ }>;
295
301
  }
296
302
  type QueryRouterServerOpts = {
297
303
  handleFn?: HandleFn;
@@ -5649,7 +5649,7 @@ class QueryRouter {
5649
5649
  const has = this.routes.find((r) => r.path === route.path && r.key === route.key);
5650
5650
  if (has) {
5651
5651
  // remove the old route
5652
- this.routes = this.routes.filter((r) => r.path === route.path && r.key === route.key);
5652
+ this.routes = this.routes.filter((r) => r.id !== has.id);
5653
5653
  }
5654
5654
  this.routes.push(route);
5655
5655
  }
@@ -5658,7 +5658,7 @@ class QueryRouter {
5658
5658
  * @param route
5659
5659
  */
5660
5660
  remove(route) {
5661
- this.routes = this.routes.filter((r) => r.path === route.path && r.key === route.key);
5661
+ this.routes = this.routes.filter((r) => r.path === route.path && r.key == route.key);
5662
5662
  }
5663
5663
  /**
5664
5664
  * remove route by id
@@ -5933,6 +5933,9 @@ class QueryRouter {
5933
5933
  throw(...args) {
5934
5934
  throw new CustomError(...args);
5935
5935
  }
5936
+ hasRoute(path, key) {
5937
+ return this.routes.find((r) => r.path === path && r.key === key);
5938
+ }
5936
5939
  }
5937
5940
  /**
5938
5941
  * QueryRouterServer
@@ -1,3 +1,4 @@
1
+ import * as querystring from 'querystring';
1
2
  import { Key } from 'path-to-regexp';
2
3
  import { ServerResponse, IncomingMessage } from 'http';
3
4
 
@@ -16,9 +17,18 @@ interface Route {
16
17
  declare class SimpleRouter {
17
18
  routes: Route[];
18
19
  constructor();
20
+ getBody(req: Req): Promise<unknown>;
21
+ getSearch(req: Req): querystring.ParsedUrlQuery;
19
22
  use(method: string, route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
20
23
  get(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
21
24
  post(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
25
+ all(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
26
+ /**
27
+ * 解析 req 和 res 请求
28
+ * @param req
29
+ * @param res
30
+ * @returns
31
+ */
22
32
  parse(req: Req, res: ServerResponse): Promise<void> | "not_found";
23
33
  }
24
34
 
@@ -1,3 +1,5 @@
1
+ import url from 'url';
2
+
1
3
  var dist = {};
2
4
 
3
5
  var hasRequiredDist;
@@ -412,6 +414,28 @@ function requireDist () {
412
414
 
413
415
  var distExports = requireDist();
414
416
 
417
+ const parseBody = async (req) => {
418
+ return new Promise((resolve, reject) => {
419
+ const arr = [];
420
+ req.on('data', (chunk) => {
421
+ arr.push(chunk);
422
+ });
423
+ req.on('end', () => {
424
+ try {
425
+ const body = Buffer.concat(arr).toString();
426
+ resolve(JSON.parse(body));
427
+ }
428
+ catch (e) {
429
+ resolve({});
430
+ }
431
+ });
432
+ });
433
+ };
434
+ const parseSearch = (req) => {
435
+ const parsedUrl = url.parse(req.url, true);
436
+ return parsedUrl.query;
437
+ };
438
+
415
439
  /**
416
440
  * SimpleRouter
417
441
  */
@@ -420,6 +444,12 @@ class SimpleRouter {
420
444
  constructor() {
421
445
  // console.log('AppSimple initialized');
422
446
  }
447
+ getBody(req) {
448
+ return parseBody(req);
449
+ }
450
+ getSearch(req) {
451
+ return parseSearch(req);
452
+ }
423
453
  use(method, route, ...fns) {
424
454
  const handlers = Array.isArray(fns) ? fns.flat() : [];
425
455
  const pattern = distExports.pathToRegexp(route);
@@ -432,6 +462,17 @@ class SimpleRouter {
432
462
  post(route, ...fns) {
433
463
  return this.use('post', route, ...fns);
434
464
  }
465
+ all(route, ...fns) {
466
+ this.use('post', route, ...fns);
467
+ this.use('get', route, ...fns);
468
+ return this;
469
+ }
470
+ /**
471
+ * 解析 req 和 res 请求
472
+ * @param req
473
+ * @param res
474
+ * @returns
475
+ */
435
476
  parse(req, res) {
436
477
  const { pathname } = new URL(req.url, 'http://localhost');
437
478
  const method = req.method.toLowerCase();
package/dist/router.d.ts CHANGED
@@ -117,6 +117,9 @@ type RouteOpts = {
117
117
  isVerify?: boolean;
118
118
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
119
119
  verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
120
+ /**
121
+ * $#$ will be used to split path and key
122
+ */
120
123
  idUsePath?: boolean;
121
124
  isDebug?: boolean;
122
125
  };
@@ -213,7 +216,7 @@ declare class QueryRouter {
213
216
  */
214
217
  remove(route: Route | {
215
218
  path: string;
216
- key: string;
219
+ key?: string;
217
220
  }): void;
218
221
  /**
219
222
  * remove route by id
@@ -297,6 +300,9 @@ declare class QueryRouter {
297
300
  importRoutes(routes: Route[]): void;
298
301
  importRouter(router: QueryRouter): void;
299
302
  throw(code?: number | string, message?: string, tips?: string): void;
303
+ hasRoute(path: string, key?: string): Route<{
304
+ [key: string]: any;
305
+ }>;
300
306
  }
301
307
  type QueryRouterServerOpts = {
302
308
  handleFn?: HandleFn;
package/dist/router.js CHANGED
@@ -5669,7 +5669,7 @@ class QueryRouter {
5669
5669
  const has = this.routes.find((r) => r.path === route.path && r.key === route.key);
5670
5670
  if (has) {
5671
5671
  // remove the old route
5672
- this.routes = this.routes.filter((r) => r.path === route.path && r.key === route.key);
5672
+ this.routes = this.routes.filter((r) => r.id !== has.id);
5673
5673
  }
5674
5674
  this.routes.push(route);
5675
5675
  }
@@ -5678,7 +5678,7 @@ class QueryRouter {
5678
5678
  * @param route
5679
5679
  */
5680
5680
  remove(route) {
5681
- this.routes = this.routes.filter((r) => r.path === route.path && r.key === route.key);
5681
+ this.routes = this.routes.filter((r) => r.path === route.path && r.key == route.key);
5682
5682
  }
5683
5683
  /**
5684
5684
  * remove route by id
@@ -5953,6 +5953,9 @@ class QueryRouter {
5953
5953
  throw(...args) {
5954
5954
  throw new CustomError(...args);
5955
5955
  }
5956
+ hasRoute(path, key) {
5957
+ return this.routes.find((r) => r.path === path && r.key === key);
5958
+ }
5956
5959
  }
5957
5960
  /**
5958
5961
  * QueryRouterServer
@@ -6133,8 +6136,6 @@ const handleServer = async (req, res) => {
6133
6136
  if (token) {
6134
6137
  token = token.replace('Bearer ', '');
6135
6138
  }
6136
- //@ts-ignore
6137
- console.log('token', req.cookies, res.cookie);
6138
6139
  // 获取查询参数
6139
6140
  const param = parsedUrl.query;
6140
6141
  let body;
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.6-alpha-4",
4
+ "version": "0.0.6",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
@@ -20,22 +20,22 @@
20
20
  "author": "abearxiong",
21
21
  "license": "MIT",
22
22
  "devDependencies": {
23
- "@rollup/plugin-commonjs": "^28.0.1",
24
- "@rollup/plugin-node-resolve": "^15.3.0",
25
- "@rollup/plugin-typescript": "^12.1.1",
23
+ "@rollup/plugin-commonjs": "^28.0.2",
24
+ "@rollup/plugin-node-resolve": "^16.0.0",
25
+ "@rollup/plugin-typescript": "^12.1.2",
26
26
  "@types/lodash-es": "^4.17.12",
27
- "@types/node": "^22.8.6",
28
- "@types/ws": "^8.5.13",
27
+ "@types/node": "^22.13.4",
28
+ "@types/ws": "^8.5.14",
29
29
  "cookie": "^1.0.2",
30
30
  "lodash-es": "^4.17.21",
31
- "nanoid": "^5.0.8",
32
- "rollup": "^4.24.3",
31
+ "nanoid": "^5.1.0",
32
+ "rollup": "^4.34.8",
33
33
  "rollup-plugin-dts": "^6.1.1",
34
- "ts-loader": "^9.5.1",
34
+ "ts-loader": "^9.5.2",
35
35
  "ts-node": "^10.9.2",
36
36
  "tslib": "^2.8.1",
37
- "typescript": "^5.6.3",
38
- "zod": "^3.23.8"
37
+ "typescript": "^5.7.3",
38
+ "zod": "^3.24.2"
39
39
  },
40
40
  "repository": {
41
41
  "type": "git",