@kevisual/router 0.0.81 → 0.0.82

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/dist/app.js CHANGED
@@ -3020,6 +3020,10 @@ class CustomError extends Error {
3020
3020
  data;
3021
3021
  message;
3022
3022
  constructor(code, opts) {
3023
+ if (typeof code === "object" && code !== null) {
3024
+ opts = code;
3025
+ code = opts.code || 500;
3026
+ }
3023
3027
  let message = opts?.message || String(code);
3024
3028
  const cause = opts?.cause;
3025
3029
  super(message, { cause });
@@ -17294,7 +17298,16 @@ class QueryRouter {
17294
17298
  this.importRoutes(router.routes);
17295
17299
  }
17296
17300
  throw(...args) {
17297
- throw new CustomError(...args);
17301
+ const [args0, args1] = args;
17302
+ if (args0 && typeof args0 === "object") {
17303
+ throw new CustomError(args0);
17304
+ }
17305
+ if (args1 && typeof args1 === "object") {
17306
+ throw new CustomError(args0, args1);
17307
+ } else if (args1) {
17308
+ throw new CustomError(args0, { message: args1 });
17309
+ }
17310
+ throw new CustomError(args0);
17298
17311
  }
17299
17312
  hasRoute(path, key = "") {
17300
17313
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -17738,8 +17751,13 @@ class ServerBase {
17738
17751
  res.end(JSON.stringify(end));
17739
17752
  }
17740
17753
  } catch (e) {
17741
- console.error(e);
17742
17754
  res.setHeader("Content-Type", "application/json; charset=utf-8");
17755
+ if (CustomError.isError(e)) {
17756
+ const parsedError = CustomError.parseError(e);
17757
+ res.end(JSON.stringify(parsedError));
17758
+ return;
17759
+ }
17760
+ console.error(e);
17743
17761
  if (e.code && typeof e.code === "number") {
17744
17762
  res.end(resultError(e.message || `Router Server error`, e.code));
17745
17763
  } else {
@@ -19519,7 +19537,7 @@ app
19519
19537
  10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
19520
19538
  `;
19521
19539
  // package.json
19522
- var version2 = "0.0.81";
19540
+ var version2 = "0.0.82";
19523
19541
 
19524
19542
  // agent/routes/route-create.ts
19525
19543
  app.route({
@@ -5,6 +5,12 @@ import { IncomingMessage, ServerResponse } from 'node:http';
5
5
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
6
6
  import { PluginInput, Hooks, Plugin } from '@opencode-ai/plugin';
7
7
 
8
+ type CustomErrorOptions = {
9
+ cause?: Error | string;
10
+ code?: number;
11
+ message?: string;
12
+ };
13
+
8
14
  declare class MockProcess {
9
15
  emitter?: EventEmitter;
10
16
  process?: NodeJS.Process;
@@ -314,7 +320,9 @@ declare class QueryRouter {
314
320
  }, SimpleObject>[];
315
321
  importRoutes(routes: Route[]): void;
316
322
  importRouter(router: QueryRouter): void;
317
- throw(code?: number | string, message?: string, tips?: string): void;
323
+ throw(code?: number | string, message?: string): void;
324
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
325
+ throw(opts?: CustomErrorOptions): void;
318
326
  hasRoute(path: string, key?: string): Route<{
319
327
  [key: string]: any;
320
328
  }, SimpleObject>;
@@ -2,6 +2,37 @@ import { EventEmitter } from 'eventemitter3';
2
2
  import { z } from 'zod';
3
3
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
4
 
5
+ type CustomErrorOptions = {
6
+ cause?: Error | string;
7
+ code?: number;
8
+ message?: string;
9
+ };
10
+ /** 自定义错误 */
11
+ declare class CustomError extends Error {
12
+ code?: number;
13
+ data?: any;
14
+ message: string;
15
+ constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions);
16
+ static fromCode(code?: number): CustomError;
17
+ static fromErrorData(code?: number, data?: any): CustomError;
18
+ static parseError(e: CustomError): {
19
+ code: number;
20
+ data: any;
21
+ message: string;
22
+ };
23
+ /**
24
+ * 判断 throw 的错误是否不是当前这个错误
25
+ * @param err
26
+ * @returns
27
+ */
28
+ static isError(error: unknown): error is CustomError;
29
+ parse(e?: CustomError): {
30
+ code: number;
31
+ data: any;
32
+ message: string;
33
+ };
34
+ }
35
+
5
36
  declare class MockProcess {
6
37
  emitter?: EventEmitter;
7
38
  process?: NodeJS.Process;
@@ -358,7 +389,9 @@ declare class QueryRouter {
358
389
  }, SimpleObject$1>[];
359
390
  importRoutes(routes: Route[]): void;
360
391
  importRouter(router: QueryRouter): void;
361
- throw(code?: number | string, message?: string, tips?: string): void;
392
+ throw(code?: number | string, message?: string): void;
393
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
394
+ throw(opts?: CustomErrorOptions): void;
362
395
  hasRoute(path: string, key?: string): Route<{
363
396
  [key: string]: any;
364
397
  }, SimpleObject$1>;
@@ -498,37 +531,6 @@ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
498
531
 
499
532
  type Schema = z.ZodType<any, any, any>;
500
533
 
501
- type CustomErrorOptions = {
502
- cause?: Error | string;
503
- code?: number;
504
- message?: string;
505
- };
506
- /** 自定义错误 */
507
- declare class CustomError extends Error {
508
- code?: number;
509
- data?: any;
510
- message: string;
511
- constructor(code?: number | string, opts?: CustomErrorOptions);
512
- static fromCode(code?: number): CustomError;
513
- static fromErrorData(code?: number, data?: any): CustomError;
514
- static parseError(e: CustomError): {
515
- code: number;
516
- data: any;
517
- message: string;
518
- };
519
- /**
520
- * 判断 throw 的错误是否不是当前这个错误
521
- * @param err
522
- * @returns
523
- */
524
- static isError(error: unknown): error is CustomError;
525
- parse(e?: CustomError): {
526
- code: number;
527
- data: any;
528
- message: string;
529
- };
530
- }
531
-
532
534
  type RouteObject = {
533
535
  [key: string]: RouteOpts;
534
536
  };
@@ -209,6 +209,10 @@ class CustomError extends Error {
209
209
  data;
210
210
  message;
211
211
  constructor(code, opts) {
212
+ if (typeof code === "object" && code !== null) {
213
+ opts = code;
214
+ code = opts.code || 500;
215
+ }
212
216
  let message = opts?.message || String(code);
213
217
  const cause = opts?.cause;
214
218
  super(message, { cause });
@@ -14458,7 +14462,16 @@ class QueryRouter {
14458
14462
  this.importRoutes(router.routes);
14459
14463
  }
14460
14464
  throw(...args) {
14461
- throw new CustomError(...args);
14465
+ const [args0, args1] = args;
14466
+ if (args0 && typeof args0 === "object") {
14467
+ throw new CustomError(args0);
14468
+ }
14469
+ if (args1 && typeof args1 === "object") {
14470
+ throw new CustomError(args0, args1);
14471
+ } else if (args1) {
14472
+ throw new CustomError(args0, { message: args1 });
14473
+ }
14474
+ throw new CustomError(args0);
14462
14475
  }
14463
14476
  hasRoute(path, key = "") {
14464
14477
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -2,6 +2,12 @@ import { EventEmitter } from 'eventemitter3';
2
2
  import { z } from 'zod';
3
3
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
4
 
5
+ type CustomErrorOptions = {
6
+ cause?: Error | string;
7
+ code?: number;
8
+ message?: string;
9
+ };
10
+
5
11
  declare class MockProcess {
6
12
  emitter?: EventEmitter;
7
13
  process?: NodeJS.Process;
@@ -311,7 +317,9 @@ declare class QueryRouter {
311
317
  }, SimpleObject$1>[];
312
318
  importRoutes(routes: Route[]): void;
313
319
  importRouter(router: QueryRouter): void;
314
- throw(code?: number | string, message?: string, tips?: string): void;
320
+ throw(code?: number | string, message?: string): void;
321
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
322
+ throw(opts?: CustomErrorOptions): void;
315
323
  hasRoute(path: string, key?: string): Route<{
316
324
  [key: string]: any;
317
325
  }, SimpleObject$1>;
package/dist/router.d.ts CHANGED
@@ -8,6 +8,37 @@ import http2 from 'node:http2';
8
8
  import { WebSocketServer } from 'ws';
9
9
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
10
10
 
11
+ type CustomErrorOptions = {
12
+ cause?: Error | string;
13
+ code?: number;
14
+ message?: string;
15
+ };
16
+ /** 自定义错误 */
17
+ declare class CustomError extends Error {
18
+ code?: number;
19
+ data?: any;
20
+ message: string;
21
+ constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions);
22
+ static fromCode(code?: number): CustomError;
23
+ static fromErrorData(code?: number, data?: any): CustomError;
24
+ static parseError(e: CustomError): {
25
+ code: number;
26
+ data: any;
27
+ message: string;
28
+ };
29
+ /**
30
+ * 判断 throw 的错误是否不是当前这个错误
31
+ * @param err
32
+ * @returns
33
+ */
34
+ static isError(error: unknown): error is CustomError;
35
+ parse(e?: CustomError): {
36
+ code: number;
37
+ data: any;
38
+ message: string;
39
+ };
40
+ }
41
+
11
42
  declare class MockProcess {
12
43
  emitter?: EventEmitter;
13
44
  process?: NodeJS.Process;
@@ -364,7 +395,9 @@ declare class QueryRouter {
364
395
  }, SimpleObject$1>[];
365
396
  importRoutes(routes: Route[]): void;
366
397
  importRouter(router: QueryRouter): void;
367
- throw(code?: number | string, message?: string, tips?: string): void;
398
+ throw(code?: number | string, message?: string): void;
399
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
400
+ throw(opts?: CustomErrorOptions): void;
368
401
  hasRoute(path: string, key?: string): Route<{
369
402
  [key: string]: any;
370
403
  }, SimpleObject$1>;
@@ -504,37 +537,6 @@ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
504
537
 
505
538
  type Schema = z.ZodType<any, any, any>;
506
539
 
507
- type CustomErrorOptions = {
508
- cause?: Error | string;
509
- code?: number;
510
- message?: string;
511
- };
512
- /** 自定义错误 */
513
- declare class CustomError extends Error {
514
- code?: number;
515
- data?: any;
516
- message: string;
517
- constructor(code?: number | string, opts?: CustomErrorOptions);
518
- static fromCode(code?: number): CustomError;
519
- static fromErrorData(code?: number, data?: any): CustomError;
520
- static parseError(e: CustomError): {
521
- code: number;
522
- data: any;
523
- message: string;
524
- };
525
- /**
526
- * 判断 throw 的错误是否不是当前这个错误
527
- * @param err
528
- * @returns
529
- */
530
- static isError(error: unknown): error is CustomError;
531
- parse(e?: CustomError): {
532
- code: number;
533
- data: any;
534
- message: string;
535
- };
536
- }
537
-
538
540
  type RouteObject = {
539
541
  [key: string]: RouteOpts;
540
542
  };
package/dist/router.js CHANGED
@@ -3020,6 +3020,10 @@ class CustomError extends Error {
3020
3020
  data;
3021
3021
  message;
3022
3022
  constructor(code, opts) {
3023
+ if (typeof code === "object" && code !== null) {
3024
+ opts = code;
3025
+ code = opts.code || 500;
3026
+ }
3023
3027
  let message = opts?.message || String(code);
3024
3028
  const cause = opts?.cause;
3025
3029
  super(message, { cause });
@@ -17291,7 +17295,16 @@ class QueryRouter {
17291
17295
  this.importRoutes(router.routes);
17292
17296
  }
17293
17297
  throw(...args) {
17294
- throw new CustomError(...args);
17298
+ const [args0, args1] = args;
17299
+ if (args0 && typeof args0 === "object") {
17300
+ throw new CustomError(args0);
17301
+ }
17302
+ if (args1 && typeof args1 === "object") {
17303
+ throw new CustomError(args0, args1);
17304
+ } else if (args1) {
17305
+ throw new CustomError(args0, { message: args1 });
17306
+ }
17307
+ throw new CustomError(args0);
17295
17308
  }
17296
17309
  hasRoute(path, key = "") {
17297
17310
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -17966,8 +17979,13 @@ class ServerBase {
17966
17979
  res.end(JSON.stringify(end));
17967
17980
  }
17968
17981
  } catch (e) {
17969
- console.error(e);
17970
17982
  res.setHeader("Content-Type", "application/json; charset=utf-8");
17983
+ if (CustomError.isError(e)) {
17984
+ const parsedError = CustomError.parseError(e);
17985
+ res.end(JSON.stringify(parsedError));
17986
+ return;
17987
+ }
17988
+ console.error(e);
17971
17989
  if (e.code && typeof e.code === "number") {
17972
17990
  res.end(resultError(e.message || `Router Server error`, e.code));
17973
17991
  } else {
package/dist/ws.d.ts CHANGED
@@ -52,6 +52,12 @@ declare class ReconnectingWebSocket {
52
52
  getRetryCount(): number;
53
53
  }
54
54
 
55
+ type CustomErrorOptions = {
56
+ cause?: Error | string;
57
+ code?: number;
58
+ message?: string;
59
+ };
60
+
55
61
  declare class MockProcess {
56
62
  emitter?: EventEmitter;
57
63
  process?: NodeJS.Process;
@@ -361,7 +367,9 @@ declare class QueryRouter {
361
367
  }, SimpleObject>[];
362
368
  importRoutes(routes: Route[]): void;
363
369
  importRouter(router: QueryRouter): void;
364
- throw(code?: number | string, message?: string, tips?: string): void;
370
+ throw(code?: number | string, message?: string): void;
371
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
372
+ throw(opts?: CustomErrorOptions): void;
365
373
  hasRoute(path: string, key?: string): Route<{
366
374
  [key: string]: any;
367
375
  }, SimpleObject>;
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.81",
4
+ "version": "0.0.82",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -1,4 +1,4 @@
1
- type CustomErrorOptions = {
1
+ export type CustomErrorOptions = {
2
2
  cause?: Error | string;
3
3
  code?: number;
4
4
  message?: string;
@@ -8,7 +8,11 @@ export class CustomError extends Error {
8
8
  code?: number;
9
9
  data?: any;
10
10
  message: string;
11
- constructor(code?: number | string, opts?: CustomErrorOptions) {
11
+ constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions) {
12
+ if (typeof code === 'object' && code !== null) {
13
+ opts = code;
14
+ code = opts.code || 500;
15
+ }
12
16
  let message = opts?.message || String(code);
13
17
  const cause = opts?.cause;
14
18
  super(message, { cause });
package/src/route.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CustomError } from './result/error.ts';
1
+ import { CustomError, CustomErrorOptions } from './result/error.ts';
2
2
  import { pick } from './utils/pick.ts';
3
3
  import { listenProcess, MockProcess } from './utils/listen-process.ts';
4
4
  import { z } from 'zod';
@@ -610,9 +610,21 @@ export class QueryRouter {
610
610
  importRouter(router: QueryRouter) {
611
611
  this.importRoutes(router.routes);
612
612
  }
613
- throw(code?: number | string, message?: string, tips?: string): void;
613
+ throw(code?: number | string, message?: string): void;
614
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
615
+ throw(opts?: CustomErrorOptions): void;
614
616
  throw(...args: any[]) {
615
- throw new CustomError(...args);
617
+ const [args0, args1] = args;
618
+ if (args0 && typeof args0 === 'object') {
619
+ throw new CustomError(args0);
620
+ }
621
+ if (args1 && typeof args1 === 'object') {
622
+ throw new CustomError(args0, args1);
623
+ } else if (args1) {
624
+ throw new CustomError(args0, { message: args1 });
625
+ }
626
+ // args1 不存在;
627
+ throw new CustomError(args0);
616
628
  }
617
629
  hasRoute(path: string, key: string = '') {
618
630
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -4,6 +4,7 @@ import * as cookie from './cookie.ts';
4
4
  import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
5
5
  import { parseIfJson } from '../utils/parse.ts';
6
6
  import { EventEmitter } from 'eventemitter3';
7
+ import { CustomError } from '../result/error.ts';
7
8
  type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
8
9
 
9
10
  export type HandleCtx = {
@@ -165,8 +166,13 @@ export class ServerBase implements ServerType {
165
166
  res.end(JSON.stringify(end));
166
167
  }
167
168
  } catch (e) {
168
- console.error(e);
169
169
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
170
+ if (CustomError.isError(e)) {
171
+ const parsedError = CustomError.parseError(e);
172
+ res.end(JSON.stringify(parsedError));
173
+ return;
174
+ }
175
+ console.error(e);
170
176
  if (e.code && typeof e.code === 'number') {
171
177
  res.end(resultError(e.message || `Router Server error`, e.code));
172
178
  } else {