@kevisual/router 0.0.29 → 0.0.31

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.
@@ -141,7 +141,7 @@ type RouteOpts = {
141
141
  [key: string]: Rule;
142
142
  };
143
143
  schema?: {
144
- [key: string]: Schema<any>;
144
+ [key: string]: any;
145
145
  };
146
146
  isVerify?: boolean;
147
147
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
@@ -157,7 +157,7 @@ type RouteOpts = {
157
157
  isDebug?: boolean;
158
158
  };
159
159
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
160
- declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
160
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware", "metadata"];
161
161
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
162
162
  declare class Route<U = {
163
163
  [key: string]: any;
@@ -182,7 +182,7 @@ declare class Route<U = {
182
182
  type?: string;
183
183
  private _validator?;
184
184
  schema?: {
185
- [key: string]: Schema<any>;
185
+ [key: string]: any;
186
186
  };
187
187
  data?: any;
188
188
  /**
@@ -193,7 +193,7 @@ declare class Route<U = {
193
193
  * 是否开启debug,开启后会打印错误信息
194
194
  */
195
195
  isDebug?: boolean;
196
- constructor(path: string, key?: string, opts?: RouteOpts);
196
+ constructor(path?: string, key?: string, opts?: RouteOpts);
197
197
  private createSchema;
198
198
  /**
199
199
  * set validator and create schema
@@ -233,6 +233,8 @@ declare class Route<U = {
233
233
  setValidator(validator: {
234
234
  [key: string]: Rule;
235
235
  }): this;
236
+ prompt(description: string): this;
237
+ prompt(description: Function): this;
236
238
  define<T extends {
237
239
  [key: string]: any;
238
240
  } = RouterContextT>(opts: DefineRouteOpts): this;
@@ -245,6 +247,7 @@ declare class Route<U = {
245
247
  define<T extends {
246
248
  [key: string]: any;
247
249
  } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
250
+ update(opts: DefineRouteOpts, checkList?: string[]): this;
248
251
  addTo(router: QueryRouter | {
249
252
  add: (route: Route) => void;
250
253
  [key: string]: any;
@@ -313,6 +316,7 @@ declare class QueryRouter {
313
316
  * @returns
314
317
  */
315
318
  queryRoute(message: {
319
+ id?: string;
316
320
  path: string;
317
321
  key?: string;
318
322
  payload?: any;
@@ -334,7 +338,8 @@ declare class QueryRouter {
334
338
  * 获取handle函数, 这里会去执行parse函数
335
339
  */
336
340
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
337
- path: string;
341
+ id?: string;
342
+ path?: string;
338
343
  key?: string;
339
344
  [key: string]: any;
340
345
  }, handleContext?: RouteContext) => Promise<{
@@ -408,6 +413,8 @@ declare class QueryRouterServer extends QueryRouter {
408
413
  route(path: string, key?: string): Route<Required<RouteContext>>;
409
414
  route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
410
415
  route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
416
+ prompt(description: string): Route<Required<RouteContext>>;
417
+ prompt(description: Function): Route<Required<RouteContext>>;
411
418
  /**
412
419
  * 等于queryRoute,但是调用了handle
413
420
  * @param param0
@@ -419,6 +426,7 @@ declare class QueryRouterServer extends QueryRouter {
419
426
  payload?: any;
420
427
  }): Promise<any>;
421
428
  }
429
+ declare const Mini: typeof QueryRouterServer;
422
430
 
423
431
  /** 自定义错误 */
424
432
  declare class CustomError extends Error {
@@ -529,5 +537,5 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
529
537
 
530
538
  declare const App: typeof QueryRouterServer;
531
539
 
532
- export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
540
+ export { App, CustomError, Mini, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
533
541
  export type { RouteArray, RouteContext, RouteObject, RouteOpts, Rule, Run, Schema };
@@ -4788,7 +4788,7 @@ const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 10
4788
4788
  }
4789
4789
  };
4790
4790
 
4791
- const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
4791
+ const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware', 'metadata'];
4792
4792
  class Route {
4793
4793
  /**
4794
4794
  * 一级路径
@@ -4817,7 +4817,10 @@ class Route {
4817
4817
  * 是否开启debug,开启后会打印错误信息
4818
4818
  */
4819
4819
  isDebug;
4820
- constructor(path, key = '', opts) {
4820
+ constructor(path = '', key = '', opts) {
4821
+ if (!path) {
4822
+ path = nanoid$1(8);
4823
+ }
4821
4824
  path = path.trim();
4822
4825
  key = key.trim();
4823
4826
  this.path = path;
@@ -4946,6 +4949,16 @@ class Route {
4946
4949
  this.validator = validator;
4947
4950
  return this;
4948
4951
  }
4952
+ prompt(...args) {
4953
+ const [description] = args;
4954
+ if (typeof description === 'string') {
4955
+ this.description = description;
4956
+ }
4957
+ else if (typeof description === 'function') {
4958
+ this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
4959
+ }
4960
+ return this;
4961
+ }
4949
4962
  define(...args) {
4950
4963
  const [path, key, opts] = args;
4951
4964
  // 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
@@ -4985,6 +4998,26 @@ class Route {
4985
4998
  }
4986
4999
  return this;
4987
5000
  }
5001
+ update(opts, checkList) {
5002
+ const keys = Object.keys(opts);
5003
+ const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
5004
+ checkList = checkList || defaultCheckList;
5005
+ for (let item of keys) {
5006
+ if (!checkList.includes(item)) {
5007
+ continue;
5008
+ }
5009
+ if (item === 'validator') {
5010
+ this.validator = opts[item];
5011
+ continue;
5012
+ }
5013
+ if (item === 'middleware') {
5014
+ this.middleware = this.middleware.concat(opts[item]);
5015
+ continue;
5016
+ }
5017
+ this[item] = opts[item];
5018
+ }
5019
+ return this;
5020
+ }
4988
5021
  addTo(router) {
4989
5022
  router.add(this);
4990
5023
  }
@@ -5265,19 +5298,20 @@ class QueryRouter {
5265
5298
  */
5266
5299
  async call(message, ctx) {
5267
5300
  let path = message.path;
5268
- message.key;
5301
+ let key = message.key;
5269
5302
  if (message.id) {
5270
5303
  const route = this.routes.find((r) => r.id === message.id);
5271
5304
  if (route) {
5272
5305
  path = route.path;
5273
- route.key;
5306
+ key = route.key;
5274
5307
  }
5275
5308
  else {
5276
5309
  return { code: 404, body: null, message: 'Not found route' };
5277
5310
  }
5311
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5278
5312
  }
5279
5313
  else if (path) {
5280
- return await this.parse({ ...message, path }, { ...this.context, ...ctx });
5314
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5281
5315
  }
5282
5316
  else {
5283
5317
  return { code: 404, body: null, message: 'Not found path' };
@@ -5317,6 +5351,17 @@ class QueryRouter {
5317
5351
  return async (msg, handleContext) => {
5318
5352
  try {
5319
5353
  const context = { ...ctx, ...handleContext };
5354
+ if (msg.id) {
5355
+ const route = router.routes.find((r) => r.id === msg.id);
5356
+ if (route) {
5357
+ msg.path = route.path;
5358
+ msg.key = route.key;
5359
+ }
5360
+ else {
5361
+ return { code: 404, message: 'Not found route' };
5362
+ }
5363
+ }
5364
+ // @ts-ignore
5320
5365
  const res = await router.parse(msg, context);
5321
5366
  if (wrapperFn) {
5322
5367
  res.data = res.body;
@@ -5400,6 +5445,17 @@ class QueryRouterServer extends QueryRouter {
5400
5445
  }
5401
5446
  return new Route(path, key, opts);
5402
5447
  }
5448
+ prompt(...args) {
5449
+ const [desc] = args;
5450
+ let description = '';
5451
+ if (typeof desc === 'string') {
5452
+ description = desc;
5453
+ }
5454
+ else if (typeof desc === 'function') {
5455
+ description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
5456
+ }
5457
+ return new Route('', '', { description });
5458
+ }
5403
5459
  /**
5404
5460
  * 等于queryRoute,但是调用了handle
5405
5461
  * @param param0
@@ -5431,6 +5487,7 @@ class QueryRouterServer extends QueryRouter {
5431
5487
  }
5432
5488
  }
5433
5489
  }
5490
+ const Mini = QueryRouterServer;
5434
5491
 
5435
5492
  const parseBody = async (req) => {
5436
5493
  return new Promise((resolve, reject) => {
@@ -5635,4 +5692,4 @@ class QueryUtil {
5635
5692
 
5636
5693
  const App = QueryRouterServer;
5637
5694
 
5638
- export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
5695
+ export { App, CustomError, Mini, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
package/dist/router.d.ts CHANGED
@@ -145,7 +145,7 @@ type RouteOpts = {
145
145
  [key: string]: Rule;
146
146
  };
147
147
  schema?: {
148
- [key: string]: Schema<any>;
148
+ [key: string]: any;
149
149
  };
150
150
  isVerify?: boolean;
151
151
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
@@ -161,7 +161,7 @@ type RouteOpts = {
161
161
  isDebug?: boolean;
162
162
  };
163
163
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
164
- declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
164
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware", "metadata"];
165
165
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
166
166
  declare class Route<U = {
167
167
  [key: string]: any;
@@ -186,7 +186,7 @@ declare class Route<U = {
186
186
  type?: string;
187
187
  private _validator?;
188
188
  schema?: {
189
- [key: string]: Schema<any>;
189
+ [key: string]: any;
190
190
  };
191
191
  data?: any;
192
192
  /**
@@ -197,7 +197,7 @@ declare class Route<U = {
197
197
  * 是否开启debug,开启后会打印错误信息
198
198
  */
199
199
  isDebug?: boolean;
200
- constructor(path: string, key?: string, opts?: RouteOpts);
200
+ constructor(path?: string, key?: string, opts?: RouteOpts);
201
201
  private createSchema;
202
202
  /**
203
203
  * set validator and create schema
@@ -237,6 +237,8 @@ declare class Route<U = {
237
237
  setValidator(validator: {
238
238
  [key: string]: Rule;
239
239
  }): this;
240
+ prompt(description: string): this;
241
+ prompt(description: Function): this;
240
242
  define<T extends {
241
243
  [key: string]: any;
242
244
  } = RouterContextT>(opts: DefineRouteOpts): this;
@@ -249,6 +251,7 @@ declare class Route<U = {
249
251
  define<T extends {
250
252
  [key: string]: any;
251
253
  } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
254
+ update(opts: DefineRouteOpts, checkList?: string[]): this;
252
255
  addTo(router: QueryRouter | {
253
256
  add: (route: Route) => void;
254
257
  [key: string]: any;
@@ -317,6 +320,7 @@ declare class QueryRouter {
317
320
  * @returns
318
321
  */
319
322
  queryRoute(message: {
323
+ id?: string;
320
324
  path: string;
321
325
  key?: string;
322
326
  payload?: any;
@@ -338,7 +342,8 @@ declare class QueryRouter {
338
342
  * 获取handle函数, 这里会去执行parse函数
339
343
  */
340
344
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
341
- path: string;
345
+ id?: string;
346
+ path?: string;
342
347
  key?: string;
343
348
  [key: string]: any;
344
349
  }, handleContext?: RouteContext) => Promise<{
@@ -412,6 +417,8 @@ declare class QueryRouterServer extends QueryRouter {
412
417
  route(path: string, key?: string): Route<Required<RouteContext>>;
413
418
  route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
414
419
  route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
420
+ prompt(description: string): Route<Required<RouteContext>>;
421
+ prompt(description: Function): Route<Required<RouteContext>>;
415
422
  /**
416
423
  * 等于queryRoute,但是调用了handle
417
424
  * @param param0
@@ -423,6 +430,7 @@ declare class QueryRouterServer extends QueryRouter {
423
430
  payload?: any;
424
431
  }): Promise<any>;
425
432
  }
433
+ declare const Mini: typeof QueryRouterServer;
426
434
 
427
435
  declare class Connect {
428
436
  path: string;
@@ -717,8 +725,11 @@ declare class App<T = {}, U = AppReqRes> {
717
725
  route(path: string, key?: string): Route<U>;
718
726
  route(path: string, opts?: RouteOpts): Route<U>;
719
727
  route(path: string, key?: string, opts?: RouteOpts): Route<U>;
728
+ prompt(description: string): Route<Required<RouteContext>>;
729
+ prompt(description: Function): Route<Required<RouteContext>>;
720
730
  call(message: {
721
- path: string;
731
+ id?: string;
732
+ path?: string;
722
733
  key?: string;
723
734
  payload?: any;
724
735
  }, ctx?: RouteContext & {
@@ -744,5 +755,5 @@ declare class App<T = {}, U = AppReqRes> {
744
755
  onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
745
756
  }
746
757
 
747
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
758
+ export { App, Connect, CustomError, Mini, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
748
759
  export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run, Schema };
package/dist/router.js CHANGED
@@ -4810,7 +4810,7 @@ const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 10
4810
4810
  }
4811
4811
  };
4812
4812
 
4813
- const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
4813
+ const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware', 'metadata'];
4814
4814
  class Route {
4815
4815
  /**
4816
4816
  * 一级路径
@@ -4839,7 +4839,10 @@ class Route {
4839
4839
  * 是否开启debug,开启后会打印错误信息
4840
4840
  */
4841
4841
  isDebug;
4842
- constructor(path, key = '', opts) {
4842
+ constructor(path = '', key = '', opts) {
4843
+ if (!path) {
4844
+ path = nanoid$1(8);
4845
+ }
4843
4846
  path = path.trim();
4844
4847
  key = key.trim();
4845
4848
  this.path = path;
@@ -4968,6 +4971,16 @@ class Route {
4968
4971
  this.validator = validator;
4969
4972
  return this;
4970
4973
  }
4974
+ prompt(...args) {
4975
+ const [description] = args;
4976
+ if (typeof description === 'string') {
4977
+ this.description = description;
4978
+ }
4979
+ else if (typeof description === 'function') {
4980
+ this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
4981
+ }
4982
+ return this;
4983
+ }
4971
4984
  define(...args) {
4972
4985
  const [path, key, opts] = args;
4973
4986
  // 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
@@ -5007,6 +5020,26 @@ class Route {
5007
5020
  }
5008
5021
  return this;
5009
5022
  }
5023
+ update(opts, checkList) {
5024
+ const keys = Object.keys(opts);
5025
+ const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
5026
+ checkList = checkList || defaultCheckList;
5027
+ for (let item of keys) {
5028
+ if (!checkList.includes(item)) {
5029
+ continue;
5030
+ }
5031
+ if (item === 'validator') {
5032
+ this.validator = opts[item];
5033
+ continue;
5034
+ }
5035
+ if (item === 'middleware') {
5036
+ this.middleware = this.middleware.concat(opts[item]);
5037
+ continue;
5038
+ }
5039
+ this[item] = opts[item];
5040
+ }
5041
+ return this;
5042
+ }
5010
5043
  addTo(router) {
5011
5044
  router.add(this);
5012
5045
  }
@@ -5287,19 +5320,20 @@ class QueryRouter {
5287
5320
  */
5288
5321
  async call(message, ctx) {
5289
5322
  let path = message.path;
5290
- message.key;
5323
+ let key = message.key;
5291
5324
  if (message.id) {
5292
5325
  const route = this.routes.find((r) => r.id === message.id);
5293
5326
  if (route) {
5294
5327
  path = route.path;
5295
- route.key;
5328
+ key = route.key;
5296
5329
  }
5297
5330
  else {
5298
5331
  return { code: 404, body: null, message: 'Not found route' };
5299
5332
  }
5333
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5300
5334
  }
5301
5335
  else if (path) {
5302
- return await this.parse({ ...message, path }, { ...this.context, ...ctx });
5336
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5303
5337
  }
5304
5338
  else {
5305
5339
  return { code: 404, body: null, message: 'Not found path' };
@@ -5339,6 +5373,17 @@ class QueryRouter {
5339
5373
  return async (msg, handleContext) => {
5340
5374
  try {
5341
5375
  const context = { ...ctx, ...handleContext };
5376
+ if (msg.id) {
5377
+ const route = router.routes.find((r) => r.id === msg.id);
5378
+ if (route) {
5379
+ msg.path = route.path;
5380
+ msg.key = route.key;
5381
+ }
5382
+ else {
5383
+ return { code: 404, message: 'Not found route' };
5384
+ }
5385
+ }
5386
+ // @ts-ignore
5342
5387
  const res = await router.parse(msg, context);
5343
5388
  if (wrapperFn) {
5344
5389
  res.data = res.body;
@@ -5422,6 +5467,17 @@ class QueryRouterServer extends QueryRouter {
5422
5467
  }
5423
5468
  return new Route(path, key, opts);
5424
5469
  }
5470
+ prompt(...args) {
5471
+ const [desc] = args;
5472
+ let description = '';
5473
+ if (typeof desc === 'string') {
5474
+ description = desc;
5475
+ }
5476
+ else if (typeof desc === 'function') {
5477
+ description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
5478
+ }
5479
+ return new Route('', '', { description });
5480
+ }
5425
5481
  /**
5426
5482
  * 等于queryRoute,但是调用了handle
5427
5483
  * @param param0
@@ -5453,6 +5509,7 @@ class QueryRouterServer extends QueryRouter {
5453
5509
  }
5454
5510
  }
5455
5511
  }
5512
+ const Mini = QueryRouterServer;
5456
5513
 
5457
5514
  class Connect {
5458
5515
  path;
@@ -11325,6 +11382,17 @@ class App {
11325
11382
  }
11326
11383
  return new Route(path, key, opts);
11327
11384
  }
11385
+ prompt(...args) {
11386
+ const [desc] = args;
11387
+ let description = '';
11388
+ if (typeof desc === 'string') {
11389
+ description = desc;
11390
+ }
11391
+ else if (typeof desc === 'function') {
11392
+ description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
11393
+ }
11394
+ return new Route('', '', { description });
11395
+ }
11328
11396
  async call(message, ctx) {
11329
11397
  const router = this.router;
11330
11398
  return await router.call(message, ctx);
@@ -11355,4 +11423,4 @@ class App {
11355
11423
  }
11356
11424
  }
11357
11425
 
11358
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
11426
+ export { App, Connect, CustomError, Mini, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
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.29",
4
+ "version": "0.0.31",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -24,18 +24,18 @@
24
24
  "@kevisual/local-proxy": "^0.0.6",
25
25
  "@kevisual/query": "^0.0.29",
26
26
  "@rollup/plugin-alias": "^5.1.1",
27
- "@rollup/plugin-commonjs": "28.0.6",
27
+ "@rollup/plugin-commonjs": "28.0.8",
28
28
  "@rollup/plugin-node-resolve": "^16.0.3",
29
- "@rollup/plugin-typescript": "^12.1.4",
29
+ "@rollup/plugin-typescript": "^12.3.0",
30
30
  "@types/lodash-es": "^4.17.12",
31
- "@types/node": "^24.7.2",
31
+ "@types/node": "^24.9.1",
32
32
  "@types/send": "^1.2.0",
33
33
  "@types/ws": "^8.18.1",
34
34
  "@types/xml2js": "^0.4.14",
35
35
  "cookie": "^1.0.2",
36
36
  "lodash-es": "^4.17.21",
37
37
  "nanoid": "^5.1.6",
38
- "rollup": "^4.52.4",
38
+ "rollup": "^4.52.5",
39
39
  "rollup-plugin-dts": "^6.2.3",
40
40
  "ts-loader": "^9.5.4",
41
41
  "ts-node": "^10.9.2",
package/src/app.ts CHANGED
@@ -5,7 +5,7 @@ import { CustomError } from './result/error.ts';
5
5
  import { handleServer } from './server/handle-server.ts';
6
6
  import { IncomingMessage, ServerResponse } from 'http';
7
7
 
8
- type RouterHandle = (msg: { path: string; [key: string]: any }) => { code: string; data?: any; message?: string; [key: string]: any };
8
+ type RouterHandle = (msg: { path: string;[key: string]: any }) => { code: string; data?: any; message?: string;[key: string]: any };
9
9
  type AppOptions<T = {}> = {
10
10
  router?: QueryRouter;
11
11
  server?: Server;
@@ -82,7 +82,20 @@ export class App<T = {}, U = AppReqRes> {
82
82
  }
83
83
  return new Route(path, key, opts);
84
84
  }
85
- async call(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
85
+ prompt(description: string): Route<Required<RouteContext>>;
86
+ prompt(description: Function): Route<Required<RouteContext>>;
87
+ prompt(...args: any[]) {
88
+ const [desc] = args;
89
+ let description = ''
90
+ if (typeof desc === 'string') {
91
+ description = desc;
92
+ } else if (typeof desc === 'function') {
93
+ description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
94
+ }
95
+ return new Route('', '', { description });
96
+ }
97
+
98
+ async call(message: { id?: string, path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
86
99
  const router = this.router;
87
100
  return await router.call(message, ctx);
88
101
  }
package/src/browser.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { Route, QueryRouter, QueryRouterServer } from './route.ts';
1
+ export { Route, QueryRouter, QueryRouterServer, Mini } from './route.ts';
2
2
 
3
3
  export type { Rule, Schema } from './validator/index.ts';
4
4
 
package/src/chat.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { QueryRouter } from "./route.ts";
2
+
3
+ type RouterChatOptions = {
4
+ router?: QueryRouter;
5
+ }
6
+ export class RouterChat {
7
+ router: QueryRouter;
8
+ prompt: string = '';
9
+ constructor(opts?: RouterChatOptions) {
10
+ this.router = opts?.router || new QueryRouter();
11
+ }
12
+ prefix(wrapperFn?: (routes: any[]) => string) {
13
+ if (this.prompt) {
14
+ return this.prompt;
15
+ }
16
+ let _prompt = `你是一个调用函数工具的助手,当用户询问时,如果拥有工具,请返回 JSON 数据,数据的值的内容是 id 和 payload 。如果有参数,请放到 payload 当中。
17
+
18
+ 下面是你可以使用的工具列表:
19
+
20
+ `;
21
+ if (!wrapperFn) {
22
+ _prompt += this.router.routes.map(r => `工具名称: ${r.id}\n描述: ${r.description}\n`).join('\n');
23
+ } else {
24
+ _prompt += wrapperFn(this.router.exportRoutes());
25
+ }
26
+ _prompt += `当你需要使用工具时,请严格按照以下格式返回:
27
+ {
28
+ "id": "工具名称",
29
+ "payload": {
30
+ // 参数列表
31
+ }
32
+ }
33
+ 如果你不需要使用工具,直接返回用户想要的内容即可,不要返回任何多余的信息。`;
34
+ return _prompt;
35
+ }
36
+ chat() {
37
+ const prompt = this.prefix();
38
+ return prompt;
39
+ }
40
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { Route, QueryRouter, QueryRouterServer } from './route.ts';
1
+ export { Route, QueryRouter, QueryRouterServer, Mini } from './route.ts';
2
2
  export { Connect, QueryConnect } from './connect.ts';
3
3
 
4
4
  export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
@@ -11,7 +11,9 @@ export { Server, handleServer } from './server/index.ts';
11
11
  */
12
12
  export { CustomError } from './result/error.ts';
13
13
 
14
- export { Rule, Schema, createSchema } from './validator/index.ts';
14
+ export { createSchema } from './validator/index.ts';
15
+
16
+ export type { Rule, Schema, } from './validator/index.ts';
15
17
 
16
18
  export { App } from './app.ts';
17
19
 
package/src/route.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { nanoid } from 'nanoid';
1
+ import { nanoid, random } from 'nanoid';
2
2
  import { CustomError } from './result/error.ts';
3
3
  import { Schema, Rule, createSchema } from './validator/index.ts';
4
4
  import { pick } from './utils/pick.ts';
@@ -88,7 +88,7 @@ export type RouteOpts = {
88
88
  * }
89
89
  */
90
90
  validator?: { [key: string]: Rule };
91
- schema?: { [key: string]: Schema<any> };
91
+ schema?: { [key: string]: any };
92
92
  isVerify?: boolean;
93
93
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
94
94
  verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
@@ -103,7 +103,7 @@ export type RouteOpts = {
103
103
  isDebug?: boolean;
104
104
  };
105
105
  export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
106
- const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'] as const;
106
+ const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware', 'metadata'] as const;
107
107
  export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
108
108
  export class Route<U = { [key: string]: any }> {
109
109
  /**
@@ -123,7 +123,7 @@ export class Route<U = { [key: string]: any }> {
123
123
  middleware?: RouteMiddleware[]; // middleware
124
124
  type? = 'route';
125
125
  private _validator?: { [key: string]: Rule };
126
- schema?: { [key: string]: Schema<any> };
126
+ schema?: { [key: string]: any };
127
127
  data?: any;
128
128
  /**
129
129
  * 是否需要验证
@@ -133,7 +133,10 @@ export class Route<U = { [key: string]: any }> {
133
133
  * 是否开启debug,开启后会打印错误信息
134
134
  */
135
135
  isDebug?: boolean;
136
- constructor(path: string, key: string = '', opts?: RouteOpts) {
136
+ constructor(path: string = '', key: string = '', opts?: RouteOpts) {
137
+ if (!path) {
138
+ path = nanoid(8)
139
+ }
137
140
  path = path.trim();
138
141
  key = key.trim();
139
142
  this.path = path;
@@ -261,6 +264,17 @@ export class Route<U = { [key: string]: any }> {
261
264
  this.validator = validator;
262
265
  return this;
263
266
  }
267
+ prompt(description: string): this;
268
+ prompt(description: Function): this;
269
+ prompt(...args: any[]) {
270
+ const [description] = args;
271
+ if (typeof description === 'string') {
272
+ this.description = description;
273
+ } else if (typeof description === 'function') {
274
+ this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
275
+ }
276
+ return this;
277
+ }
264
278
  define<T extends { [key: string]: any } = RouterContextT>(opts: DefineRouteOpts): this;
265
279
  define<T extends { [key: string]: any } = RouterContextT>(fn: Run<T & U>): this;
266
280
  define<T extends { [key: string]: any } = RouterContextT>(key: string, fn: Run<T & U>): this;
@@ -304,6 +318,27 @@ export class Route<U = { [key: string]: any }> {
304
318
  }
305
319
  return this;
306
320
  }
321
+
322
+ update(opts: DefineRouteOpts, checkList?: string[]): this {
323
+ const keys = Object.keys(opts);
324
+ const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
325
+ checkList = checkList || defaultCheckList;
326
+ for (let item of keys) {
327
+ if (!checkList.includes(item)) {
328
+ continue;
329
+ }
330
+ if (item === 'validator') {
331
+ this.validator = opts[item];
332
+ continue;
333
+ }
334
+ if (item === 'middleware') {
335
+ this.middleware = this.middleware.concat(opts[item]);
336
+ continue;
337
+ }
338
+ this[item] = opts[item];
339
+ }
340
+ return this;
341
+ }
307
342
  addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }) {
308
343
  router.add(this);
309
344
  }
@@ -583,8 +618,9 @@ export class QueryRouter {
583
618
  } else {
584
619
  return { code: 404, body: null, message: 'Not found route' };
585
620
  }
621
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
586
622
  } else if (path) {
587
- return await this.parse({ ...message, path }, { ...this.context, ...ctx });
623
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
588
624
  } else {
589
625
  return { code: 404, body: null, message: 'Not found path' };
590
626
  }
@@ -596,7 +632,7 @@ export class QueryRouter {
596
632
  * @param ctx
597
633
  * @returns
598
634
  */
599
- async queryRoute(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
635
+ async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
600
636
  const res = await this.parse(message, { ...this.context, ...ctx });
601
637
  return {
602
638
  code: res.code,
@@ -621,9 +657,19 @@ export class QueryRouter {
621
657
  * 获取handle函数, 这里会去执行parse函数
622
658
  */
623
659
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
624
- return async (msg: { path: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
660
+ return async (msg: { id?: string; path?: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
625
661
  try {
626
662
  const context = { ...ctx, ...handleContext };
663
+ if (msg.id) {
664
+ const route = router.routes.find((r) => r.id === msg.id);
665
+ if (route) {
666
+ msg.path = route.path;
667
+ msg.key = route.key;
668
+ } else {
669
+ return { code: 404, message: 'Not found route' };
670
+ }
671
+ }
672
+ // @ts-ignore
627
673
  const res = await router.parse(msg, context);
628
674
  if (wrapperFn) {
629
675
  res.data = res.body;
@@ -721,6 +767,18 @@ export class QueryRouterServer extends QueryRouter {
721
767
  }
722
768
  return new Route(path, key, opts);
723
769
  }
770
+ prompt(description: string): Route<Required<RouteContext>>;
771
+ prompt(description: Function): Route<Required<RouteContext>>;
772
+ prompt(...args: any[]) {
773
+ const [desc] = args;
774
+ let description = ''
775
+ if (typeof desc === 'string') {
776
+ description = desc;
777
+ } else if (typeof desc === 'function') {
778
+ description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
779
+ }
780
+ return new Route('', '', { description });
781
+ }
724
782
 
725
783
  /**
726
784
  * 等于queryRoute,但是调用了handle
@@ -751,3 +809,6 @@ export class QueryRouterServer extends QueryRouter {
751
809
  }
752
810
  }
753
811
  }
812
+
813
+
814
+ export const Mini = QueryRouterServer
@@ -0,0 +1,17 @@
1
+ import { App } from '../app.ts'
2
+ import { RouterChat } from '@/chat.ts';
3
+
4
+ const app = new App();
5
+
6
+ app.prompt(`获取时间的工具`).define(async (ctx) => {
7
+ ctx.body = '123'
8
+ }).addTo(app);
9
+
10
+ app.prompt('获取天气的工具。\n参数是 city 为对应的城市').define(async (ctx) => {
11
+ ctx.body = '晴天'
12
+ }).addTo(app);
13
+
14
+
15
+ export const chat = new RouterChat({ router: app.router });
16
+
17
+ console.log(chat.chat());