koatty_router 1.7.1 → 1.7.4
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 +6 -0
- package/dist/index.d.ts +40 -14
- package/dist/index.js +134 -65
- package/dist/index.mjs +135 -67
- package/dist/package.json +6 -4
- package/package.json +6 -4
- package/yarn-error.log +5609 -0
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.4](https://github.com/koatty/koatty_router/compare/v1.7.3...v1.7.4) (2022-03-14)
|
|
6
|
+
|
|
7
|
+
### [1.7.3](https://github.com/koatty/koatty_router/compare/v1.7.2...v1.7.3) (2022-03-09)
|
|
8
|
+
|
|
9
|
+
### [1.7.2](https://github.com/koatty/koatty_router/compare/v1.7.1...v1.7.2) (2022-02-25)
|
|
10
|
+
|
|
5
11
|
### [1.7.1](https://github.com/koatty/koatty_router/compare/v1.7.0...v1.7.1) (2022-02-23)
|
|
6
12
|
|
|
7
13
|
## [1.7.0](https://github.com/koatty/koatty_router/compare/v1.7.0-1...v1.7.0) (2022-02-21)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2022-
|
|
3
|
+
* @Date: 2022-03-14 16:52:33
|
|
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
|
-
|
|
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
|
|
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:
|
|
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:
|
|
374
|
+
router: KoaRouter;
|
|
350
375
|
constructor(app: Koatty, options?: RouterOptions);
|
|
351
376
|
/**
|
|
352
|
-
*
|
|
377
|
+
* Set router
|
|
353
378
|
*
|
|
354
|
-
* @param {string}
|
|
379
|
+
* @param {string} path
|
|
355
380
|
* @param {WsImplementation} func
|
|
381
|
+
* @param {RequestMethod} [method]
|
|
382
|
+
* @returns {*}
|
|
356
383
|
* @memberof WebsocketRouter
|
|
357
384
|
*/
|
|
358
|
-
SetRouter(
|
|
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():
|
|
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 = (
|
|
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-
|
|
3
|
+
* @Date: 2022-03-14 16:52:19
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -13,6 +13,7 @@ var Helper = require('koatty_lib');
|
|
|
13
13
|
var koatty_container = require('koatty_container');
|
|
14
14
|
var koatty_proto = require('koatty_proto');
|
|
15
15
|
var koatty_logger = require('koatty_logger');
|
|
16
|
+
var koatty_exception = require('koatty_exception');
|
|
16
17
|
var koatty_validation = require('koatty_validation');
|
|
17
18
|
require('reflect-metadata');
|
|
18
19
|
var KoaRouter = require('@koa/router');
|
|
@@ -45,7 +46,7 @@ var KoaRouter__default = /*#__PURE__*/_interopDefaultLegacy(KoaRouter);
|
|
|
45
46
|
* @Usage:
|
|
46
47
|
* @Author: richen
|
|
47
48
|
* @Date: 2021-11-24 23:21:26
|
|
48
|
-
* @LastEditTime: 2022-
|
|
49
|
+
* @LastEditTime: 2022-03-09 18:14:25
|
|
49
50
|
*/
|
|
50
51
|
/**
|
|
51
52
|
* Parameter binding assignment.
|
|
@@ -68,7 +69,8 @@ async function getParamter(app, ctx, params) {
|
|
|
68
69
|
index: k,
|
|
69
70
|
isDto: v.isDto,
|
|
70
71
|
type: v.type,
|
|
71
|
-
|
|
72
|
+
validRule: v.rule,
|
|
73
|
+
validOpt: v.options,
|
|
72
74
|
dtoCheck: v.dtoCheck,
|
|
73
75
|
dtoRule: v.dtoRule,
|
|
74
76
|
clazz: v.clazz,
|
|
@@ -87,29 +89,71 @@ async function getParamter(app, ctx, params) {
|
|
|
87
89
|
* @returns {*}
|
|
88
90
|
*/
|
|
89
91
|
async function checkParams(app, value, opt) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
try {
|
|
93
|
+
if (opt.isDto) {
|
|
94
|
+
// DTO class
|
|
95
|
+
if (!opt.clazz) {
|
|
96
|
+
opt.clazz = koatty_container.IOCContainer.getClass(opt.type, "COMPONENT");
|
|
97
|
+
}
|
|
98
|
+
if (opt.dtoCheck) {
|
|
99
|
+
value = await koatty_validation.ClassValidator.valid(opt.clazz, value, true);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
value = koatty_validation.plainToClass(opt.clazz, value, true);
|
|
103
|
+
}
|
|
97
104
|
}
|
|
98
105
|
else {
|
|
99
|
-
value = koatty_validation.
|
|
106
|
+
value = koatty_validation.convertParamsType(value, opt.type);
|
|
107
|
+
//@Valid()
|
|
108
|
+
if (opt) {
|
|
109
|
+
const { type, validRule, validOpt } = opt;
|
|
110
|
+
if (type && validRule) {
|
|
111
|
+
validatorFuncs(`${opt.index}`, value, type, validRule, validOpt, false);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
100
114
|
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
throw new koatty_exception.Exception(err.message || `ValidatorError: invalid arguments.`, 1, 400);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Validated by funcs.
|
|
123
|
+
*
|
|
124
|
+
* @export
|
|
125
|
+
* @param {string} name
|
|
126
|
+
* @param {*} value
|
|
127
|
+
* @param {string} type
|
|
128
|
+
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
129
|
+
* @param {ValidOtpions} [options]
|
|
130
|
+
* @param {boolean} [checkType=true]
|
|
131
|
+
* @returns
|
|
132
|
+
*/
|
|
133
|
+
function validatorFuncs(name, value, type, rule, options, checkType = true) {
|
|
134
|
+
// check type
|
|
135
|
+
if (checkType && !koatty_validation.checkParamsType(value, type)) {
|
|
136
|
+
throw new koatty_exception.Exception(options.message || `TypeError: invalid arguments '${name}'.`, 1, 400);
|
|
137
|
+
}
|
|
138
|
+
if (Helper__namespace.isFunction(rule)) {
|
|
139
|
+
// Function no return value
|
|
140
|
+
rule(value);
|
|
101
141
|
}
|
|
102
142
|
else {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
143
|
+
const funcs = [];
|
|
144
|
+
if (Helper__namespace.isString(rule)) {
|
|
145
|
+
funcs.push(rule);
|
|
146
|
+
}
|
|
147
|
+
else if (Helper__namespace.isArray(rule)) {
|
|
148
|
+
funcs.push(...rule);
|
|
149
|
+
}
|
|
150
|
+
for (const func of funcs) {
|
|
151
|
+
if (Object.hasOwnProperty.call(koatty_validation.FunctionValidator, func)) {
|
|
152
|
+
// FunctionValidator just throws error, no return value
|
|
153
|
+
koatty_validation.FunctionValidator[func](value, options);
|
|
109
154
|
}
|
|
110
155
|
}
|
|
111
156
|
}
|
|
112
|
-
return value;
|
|
113
157
|
}
|
|
114
158
|
|
|
115
159
|
/**
|
|
@@ -255,7 +299,7 @@ const HeadMapping = (path = "/", routerOptions = {}) => {
|
|
|
255
299
|
* @Usage:
|
|
256
300
|
* @Author: richen
|
|
257
301
|
* @Date: 2021-11-10 16:58:57
|
|
258
|
-
* @LastEditTime: 2022-
|
|
302
|
+
* @LastEditTime: 2022-03-09 18:14:12
|
|
259
303
|
*/
|
|
260
304
|
/**
|
|
261
305
|
* controller handler
|
|
@@ -332,6 +376,7 @@ function injectParam(app, target, instance) {
|
|
|
332
376
|
const validatedMetaDatas = koatty_container.RecursiveGetMetadata(koatty_validation.PARAM_CHECK_KEY, target);
|
|
333
377
|
const argsMetaObj = {};
|
|
334
378
|
for (const meta in metaDatas) {
|
|
379
|
+
// 实例方法带规则形参必须小于等于原型形参(如果不存在验证规则,则小于)
|
|
335
380
|
if (instance[meta] && instance[meta].length <= metaDatas[meta].length) {
|
|
336
381
|
koatty_logger.DefaultLogger.Debug(`Register inject param key ${koatty_container.IOCContainer.getIdentifier(target)}: ${Helper__namespace.toString(meta)} => value: ${JSON.stringify(metaDatas[meta])}`);
|
|
337
382
|
// cover to obj
|
|
@@ -339,10 +384,10 @@ function injectParam(app, target, instance) {
|
|
|
339
384
|
const validData = (_b = validMetaDatas[meta]) !== null && _b !== void 0 ? _b : [];
|
|
340
385
|
data.forEach((v) => {
|
|
341
386
|
var _a;
|
|
342
|
-
v.rule = {};
|
|
343
387
|
validData.forEach((it) => {
|
|
344
|
-
if (v.index === it.index) {
|
|
345
|
-
v.rule = it;
|
|
388
|
+
if (v.index === it.index && it.name === v.name) {
|
|
389
|
+
v.rule = it.rule;
|
|
390
|
+
v.options = it.options;
|
|
346
391
|
}
|
|
347
392
|
});
|
|
348
393
|
if (v.type) {
|
|
@@ -372,7 +417,7 @@ function injectParam(app, target, instance) {
|
|
|
372
417
|
* @Usage:
|
|
373
418
|
* @Author: richen
|
|
374
419
|
* @Date: 2021-06-29 14:10:30
|
|
375
|
-
* @LastEditTime: 2022-
|
|
420
|
+
* @LastEditTime: 2022-03-14 16:29:52
|
|
376
421
|
*/
|
|
377
422
|
class GrpcRouter {
|
|
378
423
|
constructor(app, options) {
|
|
@@ -394,6 +439,7 @@ class GrpcRouter {
|
|
|
394
439
|
* @memberof GrpcRouter
|
|
395
440
|
*/
|
|
396
441
|
SetRouter(name, service, implementation) {
|
|
442
|
+
var _a, _b;
|
|
397
443
|
if (Helper__namespace.isEmpty(name)) {
|
|
398
444
|
return;
|
|
399
445
|
}
|
|
@@ -402,11 +448,12 @@ class GrpcRouter {
|
|
|
402
448
|
implementation: implementation
|
|
403
449
|
};
|
|
404
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);
|
|
405
452
|
}
|
|
406
453
|
/**
|
|
407
454
|
* ListRouter
|
|
408
455
|
*
|
|
409
|
-
* @returns {*} {ServiceImplementation
|
|
456
|
+
* @returns {*} {Map<string, ServiceImplementation>}
|
|
410
457
|
* @memberof GrpcRouter
|
|
411
458
|
*/
|
|
412
459
|
ListRouter() {
|
|
@@ -419,7 +466,6 @@ class GrpcRouter {
|
|
|
419
466
|
*/
|
|
420
467
|
async LoadRouter(list) {
|
|
421
468
|
try {
|
|
422
|
-
const app = this.app;
|
|
423
469
|
// load proto files
|
|
424
470
|
const pdef = koatty_proto.LoadProto(this.options.protoFile);
|
|
425
471
|
const services = koatty_proto.ListServices(pdef);
|
|
@@ -427,9 +473,9 @@ class GrpcRouter {
|
|
|
427
473
|
for (const n in list) {
|
|
428
474
|
const ctlClass = koatty_container.IOCContainer.getClass(n, "CONTROLLER");
|
|
429
475
|
// inject router
|
|
430
|
-
const ctlRouters = injectRouter(app, ctlClass);
|
|
476
|
+
const ctlRouters = injectRouter(this.app, ctlClass);
|
|
431
477
|
// inject param
|
|
432
|
-
const ctlParams = injectParam(app, ctlClass);
|
|
478
|
+
const ctlParams = injectParam(this.app, ctlClass);
|
|
433
479
|
for (const it in ctlRouters) {
|
|
434
480
|
const router = ctlRouters[it];
|
|
435
481
|
const method = router.method;
|
|
@@ -458,9 +504,9 @@ class GrpcRouter {
|
|
|
458
504
|
const ctlItem = ctls[path];
|
|
459
505
|
koatty_logger.DefaultLogger.Debug(`Register request mapping: ["${path}" => ${ctlItem.name}.${ctlItem.method}]`);
|
|
460
506
|
impl[handler.name] = (call, callback) => {
|
|
461
|
-
return app.callback("grpc", (ctx) => {
|
|
507
|
+
return this.app.callback("grpc", (ctx) => {
|
|
462
508
|
const ctl = koatty_container.IOCContainer.getInsByClass(ctlItem.ctl, [ctx]);
|
|
463
|
-
return Handler(app, ctx, ctl, ctlItem.method, ctlItem.params);
|
|
509
|
+
return Handler(this.app, ctx, ctl, ctlItem.method, ctlItem.params);
|
|
464
510
|
})(call, callback);
|
|
465
511
|
};
|
|
466
512
|
}
|
|
@@ -479,7 +525,7 @@ class GrpcRouter {
|
|
|
479
525
|
* @Usage:
|
|
480
526
|
* @Author: richen
|
|
481
527
|
* @Date: 2021-06-28 19:02:06
|
|
482
|
-
* @LastEditTime: 2022-
|
|
528
|
+
* @LastEditTime: 2022-03-14 10:16:14
|
|
483
529
|
*/
|
|
484
530
|
/**
|
|
485
531
|
* HttpRouter class
|
|
@@ -503,8 +549,17 @@ class HttpRouter {
|
|
|
503
549
|
if (Helper__namespace.isEmpty(method)) {
|
|
504
550
|
return;
|
|
505
551
|
}
|
|
552
|
+
method = method !== null && method !== void 0 ? method : exports.RequestMethod.ALL;
|
|
506
553
|
this.router[method](path, func);
|
|
507
554
|
}
|
|
555
|
+
/**
|
|
556
|
+
* ListRouter
|
|
557
|
+
*
|
|
558
|
+
* @returns {*} {KoaRouter.Middleware<any, unknown>}
|
|
559
|
+
*/
|
|
560
|
+
ListRouter() {
|
|
561
|
+
return this.router.routes();
|
|
562
|
+
}
|
|
508
563
|
/**
|
|
509
564
|
*
|
|
510
565
|
*
|
|
@@ -512,15 +567,13 @@ class HttpRouter {
|
|
|
512
567
|
*/
|
|
513
568
|
LoadRouter(list) {
|
|
514
569
|
try {
|
|
515
|
-
const app = this.app;
|
|
516
|
-
const kRouter = this.router;
|
|
517
570
|
// tslint:disable-next-line: forin
|
|
518
571
|
for (const n in list) {
|
|
519
572
|
const ctlClass = koatty_container.IOCContainer.getClass(n, "CONTROLLER");
|
|
520
573
|
// inject router
|
|
521
|
-
const ctlRouters = injectRouter(app, ctlClass);
|
|
574
|
+
const ctlRouters = injectRouter(this.app, ctlClass);
|
|
522
575
|
// inject param
|
|
523
|
-
const ctlParams = injectParam(app, ctlClass);
|
|
576
|
+
const ctlParams = injectParam(this.app, ctlClass);
|
|
524
577
|
// tslint:disable-next-line: forin
|
|
525
578
|
for (const it in ctlRouters) {
|
|
526
579
|
const router = ctlRouters[it];
|
|
@@ -529,18 +582,16 @@ class HttpRouter {
|
|
|
529
582
|
const requestMethod = router.requestMethod;
|
|
530
583
|
const params = ctlParams[method];
|
|
531
584
|
koatty_logger.DefaultLogger.Debug(`Register request mapping: [${requestMethod}] : ["${path}" => ${n}.${method}]`);
|
|
532
|
-
|
|
585
|
+
this.SetRouter(path, (ctx) => {
|
|
533
586
|
const ctl = koatty_container.IOCContainer.getInsByClass(ctlClass, [ctx]);
|
|
534
|
-
return Handler(app, ctx, ctl, method, params);
|
|
535
|
-
});
|
|
587
|
+
return Handler(this.app, ctx, ctl, method, params);
|
|
588
|
+
}, requestMethod);
|
|
536
589
|
}
|
|
537
590
|
}
|
|
538
|
-
// Load in the 'appStart' event to facilitate the expansion of middleware
|
|
539
591
|
// exp: in middleware
|
|
540
592
|
// app.Router.SetRouter('/xxx', (ctx: Koa.KoattyContext): any => {...}, 'GET')
|
|
541
|
-
app.
|
|
542
|
-
|
|
543
|
-
});
|
|
593
|
+
this.app.use(this.ListRouter()).
|
|
594
|
+
use(this.router.allowedMethods());
|
|
544
595
|
}
|
|
545
596
|
catch (err) {
|
|
546
597
|
koatty_logger.DefaultLogger.Error(err);
|
|
@@ -553,7 +604,7 @@ class HttpRouter {
|
|
|
553
604
|
* @Usage:
|
|
554
605
|
* @Author: richen
|
|
555
606
|
* @Date: 2021-06-29 14:16:44
|
|
556
|
-
* @LastEditTime: 2022-
|
|
607
|
+
* @LastEditTime: 2022-03-14 16:50:53
|
|
557
608
|
*/
|
|
558
609
|
class WebsocketRouter {
|
|
559
610
|
constructor(app, options) {
|
|
@@ -561,26 +612,31 @@ class WebsocketRouter {
|
|
|
561
612
|
this.options = Object.assign({
|
|
562
613
|
prefix: options.prefix
|
|
563
614
|
}, options);
|
|
564
|
-
this.router = new
|
|
615
|
+
this.router = new KoaRouter__default["default"](this.options);
|
|
565
616
|
}
|
|
566
617
|
/**
|
|
567
|
-
*
|
|
618
|
+
* Set router
|
|
568
619
|
*
|
|
569
|
-
* @param {string}
|
|
620
|
+
* @param {string} path
|
|
570
621
|
* @param {WsImplementation} func
|
|
622
|
+
* @param {RequestMethod} [method]
|
|
623
|
+
* @returns {*}
|
|
571
624
|
* @memberof WebsocketRouter
|
|
572
625
|
*/
|
|
573
|
-
SetRouter(
|
|
574
|
-
|
|
626
|
+
SetRouter(path, func, method) {
|
|
627
|
+
if (Helper.Helper.isEmpty(method)) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
method = method !== null && method !== void 0 ? method : exports.RequestMethod.ALL;
|
|
631
|
+
this.router[method](path, func);
|
|
575
632
|
}
|
|
576
633
|
/**
|
|
634
|
+
* ListRouter
|
|
577
635
|
*
|
|
578
|
-
*
|
|
579
|
-
* @returns {*}
|
|
580
|
-
* @memberof WebsocketRouter
|
|
636
|
+
* @returns {*} {KoaRouter.Middleware<any, unknown>}
|
|
581
637
|
*/
|
|
582
638
|
ListRouter() {
|
|
583
|
-
return this.router;
|
|
639
|
+
return this.router.routes();
|
|
584
640
|
}
|
|
585
641
|
/**
|
|
586
642
|
*
|
|
@@ -588,32 +644,31 @@ class WebsocketRouter {
|
|
|
588
644
|
* @param {any[]} list
|
|
589
645
|
*/
|
|
590
646
|
LoadRouter(list) {
|
|
591
|
-
var _a;
|
|
592
647
|
try {
|
|
593
|
-
const app = this.app;
|
|
594
|
-
const protocol = (_a = app.config("protocol")) !== null && _a !== void 0 ? _a : "ws";
|
|
595
648
|
// tslint:disable-next-line: forin
|
|
596
649
|
for (const n in list) {
|
|
597
650
|
const ctlClass = koatty_container.IOCContainer.getClass(n, "CONTROLLER");
|
|
598
651
|
// inject router
|
|
599
|
-
const ctlRouters = injectRouter(app, ctlClass);
|
|
652
|
+
const ctlRouters = injectRouter(this.app, ctlClass);
|
|
600
653
|
// inject param
|
|
601
|
-
const ctlParams = injectParam(app, ctlClass);
|
|
654
|
+
const ctlParams = injectParam(this.app, ctlClass);
|
|
602
655
|
// tslint:disable-next-line: forin
|
|
603
656
|
for (const it in ctlRouters) {
|
|
604
657
|
const router = ctlRouters[it];
|
|
658
|
+
const path = router.path;
|
|
605
659
|
const method = router.method;
|
|
660
|
+
const requestMethod = router.requestMethod;
|
|
606
661
|
const params = ctlParams[method];
|
|
607
|
-
koatty_logger.DefaultLogger.Debug(`Register request mapping: ["${
|
|
608
|
-
this.SetRouter(
|
|
609
|
-
|
|
610
|
-
return app
|
|
611
|
-
|
|
612
|
-
return Handler(app, ctx, ctl, method, params);
|
|
613
|
-
})(request, socket);
|
|
614
|
-
});
|
|
662
|
+
koatty_logger.DefaultLogger.Debug(`Register request mapping: ["${path}" => ${n}.${method}]`);
|
|
663
|
+
this.SetRouter(path, (ctx) => {
|
|
664
|
+
const ctl = koatty_container.IOCContainer.getInsByClass(ctlClass, [ctx]);
|
|
665
|
+
return Handler(this.app, ctx, ctl, method, params);
|
|
666
|
+
}, requestMethod);
|
|
615
667
|
}
|
|
616
668
|
}
|
|
669
|
+
// Add websocket handler
|
|
670
|
+
this.app.use(this.ListRouter()).
|
|
671
|
+
use(this.router.allowedMethods());
|
|
617
672
|
}
|
|
618
673
|
catch (err) {
|
|
619
674
|
koatty_logger.DefaultLogger.Error(err);
|
|
@@ -626,7 +681,7 @@ class WebsocketRouter {
|
|
|
626
681
|
* @Usage:
|
|
627
682
|
* @Author: richen
|
|
628
683
|
* @Date: 2021-11-17 17:36:13
|
|
629
|
-
* @LastEditTime: 2022-
|
|
684
|
+
* @LastEditTime: 2022-03-14 11:23:26
|
|
630
685
|
*/
|
|
631
686
|
/**
|
|
632
687
|
* Get request header.
|
|
@@ -725,6 +780,12 @@ function RequestBody() {
|
|
|
725
780
|
return ctx.bodyParser();
|
|
726
781
|
}, "RequestBody");
|
|
727
782
|
}
|
|
783
|
+
/**
|
|
784
|
+
* Alias of @RequestBody
|
|
785
|
+
* @param {*}
|
|
786
|
+
* @return {*}
|
|
787
|
+
*/
|
|
788
|
+
const Body = RequestBody;
|
|
728
789
|
/**
|
|
729
790
|
* Get POST/GET parameters, POST priority
|
|
730
791
|
*
|
|
@@ -745,6 +806,12 @@ function RequestParam(name) {
|
|
|
745
806
|
});
|
|
746
807
|
}, "RequestParam");
|
|
747
808
|
}
|
|
809
|
+
/**
|
|
810
|
+
* Alias of @RequestParam
|
|
811
|
+
* @param {*}
|
|
812
|
+
* @return {*}
|
|
813
|
+
*/
|
|
814
|
+
const Param = RequestParam;
|
|
748
815
|
/**
|
|
749
816
|
* Inject ParameterDecorator
|
|
750
817
|
*
|
|
@@ -819,6 +886,7 @@ function NewRouter(app, options, protocol) {
|
|
|
819
886
|
return router;
|
|
820
887
|
}
|
|
821
888
|
|
|
889
|
+
exports.Body = Body;
|
|
822
890
|
exports.CONTROLLER_ROUTER = CONTROLLER_ROUTER;
|
|
823
891
|
exports.DeleteMapping = DeleteMapping;
|
|
824
892
|
exports.File = File;
|
|
@@ -830,6 +898,7 @@ exports.Header = Header;
|
|
|
830
898
|
exports.HttpRouter = HttpRouter;
|
|
831
899
|
exports.NewRouter = NewRouter;
|
|
832
900
|
exports.OptionsMapping = OptionsMapping;
|
|
901
|
+
exports.Param = Param;
|
|
833
902
|
exports.PatchMapping = PatchMapping;
|
|
834
903
|
exports.PathVariable = PathVariable;
|
|
835
904
|
exports.Post = Post;
|