koatty_router 1.7.2 → 1.7.5

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/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.7.5](https://github.com/koatty/koatty_router/compare/v1.7.4...v1.7.5) (2022-03-14)
6
+
7
+ ### [1.7.4](https://github.com/koatty/koatty_router/compare/v1.7.3...v1.7.4) (2022-03-14)
8
+
9
+ ### [1.7.3](https://github.com/koatty/koatty_router/compare/v1.7.2...v1.7.3) (2022-03-09)
10
+
5
11
  ### [1.7.2](https://github.com/koatty/koatty_router/compare/v1.7.1...v1.7.2) (2022-02-25)
6
12
 
7
13
  ### [1.7.1](https://github.com/koatty/koatty_router/compare/v1.7.0...v1.7.1) (2022-02-23)
package/dist/index.d.ts CHANGED
@@ -1,17 +1,27 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-02-25 11:29:27
3
+ * @Date: 2022-03-14 17:27:39
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
+ import { DefaultContext } from 'koa';
9
+ import { DefaultState } from 'koa';
8
10
  import KoaRouter from '@koa/router';
9
11
  import { Koatty } from 'koatty_core';
12
+ import { KoattyContext } from 'koatty_core';
13
+ import { KoattyNext } from 'koatty_core';
10
14
  import { KoattyRouter } from 'koatty_core';
11
15
  import { ServiceDefinition } from '@grpc/grpc-js';
12
16
  import { UntypedHandleCall } from '@grpc/grpc-js';
13
17
  import { UntypedServiceImplementation } from '@grpc/grpc-js';
14
- import { WebSocket } from 'ws';
18
+
19
+ /**
20
+ * Alias of @RequestBody
21
+ * @param {*}
22
+ * @return {*}
23
+ */
24
+ export declare const Body: typeof RequestBody;
15
25
 
16
26
  export declare const CONTROLLER_ROUTER = "CONTROLLER_ROUTER";
17
27
 
@@ -77,7 +87,7 @@ export declare class GrpcRouter implements KoattyRouter {
77
87
  /**
78
88
  * ListRouter
79
89
  *
80
- * @returns {*} {ServiceImplementation[]}
90
+ * @returns {*} {Map<string, ServiceImplementation>}
81
91
  * @memberof GrpcRouter
82
92
  */
83
93
  ListRouter(): Map<string, ServiceImplementation>;
@@ -121,13 +131,15 @@ export declare const HeadMapping: (path?: string, routerOptions?: {
121
131
  routerName?: string;
122
132
  }) => MethodDecorator;
123
133
 
134
+ export declare type HttpImplementation = (ctx: KoattyContext, next: KoattyNext) => Promise<any>;
135
+
124
136
  /**
125
137
  * HttpRouter class
126
138
  */
127
139
  export declare class HttpRouter implements KoattyRouter {
128
140
  app: Koatty;
129
141
  options: RouterOptions;
130
- router: KoaRouter<any, unknown>;
142
+ router: KoaRouter;
131
143
  constructor(app: Koatty, options?: RouterOptions);
132
144
  /**
133
145
  * Set router
@@ -135,7 +147,13 @@ export declare class HttpRouter implements KoattyRouter {
135
147
  * @param {string} path
136
148
  * @param {RequestMethod} [method]
137
149
  */
138
- SetRouter(path: string, func: Function, method?: RequestMethod): void;
150
+ SetRouter(path: string, func: HttpImplementation, method?: RequestMethod): void;
151
+ /**
152
+ * ListRouter
153
+ *
154
+ * @returns {*} {KoaRouter.Middleware<any, unknown>}
155
+ */
156
+ ListRouter(): KoaRouter.Middleware<DefaultState, DefaultContext>;
139
157
  /**
140
158
  *
141
159
  *
@@ -178,6 +196,13 @@ export declare const OptionsMapping: (path?: string, routerOptions?: {
178
196
  routerName?: string;
179
197
  }) => MethodDecorator;
180
198
 
199
+ /**
200
+ * Alias of @RequestParam
201
+ * @param {*}
202
+ * @return {*}
203
+ */
204
+ export declare const Param: typeof RequestParam;
205
+
181
206
  /**
182
207
  * Routes HTTP PATCH requests to the specified path.
183
208
  *
@@ -346,23 +371,24 @@ export declare interface ServiceImplementation {
346
371
  export declare class WebsocketRouter implements KoattyRouter {
347
372
  app: Koatty;
348
373
  options: WebsocketRouterOptions;
349
- router: Map<string, WsImplementation>;
374
+ router: KoaRouter;
350
375
  constructor(app: Koatty, options?: RouterOptions);
351
376
  /**
352
- * SetRouter
377
+ * Set router
353
378
  *
354
- * @param {string} method
379
+ * @param {string} path
355
380
  * @param {WsImplementation} func
381
+ * @param {RequestMethod} [method]
382
+ * @returns {*}
356
383
  * @memberof WebsocketRouter
357
384
  */
358
- SetRouter(method: string, func: WsImplementation): void;
385
+ SetRouter(path: string, func: WsImplementation, method?: RequestMethod): void;
359
386
  /**
387
+ * ListRouter
360
388
  *
361
- *
362
- * @returns {*}
363
- * @memberof WebsocketRouter
389
+ * @returns {*} {KoaRouter.Middleware<any, unknown>}
364
390
  */
365
- ListRouter(): Map<string, WsImplementation>;
391
+ ListRouter(): KoaRouter.Middleware<DefaultState, DefaultContext>;
366
392
  /**
367
393
  *
368
394
  *
@@ -381,6 +407,6 @@ export declare interface WebsocketRouterOptions extends RouterOptions {
381
407
  prefix: string;
382
408
  }
383
409
 
384
- export declare type WsImplementation = (socket: WebSocket, request: any, data: any) => Promise<any>;
410
+ export declare type WsImplementation = (ctx: KoattyContext, next: KoattyNext) => Promise<any>;
385
411
 
386
412
  export { }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-02-25 11:29:11
3
+ * @Date: 2022-03-14 17:27:24
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -46,7 +46,7 @@ var KoaRouter__default = /*#__PURE__*/_interopDefaultLegacy(KoaRouter);
46
46
  * @Usage:
47
47
  * @Author: richen
48
48
  * @Date: 2021-11-24 23:21:26
49
- * @LastEditTime: 2022-02-25 11:24:12
49
+ * @LastEditTime: 2022-03-09 18:14:25
50
50
  */
51
51
  /**
52
52
  * Parameter binding assignment.
@@ -69,7 +69,8 @@ async function getParamter(app, ctx, params) {
69
69
  index: k,
70
70
  isDto: v.isDto,
71
71
  type: v.type,
72
- validRules: v.rule,
72
+ validRule: v.rule,
73
+ validOpt: v.options,
73
74
  dtoCheck: v.dtoCheck,
74
75
  dtoRule: v.dtoRule,
75
76
  clazz: v.clazz,
@@ -104,10 +105,10 @@ async function checkParams(app, value, opt) {
104
105
  else {
105
106
  value = koatty_validation.convertParamsType(value, opt.type);
106
107
  //@Valid()
107
- if (opt.validRules[opt.index]) {
108
- const { type, rule, options } = opt.validRules[opt.index];
109
- if (type && rule) {
110
- validatorFuncs(`${opt.index}`, value, type, rule, options, false);
108
+ if (opt) {
109
+ const { type, validRule, validOpt } = opt;
110
+ if (type && validRule) {
111
+ validatorFuncs(`${opt.index}`, value, type, validRule, validOpt, false);
111
112
  }
112
113
  }
113
114
  }
@@ -298,7 +299,7 @@ const HeadMapping = (path = "/", routerOptions = {}) => {
298
299
  * @Usage:
299
300
  * @Author: richen
300
301
  * @Date: 2021-11-10 16:58:57
301
- * @LastEditTime: 2022-02-22 14:35:08
302
+ * @LastEditTime: 2022-03-09 18:14:12
302
303
  */
303
304
  /**
304
305
  * controller handler
@@ -375,6 +376,7 @@ function injectParam(app, target, instance) {
375
376
  const validatedMetaDatas = koatty_container.RecursiveGetMetadata(koatty_validation.PARAM_CHECK_KEY, target);
376
377
  const argsMetaObj = {};
377
378
  for (const meta in metaDatas) {
379
+ // 实例方法带规则形参必须小于等于原型形参(如果不存在验证规则,则小于)
378
380
  if (instance[meta] && instance[meta].length <= metaDatas[meta].length) {
379
381
  koatty_logger.DefaultLogger.Debug(`Register inject param key ${koatty_container.IOCContainer.getIdentifier(target)}: ${Helper__namespace.toString(meta)} => value: ${JSON.stringify(metaDatas[meta])}`);
380
382
  // cover to obj
@@ -382,10 +384,10 @@ function injectParam(app, target, instance) {
382
384
  const validData = (_b = validMetaDatas[meta]) !== null && _b !== void 0 ? _b : [];
383
385
  data.forEach((v) => {
384
386
  var _a;
385
- v.rule = {};
386
387
  validData.forEach((it) => {
387
- if (v.index === it.index) {
388
- v.rule = it;
388
+ if (v.index === it.index && it.name === v.name) {
389
+ v.rule = it.rule;
390
+ v.options = it.options;
389
391
  }
390
392
  });
391
393
  if (v.type) {
@@ -415,7 +417,7 @@ function injectParam(app, target, instance) {
415
417
  * @Usage:
416
418
  * @Author: richen
417
419
  * @Date: 2021-06-29 14:10:30
418
- * @LastEditTime: 2022-02-23 14:53:16
420
+ * @LastEditTime: 2022-03-14 17:22:28
419
421
  */
420
422
  class GrpcRouter {
421
423
  constructor(app, options) {
@@ -437,6 +439,7 @@ class GrpcRouter {
437
439
  * @memberof GrpcRouter
438
440
  */
439
441
  SetRouter(name, service, implementation) {
442
+ var _a, _b;
440
443
  if (Helper__namespace.isEmpty(name)) {
441
444
  return;
442
445
  }
@@ -445,11 +448,12 @@ class GrpcRouter {
445
448
  implementation: implementation
446
449
  };
447
450
  this.router.set(name, value);
451
+ (_b = (_a = this.app) === null || _a === void 0 ? void 0 : _a.server) === null || _b === void 0 ? void 0 : _b.RegisterService(value);
448
452
  }
449
453
  /**
450
454
  * ListRouter
451
455
  *
452
- * @returns {*} {ServiceImplementation[]}
456
+ * @returns {*} {Map<string, ServiceImplementation>}
453
457
  * @memberof GrpcRouter
454
458
  */
455
459
  ListRouter() {
@@ -462,17 +466,16 @@ class GrpcRouter {
462
466
  */
463
467
  async LoadRouter(list) {
464
468
  try {
465
- const app = this.app;
466
469
  // load proto files
467
470
  const pdef = koatty_proto.LoadProto(this.options.protoFile);
468
471
  const services = koatty_proto.ListServices(pdef);
469
472
  const ctls = {};
470
- for (const n in list) {
473
+ for (const n of list) {
471
474
  const ctlClass = koatty_container.IOCContainer.getClass(n, "CONTROLLER");
472
475
  // inject router
473
- const ctlRouters = injectRouter(app, ctlClass);
476
+ const ctlRouters = injectRouter(this.app, ctlClass);
474
477
  // inject param
475
- const ctlParams = injectParam(app, ctlClass);
478
+ const ctlParams = injectParam(this.app, ctlClass);
476
479
  for (const it in ctlRouters) {
477
480
  const router = ctlRouters[it];
478
481
  const method = router.method;
@@ -501,9 +504,9 @@ class GrpcRouter {
501
504
  const ctlItem = ctls[path];
502
505
  koatty_logger.DefaultLogger.Debug(`Register request mapping: ["${path}" => ${ctlItem.name}.${ctlItem.method}]`);
503
506
  impl[handler.name] = (call, callback) => {
504
- return app.callback("grpc", (ctx) => {
507
+ return this.app.callback("grpc", (ctx) => {
505
508
  const ctl = koatty_container.IOCContainer.getInsByClass(ctlItem.ctl, [ctx]);
506
- return Handler(app, ctx, ctl, ctlItem.method, ctlItem.params);
509
+ return Handler(this.app, ctx, ctl, ctlItem.method, ctlItem.params);
507
510
  })(call, callback);
508
511
  };
509
512
  }
@@ -522,7 +525,7 @@ class GrpcRouter {
522
525
  * @Usage:
523
526
  * @Author: richen
524
527
  * @Date: 2021-06-28 19:02:06
525
- * @LastEditTime: 2022-02-23 11:19:01
528
+ * @LastEditTime: 2022-03-14 17:22:32
526
529
  */
527
530
  /**
528
531
  * HttpRouter class
@@ -546,8 +549,17 @@ class HttpRouter {
546
549
  if (Helper__namespace.isEmpty(method)) {
547
550
  return;
548
551
  }
552
+ method = method !== null && method !== void 0 ? method : exports.RequestMethod.ALL;
549
553
  this.router[method](path, func);
550
554
  }
555
+ /**
556
+ * ListRouter
557
+ *
558
+ * @returns {*} {KoaRouter.Middleware<any, unknown>}
559
+ */
560
+ ListRouter() {
561
+ return this.router.routes();
562
+ }
551
563
  /**
552
564
  *
553
565
  *
@@ -555,15 +567,12 @@ class HttpRouter {
555
567
  */
556
568
  LoadRouter(list) {
557
569
  try {
558
- const app = this.app;
559
- const kRouter = this.router;
560
- // tslint:disable-next-line: forin
561
- for (const n in list) {
570
+ for (const n of list) {
562
571
  const ctlClass = koatty_container.IOCContainer.getClass(n, "CONTROLLER");
563
572
  // inject router
564
- const ctlRouters = injectRouter(app, ctlClass);
573
+ const ctlRouters = injectRouter(this.app, ctlClass);
565
574
  // inject param
566
- const ctlParams = injectParam(app, ctlClass);
575
+ const ctlParams = injectParam(this.app, ctlClass);
567
576
  // tslint:disable-next-line: forin
568
577
  for (const it in ctlRouters) {
569
578
  const router = ctlRouters[it];
@@ -572,18 +581,16 @@ class HttpRouter {
572
581
  const requestMethod = router.requestMethod;
573
582
  const params = ctlParams[method];
574
583
  koatty_logger.DefaultLogger.Debug(`Register request mapping: [${requestMethod}] : ["${path}" => ${n}.${method}]`);
575
- kRouter[requestMethod](path, function (ctx) {
584
+ this.SetRouter(path, (ctx) => {
576
585
  const ctl = koatty_container.IOCContainer.getInsByClass(ctlClass, [ctx]);
577
- return Handler(app, ctx, ctl, method, params);
578
- });
586
+ return Handler(this.app, ctx, ctl, method, params);
587
+ }, requestMethod);
579
588
  }
580
589
  }
581
- // Load in the 'appStart' event to facilitate the expansion of middleware
582
590
  // exp: in middleware
583
591
  // app.Router.SetRouter('/xxx', (ctx: Koa.KoattyContext): any => {...}, 'GET')
584
- app.on('appStart', () => {
585
- app.use(kRouter.routes()).use(kRouter.allowedMethods());
586
- });
592
+ this.app.use(this.ListRouter()).
593
+ use(this.router.allowedMethods());
587
594
  }
588
595
  catch (err) {
589
596
  koatty_logger.DefaultLogger.Error(err);
@@ -596,7 +603,7 @@ class HttpRouter {
596
603
  * @Usage:
597
604
  * @Author: richen
598
605
  * @Date: 2021-06-29 14:16:44
599
- * @LastEditTime: 2022-02-23 11:19:40
606
+ * @LastEditTime: 2022-03-14 17:22:41
600
607
  */
601
608
  class WebsocketRouter {
602
609
  constructor(app, options) {
@@ -604,26 +611,31 @@ class WebsocketRouter {
604
611
  this.options = Object.assign({
605
612
  prefix: options.prefix
606
613
  }, options);
607
- this.router = new Map();
614
+ this.router = new KoaRouter__default["default"](this.options);
608
615
  }
609
616
  /**
610
- * SetRouter
617
+ * Set router
611
618
  *
612
- * @param {string} method
619
+ * @param {string} path
613
620
  * @param {WsImplementation} func
621
+ * @param {RequestMethod} [method]
622
+ * @returns {*}
614
623
  * @memberof WebsocketRouter
615
624
  */
616
- SetRouter(method, func) {
617
- this.router.set(method, func);
625
+ SetRouter(path, func, method) {
626
+ if (Helper.Helper.isEmpty(method)) {
627
+ return;
628
+ }
629
+ method = method !== null && method !== void 0 ? method : exports.RequestMethod.ALL;
630
+ this.router[method](path, func);
618
631
  }
619
632
  /**
633
+ * ListRouter
620
634
  *
621
- *
622
- * @returns {*}
623
- * @memberof WebsocketRouter
635
+ * @returns {*} {KoaRouter.Middleware<any, unknown>}
624
636
  */
625
637
  ListRouter() {
626
- return this.router;
638
+ return this.router.routes();
627
639
  }
628
640
  /**
629
641
  *
@@ -631,32 +643,30 @@ class WebsocketRouter {
631
643
  * @param {any[]} list
632
644
  */
633
645
  LoadRouter(list) {
634
- var _a;
635
646
  try {
636
- const app = this.app;
637
- const protocol = (_a = app.config("protocol")) !== null && _a !== void 0 ? _a : "ws";
638
- // tslint:disable-next-line: forin
639
- for (const n in list) {
647
+ for (const n of list) {
640
648
  const ctlClass = koatty_container.IOCContainer.getClass(n, "CONTROLLER");
641
649
  // inject router
642
- const ctlRouters = injectRouter(app, ctlClass);
650
+ const ctlRouters = injectRouter(this.app, ctlClass);
643
651
  // inject param
644
- const ctlParams = injectParam(app, ctlClass);
652
+ const ctlParams = injectParam(this.app, ctlClass);
645
653
  // tslint:disable-next-line: forin
646
654
  for (const it in ctlRouters) {
647
655
  const router = ctlRouters[it];
656
+ const path = router.path;
648
657
  const method = router.method;
658
+ const requestMethod = router.requestMethod;
649
659
  const params = ctlParams[method];
650
- koatty_logger.DefaultLogger.Debug(`Register request mapping: ["${ctlRouters[it].path}" => ${n}.${method}]`);
651
- this.SetRouter(ctlRouters[it].path, (socket, request, data) => {
652
- request.data = data;
653
- return app.callback(protocol, (ctx) => {
654
- const ctl = koatty_container.IOCContainer.getInsByClass(ctlClass, [ctx]);
655
- return Handler(app, ctx, ctl, method, params);
656
- })(request, socket);
657
- });
660
+ koatty_logger.DefaultLogger.Debug(`Register request mapping: ["${path}" => ${n}.${method}]`);
661
+ this.SetRouter(path, (ctx) => {
662
+ const ctl = koatty_container.IOCContainer.getInsByClass(ctlClass, [ctx]);
663
+ return Handler(this.app, ctx, ctl, method, params);
664
+ }, requestMethod);
658
665
  }
659
666
  }
667
+ // Add websocket handler
668
+ this.app.use(this.ListRouter()).
669
+ use(this.router.allowedMethods());
660
670
  }
661
671
  catch (err) {
662
672
  koatty_logger.DefaultLogger.Error(err);
@@ -669,7 +679,7 @@ class WebsocketRouter {
669
679
  * @Usage:
670
680
  * @Author: richen
671
681
  * @Date: 2021-11-17 17:36:13
672
- * @LastEditTime: 2022-02-22 14:48:38
682
+ * @LastEditTime: 2022-03-14 11:23:26
673
683
  */
674
684
  /**
675
685
  * Get request header.
@@ -768,6 +778,12 @@ function RequestBody() {
768
778
  return ctx.bodyParser();
769
779
  }, "RequestBody");
770
780
  }
781
+ /**
782
+ * Alias of @RequestBody
783
+ * @param {*}
784
+ * @return {*}
785
+ */
786
+ const Body = RequestBody;
771
787
  /**
772
788
  * Get POST/GET parameters, POST priority
773
789
  *
@@ -788,6 +804,12 @@ function RequestParam(name) {
788
804
  });
789
805
  }, "RequestParam");
790
806
  }
807
+ /**
808
+ * Alias of @RequestParam
809
+ * @param {*}
810
+ * @return {*}
811
+ */
812
+ const Param = RequestParam;
791
813
  /**
792
814
  * Inject ParameterDecorator
793
815
  *
@@ -862,6 +884,7 @@ function NewRouter(app, options, protocol) {
862
884
  return router;
863
885
  }
864
886
 
887
+ exports.Body = Body;
865
888
  exports.CONTROLLER_ROUTER = CONTROLLER_ROUTER;
866
889
  exports.DeleteMapping = DeleteMapping;
867
890
  exports.File = File;
@@ -873,6 +896,7 @@ exports.Header = Header;
873
896
  exports.HttpRouter = HttpRouter;
874
897
  exports.NewRouter = NewRouter;
875
898
  exports.OptionsMapping = OptionsMapping;
899
+ exports.Param = Param;
876
900
  exports.PatchMapping = PatchMapping;
877
901
  exports.PathVariable = PathVariable;
878
902
  exports.Post = Post;
package/dist/index.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-02-25 11:29:11
3
+ * @Date: 2022-03-14 17:27:24
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
8
  import * as Helper from 'koatty_lib';
9
+ import { Helper as Helper$1 } from 'koatty_lib';
9
10
  import { IOCContainer, RecursiveGetMetadata, TAGGED_PARAM, getOriginMetadata } from 'koatty_container';
10
11
  import { LoadProto, ListServices } from 'koatty_proto';
11
12
  import { DefaultLogger } from 'koatty_logger';
@@ -19,7 +20,7 @@ import KoaRouter from '@koa/router';
19
20
  * @Usage:
20
21
  * @Author: richen
21
22
  * @Date: 2021-11-24 23:21:26
22
- * @LastEditTime: 2022-02-25 11:24:12
23
+ * @LastEditTime: 2022-03-09 18:14:25
23
24
  */
24
25
  /**
25
26
  * Parameter binding assignment.
@@ -42,7 +43,8 @@ async function getParamter(app, ctx, params) {
42
43
  index: k,
43
44
  isDto: v.isDto,
44
45
  type: v.type,
45
- validRules: v.rule,
46
+ validRule: v.rule,
47
+ validOpt: v.options,
46
48
  dtoCheck: v.dtoCheck,
47
49
  dtoRule: v.dtoRule,
48
50
  clazz: v.clazz,
@@ -77,10 +79,10 @@ async function checkParams(app, value, opt) {
77
79
  else {
78
80
  value = convertParamsType(value, opt.type);
79
81
  //@Valid()
80
- if (opt.validRules[opt.index]) {
81
- const { type, rule, options } = opt.validRules[opt.index];
82
- if (type && rule) {
83
- validatorFuncs(`${opt.index}`, value, type, rule, options, false);
82
+ if (opt) {
83
+ const { type, validRule, validOpt } = opt;
84
+ if (type && validRule) {
85
+ validatorFuncs(`${opt.index}`, value, type, validRule, validOpt, false);
84
86
  }
85
87
  }
86
88
  }
@@ -271,7 +273,7 @@ const HeadMapping = (path = "/", routerOptions = {}) => {
271
273
  * @Usage:
272
274
  * @Author: richen
273
275
  * @Date: 2021-11-10 16:58:57
274
- * @LastEditTime: 2022-02-22 14:35:08
276
+ * @LastEditTime: 2022-03-09 18:14:12
275
277
  */
276
278
  /**
277
279
  * controller handler
@@ -348,6 +350,7 @@ function injectParam(app, target, instance) {
348
350
  const validatedMetaDatas = RecursiveGetMetadata(PARAM_CHECK_KEY, target);
349
351
  const argsMetaObj = {};
350
352
  for (const meta in metaDatas) {
353
+ // 实例方法带规则形参必须小于等于原型形参(如果不存在验证规则,则小于)
351
354
  if (instance[meta] && instance[meta].length <= metaDatas[meta].length) {
352
355
  DefaultLogger.Debug(`Register inject param key ${IOCContainer.getIdentifier(target)}: ${Helper.toString(meta)} => value: ${JSON.stringify(metaDatas[meta])}`);
353
356
  // cover to obj
@@ -355,10 +358,10 @@ function injectParam(app, target, instance) {
355
358
  const validData = (_b = validMetaDatas[meta]) !== null && _b !== void 0 ? _b : [];
356
359
  data.forEach((v) => {
357
360
  var _a;
358
- v.rule = {};
359
361
  validData.forEach((it) => {
360
- if (v.index === it.index) {
361
- v.rule = it;
362
+ if (v.index === it.index && it.name === v.name) {
363
+ v.rule = it.rule;
364
+ v.options = it.options;
362
365
  }
363
366
  });
364
367
  if (v.type) {
@@ -388,7 +391,7 @@ function injectParam(app, target, instance) {
388
391
  * @Usage:
389
392
  * @Author: richen
390
393
  * @Date: 2021-06-29 14:10:30
391
- * @LastEditTime: 2022-02-23 14:53:16
394
+ * @LastEditTime: 2022-03-14 17:22:28
392
395
  */
393
396
  class GrpcRouter {
394
397
  constructor(app, options) {
@@ -410,6 +413,7 @@ class GrpcRouter {
410
413
  * @memberof GrpcRouter
411
414
  */
412
415
  SetRouter(name, service, implementation) {
416
+ var _a, _b;
413
417
  if (Helper.isEmpty(name)) {
414
418
  return;
415
419
  }
@@ -418,11 +422,12 @@ class GrpcRouter {
418
422
  implementation: implementation
419
423
  };
420
424
  this.router.set(name, value);
425
+ (_b = (_a = this.app) === null || _a === void 0 ? void 0 : _a.server) === null || _b === void 0 ? void 0 : _b.RegisterService(value);
421
426
  }
422
427
  /**
423
428
  * ListRouter
424
429
  *
425
- * @returns {*} {ServiceImplementation[]}
430
+ * @returns {*} {Map<string, ServiceImplementation>}
426
431
  * @memberof GrpcRouter
427
432
  */
428
433
  ListRouter() {
@@ -435,17 +440,16 @@ class GrpcRouter {
435
440
  */
436
441
  async LoadRouter(list) {
437
442
  try {
438
- const app = this.app;
439
443
  // load proto files
440
444
  const pdef = LoadProto(this.options.protoFile);
441
445
  const services = ListServices(pdef);
442
446
  const ctls = {};
443
- for (const n in list) {
447
+ for (const n of list) {
444
448
  const ctlClass = IOCContainer.getClass(n, "CONTROLLER");
445
449
  // inject router
446
- const ctlRouters = injectRouter(app, ctlClass);
450
+ const ctlRouters = injectRouter(this.app, ctlClass);
447
451
  // inject param
448
- const ctlParams = injectParam(app, ctlClass);
452
+ const ctlParams = injectParam(this.app, ctlClass);
449
453
  for (const it in ctlRouters) {
450
454
  const router = ctlRouters[it];
451
455
  const method = router.method;
@@ -474,9 +478,9 @@ class GrpcRouter {
474
478
  const ctlItem = ctls[path];
475
479
  DefaultLogger.Debug(`Register request mapping: ["${path}" => ${ctlItem.name}.${ctlItem.method}]`);
476
480
  impl[handler.name] = (call, callback) => {
477
- return app.callback("grpc", (ctx) => {
481
+ return this.app.callback("grpc", (ctx) => {
478
482
  const ctl = IOCContainer.getInsByClass(ctlItem.ctl, [ctx]);
479
- return Handler(app, ctx, ctl, ctlItem.method, ctlItem.params);
483
+ return Handler(this.app, ctx, ctl, ctlItem.method, ctlItem.params);
480
484
  })(call, callback);
481
485
  };
482
486
  }
@@ -495,7 +499,7 @@ class GrpcRouter {
495
499
  * @Usage:
496
500
  * @Author: richen
497
501
  * @Date: 2021-06-28 19:02:06
498
- * @LastEditTime: 2022-02-23 11:19:01
502
+ * @LastEditTime: 2022-03-14 17:22:32
499
503
  */
500
504
  /**
501
505
  * HttpRouter class
@@ -519,8 +523,17 @@ class HttpRouter {
519
523
  if (Helper.isEmpty(method)) {
520
524
  return;
521
525
  }
526
+ method = method !== null && method !== void 0 ? method : RequestMethod.ALL;
522
527
  this.router[method](path, func);
523
528
  }
529
+ /**
530
+ * ListRouter
531
+ *
532
+ * @returns {*} {KoaRouter.Middleware<any, unknown>}
533
+ */
534
+ ListRouter() {
535
+ return this.router.routes();
536
+ }
524
537
  /**
525
538
  *
526
539
  *
@@ -528,15 +541,12 @@ class HttpRouter {
528
541
  */
529
542
  LoadRouter(list) {
530
543
  try {
531
- const app = this.app;
532
- const kRouter = this.router;
533
- // tslint:disable-next-line: forin
534
- for (const n in list) {
544
+ for (const n of list) {
535
545
  const ctlClass = IOCContainer.getClass(n, "CONTROLLER");
536
546
  // inject router
537
- const ctlRouters = injectRouter(app, ctlClass);
547
+ const ctlRouters = injectRouter(this.app, ctlClass);
538
548
  // inject param
539
- const ctlParams = injectParam(app, ctlClass);
549
+ const ctlParams = injectParam(this.app, ctlClass);
540
550
  // tslint:disable-next-line: forin
541
551
  for (const it in ctlRouters) {
542
552
  const router = ctlRouters[it];
@@ -545,18 +555,16 @@ class HttpRouter {
545
555
  const requestMethod = router.requestMethod;
546
556
  const params = ctlParams[method];
547
557
  DefaultLogger.Debug(`Register request mapping: [${requestMethod}] : ["${path}" => ${n}.${method}]`);
548
- kRouter[requestMethod](path, function (ctx) {
558
+ this.SetRouter(path, (ctx) => {
549
559
  const ctl = IOCContainer.getInsByClass(ctlClass, [ctx]);
550
- return Handler(app, ctx, ctl, method, params);
551
- });
560
+ return Handler(this.app, ctx, ctl, method, params);
561
+ }, requestMethod);
552
562
  }
553
563
  }
554
- // Load in the 'appStart' event to facilitate the expansion of middleware
555
564
  // exp: in middleware
556
565
  // app.Router.SetRouter('/xxx', (ctx: Koa.KoattyContext): any => {...}, 'GET')
557
- app.on('appStart', () => {
558
- app.use(kRouter.routes()).use(kRouter.allowedMethods());
559
- });
566
+ this.app.use(this.ListRouter()).
567
+ use(this.router.allowedMethods());
560
568
  }
561
569
  catch (err) {
562
570
  DefaultLogger.Error(err);
@@ -569,7 +577,7 @@ class HttpRouter {
569
577
  * @Usage:
570
578
  * @Author: richen
571
579
  * @Date: 2021-06-29 14:16:44
572
- * @LastEditTime: 2022-02-23 11:19:40
580
+ * @LastEditTime: 2022-03-14 17:22:41
573
581
  */
574
582
  class WebsocketRouter {
575
583
  constructor(app, options) {
@@ -577,26 +585,31 @@ class WebsocketRouter {
577
585
  this.options = Object.assign({
578
586
  prefix: options.prefix
579
587
  }, options);
580
- this.router = new Map();
588
+ this.router = new KoaRouter(this.options);
581
589
  }
582
590
  /**
583
- * SetRouter
591
+ * Set router
584
592
  *
585
- * @param {string} method
593
+ * @param {string} path
586
594
  * @param {WsImplementation} func
595
+ * @param {RequestMethod} [method]
596
+ * @returns {*}
587
597
  * @memberof WebsocketRouter
588
598
  */
589
- SetRouter(method, func) {
590
- this.router.set(method, func);
599
+ SetRouter(path, func, method) {
600
+ if (Helper$1.isEmpty(method)) {
601
+ return;
602
+ }
603
+ method = method !== null && method !== void 0 ? method : RequestMethod.ALL;
604
+ this.router[method](path, func);
591
605
  }
592
606
  /**
607
+ * ListRouter
593
608
  *
594
- *
595
- * @returns {*}
596
- * @memberof WebsocketRouter
609
+ * @returns {*} {KoaRouter.Middleware<any, unknown>}
597
610
  */
598
611
  ListRouter() {
599
- return this.router;
612
+ return this.router.routes();
600
613
  }
601
614
  /**
602
615
  *
@@ -604,32 +617,30 @@ class WebsocketRouter {
604
617
  * @param {any[]} list
605
618
  */
606
619
  LoadRouter(list) {
607
- var _a;
608
620
  try {
609
- const app = this.app;
610
- const protocol = (_a = app.config("protocol")) !== null && _a !== void 0 ? _a : "ws";
611
- // tslint:disable-next-line: forin
612
- for (const n in list) {
621
+ for (const n of list) {
613
622
  const ctlClass = IOCContainer.getClass(n, "CONTROLLER");
614
623
  // inject router
615
- const ctlRouters = injectRouter(app, ctlClass);
624
+ const ctlRouters = injectRouter(this.app, ctlClass);
616
625
  // inject param
617
- const ctlParams = injectParam(app, ctlClass);
626
+ const ctlParams = injectParam(this.app, ctlClass);
618
627
  // tslint:disable-next-line: forin
619
628
  for (const it in ctlRouters) {
620
629
  const router = ctlRouters[it];
630
+ const path = router.path;
621
631
  const method = router.method;
632
+ const requestMethod = router.requestMethod;
622
633
  const params = ctlParams[method];
623
- DefaultLogger.Debug(`Register request mapping: ["${ctlRouters[it].path}" => ${n}.${method}]`);
624
- this.SetRouter(ctlRouters[it].path, (socket, request, data) => {
625
- request.data = data;
626
- return app.callback(protocol, (ctx) => {
627
- const ctl = IOCContainer.getInsByClass(ctlClass, [ctx]);
628
- return Handler(app, ctx, ctl, method, params);
629
- })(request, socket);
630
- });
634
+ DefaultLogger.Debug(`Register request mapping: ["${path}" => ${n}.${method}]`);
635
+ this.SetRouter(path, (ctx) => {
636
+ const ctl = IOCContainer.getInsByClass(ctlClass, [ctx]);
637
+ return Handler(this.app, ctx, ctl, method, params);
638
+ }, requestMethod);
631
639
  }
632
640
  }
641
+ // Add websocket handler
642
+ this.app.use(this.ListRouter()).
643
+ use(this.router.allowedMethods());
633
644
  }
634
645
  catch (err) {
635
646
  DefaultLogger.Error(err);
@@ -642,7 +653,7 @@ class WebsocketRouter {
642
653
  * @Usage:
643
654
  * @Author: richen
644
655
  * @Date: 2021-11-17 17:36:13
645
- * @LastEditTime: 2022-02-22 14:48:38
656
+ * @LastEditTime: 2022-03-14 11:23:26
646
657
  */
647
658
  /**
648
659
  * Get request header.
@@ -741,6 +752,12 @@ function RequestBody() {
741
752
  return ctx.bodyParser();
742
753
  }, "RequestBody");
743
754
  }
755
+ /**
756
+ * Alias of @RequestBody
757
+ * @param {*}
758
+ * @return {*}
759
+ */
760
+ const Body = RequestBody;
744
761
  /**
745
762
  * Get POST/GET parameters, POST priority
746
763
  *
@@ -761,6 +778,12 @@ function RequestParam(name) {
761
778
  });
762
779
  }, "RequestParam");
763
780
  }
781
+ /**
782
+ * Alias of @RequestParam
783
+ * @param {*}
784
+ * @return {*}
785
+ */
786
+ const Param = RequestParam;
764
787
  /**
765
788
  * Inject ParameterDecorator
766
789
  *
@@ -835,4 +858,4 @@ function NewRouter(app, options, protocol) {
835
858
  return router;
836
859
  }
837
860
 
838
- export { CONTROLLER_ROUTER, DeleteMapping, File, Get, GetMapping, GrpcRouter, HeadMapping, Header, HttpRouter, NewRouter, OptionsMapping, PatchMapping, PathVariable, Post, PostMapping, PutMapping, ROUTER_KEY, RequestBody, RequestMapping, RequestMethod, RequestParam, WebsocketRouter };
861
+ export { Body, CONTROLLER_ROUTER, DeleteMapping, File, Get, GetMapping, GrpcRouter, HeadMapping, Header, HttpRouter, NewRouter, OptionsMapping, Param, PatchMapping, PathVariable, Post, PostMapping, PutMapping, ROUTER_KEY, RequestBody, RequestMapping, RequestMethod, RequestParam, WebsocketRouter };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_router",
3
- "version": "1.7.2",
3
+ "version": "1.7.5",
4
4
  "description": "Koatty routing component, adapt to http1/2, websocket, gRPC.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -34,6 +34,7 @@
34
34
  "author": "richenlin@gmail.com",
35
35
  "license": "BSD-3-Clause",
36
36
  "devDependencies": {
37
+ "@grpc/grpc-js": "^1.x.x",
37
38
  "@microsoft/api-documenter": "^7.x.x",
38
39
  "@microsoft/api-extractor": "^7.x.x",
39
40
  "@rollup/plugin-json": "^4.x.x",
@@ -42,7 +43,7 @@
42
43
  "@types/koa__router": "^8.0.11",
43
44
  "@types/node": "^16.x.x",
44
45
  "@types/validator": "^13.7.0",
45
- "@types/ws": "^8.2.2",
46
+ "@types/ws": "^8.x.x",
46
47
  "@typescript-eslint/eslint-plugin": "^5.x.x",
47
48
  "@typescript-eslint/parser": "^5.x.x",
48
49
  "commitlint": "^15.x.x",
@@ -63,7 +64,6 @@
63
64
  "typescript": "^4.x.x"
64
65
  },
65
66
  "dependencies": {
66
- "@grpc/grpc-js": "^1.5.5",
67
67
  "@koa/router": "^10.1.1",
68
68
  "koatty_container": "^1.x.x",
69
69
  "koatty_core": "^1.x.x",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_router",
3
- "version": "1.7.2",
3
+ "version": "1.7.5",
4
4
  "description": "Koatty routing component, adapt to http1/2, websocket, gRPC.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -34,6 +34,7 @@
34
34
  "author": "richenlin@gmail.com",
35
35
  "license": "BSD-3-Clause",
36
36
  "devDependencies": {
37
+ "@grpc/grpc-js": "^1.x.x",
37
38
  "@microsoft/api-documenter": "^7.x.x",
38
39
  "@microsoft/api-extractor": "^7.x.x",
39
40
  "@rollup/plugin-json": "^4.x.x",
@@ -42,7 +43,7 @@
42
43
  "@types/koa__router": "^8.0.11",
43
44
  "@types/node": "^16.x.x",
44
45
  "@types/validator": "^13.7.0",
45
- "@types/ws": "^8.2.2",
46
+ "@types/ws": "^8.x.x",
46
47
  "@typescript-eslint/eslint-plugin": "^5.x.x",
47
48
  "@typescript-eslint/parser": "^5.x.x",
48
49
  "commitlint": "^15.x.x",
@@ -63,7 +64,6 @@
63
64
  "typescript": "^4.x.x"
64
65
  },
65
66
  "dependencies": {
66
- "@grpc/grpc-js": "^1.5.5",
67
67
  "@koa/router": "^10.1.1",
68
68
  "koatty_container": "^1.x.x",
69
69
  "koatty_core": "^1.x.x",