@kevisual/router 0.0.28 → 0.0.30

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;
@@ -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
  /**
@@ -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<{
@@ -360,6 +365,22 @@ declare class QueryRouter {
360
365
  hasRoute(path: string, key?: string): Route<{
361
366
  [key: string]: any;
362
367
  }>;
368
+ /**
369
+ * 等待程序运行, 获取到message的数据,就执行
370
+ *
371
+ * emitter = process
372
+ * -- .exit
373
+ * -- .on
374
+ * -- .send
375
+ */
376
+ wait(params?: {
377
+ path?: string;
378
+ key?: string;
379
+ payload?: any;
380
+ }, opts?: {
381
+ emitter?: any;
382
+ timeout?: number;
383
+ }): Promise<void>;
363
384
  }
364
385
  type QueryRouterServerOpts = {
365
386
  handleFn?: HandleFn;
@@ -403,6 +424,7 @@ declare class QueryRouterServer extends QueryRouter {
403
424
  payload?: any;
404
425
  }): Promise<any>;
405
426
  }
427
+ declare const Mini: typeof QueryRouterServer;
406
428
 
407
429
  /** 自定义错误 */
408
430
  declare class CustomError extends Error {
@@ -513,5 +535,5 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
513
535
 
514
536
  declare const App: typeof QueryRouterServer;
515
537
 
516
- export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
538
+ export { App, CustomError, Mini, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
517
539
  export type { RouteArray, RouteContext, RouteObject, RouteOpts, Rule, Run, Schema };
@@ -4743,6 +4743,51 @@ function get(object, path, defaultValue) {
4743
4743
  return result === undefined ? defaultValue : result;
4744
4744
  }
4745
4745
 
4746
+ const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }) => {
4747
+ const process = emitter || globalThis.process;
4748
+ let isEnd = false;
4749
+ const timer = setTimeout(() => {
4750
+ if (isEnd)
4751
+ return;
4752
+ isEnd = true;
4753
+ process.send?.({ success: false, error: 'Timeout' });
4754
+ process.exit?.(1);
4755
+ }, timeout);
4756
+ // 监听来自主进程的消息
4757
+ const getParams = async () => {
4758
+ return new Promise((resolve) => {
4759
+ process.on('message', (msg) => {
4760
+ if (isEnd)
4761
+ return;
4762
+ isEnd = true;
4763
+ clearTimeout(timer);
4764
+ resolve(msg);
4765
+ });
4766
+ });
4767
+ };
4768
+ try {
4769
+ const { path = 'main', ...rest } = await getParams();
4770
+ // 执行主要逻辑
4771
+ const result = await app.queryRoute({ path, ...rest, ...params });
4772
+ // 发送结果回主进程
4773
+ const response = {
4774
+ success: true,
4775
+ data: result,
4776
+ timestamp: new Date().toISOString()
4777
+ };
4778
+ process.send?.(response, (error) => {
4779
+ process.exit?.(0);
4780
+ });
4781
+ }
4782
+ catch (error) {
4783
+ process.send?.({
4784
+ success: false,
4785
+ error: error.message
4786
+ });
4787
+ process.exit?.(1);
4788
+ }
4789
+ };
4790
+
4746
4791
  const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
