@kevisual/router 0.0.9 → 0.0.10

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.
@@ -391,4 +391,5 @@ declare class CustomError extends Error {
391
391
  };
392
392
  }
393
393
 
394
- export { CustomError, QueryRouter, QueryRouterServer, Route, type RouteContext, type RouteOpts, type Rule, type Run, createSchema };
394
+ export { CustomError, QueryRouter, QueryRouterServer, Route, createSchema };
395
+ export type { RouteContext, RouteOpts, Rule, Run };
@@ -16,8 +16,11 @@ interface Route {
16
16
  */
17
17
  declare class SimpleRouter {
18
18
  routes: Route[];
19
- constructor();
20
- getBody(req: Req): Promise<unknown>;
19
+ exclude: string[];
20
+ constructor(opts?: {
21
+ exclude?: string[];
22
+ });
23
+ getBody(req: Req): Promise<Record<string, any>>;
21
24
  getSearch(req: Req): querystring.ParsedUrlQuery;
22
25
  use(method: string, route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
23
26
  get(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
@@ -29,7 +32,7 @@ declare class SimpleRouter {
29
32
  * @param res
30
33
  * @returns
31
34
  */
32
- parse(req: Req, res: ServerResponse): Promise<void> | "not_found";
35
+ parse(req: Req, res: ServerResponse): Promise<void> | "is_exclude" | "not_found";
33
36
  }
34
37
 
35
38
  export { SimpleRouter };
@@ -441,8 +441,9 @@ const parseSearch = (req) => {
441
441
  */
442
442
  class SimpleRouter {
443
443
  routes = [];
444
- constructor() {
445
- // console.log('AppSimple initialized');
444
+ exclude = []; // 排除的请求
445
+ constructor(opts) {
446
+ this.exclude = opts?.exclude || ['/api/router'];
446
447
  }
447
448
  getBody(req) {
448
449
  return parseBody(req);
@@ -476,6 +477,9 @@ class SimpleRouter {
476
477
  parse(req, res) {
477
478
  const { pathname } = new URL(req.url, 'http://localhost');
478
479
  const method = req.method.toLowerCase();
480
+ if (this.exclude.includes(pathname)) {
481
+ return 'is_exclude';
482
+ }
479
483
  const route = this.routes.find((route) => {
480
484
  const matchResult = route.regexp.exec(pathname);
481
485
  if (matchResult && route.method === method) {
package/dist/router.d.ts CHANGED
@@ -478,6 +478,10 @@ declare class Server {
478
478
 
479
479
  /**
480
480
  * get params and body
481
+ * 优先原则
482
+ * 1. 请求参数中的 payload 的token 优先
483
+ * 2. 请求头中的 authorization 优先
484
+ * 3. 请求头中的 cookie 优先
481
485
  * @param req
482
486
  * @param res
483
487
  * @returns
@@ -610,4 +614,5 @@ declare class App<T = {}, U = AppReqRes> {
610
614
  throw(code?: number | string, message?: string, tips?: string): void;
611
615
  }
612
616
 
613
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, type RouteContext, type RouteOpts, type Rule, type Run, Server, createSchema, handleServer };
617
+ export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, Server, createSchema, handleServer };
618
+ export type { RouteContext, RouteOpts, Rule, Run };
package/dist/router.js CHANGED
@@ -6328,6 +6328,10 @@ const parseBody = async (req) => {
6328
6328
 
6329
6329
  /**
6330
6330
  * get params and body
6331
+ * 优先原则
6332
+ * 1. 请求参数中的 payload 的token 优先
6333
+ * 2. 请求头中的 authorization 优先
6334
+ * 3. 请求头中的 cookie 优先
6331
6335
  * @param req
6332
6336
  * @param res
6333
6337
  * @returns
@@ -6745,8 +6749,6 @@ class Server {
6745
6749
  // 交给其他监听处理
6746
6750
  return;
6747
6751
  }
6748
- // res.setHeader('Content-Type', 'text/html; charset=utf-8');
6749
- res.setHeader('Content-Type', 'application/json; charset=utf-8');
6750
6752
  if (cors) {
6751
6753
  res.setHeader('Access-Control-Allow-Origin', cors?.origin || '*'); // 允许所有域名的请求访问,可以根据需要设置具体的域名
6752
6754
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
@@ -6756,7 +6758,6 @@ class Server {
6756
6758
  return;
6757
6759
  }
6758
6760
  }
6759
- // res.writeHead(200); // 设置响应头,给予其他任何listen 知道headersSent,它已经被响应了
6760
6761
  const url = req.url;
6761
6762
  if (!url.startsWith(path)) {
6762
6763
  res.end(resultError(`not path:[${path}]`));
@@ -6770,8 +6771,10 @@ class Server {
6770
6771
  try {
6771
6772
  const end = await handle(messages, { req, res });
6772
6773
  if (res.writableEnded) {
6774
+ // 如果响应已经结束,则不进行任何操作
6773
6775
  return;
6774
6776
  }
6777
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
6775
6778
  if (typeof end === 'string') {
6776
6779
  res.end(end);
6777
6780
  }
@@ -6781,6 +6784,7 @@ class Server {
6781
6784
  }
6782
6785
  catch (e) {
6783
6786
  console.error(e);
6787
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
6784
6788
  if (e.code && typeof e.code === 'number') {
6785
6789
  res.end(resultError(e.message || `Router Server error`, e.code));
6786
6790
  }
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.9",
4
+ "version": "0.0.10",
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.2",
24
- "@rollup/plugin-node-resolve": "^16.0.0",
23
+ "@rollup/plugin-commonjs": "^28.0.3",
24
+ "@rollup/plugin-node-resolve": "^16.0.1",
25
25
  "@rollup/plugin-typescript": "^12.1.2",
26
26
  "@types/lodash-es": "^4.17.12",
27
- "@types/node": "^22.13.4",
28
- "@types/ws": "^8.5.14",
27
+ "@types/node": "^22.14.0",
28
+ "@types/ws": "^8.18.1",
29
29
  "@types/xml2js": "^0.4.14",
30
30
  "cookie": "^1.0.2",
31
31
  "lodash-es": "^4.17.21",
32
- "nanoid": "^5.1.0",
33
- "rollup": "^4.34.8",
34
- "rollup-plugin-dts": "^6.1.1",
32
+ "nanoid": "^5.1.5",
33
+ "rollup": "^4.39.0",
34
+ "rollup-plugin-dts": "^6.2.1",
35
35
  "ts-loader": "^9.5.2",
36
36
  "ts-node": "^10.9.2",
37
37
  "tslib": "^2.8.1",
38
- "typescript": "^5.7.3",
38
+ "typescript": "^5.8.2",
39
39
  "xml2js": "^0.6.2",
40
40
  "zod": "^3.24.2"
41
41
  },
@@ -46,7 +46,7 @@
46
46
  "dependencies": {
47
47
  "path-to-regexp": "^8.2.0",
48
48
  "selfsigned": "^2.4.1",
49
- "ws": "^8.18.0"
49
+ "ws": "^8.18.1"
50
50
  },
51
51
  "publishConfig": {
52
52
  "access": "public"