@midwayjs/core 3.0.0-beta.1 → 3.0.0-beta.13
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 +138 -0
- package/README.md +1 -1
- package/dist/baseFramework.d.ts +13 -10
- package/dist/baseFramework.js +32 -23
- package/dist/common/applicationManager.d.ts +12 -0
- package/dist/common/applicationManager.js +66 -0
- 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 +68 -0
- package/dist/common/middlewareManager.js +183 -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/common/webGenerator.d.ts +16 -0
- package/dist/{util → common}/webGenerator.js +36 -51
- package/dist/{util → common}/webRouterCollector.d.ts +9 -4
- package/dist/{util → common}/webRouterCollector.js +53 -29
- package/dist/config/config.default.d.ts +3 -17
- package/dist/context/container.js +13 -2
- package/dist/context/managedResolverFactory.js +5 -5
- package/dist/definitions/objectCreator.js +3 -2
- package/dist/error/base.d.ts +24 -3
- package/dist/error/base.js +34 -4
- package/dist/error/framework.d.ts +26 -2
- package/dist/error/framework.js +48 -10
- package/dist/error/http.d.ts +145 -38
- package/dist/error/http.js +162 -30
- package/dist/error/index.d.ts +1 -0
- package/dist/error/index.js +1 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.js +13 -11
- package/dist/interface.d.ts +59 -30
- package/dist/service/configService.js +4 -5
- package/dist/service/decoratorService.js +33 -25
- package/dist/service/frameworkService.d.ts +6 -4
- package/dist/service/frameworkService.js +26 -11
- package/dist/service/lifeCycleService.js +7 -5
- package/dist/service/loggerService.d.ts +1 -2
- package/dist/service/loggerService.js +1 -10
- package/dist/service/middlewareService.d.ts +3 -4
- package/dist/service/middlewareService.js +28 -46
- package/dist/setup.js +10 -2
- package/dist/util/contextUtil.d.ts +1 -1
- package/dist/util/index.d.ts +46 -0
- package/dist/util/index.js +150 -1
- package/dist/util/webRouterParam.d.ts +2 -2
- package/dist/util/webRouterParam.js +41 -22
- package/package.json +7 -7
- package/dist/error/code.d.ts +0 -59
- package/dist/error/code.js +0 -64
- package/dist/util/exceptionFilterManager.d.ts +0 -13
- package/dist/util/exceptionFilterManager.js +0 -53
- package/dist/util/middlewareManager.d.ts +0 -11
- package/dist/util/middlewareManager.js +0 -48
- package/dist/util/webGenerator.d.ts +0 -30
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContextMiddlewareManager = void 0;
|
|
4
|
+
class ContextMiddlewareManager extends Array {
|
|
5
|
+
/**
|
|
6
|
+
* insert a middleware or middleware array to first
|
|
7
|
+
* @param middleware
|
|
8
|
+
*/
|
|
9
|
+
insertFirst(middleware) {
|
|
10
|
+
if (Array.isArray(middleware)) {
|
|
11
|
+
this.unshift(...middleware);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
this.unshift(middleware);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* insert a middleware or middleware array to last
|
|
19
|
+
* @param middleware
|
|
20
|
+
*/
|
|
21
|
+
insertLast(middleware) {
|
|
22
|
+
if (Array.isArray(middleware)) {
|
|
23
|
+
this.push(...middleware);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
this.push(middleware);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* insert a middleware or middleware array to after another middleware
|
|
31
|
+
* @param middleware
|
|
32
|
+
* @param idxOrBeforeMiddleware
|
|
33
|
+
*/
|
|
34
|
+
insertBefore(middleware, idxOrBeforeMiddleware) {
|
|
35
|
+
if (typeof idxOrBeforeMiddleware !== 'number') {
|
|
36
|
+
idxOrBeforeMiddleware = this.findItemIndex(idxOrBeforeMiddleware);
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(middleware)) {
|
|
39
|
+
this.splice(idxOrBeforeMiddleware, 0, ...middleware);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.splice(idxOrBeforeMiddleware, 0, middleware);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* insert a middleware or middleware array to after another middleware
|
|
47
|
+
* @param middleware
|
|
48
|
+
* @param idxOrAfterMiddleware
|
|
49
|
+
*/
|
|
50
|
+
insertAfter(middleware, idxOrAfterMiddleware) {
|
|
51
|
+
if (typeof idxOrAfterMiddleware !== 'number') {
|
|
52
|
+
idxOrAfterMiddleware = this.findItemIndex(idxOrAfterMiddleware);
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(middleware)) {
|
|
55
|
+
this.splice(idxOrAfterMiddleware + 1, 0, ...middleware);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
this.splice(idxOrAfterMiddleware + 1, 0, middleware);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* move a middleware after another middleware
|
|
63
|
+
* @param middlewareOrName
|
|
64
|
+
* @param afterMiddleware
|
|
65
|
+
*/
|
|
66
|
+
findAndInsertAfter(middlewareOrName, afterMiddleware) {
|
|
67
|
+
middlewareOrName = this.findItem(middlewareOrName);
|
|
68
|
+
afterMiddleware = this.findItem(afterMiddleware);
|
|
69
|
+
if (!middlewareOrName ||
|
|
70
|
+
!afterMiddleware ||
|
|
71
|
+
middlewareOrName === afterMiddleware) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (afterMiddleware) {
|
|
75
|
+
const mw = this.remove(middlewareOrName);
|
|
76
|
+
if (mw) {
|
|
77
|
+
this.insertAfter(mw, afterMiddleware);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* move a middleware before another middleware
|
|
83
|
+
* @param middlewareOrName
|
|
84
|
+
* @param beforeMiddleware
|
|
85
|
+
*/
|
|
86
|
+
findAndInsertBefore(middlewareOrName, beforeMiddleware) {
|
|
87
|
+
middlewareOrName = this.findItem(middlewareOrName);
|
|
88
|
+
beforeMiddleware = this.findItem(beforeMiddleware);
|
|
89
|
+
if (!middlewareOrName ||
|
|
90
|
+
!beforeMiddleware ||
|
|
91
|
+
middlewareOrName === beforeMiddleware) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (beforeMiddleware) {
|
|
95
|
+
const mw = this.remove(middlewareOrName);
|
|
96
|
+
if (mw) {
|
|
97
|
+
this.insertBefore(mw, beforeMiddleware);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* find middleware and move to first
|
|
103
|
+
* @param middlewareOrName
|
|
104
|
+
*/
|
|
105
|
+
findAndInsertFirst(middlewareOrName) {
|
|
106
|
+
const mw = this.remove(middlewareOrName);
|
|
107
|
+
if (mw) {
|
|
108
|
+
this.insertFirst(mw);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* find middleware and move to last
|
|
113
|
+
* @param middlewareOrName
|
|
114
|
+
*/
|
|
115
|
+
findAndInsertLast(middlewareOrName) {
|
|
116
|
+
const mw = this.remove(middlewareOrName);
|
|
117
|
+
if (mw) {
|
|
118
|
+
this.insertLast(mw);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* find a middleware and return index
|
|
123
|
+
* @param middlewareOrName
|
|
124
|
+
*/
|
|
125
|
+
findItemIndex(middlewareOrName) {
|
|
126
|
+
if (typeof middlewareOrName === 'number') {
|
|
127
|
+
return middlewareOrName;
|
|
128
|
+
}
|
|
129
|
+
else if (typeof middlewareOrName === 'string') {
|
|
130
|
+
return this.findIndex(item => this.getMiddlewareName(item) === middlewareOrName);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return this.findIndex(item => item === middlewareOrName);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
findItem(middlewareOrName) {
|
|
137
|
+
if (typeof middlewareOrName === 'number') {
|
|
138
|
+
if (middlewareOrName >= 0 && middlewareOrName <= this.length - 1) {
|
|
139
|
+
return this[middlewareOrName];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (typeof middlewareOrName === 'string') {
|
|
143
|
+
return this.find(item => this.getMiddlewareName(item) === middlewareOrName);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
return middlewareOrName;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* get name from middleware
|
|
151
|
+
* @param middleware
|
|
152
|
+
*/
|
|
153
|
+
getMiddlewareName(middleware) {
|
|
154
|
+
var _a, _b;
|
|
155
|
+
return ((_b = (_a = (middleware.getName && middleware.getName())) !== null && _a !== void 0 ? _a : middleware._name) !== null && _b !== void 0 ? _b : middleware.name);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* remove a middleware
|
|
159
|
+
* @param middlewareOrNameOrIdx
|
|
160
|
+
*/
|
|
161
|
+
remove(middlewareOrNameOrIdx) {
|
|
162
|
+
if (typeof middlewareOrNameOrIdx === 'number' &&
|
|
163
|
+
middlewareOrNameOrIdx !== -1) {
|
|
164
|
+
return this.splice(middlewareOrNameOrIdx, 1)[0];
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
const idx = this.findItemIndex(middlewareOrNameOrIdx);
|
|
168
|
+
if (idx !== -1) {
|
|
169
|
+
return this.splice(idx, 1)[0];
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* get middleware name list
|
|
175
|
+
*/
|
|
176
|
+
getNames() {
|
|
177
|
+
return this.map(item => {
|
|
178
|
+
return this.getMiddlewareName(item);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
exports.ContextMiddlewareManager = ContextMiddlewareManager;
|
|
183
|
+
//# sourceMappingURL=middlewareManager.js.map
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RouterInfo, IMidwayApplication } from '../index';
|
|
2
|
+
export declare abstract class WebControllerGenerator<Router extends {
|
|
3
|
+
use: (...args: any[]) => void;
|
|
4
|
+
}> {
|
|
5
|
+
readonly app: IMidwayApplication;
|
|
6
|
+
protected constructor(app: IMidwayApplication);
|
|
7
|
+
/**
|
|
8
|
+
* wrap controller string to middleware function
|
|
9
|
+
* @param routeInfo
|
|
10
|
+
*/
|
|
11
|
+
generateKoaController(routeInfo: RouterInfo): (ctx: any, next: any) => Promise<void>;
|
|
12
|
+
loadMidwayController(globalPrefix: string, routerHandler?: (newRouter: Router) => void): Promise<void>;
|
|
13
|
+
abstract createRouter(routerOptions: any): Router;
|
|
14
|
+
abstract generateController(routeInfo: RouterInfo): any;
|
|
15
|
+
}
|
|
16
|
+
//# 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
|
+
const util = require("util");
|
|
13
|
+
const debug = util.debuglog('midway:debug');
|
|
12
14
|
class WebControllerGenerator {
|
|
13
|
-
constructor(
|
|
14
|
-
this.
|
|
15
|
-
this.frameworkType = frameworkType;
|
|
16
|
-
this.logger = logger;
|
|
17
|
-
this.controllerIds = [];
|
|
15
|
+
constructor(app) {
|
|
16
|
+
this.app = app;
|
|
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,40 +56,43 @@ 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();
|
|
66
|
+
const applicationContext = this.app.getApplicationContext();
|
|
67
|
+
const logger = this.app.getCoreLogger();
|
|
68
|
+
const middlewareService = applicationContext.get(index_1.MidwayMiddlewareService);
|
|
67
69
|
for (const routerInfo of routerList) {
|
|
68
70
|
// bind controller first
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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}`);
|
|
71
|
+
applicationContext.bindClass(routerInfo.routerModule);
|
|
72
|
+
logger.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
73
|
+
debug(`[core]: Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
77
74
|
// new router
|
|
78
75
|
const newRouter = this.createRouter({
|
|
79
76
|
prefix: routerInfo.prefix,
|
|
80
77
|
...routerInfo.routerOptions,
|
|
81
78
|
});
|
|
82
79
|
// add router middleware
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
routerInfo.middleware = (_a = routerInfo.middleware) !== null && _a !== void 0 ? _a : [];
|
|
81
|
+
if (routerInfo.middleware.length) {
|
|
82
|
+
const routerMiddlewareFn = await middlewareService.compose(routerInfo.middleware, this.app);
|
|
83
|
+
newRouter.use(routerMiddlewareFn);
|
|
84
|
+
}
|
|
86
85
|
// add route
|
|
87
86
|
const routes = routerTable.get(routerInfo.prefix);
|
|
88
87
|
for (const routeInfo of routes) {
|
|
89
88
|
// get middleware
|
|
90
|
-
const middlewares2 = routeInfo.middleware;
|
|
91
89
|
const methodMiddlewares = [];
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
90
|
+
routeInfo.middleware = (_b = routeInfo.middleware) !== null && _b !== void 0 ? _b : [];
|
|
91
|
+
if (routeInfo.middleware.length) {
|
|
92
|
+
const routeMiddlewareFn = await middlewareService.compose(routeInfo.middleware, this.app);
|
|
93
|
+
methodMiddlewares.push(routeMiddlewareFn);
|
|
94
|
+
}
|
|
95
|
+
if (this.app.getFrameworkType() === decorator_1.MidwayFrameworkType.WEB_KOA) {
|
|
96
96
|
if (typeof routeInfo.url === 'string' && /\*$/.test(routeInfo.url)) {
|
|
97
97
|
routeInfo.url = routeInfo.url.replace('*', '(.*)');
|
|
98
98
|
}
|
|
@@ -101,9 +101,10 @@ class WebControllerGenerator {
|
|
|
101
101
|
routeInfo.routerName,
|
|
102
102
|
routeInfo.url,
|
|
103
103
|
...methodMiddlewares,
|
|
104
|
-
this.generateController(routeInfo
|
|
104
|
+
this.generateController(routeInfo),
|
|
105
105
|
];
|
|
106
|
-
|
|
106
|
+
logger.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
107
|
+
debug(`[core]: Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
107
108
|
// apply controller from request context
|
|
108
109
|
// eslint-disable-next-line prefer-spread
|
|
109
110
|
newRouter[routeInfo.requestMethod].apply(newRouter, routerArgs);
|
|
@@ -111,22 +112,6 @@ class WebControllerGenerator {
|
|
|
111
112
|
routerHandler && routerHandler(newRouter);
|
|
112
113
|
}
|
|
113
114
|
}
|
|
114
|
-
async handlerWebMiddleware(middlewares, handlerCallback) {
|
|
115
|
-
if (middlewares && middlewares.length) {
|
|
116
|
-
for (const middleware of middlewares) {
|
|
117
|
-
if (typeof middleware === 'function') {
|
|
118
|
-
// web function middleware
|
|
119
|
-
handlerCallback(middleware);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
const middlewareImpl = await this.applicationContext.getAsync(middleware);
|
|
123
|
-
if (middlewareImpl && typeof middlewareImpl.resolve === 'function') {
|
|
124
|
-
handlerCallback(middlewareImpl.resolve());
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
115
|
}
|
|
131
116
|
exports.WebControllerGenerator = WebControllerGenerator;
|
|
132
117
|
//# sourceMappingURL=webGenerator.js.map
|
|
@@ -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,16 +2,16 @@
|
|
|
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");
|
|
9
|
+
const error_1 = require("../error");
|
|
9
10
|
const debug = util.debuglog('midway:debug');
|
|
10
11
|
class WebRouterCollector {
|
|
11
12
|
constructor(baseDir = '', options = {}) {
|
|
12
13
|
this.isReady = false;
|
|
13
14
|
this.routes = new Map();
|
|
14
|
-
this.routerModules = new Set();
|
|
15
15
|
this.routesPriority = [];
|
|
16
16
|
this.baseDir = baseDir;
|
|
17
17
|
this.options = options;
|
|
@@ -34,6 +34,17 @@ class WebRouterCollector {
|
|
|
34
34
|
this.collectFunctionRoute(module);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
+
// filter empty prefix
|
|
38
|
+
this.routesPriority = this.routesPriority.filter(item => {
|
|
39
|
+
const prefixList = this.routes.get(item.prefix);
|
|
40
|
+
if (prefixList.length > 0) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.routes.delete(item.prefix);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
37
48
|
// sort router
|
|
38
49
|
for (const prefix of this.routes.keys()) {
|
|
39
50
|
const routerInfo = this.routes.get(prefix);
|
|
@@ -41,32 +52,51 @@ class WebRouterCollector {
|
|
|
41
52
|
}
|
|
42
53
|
// sort prefix
|
|
43
54
|
this.routesPriority = this.routesPriority.sort((routeA, routeB) => {
|
|
44
|
-
return routeB.
|
|
55
|
+
return routeB.prefix.length - routeA.prefix.length;
|
|
45
56
|
});
|
|
46
57
|
}
|
|
47
58
|
collectRoute(module, functionMeta = false) {
|
|
59
|
+
var _a;
|
|
48
60
|
const controllerId = (0, decorator_1.getProviderName)(module);
|
|
49
|
-
debug(`[core
|
|
61
|
+
debug(`[core]: Found Controller ${controllerId}.`);
|
|
50
62
|
const id = (0, decorator_1.getProviderUUId)(module);
|
|
51
63
|
const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, module);
|
|
52
64
|
let priority;
|
|
53
65
|
// implement middleware in controller
|
|
54
66
|
const middleware = controllerOption.routerOptions.middleware;
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
67
|
+
const controllerIgnoreGlobalPrefix = !!((_a = controllerOption.routerOptions) === null || _a === void 0 ? void 0 : _a.ignoreGlobalPrefix);
|
|
68
|
+
let prefix = (0, util_1.joinURLPath)(this.options.globalPrefix, controllerOption.prefix || '/');
|
|
69
|
+
const ignorePrefix = controllerOption.prefix || '/';
|
|
70
|
+
// if controller set ignore global prefix, all router will be ignore too.
|
|
71
|
+
if (controllerIgnoreGlobalPrefix) {
|
|
72
|
+
prefix = ignorePrefix;
|
|
73
|
+
}
|
|
74
|
+
if (/\*/.test(prefix)) {
|
|
75
|
+
throw new error_1.MidwayCommonError(`Router prefix ${prefix} can't set string with *`);
|
|
58
76
|
}
|
|
77
|
+
// set prefix
|
|
59
78
|
if (!this.routes.has(prefix)) {
|
|
60
79
|
this.routes.set(prefix, []);
|
|
61
80
|
this.routesPriority.push({
|
|
62
81
|
prefix,
|
|
63
|
-
priority: priority
|
|
82
|
+
priority: prefix === '/' && priority === undefined ? -999 : 0,
|
|
83
|
+
middleware,
|
|
84
|
+
routerOptions: controllerOption.routerOptions,
|
|
85
|
+
controllerId,
|
|
86
|
+
routerModule: module,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
// set ignorePrefix
|
|
90
|
+
if (!this.routes.has(ignorePrefix)) {
|
|
91
|
+
this.routes.set(ignorePrefix, []);
|
|
92
|
+
this.routesPriority.push({
|
|
93
|
+
prefix: ignorePrefix,
|
|
94
|
+
priority: ignorePrefix === '/' && priority === undefined ? -999 : 0,
|
|
64
95
|
middleware,
|
|
65
96
|
routerOptions: controllerOption.routerOptions,
|
|
66
97
|
controllerId,
|
|
67
98
|
routerModule: module,
|
|
68
99
|
});
|
|
69
|
-
this.routerModules.add(module);
|
|
70
100
|
}
|
|
71
101
|
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.WEB_ROUTER_KEY, module);
|
|
72
102
|
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
|
|
@@ -75,7 +105,7 @@ class WebRouterCollector {
|
|
|
75
105
|
const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter.method) || [];
|
|
76
106
|
const data = {
|
|
77
107
|
id,
|
|
78
|
-
prefix,
|
|
108
|
+
prefix: webRouter.ignoreGlobalPrefix ? ignorePrefix : prefix,
|
|
79
109
|
routerName: webRouter.routerName || '',
|
|
80
110
|
url: webRouter.path,
|
|
81
111
|
requestMethod: webRouter.requestMethod,
|
|
@@ -95,14 +125,14 @@ class WebRouterCollector {
|
|
|
95
125
|
data.functionName = controllerId + '-' + webRouter.method;
|
|
96
126
|
data.functionTriggerName = decorator_1.ServerlessTriggerType.HTTP;
|
|
97
127
|
data.functionTriggerMetadata = {
|
|
98
|
-
path: (0,
|
|
128
|
+
path: (0, util_1.joinURLPath)(prefix, webRouter.path.toString()),
|
|
99
129
|
method: webRouter.requestMethod,
|
|
100
130
|
};
|
|
101
131
|
data.functionMetadata = {
|
|
102
132
|
functionName: data.functionName,
|
|
103
133
|
};
|
|
104
134
|
}
|
|
105
|
-
this.checkDuplicateAndPush(prefix, data);
|
|
135
|
+
this.checkDuplicateAndPush(data.prefix, data);
|
|
106
136
|
}
|
|
107
137
|
}
|
|
108
138
|
}
|
|
@@ -123,7 +153,6 @@ class WebRouterCollector {
|
|
|
123
153
|
controllerId,
|
|
124
154
|
routerModule: module,
|
|
125
155
|
});
|
|
126
|
-
this.routerModules.add(module);
|
|
127
156
|
}
|
|
128
157
|
for (const webRouter of webRouterInfo) {
|
|
129
158
|
// 新的 @ServerlessTrigger 写法
|
|
@@ -204,12 +233,14 @@ class WebRouterCollector {
|
|
|
204
233
|
.map(item => {
|
|
205
234
|
const urlString = item.url.toString();
|
|
206
235
|
const weightArr = (0, decorator_1.isRegExp)(item.url)
|
|
207
|
-
? urlString.split('
|
|
236
|
+
? urlString.split('\\/')
|
|
208
237
|
: urlString.split('/');
|
|
209
238
|
let weight = 0;
|
|
210
239
|
// 权重,比如通配的不加权,非通配加权,防止通配出现在最前面
|
|
211
240
|
for (const fragment of weightArr) {
|
|
212
|
-
if (fragment
|
|
241
|
+
if (fragment === '' ||
|
|
242
|
+
fragment.includes(':') ||
|
|
243
|
+
fragment.includes('*')) {
|
|
213
244
|
weight += 0;
|
|
214
245
|
}
|
|
215
246
|
else {
|
|
@@ -240,12 +271,12 @@ class WebRouterCollector {
|
|
|
240
271
|
if (handlerA._category !== handlerB._category) {
|
|
241
272
|
return handlerB._category - handlerA._category;
|
|
242
273
|
}
|
|
274
|
+
// 不同权重
|
|
275
|
+
if (handlerA._weight !== handlerB._weight) {
|
|
276
|
+
return handlerB._weight - handlerA._weight;
|
|
277
|
+
}
|
|
243
278
|
// 不同长度
|
|
244
279
|
if (handlerA._level === handlerB._level) {
|
|
245
|
-
// 不同权重
|
|
246
|
-
if (handlerA._weight !== handlerB._weight) {
|
|
247
|
-
return handlerB._weight - handlerA._weight;
|
|
248
|
-
}
|
|
249
280
|
if (handlerB._pureRouter === handlerA._pureRouter) {
|
|
250
281
|
return (handlerA.url.toString().length - handlerB.url.toString().length);
|
|
251
282
|
}
|
|
@@ -274,18 +305,11 @@ class WebRouterCollector {
|
|
|
274
305
|
this.isReady = true;
|
|
275
306
|
}
|
|
276
307
|
let routeArr = [];
|
|
277
|
-
for (const
|
|
278
|
-
routeArr = routeArr.concat(
|
|
308
|
+
for (const routerPriority of this.routesPriority) {
|
|
309
|
+
routeArr = routeArr.concat(this.routes.get(routerPriority.prefix));
|
|
279
310
|
}
|
|
280
311
|
return routeArr;
|
|
281
312
|
}
|
|
282
|
-
async getRouterModules() {
|
|
283
|
-
if (!this.isReady) {
|
|
284
|
-
await this.analyze();
|
|
285
|
-
this.isReady = true;
|
|
286
|
-
}
|
|
287
|
-
return Array.from(this.routerModules);
|
|
288
|
-
}
|
|
289
313
|
checkDuplicateAndPush(prefix, routerInfo) {
|
|
290
314
|
const prefixList = this.routes.get(prefix);
|
|
291
315
|
const matched = prefixList.filter(item => {
|
|
@@ -295,7 +319,7 @@ class WebRouterCollector {
|
|
|
295
319
|
item.requestMethod === routerInfo.requestMethod);
|
|
296
320
|
});
|
|
297
321
|
if (matched && matched.length) {
|
|
298
|
-
throw new
|
|
322
|
+
throw new error_1.MidwayDuplicateRouteError(`${routerInfo.requestMethod} ${routerInfo.url}`, `${matched[0].handlerName}`, `${routerInfo.handlerName}`);
|
|
299
323
|
}
|
|
300
324
|
prefixList.push(routerInfo);
|
|
301
325
|
}
|
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
import { MidwayAppInfo } from '../interface';
|
|
1
|
+
import { MidwayAppInfo, ServiceFactoryConfigOption } from '../interface';
|
|
2
|
+
import type { LoggerOptions } from '@midwayjs/logger';
|
|
2
3
|
declare const _default: (appInfo: MidwayAppInfo) => {
|
|
3
|
-
midwayLogger
|
|
4
|
-
default: {
|
|
5
|
-
dir: string;
|
|
6
|
-
level: string;
|
|
7
|
-
consoleLevel: string;
|
|
8
|
-
};
|
|
9
|
-
clients: {
|
|
10
|
-
coreLogger: {
|
|
11
|
-
fileLogName: string;
|
|
12
|
-
};
|
|
13
|
-
appLogger: {
|
|
14
|
-
fileLogName: string;
|
|
15
|
-
aliasName: string;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
};
|
|
4
|
+
midwayLogger?: ServiceFactoryConfigOption<LoggerOptions>;
|
|
19
5
|
};
|
|
20
6
|
export default _default;
|
|
21
7
|
//# sourceMappingURL=config.default.d.ts.map
|
|
@@ -64,8 +64,13 @@ class ContainerConfiguration {
|
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
addImportConfigs(importConfigs) {
|
|
67
|
-
if (importConfigs
|
|
68
|
-
|
|
67
|
+
if (importConfigs) {
|
|
68
|
+
if (Array.isArray(importConfigs)) {
|
|
69
|
+
this.container.get(configService_1.MidwayConfigService).add(importConfigs);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.container.get(configService_1.MidwayConfigService).addObject(importConfigs);
|
|
73
|
+
}
|
|
69
74
|
}
|
|
70
75
|
}
|
|
71
76
|
addImports(imports = []) {
|
|
@@ -379,7 +384,10 @@ class MidwayContainer {
|
|
|
379
384
|
}
|
|
380
385
|
get(identifier, args, objectContext) {
|
|
381
386
|
var _a;
|
|
387
|
+
args = args !== null && args !== void 0 ? args : [];
|
|
388
|
+
objectContext = objectContext !== null && objectContext !== void 0 ? objectContext : { originName: identifier };
|
|
382
389
|
if (typeof identifier !== 'string') {
|
|
390
|
+
objectContext.originName = identifier.name;
|
|
383
391
|
identifier = this.getIdentifier(identifier);
|
|
384
392
|
}
|
|
385
393
|
if (this.registry.hasObject(identifier)) {
|
|
@@ -396,7 +404,10 @@ class MidwayContainer {
|
|
|
396
404
|
}
|
|
397
405
|
async getAsync(identifier, args, objectContext) {
|
|
398
406
|
var _a;
|
|
407
|
+
args = args !== null && args !== void 0 ? args : [];
|
|
408
|
+
objectContext = objectContext !== null && objectContext !== void 0 ? objectContext : { originName: identifier };
|
|
399
409
|
if (typeof identifier !== 'string') {
|
|
410
|
+
objectContext.originName = identifier.name;
|
|
400
411
|
identifier = this.getIdentifier(identifier);
|
|
401
412
|
}
|
|
402
413
|
if (this.registry.hasObject(identifier)) {
|