@kevisual/router 0.0.81 → 0.0.83

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 });
@@ -3047,6 +3051,18 @@ class CustomError extends Error {
3047
3051
  static isError(error) {
3048
3052
  return error instanceof CustomError || typeof error === "object" && error !== null && "code" in error;
3049
3053
  }
3054
+ static throw(...args) {
3055
+ const [args0, args1] = args;
3056
+ if (args0 && typeof args0 === "object") {
3057
+ throw new CustomError(args0);
3058
+ }
3059
+ if (args1 && typeof args1 === "object") {
3060
+ throw new CustomError(args0, args1);
3061
+ } else if (args1) {
3062
+ throw new CustomError(args0, { message: args1 });
3063
+ }
3064
+ throw new CustomError(args0);
3065
+ }
3050
3066
  parse(e) {
3051
3067
  if (e) {
3052
3068
  return CustomError.parseError(e);
@@ -16996,7 +17012,7 @@ class Route {
16996
17012
  router.add(this, opts);
16997
17013
  }
16998
17014
  throw(...args) {
16999
- throw new CustomError(...args);
17015
+ CustomError.throw(...args);
17000
17016
  }
17001
17017
  }
17002
17018
  var toJSONSchemaRoute = (route) => {
@@ -17294,7 +17310,7 @@ class QueryRouter {
17294
17310
  this.importRoutes(router.routes);
17295
17311
  }
17296
17312
  throw(...args) {
17297
- throw new CustomError(...args);
17313
+ CustomError.throw(...args);
17298
17314
  }
17299
17315
  hasRoute(path, key = "") {
17300
17316
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -17738,8 +17754,13 @@ class ServerBase {
17738
17754
  res.end(JSON.stringify(end));
17739
17755
  }
17740
17756
  } catch (e) {
17741
- console.error(e);
17742
17757
  res.setHeader("Content-Type", "application/json; charset=utf-8");
17758
+ if (CustomError.isError(e)) {
17759
+ const parsedError = CustomError.parseError(e);
17760
+ res.end(JSON.stringify(parsedError));
17761
+ return;
17762
+ }
17763
+ console.error(e);
17743
17764
  if (e.code && typeof e.code === "number") {
17744
17765
  res.end(resultError(e.message || `Router Server error`, e.code));
17745
17766
  } else {
@@ -19519,7 +19540,7 @@ app
19519
19540
  10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
19520
19541
  `;
19521
19542
  // package.json
19522
- var version2 = "0.0.81";
19543
+ var version2 = "0.0.82";
19523
19544
 
19524
19545
  // agent/routes/route-create.ts
19525
19546
  app.route({
@@ -5,6 +5,17 @@ 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
+ interface throwError {
14
+ throw(code?: number | string, message?: string): void;
15
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
16
+ throw(opts?: CustomErrorOptions): void;
17
+ }
18
+
8
19
  declare class MockProcess {
9
20
  emitter?: EventEmitter;
10
21
  process?: NodeJS.Process;
@@ -92,7 +103,7 @@ type RouteContext<T = {
92
103
  [key: string]: any;
93
104
  }) => Promise<any>;
94
105
  index?: number;
95
- throw?: (code?: number | string, message?: string, tips?: string) => void;
106
+ throw?: throwError['throw'];
96
107
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
97
108
  needSerialize?: boolean;
98
109
  } & T;
@@ -135,7 +146,7 @@ declare const pickValue: readonly ["path", "key", "id", "description", "type", "
135
146
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
136
147
  declare class Route<U = {
137
148
  [key: string]: any;
138
- }, T extends SimpleObject = SimpleObject> {
149
+ }, T extends SimpleObject = SimpleObject> implements throwError {
139
150
  /**
140
151
  * 一级路径
141
152
  */
@@ -175,7 +186,7 @@ declare class Route<U = {
175
186
  add: (route: Route) => void;
176
187
  [key: string]: any;
177
188
  }, opts?: AddOpts): void;
178
- throw(code?: number | string, message?: string, tips?: string): void;
189
+ throw(...args: any[]): void;
179
190
  }
180
191
  /**
181
192
  * @parmas overwrite 是否覆盖已存在的route,默认true
@@ -183,7 +194,7 @@ declare class Route<U = {
183
194
  type AddOpts = {
184
195
  overwrite?: boolean;
185
196
  };
186
- declare class QueryRouter {
197
+ declare class QueryRouter implements throwError {
187
198
  appId: string;
188
199
  routes: Route[];
189
200
  maxNextRoute: number;
@@ -314,7 +325,7 @@ declare class QueryRouter {
314
325
  }, SimpleObject>[];
315
326
  importRoutes(routes: Route[]): void;
316
327
  importRouter(router: QueryRouter): void;
317
- throw(code?: number | string, message?: string, tips?: string): void;
328
+ throw(...args: any[]): void;
318
329
  hasRoute(path: string, key?: string): Route<{
319
330
  [key: string]: any;
320
331
  }, SimpleObject>;
@@ -2,6 +2,45 @@ 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
+ static throw(code?: number | string, message?: string): void;
30
+ static throw(code?: number | string, opts?: CustomErrorOptions): void;
31
+ static throw(opts?: CustomErrorOptions): void;
32
+ parse(e?: CustomError): {
33
+ code: number;
34
+ data: any;
35
+ message: string;
36
+ };
37
+ }
38
+ interface throwError {
39
+ throw(code?: number | string, message?: string): void;
40
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
41
+ throw(opts?: CustomErrorOptions): void;
42
+ }
43
+
5
44
  declare class MockProcess {
6
45
  emitter?: EventEmitter;
7
46
  process?: NodeJS.Process;
@@ -105,7 +144,7 @@ type RouteContext<T = {
105
144
  [key: string]: any;
106
145
  }) => Promise<any>;
107
146
  index?: number;
108
- throw?: (code?: number | string, message?: string, tips?: string) => void;
147
+ throw?: throwError['throw'];
109
148
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
110
149
  needSerialize?: boolean;
111
150
  } & T;
@@ -162,7 +201,7 @@ declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
162
201
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
163
202
  declare class Route<U = {
164
203
  [key: string]: any;
165
- }, T extends SimpleObject$1 = SimpleObject$1> {
204
+ }, T extends SimpleObject$1 = SimpleObject$1> implements throwError {
166
205
  /**
167
206
  * 一级路径
168
207
  */
@@ -202,7 +241,7 @@ declare class Route<U = {
202
241
  add: (route: Route) => void;
203
242
  [key: string]: any;
204
243
  }, opts?: AddOpts): void;
205
- throw(code?: number | string, message?: string, tips?: string): void;
244
+ throw(...args: any[]): void;
206
245
  }
207
246
  declare const toJSONSchema: (args: any, opts?: {
208
247
  mergeObject?: boolean;
@@ -227,7 +266,7 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
227
266
  type AddOpts = {
228
267
  overwrite?: boolean;
229
268
  };
230
- declare class QueryRouter {
269
+ declare class QueryRouter implements throwError {
231
270
  appId: string;
232
271
  routes: Route[];
233
272
  maxNextRoute: number;
@@ -358,7 +397,7 @@ declare class QueryRouter {
358
397
  }, SimpleObject$1>[];
359
398
  importRoutes(routes: Route[]): void;
360
399
  importRouter(router: QueryRouter): void;
361
- throw(code?: number | string, message?: string, tips?: string): void;
400
+ throw(...args: any[]): void;
362
401
  hasRoute(path: string, key?: string): Route<{
363
402
  [key: string]: any;
364
403
  }, SimpleObject$1>;
@@ -498,37 +537,6 @@ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
498
537
 
499
538
  type Schema = z.ZodType<any, any, any>;
500
539
 
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
540
  type RouteObject = {
533
541
  [key: string]: RouteOpts;
534
542
  };
@@ -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 });
@@ -236,6 +240,18 @@ class CustomError extends Error {
236
240
  static isError(error) {
237
241
  return error instanceof CustomError || typeof error === "object" && error !== null && "code" in error;
238
242
  }
243
+ static throw(...args) {
244
+ const [args0, args1] = args;
245
+ if (args0 && typeof args0 === "object") {
246
+ throw new CustomError(args0);
247
+ }
248
+ if (args1 && typeof args1 === "object") {
249
+ throw new CustomError(args0, args1);
250
+ } else if (args1) {
251
+ throw new CustomError(args0, { message: args1 });
252
+ }
253
+ throw new CustomError(args0);
254
+ }
239
255
  parse(e) {
240
256
  if (e) {
241
257
  return CustomError.parseError(e);
@@ -14160,7 +14176,7 @@ class Route {
14160
14176
  router.add(this, opts);
14161
14177
  }
14162
14178
  throw(...args) {
14163
- throw new CustomError(...args);
14179
+ CustomError.throw(...args);
14164
14180
  }
14165
14181
  }
14166
14182
  var toJSONSchemaRoute = (route) => {
@@ -14458,7 +14474,7 @@ class QueryRouter {
14458
14474
  this.importRoutes(router.routes);
14459
14475
  }
14460
14476
  throw(...args) {
14461
- throw new CustomError(...args);
14477
+ CustomError.throw(...args);
14462
14478
  }
14463
14479
  hasRoute(path, key = "") {
14464
14480
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -2,6 +2,17 @@ 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
+ interface throwError {
11
+ throw(code?: number | string, message?: string): void;
12
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
13
+ throw(opts?: CustomErrorOptions): void;
14
+ }
15
+
5
16
  declare class MockProcess {
6
17
  emitter?: EventEmitter;
7
18
  process?: NodeJS.Process;
@@ -89,7 +100,7 @@ type RouteContext<T = {
89
100
  [key: string]: any;
90
101
  }) => Promise<any>;
91
102
  index?: number;
92
- throw?: (code?: number | string, message?: string, tips?: string) => void;
103
+ throw?: throwError['throw'];
93
104
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
94
105
  needSerialize?: boolean;
95
106
  } & T;
@@ -132,7 +143,7 @@ declare const pickValue: readonly ["path", "key", "id", "description", "type", "
132
143
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
133
144
  declare class Route<U = {
134
145
  [key: string]: any;
135
- }, T extends SimpleObject$1 = SimpleObject$1> {
146
+ }, T extends SimpleObject$1 = SimpleObject$1> implements throwError {
136
147
  /**
137
148
  * 一级路径
138
149
  */
@@ -172,7 +183,7 @@ declare class Route<U = {
172
183
  add: (route: Route) => void;
173
184
  [key: string]: any;
174
185
  }, opts?: AddOpts): void;
175
- throw(code?: number | string, message?: string, tips?: string): void;
186
+ throw(...args: any[]): void;
176
187
  }
177
188
  /**
178
189
  * @parmas overwrite 是否覆盖已存在的route,默认true
@@ -180,7 +191,7 @@ declare class Route<U = {
180
191
  type AddOpts = {
181
192
  overwrite?: boolean;
182
193
  };
183
- declare class QueryRouter {
194
+ declare class QueryRouter implements throwError {
184
195
  appId: string;
185
196
  routes: Route[];
186
197
  maxNextRoute: number;
@@ -311,7 +322,7 @@ declare class QueryRouter {
311
322
  }, SimpleObject$1>[];
312
323
  importRoutes(routes: Route[]): void;
313
324
  importRouter(router: QueryRouter): void;
314
- throw(code?: number | string, message?: string, tips?: string): void;
325
+ throw(...args: any[]): void;
315
326
  hasRoute(path: string, key?: string): Route<{
316
327
  [key: string]: any;
317
328
  }, SimpleObject$1>;
package/dist/router.d.ts CHANGED
@@ -8,6 +8,45 @@ 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
+ static throw(code?: number | string, message?: string): void;
36
+ static throw(code?: number | string, opts?: CustomErrorOptions): void;
37
+ static throw(opts?: CustomErrorOptions): void;
38
+ parse(e?: CustomError): {
39
+ code: number;
40
+ data: any;
41
+ message: string;
42
+ };
43
+ }
44
+ interface throwError {
45
+ throw(code?: number | string, message?: string): void;
46
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
47
+ throw(opts?: CustomErrorOptions): void;
48
+ }
49
+
11
50
  declare class MockProcess {
12
51
  emitter?: EventEmitter;
13
52
  process?: NodeJS.Process;
@@ -111,7 +150,7 @@ type RouteContext<T = {
111
150
  [key: string]: any;
112
151
  }) => Promise<any>;
113
152
  index?: number;
114
- throw?: (code?: number | string, message?: string, tips?: string) => void;
153
+ throw?: throwError['throw'];
115
154
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
116
155
  needSerialize?: boolean;
117
156
  } & T;
@@ -168,7 +207,7 @@ declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
168
207
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
169
208
  declare class Route<U = {
170
209
  [key: string]: any;
171
- }, T extends SimpleObject$1 = SimpleObject$1> {
210
+ }, T extends SimpleObject$1 = SimpleObject$1> implements throwError {
172
211
  /**
173
212
  * 一级路径
174
213
  */
@@ -208,7 +247,7 @@ declare class Route<U = {
208
247
  add: (route: Route) => void;
209
248
  [key: string]: any;
210
249
  }, opts?: AddOpts): void;
211
- throw(code?: number | string, message?: string, tips?: string): void;
250
+ throw(...args: any[]): void;
212
251
  }
213
252
  declare const toJSONSchema: (args: any, opts?: {
214
253
  mergeObject?: boolean;
@@ -233,7 +272,7 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
233
272
  type AddOpts = {
234
273
  overwrite?: boolean;
235
274
  };
236
- declare class QueryRouter {
275
+ declare class QueryRouter implements throwError {
237
276
  appId: string;
238
277
  routes: Route[];
239
278
  maxNextRoute: number;
@@ -364,7 +403,7 @@ declare class QueryRouter {
364
403
  }, SimpleObject$1>[];
365
404
  importRoutes(routes: Route[]): void;
366
405
  importRouter(router: QueryRouter): void;
367
- throw(code?: number | string, message?: string, tips?: string): void;
406
+ throw(...args: any[]): void;
368
407
  hasRoute(path: string, key?: string): Route<{
369
408
  [key: string]: any;
370
409
  }, SimpleObject$1>;
@@ -504,37 +543,6 @@ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
504
543
 
505
544
  type Schema = z.ZodType<any, any, any>;
506
545
 
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
546
  type RouteObject = {
539
547
  [key: string]: RouteOpts;
540
548
  };
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 });
@@ -3047,6 +3051,18 @@ class CustomError extends Error {
3047
3051
  static isError(error) {
3048
3052
  return error instanceof CustomError || typeof error === "object" && error !== null && "code" in error;
3049
3053
  }
3054
+ static throw(...args) {
3055
+ const [args0, args1] = args;
3056
+ if (args0 && typeof args0 === "object") {
3057
+ throw new CustomError(args0);
3058
+ }
3059
+ if (args1 && typeof args1 === "object") {
3060
+ throw new CustomError(args0, args1);
3061
+ } else if (args1) {
3062
+ throw new CustomError(args0, { message: args1 });
3063
+ }
3064
+ throw new CustomError(args0);
3065
+ }
3050
3066
  parse(e) {
3051
3067
  if (e) {
3052
3068
  return CustomError.parseError(e);
@@ -16993,7 +17009,7 @@ class Route {
16993
17009
  router.add(this, opts);
16994
17010
  }
16995
17011
  throw(...args) {
16996
- throw new CustomError(...args);
17012
+ CustomError.throw(...args);
16997
17013
  }
16998
17014
  }
16999
17015
  var toJSONSchemaRoute = (route) => {
@@ -17291,7 +17307,7 @@ class QueryRouter {
17291
17307
  this.importRoutes(router.routes);
17292
17308
  }
17293
17309
  throw(...args) {
17294
- throw new CustomError(...args);
17310
+ CustomError.throw(...args);
17295
17311
  }
17296
17312
  hasRoute(path, key = "") {
17297
17313
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -17966,8 +17982,13 @@ class ServerBase {
17966
17982
  res.end(JSON.stringify(end));
17967
17983
  }
17968
17984
  } catch (e) {
17969
- console.error(e);
17970
17985
  res.setHeader("Content-Type", "application/json; charset=utf-8");
17986
+ if (CustomError.isError(e)) {
17987
+ const parsedError = CustomError.parseError(e);
17988
+ res.end(JSON.stringify(parsedError));
17989
+ return;
17990
+ }
17991
+ console.error(e);
17971
17992
  if (e.code && typeof e.code === "number") {
17972
17993
  res.end(resultError(e.message || `Router Server error`, e.code));
17973
17994
  } else {
package/dist/ws.d.ts CHANGED
@@ -52,6 +52,17 @@ 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
+ interface throwError {
61
+ throw(code?: number | string, message?: string): void;
62
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
63
+ throw(opts?: CustomErrorOptions): void;
64
+ }
65
+
55
66
  declare class MockProcess {
56
67
  emitter?: EventEmitter;
57
68
  process?: NodeJS.Process;
@@ -139,7 +150,7 @@ type RouteContext<T = {
139
150
  [key: string]: any;
140
151
  }) => Promise<any>;
141
152
  index?: number;
142
- throw?: (code?: number | string, message?: string, tips?: string) => void;
153
+ throw?: throwError['throw'];
143
154
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
144
155
  needSerialize?: boolean;
145
156
  } & T;
@@ -182,7 +193,7 @@ declare const pickValue: readonly ["path", "key", "id", "description", "type", "
182
193
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
183
194
  declare class Route<U = {
184
195
  [key: string]: any;
185
- }, T extends SimpleObject = SimpleObject> {
196
+ }, T extends SimpleObject = SimpleObject> implements throwError {
186
197
  /**
187
198
  * 一级路径
188
199
  */
@@ -222,7 +233,7 @@ declare class Route<U = {
222
233
  add: (route: Route) => void;
223
234
  [key: string]: any;
224
235
  }, opts?: AddOpts): void;
225
- throw(code?: number | string, message?: string, tips?: string): void;
236
+ throw(...args: any[]): void;
226
237
  }
227
238
  /**
228
239
  * @parmas overwrite 是否覆盖已存在的route,默认true
@@ -230,7 +241,7 @@ declare class Route<U = {
230
241
  type AddOpts = {
231
242
  overwrite?: boolean;
232
243
  };
233
- declare class QueryRouter {
244
+ declare class QueryRouter implements throwError {
234
245
  appId: string;
235
246
  routes: Route[];
236
247
  maxNextRoute: number;
@@ -361,7 +372,7 @@ declare class QueryRouter {
361
372
  }, SimpleObject>[];
362
373
  importRoutes(routes: Route[]): void;
363
374
  importRouter(router: QueryRouter): void;
364
- throw(code?: number | string, message?: string, tips?: string): void;
375
+ throw(...args: any[]): void;
365
376
  hasRoute(path: string, key?: string): Route<{
366
377
  [key: string]: any;
367
378
  }, 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.83",
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 });
@@ -43,6 +47,22 @@ export class CustomError extends Error {
43
47
  static isError(error: unknown): error is CustomError {
44
48
  return error instanceof CustomError || (typeof error === 'object' && error !== null && 'code' in error);
45
49
  }
50
+ static throw(code?: number | string, message?: string): void;
51
+ static throw(code?: number | string, opts?: CustomErrorOptions): void;
52
+ static throw(opts?: CustomErrorOptions): void;
53
+ static throw(...args: any[]) {
54
+ const [args0, args1] = args;
55
+ if (args0 && typeof args0 === 'object') {
56
+ throw new CustomError(args0);
57
+ }
58
+ if (args1 && typeof args1 === 'object') {
59
+ throw new CustomError(args0, args1);
60
+ } else if (args1) {
61
+ throw new CustomError(args0, { message: args1 });
62
+ }
63
+ // args1 不存在;
64
+ throw new CustomError(args0);
65
+ }
46
66
  parse(e?: CustomError) {
47
67
  if (e) {
48
68
  return CustomError.parseError(e);
@@ -57,6 +77,12 @@ export class CustomError extends Error {
57
77
  }
58
78
  }
59
79
 
80
+ export interface throwError {
81
+ throw(code?: number | string, message?: string): void;
82
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
83
+ throw(opts?: CustomErrorOptions): void;
84
+ }
85
+
60
86
  /*
61
87
  try {
62
88
  //
package/src/route.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CustomError } from './result/error.ts';
1
+ import { CustomError, throwError, 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';
@@ -56,7 +56,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
56
56
  /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
57
57
  run?: (message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
58
58
  index?: number;
59
- throw?: (code?: number | string, message?: string, tips?: string) => void;
59
+ throw?: throwError['throw'];
60
60
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
61
61
  needSerialize?: boolean;
62
62
  } & T;
@@ -123,7 +123,7 @@ export const createSkill = <T = SimpleObject>(skill: Skill<T>): Skill<T> => {
123
123
 
124
124
  export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
125
125
 
126
- export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleObject> {
126
+ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleObject> implements throwError {
127
127
  /**
128
128
  * 一级路径
129
129
  */
@@ -242,9 +242,8 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
242
242
  addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }, opts?: AddOpts) {
243
243
  router.add(this, opts);
244
244
  }
245
- throw(code?: number | string, message?: string, tips?: string): void;
246
245
  throw(...args: any[]) {
247
- throw new CustomError(...args);
246
+ CustomError.throw(...args);
248
247
  }
249
248
  }
250
249
 
@@ -263,7 +262,7 @@ export const fromJSONSchema = schema.fromJSONSchema;
263
262
  * @parmas overwrite 是否覆盖已存在的route,默认true
264
263
  */
265
264
  export type AddOpts = { overwrite?: boolean };
266
- export class QueryRouter {
265
+ export class QueryRouter implements throwError {
267
266
  appId: string = '';
268
267
  routes: Route[];
269
268
  maxNextRoute = 40;
@@ -610,9 +609,8 @@ export class QueryRouter {
610
609
  importRouter(router: QueryRouter) {
611
610
  this.importRoutes(router.routes);
612
611
  }
613
- throw(code?: number | string, message?: string, tips?: string): void;
614
612
  throw(...args: any[]) {
615
- throw new CustomError(...args);
613
+ CustomError.throw(...args);
616
614
  }
617
615
  hasRoute(path: string, key: string = '') {
618
616
  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 {