4747
4792
  class Route {
4748
4793
  /**
@@ -4773,6 +4818,9 @@ class Route {
4773
4818
  */
4774
4819
  isDebug;
4775
4820
  constructor(path, key = '', opts) {
4821
+ if (!path) {
4822
+ path = nanoid$1(8);
4823
+ }
4776
4824
  path = path.trim();
4777
4825
  key = key.trim();
4778
4826
  this.path = path;
@@ -4901,6 +4949,16 @@ class Route {
4901
4949
  this.validator = validator;
4902
4950
  return this;
4903
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
+ }
4904
4962
  define(...args) {
4905
4963
  const [path, key, opts] = args;
4906
4964
  // 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
@@ -4940,6 +4998,26 @@ class Route {
4940
4998
  }
4941
4999
  return this;
4942
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
+ }
4943
5021
  addTo(router) {
4944
5022
  router.add(this);
4945
5023
  }
@@ -5220,19 +5298,20 @@ class QueryRouter {
5220
5298
  */
5221
5299
  async call(message, ctx) {
5222
5300
  let path = message.path;
5223
- message.key;
5301
+ let key = message.key;
5224
5302
  if (message.id) {
5225
5303
  const route = this.routes.find((r) => r.id === message.id);
5226
5304
  if (route) {
5227
5305
  path = route.path;
5228
- route.key;
5306
+ key = route.key;
5229
5307
  }
5230
5308
  else {
5231
5309
  return { code: 404, body: null, message: 'Not found route' };
5232
5310
  }
5311
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5233
5312
  }
5234
5313
  else if (path) {
5235
- return await this.parse({ ...message, path }, { ...this.context, ...ctx });
5314
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5236
5315
  }
5237
5316
  else {
5238
5317
  return { code: 404, body: null, message: 'Not found path' };
@@ -5272,6 +5351,17 @@ class QueryRouter {
5272
5351
  return async (msg, handleContext) => {
5273
5352
  try {
5274
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
5275
5365
  const res = await router.parse(msg, context);
5276
5366
  if (wrapperFn) {
5277
5367
  res.data = res.body;
@@ -5304,6 +5394,17 @@ class QueryRouter {
5304
5394
  hasRoute(path, key = '') {
5305
5395
  return this.routes.find((r) => r.path === path && r.key === key);
5306
5396
  }
5397
+ /**
5398
+ * 等待程序运行, 获取到message的数据,就执行
5399
+ *
5400
+ * emitter = process
5401
+ * -- .exit
5402
+ * -- .on
5403
+ * -- .send
5404
+ */
5405
+ wait(params, opts) {
5406
+ return listenProcess({ app: this, params, ...opts });
5407
+ }
5307
5408
  }
5308
5409
  /**
5309
5410
  * QueryRouterServer
@@ -5375,6 +5476,7 @@ class QueryRouterServer extends QueryRouter {
5375
5476
  }
5376
5477
  }
5377
5478
  }
5479
+ const Mini = QueryRouterServer;
5378
5480
 
5379
5481
  const parseBody = async (req) => {
5380
5482
  return new Promise((resolve, reject) => {
@@ -5579,4 +5681,4 @@ class QueryUtil {
5579
5681
 
5580
5682
  const App = QueryRouterServer;
5581
5683
 
5582
- export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
5684
+ 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;
@@ -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
  /**
@@ -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<{
@@ -364,6 +369,22 @@ declare class QueryRouter {
364
369
  hasRoute(path: string, key?: string): Route<{
365
370
  [key: string]: any;
366
371
  }>;
372
+ /**
373
+ * 等待程序运行, 获取到message的数据,就执行
374
+ *
375
+ * emitter = process
376
+ * -- .exit
377
+ * -- .on
378
+ * -- .send
379
+ */
380
+ wait(params?: {
381
+ path?: string;
382
+ key?: string;
383
+ payload?: any;
384
+ }, opts?: {
385
+ emitter?: any;
386
+ timeout?: number;
387
+ }): Promise<void>;
367
388
  }
368
389
  type QueryRouterServerOpts = {
369
390
  handleFn?: HandleFn;
@@ -407,6 +428,7 @@ declare class QueryRouterServer extends QueryRouter {
407
428
  payload?: any;
408
429
  }): Promise<any>;
409
430
  }
431
+ declare const Mini: typeof QueryRouterServer;
410
432
 
411
433
  declare class Connect {
412
434
  path: string;
@@ -702,7 +724,8 @@ declare class App<T = {}, U = AppReqRes> {
702
724
  route(path: string, opts?: RouteOpts): Route<U>;
703
725
  route(path: string, key?: string, opts?: RouteOpts): Route<U>;
704
726
  call(message: {
705
- path: string;
727
+ id?: string;
728
+ path?: string;
706
729
  key?: string;
707
730
  payload?: any;
708
731
  }, ctx?: RouteContext & {
@@ -728,5 +751,5 @@ declare class App<T = {}, U = AppReqRes> {
728
751
  onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
729
752
  }
730
753
 
731
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
754
+ export { App, Connect, CustomError, Mini, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
732
755
  export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run, Schema };
package/dist/router.js CHANGED
@@ -4765,6 +4765,51 @@ function get(object, path, defaultValue) {
4765
4765
  return result === undefined ? defaultValue : result;
4766
4766
  }
4767
4767
 
4768
+ const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }) => {
4769
+ const process = emitter || globalThis.process;
4770
+ let isEnd = false;
4771
+ const timer = setTimeout(() => {
4772
+ if (isEnd)
4773
+ return;
4774
+ isEnd = true;
4775
+ process.send?.({ success: false, error: 'Timeout' });
4776
+ process.exit?.(1);
4777
+ }, timeout);
4778
+ // 监听来自主进程的消息
4779
+ const getParams = async () => {
4780
+ return new Promise((resolve) => {
4781
+ process.on('message', (msg) => {
4782
+ if (isEnd)
4783
+ return;
4784
+ isEnd = true;
4785
+ clearTimeout(timer);
4786
+ resolve(msg);
4787
+ });
4788
+ });
4789
+ };
4790
+ try {
4791
+ const { path = 'main', ...rest } = await getParams();
4792
+ // 执行主要逻辑
4793
+ const result = await app.queryRoute({ path, ...rest, ...params });
4794
+ // 发送结果回主进程
4795
+ const response = {
4796
+ success: true,
4797
+ data: result,
4798
+ timestamp: new Date().toISOString()
4799
+ };
4800
+ process.send?.(response, (error) => {
4801
+ process.exit?.(0);
4802
+ });
4803
+ }
4804
+ catch (error) {
4805
+ process.send?.({
4806
+ success: false,
4807
+ error: error.message
4808
+ });
4809
+ process.exit?.(1);
4810
+ }
4811
+ };
4812
+
4768
4813
  const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
4769
4814
  class Route {
4770
4815
  /**
@@ -4795,6 +4840,9 @@ class Route {
4795
4840
  */
4796
4841
  isDebug;
4797
4842
  constructor(path, key = '', opts) {
4843
+ if (!path) {
4844
+ path = nanoid$1(8);
4845
+ }
4798
4846
  path = path.trim();
4799
4847
  key = key.trim();
4800
4848
  this.path = path;
@@ -4923,6 +4971,16 @@ class Route {
4923
4971
  this.validator = validator;
4924
4972
  return this;
4925
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
+ }
4926
4984
  define(...args) {
4927
4985
  const [path, key, opts] = args;
4928
4986
  // 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
@@ -4962,6 +5020,26 @@ class Route {
4962
5020
  }
4963
5021
  return this;
4964
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
+ }
4965
5043
  addTo(router) {
4966
5044
  router.add(this);
4967
5045
  }
@@ -5242,19 +5320,20 @@ class QueryRouter {
5242
5320
  */
5243
5321
  async call(message, ctx) {
5244
5322
  let path = message.path;
5245
- message.key;
5323
+ let key = message.key;
5246
5324
  if (message.id) {
5247
5325
  const route = this.routes.find((r) => r.id === message.id);
5248
5326
  if (route) {
5249
5327
  path = route.path;
5250
- route.key;
5328
+ key = route.key;
5251
5329
  }
5252
5330
  else {
5253
5331
  return { code: 404, body: null, message: 'Not found route' };
5254
5332
  }
5333
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5255
5334
  }
5256
5335
  else if (path) {
5257
- return await this.parse({ ...message, path }, { ...this.context, ...ctx });
5336
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
5258
5337
  }
5259
5338
  else {
5260
5339
  return { code: 404, body: null, message: 'Not found path' };
@@ -5294,6 +5373,17 @@ class QueryRouter {
5294
5373
  return async (msg, handleContext) => {
5295
5374
  try {
5296
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
5297
5387
  const res = await router.parse(msg, context);
5298
5388
  if (wrapperFn) {
5299
5389
  res.data = res.body;
@@ -5326,6 +5416,17 @@ class QueryRouter {
5326
5416
  hasRoute(path, key = '') {
5327
5417
  return this.routes.find((r) => r.path === path && r.key === key);
5328
5418
  }
5419
+ /**
5420
+ * 等待程序运行, 获取到message的数据,就执行
5421
+ *
5422
+ * emitter = process
5423
+ * -- .exit
5424
+ * -- .on
5425
+ * -- .send
5426
+ */
5427
+ wait(params, opts) {
5428
+ return listenProcess({ app: this, params, ...opts });
5429
+ }
5329
5430
  }
5330
5431
  /**
5331
5432
  * QueryRouterServer
@@ -5397,6 +5498,7 @@ class QueryRouterServer extends QueryRouter {
5397
5498
  }
5398
5499
  }
5399
5500
  }
5501
+ const Mini = QueryRouterServer;
5400
5502
 
5401
5503
  class Connect {
5402
5504
  path;
@@ -11299,4 +11401,4 @@ class App {
11299
11401
  }
11300
11402
  }
11301
11403
 
11302
- export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
11404
+ 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.28",
4
+ "version": "0.0.30",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -24,7 +24,7 @@
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.6",
28
28
  "@rollup/plugin-node-resolve": "^16.0.3",
29
29
  "@rollup/plugin-typescript": "^12.1.4",
30
30
  "@types/lodash-es": "^4.17.12",
package/src/app.ts CHANGED
@@ -82,7 +82,7 @@ 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
+ async call(message: { id?: string, path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
86
86
  const router = this.router;
87
87
  return await router.call(message, ctx);
88
88
  }
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/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,10 +1,11 @@
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';
5
5
  import { get } from 'lodash-es';
6
+ import { listenProcess } from './utils/listen-process.ts';
6
7
 
7
- export type RouterContextT = { code?: number; [key: string]: any };
8
+ export type RouterContextT = { code?: number;[key: string]: any };
8
9
  export type RouteContext<T = { code?: number }, S = any> = {
9
10
  // run first
10
11
  query?: { [key: string]: any };
@@ -47,7 +48,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
47
48
  error?: any;
48
49
  /** 请求 route的返回结果,包函ctx */
49
50
  call?: (
50
- message: { path: string; key?: string; payload?: any; [key: string]: any } | { id: string; apyload?: any; [key: string]: any },
51
+ message: { path: string; key?: string; payload?: any;[key: string]: any } | { id: string; apyload?: any;[key: string]: any },
51
52
  ctx?: RouteContext & { [key: string]: any },
52
53
  ) => Promise<any>;
53
54
  /** 请求 route的返回结果,不包函ctx */
@@ -63,10 +64,10 @@ export type Run<T extends SimpleObject = {}> = (ctx: RouteContext<T>) => Promise
63
64
  export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
64
65
  export type RouteMiddleware =
65
66
  | {
66
- path: string;
67
- key?: string;
68
- id?: string;
69
- }
67
+ path: string;
68
+ key?: string;
69
+ id?: string;
70
+ }
70
71
  | string;
71
72
  export type RouteOpts = {
72
73
  path?: string;
@@ -87,7 +88,7 @@ export type RouteOpts = {
87
88
  * }
88
89
  */
89
90
  validator?: { [key: string]: Rule };
90
- schema?: { [key: string]: Schema<any> };
91
+ schema?: { [key: string]: any };
91
92
  isVerify?: boolean;
92
93
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
93
94
  verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
@@ -122,7 +123,7 @@ export class Route<U = { [key: string]: any }> {
122
123
  middleware?: RouteMiddleware[]; // middleware
123
124
  type? = 'route';
124
125
  private _validator?: { [key: string]: Rule };
125
- schema?: { [key: string]: Schema<any> };
126
+ schema?: { [key: string]: any };
126
127
  data?: any;
127
128
  /**
128
129
  * 是否需要验证
@@ -133,6 +134,9 @@ export class Route<U = { [key: string]: any }> {
133
134
  */
134
135
  isDebug?: boolean;
135
136
  constructor(path: string, key: string = '', opts?: RouteOpts) {
137
+ if (!path) {
138
+ path = nanoid(8)
139
+ }
136
140
  path = path.trim();
137
141
  key = key.trim();
138
142
  this.path = path;
@@ -260,6 +264,17 @@ export class Route<U = { [key: string]: any }> {
260
264
  this.validator = validator;
261
265
  return this;
262
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
+ }
263
278
  define<T extends { [key: string]: any } = RouterContextT>(opts: DefineRouteOpts): this;
264
279
  define<T extends { [key: string]: any } = RouterContextT>(fn: Run<T & U>): this;
265
280
  define<T extends { [key: string]: any } = RouterContextT>(key: string, fn: Run<T & U>): this;
@@ -303,7 +318,28 @@ export class Route<U = { [key: string]: any }> {
303
318
  }
304
319
  return this;
305
320
  }
306
- addTo(router: QueryRouter | { add: (route: Route) => void; [key: string]: any }) {
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
+ }
342
+ addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }) {
307
343
  router.add(this);
308
344
  }
309
345
  setData(data: any) {
@@ -582,8 +618,9 @@ export class QueryRouter {
582
618
  } else {
583
619
  return { code: 404, body: null, message: 'Not found route' };
584
620
  }
621
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
585
622
  } else if (path) {
586
- return await this.parse({ ...message, path }, { ...this.context, ...ctx });
623
+ return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
587
624
  } else {
588
625
  return { code: 404, body: null, message: 'Not found path' };
589
626
  }
@@ -595,7 +632,7 @@ export class QueryRouter {
595
632
  * @param ctx
596
633
  * @returns
597
634
  */
598
- 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 }) {
599
636
  const res = await this.parse(message, { ...this.context, ...ctx });
600
637
  return {
601
638
  code: res.code,
@@ -608,7 +645,7 @@ export class QueryRouter {
608
645
  * @description 这里的上下文是为了在handle函数中使用
609
646
  * @param ctx
610
647
  */
611
- setContext(ctx: RouteContext) {
648
+ setContext(ctx: RouteContext) {
612
649
  this.context = ctx;
613
650
  }
614
651
  getList(): RouteInfo[] {
@@ -620,9 +657,19 @@ export class QueryRouter {
620
657
  * 获取handle函数, 这里会去执行parse函数
621
658
  */
622
659
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
623
- 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) => {
624
661
  try {
625
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
626
673
  const res = await router.parse(msg, context);
627
674
  if (wrapperFn) {
628
675
  res.data = res.body;
@@ -655,6 +702,17 @@ export class QueryRouter {
655
702
  hasRoute(path: string, key: string = '') {
656
703
  return this.routes.find((r) => r.path === path && r.key === key);
657
704
  }
705
+ /**
706
+ * 等待程序运行, 获取到message的数据,就执行
707
+ *
708
+ * emitter = process
709
+ * -- .exit
710
+ * -- .on
711
+ * -- .send
712
+ */
713
+ wait(params?: { path?: string; key?: string; payload?: any }, opts?: { emitter?: any, timeout?: number }) {
714
+ return listenProcess({ app: this, params, ...opts });
715
+ }
658
716
  }
659
717
 
660
718
  type QueryRouterServerOpts = {
@@ -662,7 +720,7 @@ type QueryRouterServerOpts = {
662
720
  context?: RouteContext;
663
721
  };
664
722
  interface HandleFn<T = any> {
665
- (msg: { path: string; [key: string]: any }, ctx?: any): { code: string; data?: any; message?: string; [key: string]: any };
723
+ (msg: { path: string;[key: string]: any }, ctx?: any): { code: string; data?: any; message?: string;[key: string]: any };
666
724
  (res: RouteContext<T>): any;
667
725
  }
668
726
  /**
@@ -739,3 +797,6 @@ export class QueryRouterServer extends QueryRouter {
739
797
  }
740
798
  }
741
799
  }
800
+
801
+
802
+ export const Mini = QueryRouterServer
@@ -0,0 +1,50 @@
1
+ export type ListenProcessOptions = {
2
+ app?: any; // 传入的应用实例
3
+ emitter?: any; // 可选的事件发射器
4
+ params?: any; // 可选的参数
5
+ timeout?: number; // 可选的超时时间 (单位: 毫秒)
6
+ };
7
+ export const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
8
+ const process = emitter || globalThis.process;
9
+ let isEnd = false;
10
+ const timer = setTimeout(() => {
11
+ if (isEnd) return;
12
+ isEnd = true;
13
+ process.send?.({ success: false, error: 'Timeout' });
14
+ process.exit?.(1);
15
+ }, timeout);
16
+
17
+ // 监听来自主进程的消息
18
+ const getParams = async (): Promise<any> => {
19
+ return new Promise((resolve) => {
20
+ process.on('message', (msg) => {
21
+ if (isEnd) return;
22
+ isEnd = true;
23
+ clearTimeout(timer);
24
+ resolve(msg)
25
+ })
26
+ })
27
+ }
28
+
29
+ try {
30
+ const { path = 'main', ...rest } = await getParams()
31
+ // 执行主要逻辑
32
+ const result = await app.queryRoute({ path, ...rest, ...params })
33
+ // 发送结果回主进程
34
+ const response = {
35
+ success: true,
36
+ data: result,
37
+ timestamp: new Date().toISOString()
38
+ }
39
+
40
+ process.send?.(response, (error) => {
41
+ process.exit?.(0)
42
+ })
43
+ } catch (error) {
44
+ process.send?.({
45
+ success: false,
46
+ error: error.message
47
+ })
48
+ process.exit?.(1)
49
+ }
50
+ }