@midwayjs/core 3.0.0-beta.2 → 3.0.0-beta.6
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 +52 -0
- package/dist/baseFramework.d.ts +11 -9
- package/dist/baseFramework.js +17 -14
- package/dist/{util → common}/fileDetector.d.ts +0 -0
- package/dist/{util → common}/fileDetector.js +0 -0
- package/dist/common/filterManager.d.ts +19 -0
- package/dist/common/filterManager.js +85 -0
- package/dist/common/middlewareManager.d.ts +11 -0
- package/dist/{util → common}/middlewareManager.js +0 -0
- package/dist/{util → common}/serviceFactory.d.ts +0 -0
- package/dist/{util → common}/serviceFactory.js +0 -0
- package/dist/{util → common}/triggerCollector.d.ts +0 -0
- package/dist/{util → common}/triggerCollector.js +0 -0
- package/dist/{util → common}/webGenerator.d.ts +6 -9
- package/dist/{util → common}/webGenerator.js +14 -21
- package/dist/{util → common}/webRouterCollector.d.ts +9 -4
- package/dist/{util → common}/webRouterCollector.js +47 -27
- package/dist/config/config.default.d.ts +3 -17
- package/dist/error/base.d.ts +3 -1
- package/dist/error/base.js +1 -0
- package/dist/error/code.d.ts +1 -1
- package/dist/error/code.js +1 -1
- package/dist/error/framework.d.ts +2 -2
- package/dist/error/framework.js +8 -5
- package/dist/error/http.d.ts +2 -1
- package/dist/error/http.js +3 -3
- package/dist/error/index.d.ts +1 -0
- package/dist/error/index.js +1 -0
- package/dist/index.d.ts +8 -9
- package/dist/index.js +10 -11
- package/dist/interface.d.ts +50 -28
- package/dist/service/configService.js +4 -5
- package/dist/service/decoratorService.js +23 -21
- package/dist/service/frameworkService.d.ts +4 -3
- package/dist/service/frameworkService.js +10 -0
- package/dist/service/lifeCycleService.js +2 -0
- package/dist/service/loggerService.d.ts +1 -2
- package/dist/service/loggerService.js +1 -10
- package/dist/service/middlewareService.d.ts +2 -2
- package/dist/service/middlewareService.js +3 -25
- package/dist/setup.js +2 -0
- package/dist/util/contextUtil.d.ts +1 -1
- package/dist/util/index.d.ts +37 -0
- package/dist/util/index.js +96 -1
- package/dist/util/webRouterParam.d.ts +2 -2
- package/dist/util/webRouterParam.js +17 -18
- package/package.json +5 -5
- package/dist/util/exceptionFilterManager.d.ts +0 -13
- package/dist/util/exceptionFilterManager.js +0 -53
- package/dist/util/middlewareManager.d.ts +0 -11
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,58 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* [@match](https://github.com/match) empty args ([#1384](https://github.com/midwayjs/midway/issues/1384)) ([6f90fc9](https://github.com/midwayjs/midway/commit/6f90fc993ff01e078288ff664833c61c02dede51))
|
|
23
|
+
* router sort ([#1383](https://github.com/midwayjs/midway/issues/1383)) ([f253887](https://github.com/midwayjs/midway/commit/f2538876d3eaf7dec55173d86b5b9caeeeb7be64))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
### Bug Fixes
|
|
33
|
+
|
|
34
|
+
* logger close before bootstrap close ([#1370](https://github.com/midwayjs/midway/issues/1370)) ([6cc2720](https://github.com/midwayjs/midway/commit/6cc2720ed3445e8ffccc96d124b80ed7e2517f08))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
### Features
|
|
38
|
+
|
|
39
|
+
* add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
|
|
40
|
+
* auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
|
|
41
|
+
* support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Features
|
|
51
|
+
|
|
52
|
+
* add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
6
58
|
# [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
|
|
7
59
|
|
|
8
60
|
**Note:** Version bump only for package @midwayjs/core
|
package/dist/baseFramework.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContainer, IMidwayContext, IMidwayFramework,
|
|
1
|
+
import { CommonMiddlewareUnion, IConfigurationOptions, IMidwayApplication, IMidwayBootstrapOptions, IMidwayContainer, IMidwayContext, IMidwayFramework, CommonFilterUnion, CommonMiddleware, MiddlewareRespond } from './interface';
|
|
2
2
|
import { FrameworkType } from '@midwayjs/decorator';
|
|
3
3
|
import { ILogger, LoggerOptions } from '@midwayjs/logger';
|
|
4
4
|
import { MidwayEnvironmentService } from './service/environmentService';
|
|
5
5
|
import { MidwayConfigService } from './service/configService';
|
|
6
6
|
import { MidwayInformationService } from './service/informationService';
|
|
7
7
|
import { MidwayLoggerService } from './service/loggerService';
|
|
8
|
-
import { ContextMiddlewareManager } from './
|
|
8
|
+
import { ContextMiddlewareManager } from './common/middlewareManager';
|
|
9
9
|
import { MidwayMiddlewareService } from './service/middlewareService';
|
|
10
|
-
import {
|
|
11
|
-
export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>, CTX extends IMidwayContext, OPT extends IConfigurationOptions> implements IMidwayFramework<APP, OPT> {
|
|
10
|
+
import { FilterManager } from './common/filterManager';
|
|
11
|
+
export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>, CTX extends IMidwayContext, OPT extends IConfigurationOptions, ResOrNext = unknown, Next = unknown> implements IMidwayFramework<APP, CTX, OPT, ResOrNext, Next> {
|
|
12
12
|
readonly applicationContext: IMidwayContainer;
|
|
13
13
|
app: APP;
|
|
14
14
|
configurationOptions: OPT;
|
|
@@ -17,14 +17,14 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
17
17
|
protected defaultContext: {};
|
|
18
18
|
protected BaseContextLoggerClass: any;
|
|
19
19
|
protected ContextLoggerApplyLogger: string;
|
|
20
|
-
protected middlewareManager: ContextMiddlewareManager<CTX,
|
|
21
|
-
protected
|
|
20
|
+
protected middlewareManager: ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
21
|
+
protected filterManager: FilterManager<CTX, ResOrNext, Next>;
|
|
22
22
|
protected composeMiddleware: any;
|
|
23
23
|
loggerService: MidwayLoggerService;
|
|
24
24
|
environmentService: MidwayEnvironmentService;
|
|
25
25
|
configService: MidwayConfigService;
|
|
26
26
|
informationService: MidwayInformationService;
|
|
27
|
-
middlewareService: MidwayMiddlewareService<CTX>;
|
|
27
|
+
middlewareService: MidwayMiddlewareService<CTX, ResOrNext, Next>;
|
|
28
28
|
constructor(applicationContext: IMidwayContainer);
|
|
29
29
|
init(): Promise<this>;
|
|
30
30
|
abstract configure(options?: OPT): any;
|
|
@@ -79,7 +79,9 @@ export declare abstract class BaseFramework<APP extends IMidwayApplication<CTX>,
|
|
|
79
79
|
getProjectName(): string;
|
|
80
80
|
getFrameworkName(): string;
|
|
81
81
|
getDefaultContextLoggerClass(): any;
|
|
82
|
-
useMiddleware(Middleware: CommonMiddlewareUnion<CTX>): void;
|
|
83
|
-
useFilter(Filter:
|
|
82
|
+
useMiddleware(Middleware: CommonMiddlewareUnion<CTX, ResOrNext, Next>): void;
|
|
83
|
+
useFilter(Filter: CommonFilterUnion<CTX, ResOrNext, Next>): void;
|
|
84
|
+
protected createMiddlewareManager(): ContextMiddlewareManager<CTX, ResOrNext, Next>;
|
|
85
|
+
protected createFilterManager(): FilterManager<CTX, ResOrNext, Next>;
|
|
84
86
|
}
|
|
85
87
|
//# sourceMappingURL=baseFramework.d.ts.map
|
package/dist/baseFramework.js
CHANGED
|
@@ -18,15 +18,15 @@ const environmentService_1 = require("./service/environmentService");
|
|
|
18
18
|
const configService_1 = require("./service/configService");
|
|
19
19
|
const informationService_1 = require("./service/informationService");
|
|
20
20
|
const loggerService_1 = require("./service/loggerService");
|
|
21
|
-
const middlewareManager_1 = require("./
|
|
21
|
+
const middlewareManager_1 = require("./common/middlewareManager");
|
|
22
22
|
const middlewareService_1 = require("./service/middlewareService");
|
|
23
|
-
const
|
|
23
|
+
const filterManager_1 = require("./common/filterManager");
|
|
24
24
|
class BaseFramework {
|
|
25
25
|
constructor(applicationContext) {
|
|
26
26
|
this.applicationContext = applicationContext;
|
|
27
27
|
this.defaultContext = {};
|
|
28
|
-
this.middlewareManager =
|
|
29
|
-
this.
|
|
28
|
+
this.middlewareManager = this.createMiddlewareManager();
|
|
29
|
+
this.filterManager = this.createFilterManager();
|
|
30
30
|
this.composeMiddleware = null;
|
|
31
31
|
}
|
|
32
32
|
async init() {
|
|
@@ -179,7 +179,7 @@ class BaseFramework {
|
|
|
179
179
|
return this.middlewareManager;
|
|
180
180
|
},
|
|
181
181
|
useFilter: (Filter) => {
|
|
182
|
-
this.
|
|
182
|
+
this.filterManager.useFilter(Filter);
|
|
183
183
|
},
|
|
184
184
|
};
|
|
185
185
|
for (const method of whiteList) {
|
|
@@ -206,22 +206,19 @@ class BaseFramework {
|
|
|
206
206
|
async afterContainerReady(options) { }
|
|
207
207
|
async getMiddleware(lastMiddleware) {
|
|
208
208
|
if (!this.composeMiddleware) {
|
|
209
|
-
this.middlewareManager.insertFirst(async (ctx, next) => {
|
|
209
|
+
this.middlewareManager.insertFirst((async (ctx, next) => {
|
|
210
210
|
let returnResult = undefined;
|
|
211
211
|
try {
|
|
212
212
|
const result = await next();
|
|
213
|
-
returnResult =
|
|
214
|
-
result,
|
|
215
|
-
error: undefined,
|
|
216
|
-
};
|
|
213
|
+
returnResult = await this.filterManager.runResultFilter(result, ctx);
|
|
217
214
|
}
|
|
218
215
|
catch (err) {
|
|
219
|
-
returnResult = await this.
|
|
216
|
+
returnResult = await this.filterManager.runErrorFilter(err, ctx);
|
|
220
217
|
}
|
|
221
218
|
return returnResult;
|
|
222
|
-
});
|
|
219
|
+
}));
|
|
223
220
|
this.composeMiddleware = await this.middlewareService.compose(this.middlewareManager);
|
|
224
|
-
await this.
|
|
221
|
+
await this.filterManager.init(this.applicationContext);
|
|
225
222
|
}
|
|
226
223
|
if (lastMiddleware) {
|
|
227
224
|
return await this.middlewareService.compose([
|
|
@@ -256,7 +253,13 @@ class BaseFramework {
|
|
|
256
253
|
this.middlewareManager.insertLast(Middleware);
|
|
257
254
|
}
|
|
258
255
|
useFilter(Filter) {
|
|
259
|
-
this.
|
|
256
|
+
this.filterManager.useFilter(Filter);
|
|
257
|
+
}
|
|
258
|
+
createMiddlewareManager() {
|
|
259
|
+
return new middlewareManager_1.ContextMiddlewareManager();
|
|
260
|
+
}
|
|
261
|
+
createFilterManager() {
|
|
262
|
+
return new filterManager_1.FilterManager();
|
|
260
263
|
}
|
|
261
264
|
}
|
|
262
265
|
__decorate([
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CommonFilterUnion, IMidwayContainer, IMidwayContext } from '../interface';
|
|
2
|
+
export declare class FilterManager<CTX extends IMidwayContext = IMidwayContext, R = any, N = any> {
|
|
3
|
+
private errFilterList;
|
|
4
|
+
private successFilterList;
|
|
5
|
+
private exceptionMap;
|
|
6
|
+
private defaultErrFilter;
|
|
7
|
+
private matchFnList;
|
|
8
|
+
useFilter(Filters: CommonFilterUnion<CTX, R, N>): void;
|
|
9
|
+
init(applicationContext: IMidwayContainer): Promise<void>;
|
|
10
|
+
runErrorFilter(err: Error, ctx: CTX, res?: R, next?: N): Promise<{
|
|
11
|
+
result: any;
|
|
12
|
+
error: any;
|
|
13
|
+
}>;
|
|
14
|
+
runResultFilter(result: any, ctx: CTX, res?: R, next?: N): Promise<{
|
|
15
|
+
result: any;
|
|
16
|
+
error: any;
|
|
17
|
+
}>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=filterManager.d.ts.map
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FilterManager = void 0;
|
|
4
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
5
|
+
const util_1 = require("../util");
|
|
6
|
+
class FilterManager {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.errFilterList = [];
|
|
9
|
+
this.successFilterList = [];
|
|
10
|
+
this.exceptionMap = new WeakMap();
|
|
11
|
+
this.defaultErrFilter = undefined;
|
|
12
|
+
this.matchFnList = [];
|
|
13
|
+
}
|
|
14
|
+
useFilter(Filters) {
|
|
15
|
+
if (!Array.isArray(Filters)) {
|
|
16
|
+
Filters = [Filters];
|
|
17
|
+
}
|
|
18
|
+
for (const Filter of Filters) {
|
|
19
|
+
if ((0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, Filter)) {
|
|
20
|
+
this.errFilterList.push(Filter);
|
|
21
|
+
}
|
|
22
|
+
if ((0, decorator_1.getClassMetadata)(decorator_1.MATCH_KEY, Filter)) {
|
|
23
|
+
this.successFilterList.push(Filter);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async init(applicationContext) {
|
|
28
|
+
// for catch exception
|
|
29
|
+
for (const FilterClass of this.errFilterList) {
|
|
30
|
+
const filter = await applicationContext.getAsync(FilterClass);
|
|
31
|
+
const exceptionMetadata = (0, decorator_1.getClassMetadata)(decorator_1.CATCH_KEY, FilterClass);
|
|
32
|
+
if (exceptionMetadata && exceptionMetadata.catchTargets) {
|
|
33
|
+
for (const Exception of exceptionMetadata.catchTargets) {
|
|
34
|
+
this.exceptionMap.set(Exception, filter);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// default filter
|
|
39
|
+
this.defaultErrFilter = filter;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// for success return
|
|
43
|
+
for (const FilterClass of this.successFilterList) {
|
|
44
|
+
const filter = await applicationContext.getAsync(FilterClass);
|
|
45
|
+
const matchMetadata = (0, decorator_1.getClassMetadata)(decorator_1.MATCH_KEY, FilterClass);
|
|
46
|
+
if (matchMetadata && matchMetadata.matchPattern) {
|
|
47
|
+
this.matchFnList.push({
|
|
48
|
+
matchFn: (0, util_1.toPathMatch)(matchMetadata.matchPattern),
|
|
49
|
+
target: filter,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async runErrorFilter(err, ctx, res, next) {
|
|
55
|
+
let result, error;
|
|
56
|
+
if (this.exceptionMap.has(err.constructor)) {
|
|
57
|
+
const filter = this.exceptionMap.get(err.constructor);
|
|
58
|
+
result = await filter.catch(err, ctx, res, next);
|
|
59
|
+
}
|
|
60
|
+
else if (this.defaultErrFilter) {
|
|
61
|
+
result = await this.defaultErrFilter.catch(err, ctx, res, next);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
error = err;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
result,
|
|
68
|
+
error,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async runResultFilter(result, ctx, res, next) {
|
|
72
|
+
let returnValue = result;
|
|
73
|
+
for (const matchData of this.matchFnList) {
|
|
74
|
+
if (matchData.matchFn(ctx, res)) {
|
|
75
|
+
returnValue = await matchData.target.match(returnValue, ctx, res, next);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
result: returnValue,
|
|
80
|
+
error: undefined,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.FilterManager = FilterManager;
|
|
85
|
+
//# sourceMappingURL=filterManager.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommonMiddleware, CommonMiddlewareUnion, IMidwayContext } from '../interface';
|
|
2
|
+
export declare class ContextMiddlewareManager<CTX extends IMidwayContext, R, N> extends Array<CommonMiddleware<CTX, R, N>> {
|
|
3
|
+
insertFirst(middleware: CommonMiddlewareUnion<CTX, R, N>): void;
|
|
4
|
+
insertBefore(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrBeforeMiddleware: number): any;
|
|
5
|
+
insertBefore(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrBeforeMiddleware: CommonMiddlewareUnion<CTX, R, N>): any;
|
|
6
|
+
insertAfter(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrAfterMiddleware: number): any;
|
|
7
|
+
insertAfter(middleware: CommonMiddlewareUnion<CTX, R, N>, idxOrAfterMiddleware: CommonMiddlewareUnion<CTX, R, N>): any;
|
|
8
|
+
insertLast(middleware: CommonMiddlewareUnion<CTX, R, N>): void;
|
|
9
|
+
private findItemIndex;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=middlewareManager.d.ts.map
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* @param routeArgsInfo
|
|
5
5
|
* @param routerResponseData
|
|
6
6
|
*/
|
|
7
|
-
import { MidwayFrameworkType
|
|
8
|
-
import { IMidwayContainer } from '../';
|
|
7
|
+
import { MidwayFrameworkType } from '@midwayjs/decorator';
|
|
8
|
+
import { IMidwayContainer, RouterInfo } from '../index';
|
|
9
9
|
import { ILogger } from '@midwayjs/logger';
|
|
10
10
|
export declare abstract class WebControllerGenerator<Router extends {
|
|
11
11
|
use: (...args: any[]) => void;
|
|
@@ -13,18 +13,15 @@ export declare abstract class WebControllerGenerator<Router extends {
|
|
|
13
13
|
readonly applicationContext: IMidwayContainer;
|
|
14
14
|
readonly frameworkType: MidwayFrameworkType;
|
|
15
15
|
readonly logger?: ILogger;
|
|
16
|
-
private controllerIds;
|
|
17
16
|
protected constructor(applicationContext: IMidwayContainer, frameworkType: MidwayFrameworkType, logger?: ILogger);
|
|
18
17
|
/**
|
|
19
18
|
* wrap controller string to middleware function
|
|
20
|
-
* @param
|
|
21
|
-
* @param routeArgsInfo
|
|
22
|
-
* @param routerResponseData
|
|
19
|
+
* @param routeInfo
|
|
23
20
|
*/
|
|
24
|
-
generateKoaController(
|
|
25
|
-
loadMidwayController(routerHandler?: (newRouter: Router) => void): Promise<void>;
|
|
21
|
+
generateKoaController(routeInfo: RouterInfo): (ctx: any, next: any) => Promise<void>;
|
|
22
|
+
loadMidwayController(globalPrefix: string, routerHandler?: (newRouter: Router) => void): Promise<void>;
|
|
26
23
|
abstract createRouter(routerOptions: any): Router;
|
|
27
|
-
abstract generateController(
|
|
24
|
+
abstract generateController(routeInfo: RouterInfo): any;
|
|
28
25
|
protected handlerWebMiddleware(middlewares: any[], handlerCallback: (middlewareImpl: any) => void): Promise<void>;
|
|
29
26
|
}
|
|
30
27
|
//# sourceMappingURL=webGenerator.d.ts.map
|
|
@@ -8,27 +8,23 @@ exports.WebControllerGenerator = void 0;
|
|
|
8
8
|
* @param routerResponseData
|
|
9
9
|
*/
|
|
10
10
|
const decorator_1 = require("@midwayjs/decorator");
|
|
11
|
-
const
|
|
11
|
+
const index_1 = require("../index");
|
|
12
12
|
class WebControllerGenerator {
|
|
13
13
|
constructor(applicationContext, frameworkType, logger) {
|
|
14
14
|
this.applicationContext = applicationContext;
|
|
15
15
|
this.frameworkType = frameworkType;
|
|
16
16
|
this.logger = logger;
|
|
17
|
-
this.controllerIds = [];
|
|
18
17
|
}
|
|
19
18
|
/**
|
|
20
19
|
* wrap controller string to middleware function
|
|
21
|
-
* @param
|
|
22
|
-
* @param routeArgsInfo
|
|
23
|
-
* @param routerResponseData
|
|
20
|
+
* @param routeInfo
|
|
24
21
|
*/
|
|
25
|
-
generateKoaController(
|
|
26
|
-
const [controllerId, methodName] = controllerMapping.split('.');
|
|
22
|
+
generateKoaController(routeInfo) {
|
|
27
23
|
return async (ctx, next) => {
|
|
28
24
|
const args = [ctx, next];
|
|
29
|
-
const controller = await ctx.requestContext.getAsync(
|
|
25
|
+
const controller = await ctx.requestContext.getAsync(routeInfo.id);
|
|
30
26
|
// eslint-disable-next-line prefer-spread
|
|
31
|
-
const result = await controller[
|
|
27
|
+
const result = await controller[routeInfo.method].apply(controller, args);
|
|
32
28
|
if (result !== undefined) {
|
|
33
29
|
ctx.body = result;
|
|
34
30
|
}
|
|
@@ -36,8 +32,9 @@ class WebControllerGenerator {
|
|
|
36
32
|
ctx.body = undefined;
|
|
37
33
|
}
|
|
38
34
|
// implement response decorator
|
|
39
|
-
if (Array.isArray(
|
|
40
|
-
|
|
35
|
+
if (Array.isArray(routeInfo.responseMetadata) &&
|
|
36
|
+
routeInfo.responseMetadata.length) {
|
|
37
|
+
for (const routerRes of routeInfo.responseMetadata) {
|
|
41
38
|
switch (routerRes.type) {
|
|
42
39
|
case decorator_1.WEB_RESPONSE_HTTP_CODE:
|
|
43
40
|
ctx.status = routerRes.code;
|
|
@@ -59,21 +56,17 @@ class WebControllerGenerator {
|
|
|
59
56
|
}
|
|
60
57
|
};
|
|
61
58
|
}
|
|
62
|
-
async loadMidwayController(routerHandler) {
|
|
59
|
+
async loadMidwayController(globalPrefix, routerHandler) {
|
|
63
60
|
var _a, _b;
|
|
64
|
-
const collector = new
|
|
61
|
+
const collector = new index_1.WebRouterCollector('', {
|
|
62
|
+
globalPrefix,
|
|
63
|
+
});
|
|
65
64
|
const routerTable = await collector.getRouterTable();
|
|
66
65
|
const routerList = await collector.getRoutePriorityList();
|
|
67
66
|
for (const routerInfo of routerList) {
|
|
68
67
|
// bind controller first
|
|
69
68
|
this.applicationContext.bindClass(routerInfo.routerModule);
|
|
70
|
-
|
|
71
|
-
// controller id check
|
|
72
|
-
if (this.controllerIds.indexOf(providerId) > -1) {
|
|
73
|
-
throw new Error(`Controller identifier [${providerId}] already exists!`);
|
|
74
|
-
}
|
|
75
|
-
this.controllerIds.push(providerId);
|
|
76
|
-
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`Load Controller "${providerId}", prefix=${routerInfo.prefix}`);
|
|
69
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
77
70
|
// new router
|
|
78
71
|
const newRouter = this.createRouter({
|
|
79
72
|
prefix: routerInfo.prefix,
|
|
@@ -101,7 +94,7 @@ class WebControllerGenerator {
|
|
|
101
94
|
routeInfo.routerName,
|
|
102
95
|
routeInfo.url,
|
|
103
96
|
...methodMiddlewares,
|
|
104
|
-
this.generateController(routeInfo
|
|
97
|
+
this.generateController(routeInfo),
|
|
105
98
|
];
|
|
106
99
|
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
107
100
|
// apply controller from request context
|
|
@@ -4,7 +4,7 @@ export interface RouterInfo {
|
|
|
4
4
|
*/
|
|
5
5
|
id: string;
|
|
6
6
|
/**
|
|
7
|
-
* router prefix
|
|
7
|
+
* router prefix from controller
|
|
8
8
|
*/
|
|
9
9
|
prefix: string;
|
|
10
10
|
/**
|
|
@@ -23,6 +23,9 @@ export interface RouterInfo {
|
|
|
23
23
|
* invoke function method
|
|
24
24
|
*/
|
|
25
25
|
method: string;
|
|
26
|
+
/**
|
|
27
|
+
* router description
|
|
28
|
+
*/
|
|
26
29
|
description: string;
|
|
27
30
|
summary: string;
|
|
28
31
|
/**
|
|
@@ -83,12 +86,12 @@ export interface RouterPriority {
|
|
|
83
86
|
}
|
|
84
87
|
export interface RouterCollectorOptions {
|
|
85
88
|
includeFunctionRouter?: boolean;
|
|
89
|
+
globalPrefix?: string;
|
|
86
90
|
}
|
|
87
91
|
export declare class WebRouterCollector {
|
|
88
92
|
protected readonly baseDir: string;
|
|
89
93
|
private isReady;
|
|
90
94
|
protected routes: Map<string, RouterInfo[]>;
|
|
91
|
-
protected routerModules: Set<unknown>;
|
|
92
95
|
private routesPriority;
|
|
93
96
|
protected options: RouterCollectorOptions;
|
|
94
97
|
constructor(baseDir?: string, options?: RouterCollectorOptions);
|
|
@@ -106,7 +109,7 @@ export declare class WebRouterCollector {
|
|
|
106
109
|
*/
|
|
107
110
|
id: string;
|
|
108
111
|
/**
|
|
109
|
-
* router prefix
|
|
112
|
+
* router prefix from controller
|
|
110
113
|
*/
|
|
111
114
|
prefix: string;
|
|
112
115
|
/**
|
|
@@ -125,6 +128,9 @@ export declare class WebRouterCollector {
|
|
|
125
128
|
* invoke function method
|
|
126
129
|
*/
|
|
127
130
|
method: string;
|
|
131
|
+
/**
|
|
132
|
+
* router description
|
|
133
|
+
*/
|
|
128
134
|
description: string;
|
|
129
135
|
summary: string;
|
|
130
136
|
/**
|
|
@@ -175,7 +181,6 @@ export declare class WebRouterCollector {
|
|
|
175
181
|
getRoutePriorityList(): Promise<RouterPriority[]>;
|
|
176
182
|
getRouterTable(): Promise<Map<string, RouterInfo[]>>;
|
|
177
183
|
getFlattenRouterTable(): Promise<RouterInfo[]>;
|
|
178
|
-
getRouterModules(): Promise<any[]>;
|
|
179
184
|
private checkDuplicateAndPush;
|
|
180
185
|
}
|
|
181
186
|
//# sourceMappingURL=webRouterCollector.d.ts.map
|