@midwayjs/core 3.0.0-beta.1 → 3.0.0-beta.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 +49 -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 -8
- package/dist/index.js +10 -8
- package/dist/interface.d.ts +50 -28
- package/dist/service/configService.js +4 -5
- package/dist/service/decoratorService.js +2 -0
- 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 +95 -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,55 @@
|
|
|
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.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* [@match](https://github.com/match) empty args ([#1384](https://github.com/midwayjs/midway/issues/1384)) ([6f90fc9](https://github.com/midwayjs/midway/commit/6f90fc993ff01e078288ff664833c61c02dede51))
|
|
12
|
+
* router sort ([#1383](https://github.com/midwayjs/midway/issues/1383)) ([f253887](https://github.com/midwayjs/midway/commit/f2538876d3eaf7dec55173d86b5b9caeeeb7be64))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Bug Fixes
|
|
22
|
+
|
|
23
|
+
* logger close before bootstrap close ([#1370](https://github.com/midwayjs/midway/issues/1370)) ([6cc2720](https://github.com/midwayjs/midway/commit/6cc2720ed3445e8ffccc96d124b80ed7e2517f08))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
|
|
29
|
+
* auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
|
|
30
|
+
* support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
|
|
48
|
+
|
|
49
|
+
**Note:** Version bump only for package @midwayjs/core
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
6
55
|
# [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
|
|
7
56
|
|
|
8
57
|
|
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
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WebRouterCollector = void 0;
|
|
4
4
|
const decorator_1 = require("@midwayjs/decorator");
|
|
5
|
-
const
|
|
5
|
+
const util_1 = require("../util");
|
|
6
6
|
const container_1 = require("../context/container");
|
|
7
7
|
const fileDetector_1 = require("./fileDetector");
|
|
8
8
|
const util = require("util");
|
|
@@ -11,7 +11,6 @@ class WebRouterCollector {
|
|
|
11
11
|
constructor(baseDir = '', options = {}) {
|
|
12
12
|
this.isReady = false;
|
|
13
13
|
this.routes = new Map();
|
|
14
|
-
this.routerModules = new Set();
|
|
15
14
|
this.routesPriority = [];
|
|
16
15
|
this.baseDir = baseDir;
|
|
17
16
|
this.options = options;
|
|
@@ -34,6 +33,17 @@ class WebRouterCollector {
|
|
|
34
33
|
this.collectFunctionRoute(module);
|
|
35
34
|
}
|
|
36
35
|
}
|
|
36
|
+
// filter empty prefix
|
|
37
|
+
this.routesPriority = this.routesPriority.filter(item => {
|
|
38
|
+
const prefixList = this.routes.get(item.prefix);
|
|
39
|
+
if (prefixList.length > 0) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.routes.delete(item.prefix);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
37
47
|
// sort router
|
|
38
48
|
for (const prefix of this.routes.keys()) {
|
|
39
49
|
const routerInfo = this.routes.get(prefix);
|
|
@@ -41,10 +51,11 @@ class WebRouterCollector {
|
|
|
41
51
|
}
|
|
42
52
|
// sort prefix
|
|
43
53
|
this.routesPriority = this.routesPriority.sort((routeA, routeB) => {
|
|
44
|
-
return routeB.
|
|
54
|
+
return routeB.prefix.length - routeA.prefix.length;
|
|
45
55
|
});
|
|
46
56
|
}
|
|
47
57
|
collectRoute(module, functionMeta = false) {
|
|
58
|
+
var _a;
|
|
48
59
|
const controllerId = (0, decorator_1.getProviderName)(module);
|
|
49
60
|
debug(`[core:webCollector]: Found Controller ${controllerId}.`);
|
|
50
61
|
const id = (0, decorator_1.getProviderUUId)(module);
|
|
@@ -52,21 +63,36 @@ class WebRouterCollector {
|
|
|
52
63
|
let priority;
|
|
53
64
|
// implement middleware in controller
|
|
54
65
|
const middleware = controllerOption.routerOptions.middleware;
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
const controllerIgnoreGlobalPrefix = !!((_a = controllerOption.routerOptions) === null || _a === void 0 ? void 0 : _a.ignoreGlobalPrefix);
|
|
67
|
+
let prefix = (0, util_1.joinURLPath)(this.options.globalPrefix, controllerOption.prefix || '/');
|
|
68
|
+
const ignorePrefix = controllerOption.prefix || '/';
|
|
69
|
+
// if controller set ignore global prefix, all router will be ignore too.
|
|
70
|
+
if (controllerIgnoreGlobalPrefix) {
|
|
71
|
+
prefix = ignorePrefix;
|
|
58
72
|
}
|
|
73
|
+
// set prefix
|
|
59
74
|
if (!this.routes.has(prefix)) {
|
|
60
75
|
this.routes.set(prefix, []);
|
|
61
76
|
this.routesPriority.push({
|
|
62
77
|
prefix,
|
|
63
|
-
priority: priority
|
|
78
|
+
priority: prefix === '/' && priority === undefined ? -999 : 0,
|
|
79
|
+
middleware,
|
|
80
|
+
routerOptions: controllerOption.routerOptions,
|
|
81
|
+
controllerId,
|
|
82
|
+
routerModule: module,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// set ignorePrefix
|
|
86
|
+
if (!this.routes.has(ignorePrefix)) {
|
|
87
|
+
this.routes.set(ignorePrefix, []);
|
|
88
|
+
this.routesPriority.push({
|
|
89
|
+
prefix: ignorePrefix,
|
|
90
|
+
priority: ignorePrefix === '/' && priority === undefined ? -999 : 0,
|
|
64
91
|
middleware,
|
|
65
92
|
routerOptions: controllerOption.routerOptions,
|
|
66
93
|
controllerId,
|
|
67
94
|
routerModule: module,
|
|
68
95
|
});
|
|
69
|
-
this.routerModules.add(module);
|
|
70
96
|
}
|
|
71
97
|
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.WEB_ROUTER_KEY, module);
|
|
72
98
|
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
|
|
@@ -75,7 +101,7 @@ class WebRouterCollector {
|
|
|
75
101
|
const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter.method) || [];
|
|
76
102
|
const data = {
|
|
77
103
|
id,
|
|
78
|
-
prefix,
|
|
104
|
+
prefix: webRouter.ignoreGlobalPrefix ? ignorePrefix : prefix,
|
|
79
105
|
routerName: webRouter.routerName || '',
|
|
80
106
|
url: webRouter.path,
|
|
81
107
|
requestMethod: webRouter.requestMethod,
|
|
@@ -95,14 +121,14 @@ class WebRouterCollector {
|
|
|
95
121
|
data.functionName = controllerId + '-' + webRouter.method;
|
|
96
122
|
data.functionTriggerName = decorator_1.ServerlessTriggerType.HTTP;
|
|
97
123
|
data.functionTriggerMetadata = {
|
|
98
|
-
path: (0,
|
|
124
|
+
path: (0, util_1.joinURLPath)(prefix, webRouter.path.toString()),
|
|
99
125
|
method: webRouter.requestMethod,
|
|
100
126
|
};
|
|
101
127
|
data.functionMetadata = {
|
|
102
128
|
functionName: data.functionName,
|
|
103
129
|
};
|
|
104
130
|
}
|
|
105
|
-
this.checkDuplicateAndPush(prefix, data);
|
|
131
|
+
this.checkDuplicateAndPush(data.prefix, data);
|
|
106
132
|
}
|
|
107
133
|
}
|
|
108
134
|
}
|
|
@@ -123,7 +149,6 @@ class WebRouterCollector {
|
|
|
123
149
|
controllerId,
|
|
124
150
|
routerModule: module,
|
|
125
151
|
});
|
|
126
|
-
this.routerModules.add(module);
|
|
127
152
|
}
|
|
128
153
|
for (const webRouter of webRouterInfo) {
|
|
129
154
|
// 新的 @ServerlessTrigger 写法
|
|
@@ -204,12 +229,14 @@ class WebRouterCollector {
|
|
|
204
229
|
.map(item => {
|
|
205
230
|
const urlString = item.url.toString();
|
|
206
231
|
const weightArr = (0, decorator_1.isRegExp)(item.url)
|
|
207
|
-
? urlString.split('
|
|
232
|
+
? urlString.split('\\/')
|
|
208
233
|
: urlString.split('/');
|
|
209
234
|
let weight = 0;
|
|
210
235
|
// 权重,比如通配的不加权,非通配加权,防止通配出现在最前面
|
|
211
236
|
for (const fragment of weightArr) {
|
|
212
|
-
if (fragment
|
|
237
|
+
if (fragment === '' ||
|
|
238
|
+
fragment.includes(':') ||
|
|
239
|
+
fragment.includes('*')) {
|
|
213
240
|
weight += 0;
|
|
214
241
|
}
|
|
215
242
|
else {
|
|
@@ -240,12 +267,12 @@ class WebRouterCollector {
|
|
|
240
267
|
if (handlerA._category !== handlerB._category) {
|
|
241
268
|
return handlerB._category - handlerA._category;
|
|
242
269
|
}
|
|
270
|
+
// 不同权重
|
|
271
|
+
if (handlerA._weight !== handlerB._weight) {
|
|
272
|
+
return handlerB._weight - handlerA._weight;
|
|
273
|
+
}
|
|
243
274
|
// 不同长度
|
|
244
275
|
if (handlerA._level === handlerB._level) {
|
|
245
|
-
// 不同权重
|
|
246
|
-
if (handlerA._weight !== handlerB._weight) {
|
|
247
|
-
return handlerB._weight - handlerA._weight;
|
|
248
|
-
}
|
|
249
276
|
if (handlerB._pureRouter === handlerA._pureRouter) {
|
|
250
277
|
return (handlerA.url.toString().length - handlerB.url.toString().length);
|
|
251
278
|
}
|
|
@@ -274,18 +301,11 @@ class WebRouterCollector {
|
|
|
274
301
|
this.isReady = true;
|
|
275
302
|
}
|
|
276
303
|
let routeArr = [];
|
|
277
|
-
for (const
|
|
278
|
-
routeArr = routeArr.concat(
|
|
304
|
+
for (const routerPriority of this.routesPriority) {
|
|
305
|
+
routeArr = routeArr.concat(this.routes.get(routerPriority.prefix));
|
|
279
306
|
}
|
|
280
307
|
return routeArr;
|
|
281
308
|
}
|
|
282
|
-
async getRouterModules() {
|
|
283
|
-
if (!this.isReady) {
|
|
284
|
-
await this.analyze();
|
|
285
|
-
this.isReady = true;
|
|
286
|
-
}
|
|
287
|
-
return Array.from(this.routerModules);
|
|
288
|
-
}
|
|
289
309
|
checkDuplicateAndPush(prefix, routerInfo) {
|
|
290
310
|
const prefixList = this.routes.get(prefix);
|
|
291
311
|
const matched = prefixList.filter(item => {
|