@midwayjs/core 3.4.0-beta.1 → 3.4.0-beta.2
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/dist/common/webGenerator.d.ts +4 -3
- package/dist/common/webGenerator.js +22 -14
- package/dist/index.d.ts +1 -2
- package/dist/index.js +4 -3
- package/dist/service/frameworkService.js +7 -6
- package/dist/service/middlewareService.js +9 -5
- package/dist/{common/webRouterCollector.d.ts → service/webRouterService.d.ts} +60 -33
- package/dist/{common/webRouterCollector.js → service/webRouterService.js} +115 -29
- package/dist/setup.js +1 -0
- package/package.json +3 -3
- package/dist/common/triggerCollector.d.ts +0 -8
- package/dist/common/triggerCollector.js +0 -37
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { RouterInfo, IMidwayApplication } from '../index';
|
|
1
|
+
import { MidwayWebRouterService, RouterInfo, IMidwayApplication } from '../index';
|
|
2
2
|
export declare abstract class WebControllerGenerator<Router extends {
|
|
3
3
|
use: (...args: any[]) => void;
|
|
4
4
|
}> {
|
|
5
5
|
readonly app: IMidwayApplication;
|
|
6
|
-
|
|
6
|
+
readonly midwayWebRouterService: MidwayWebRouterService;
|
|
7
|
+
protected constructor(app: IMidwayApplication, midwayWebRouterService: MidwayWebRouterService);
|
|
7
8
|
/**
|
|
8
9
|
* wrap controller string to middleware function
|
|
9
10
|
* @param routeInfo
|
|
10
11
|
*/
|
|
11
12
|
generateKoaController(routeInfo: RouterInfo): (ctx: any, next: any) => Promise<void>;
|
|
12
|
-
loadMidwayController(
|
|
13
|
+
loadMidwayController(routerHandler?: (newRouter: Router) => void): Promise<void>;
|
|
13
14
|
abstract createRouter(routerOptions: any): Router;
|
|
14
15
|
abstract generateController(routeInfo: RouterInfo): any;
|
|
15
16
|
}
|
|
@@ -12,8 +12,9 @@ const index_1 = require("../index");
|
|
|
12
12
|
const util = require("util");
|
|
13
13
|
const debug = util.debuglog('midway:debug');
|
|
14
14
|
class WebControllerGenerator {
|
|
15
|
-
constructor(app) {
|
|
15
|
+
constructor(app, midwayWebRouterService) {
|
|
16
16
|
this.app = app;
|
|
17
|
+
this.midwayWebRouterService = midwayWebRouterService;
|
|
17
18
|
}
|
|
18
19
|
/**
|
|
19
20
|
* wrap controller string to middleware function
|
|
@@ -22,14 +23,24 @@ class WebControllerGenerator {
|
|
|
22
23
|
generateKoaController(routeInfo) {
|
|
23
24
|
return async (ctx, next) => {
|
|
24
25
|
const args = [ctx, next];
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
let result;
|
|
27
|
+
if (typeof routeInfo.method !== 'string') {
|
|
28
|
+
result = await routeInfo.method(ctx, next);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const controller = await ctx.requestContext.getAsync(routeInfo.id);
|
|
32
|
+
// eslint-disable-next-line prefer-spread
|
|
33
|
+
result = await controller[routeInfo.method].apply(controller, args);
|
|
30
34
|
}
|
|
31
|
-
if (
|
|
32
|
-
|
|
35
|
+
if (result !== undefined) {
|
|
36
|
+
if (result === null) {
|
|
37
|
+
// 这样设置可以绕过 koa 的 _explicitStatus 赋值机制
|
|
38
|
+
ctx.response._body = null;
|
|
39
|
+
ctx.response._midwayControllerNullBody = true;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
ctx.body = result;
|
|
43
|
+
}
|
|
33
44
|
}
|
|
34
45
|
// implement response decorator
|
|
35
46
|
if (Array.isArray(routeInfo.responseMetadata) &&
|
|
@@ -56,13 +67,10 @@ class WebControllerGenerator {
|
|
|
56
67
|
}
|
|
57
68
|
};
|
|
58
69
|
}
|
|
59
|
-
async loadMidwayController(
|
|
70
|
+
async loadMidwayController(routerHandler) {
|
|
60
71
|
var _a, _b;
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
});
|
|
64
|
-
const routerTable = await collector.getRouterTable();
|
|
65
|
-
const routerList = await collector.getRoutePriorityList();
|
|
72
|
+
const routerTable = await this.midwayWebRouterService.getRouterTable();
|
|
73
|
+
const routerList = await this.midwayWebRouterService.getRoutePriorityList();
|
|
66
74
|
const applicationContext = this.app.getApplicationContext();
|
|
67
75
|
const logger = this.app.getCoreLogger();
|
|
68
76
|
const middlewareService = applicationContext.get(index_1.MidwayMiddlewareService);
|
package/dist/index.d.ts
CHANGED
|
@@ -8,8 +8,6 @@ export { safelyGet, safeRequire, delegateTargetPrototypeMethod, delegateTargetMe
|
|
|
8
8
|
export { extend } from './util/extend';
|
|
9
9
|
export * from './util/pathFileUtil';
|
|
10
10
|
export * from './util/webRouterParam';
|
|
11
|
-
export * from './common/webRouterCollector';
|
|
12
|
-
export * from './common/triggerCollector';
|
|
13
11
|
export { createConfiguration } from './functional/configuration';
|
|
14
12
|
export { MidwayConfigService } from './service/configService';
|
|
15
13
|
export { MidwayEnvironmentService } from './service/environmentService';
|
|
@@ -21,6 +19,7 @@ export { MidwayLifeCycleService } from './service/lifeCycleService';
|
|
|
21
19
|
export { MidwayMiddlewareService } from './service/middlewareService';
|
|
22
20
|
export { MidwayDecoratorService } from './service/decoratorService';
|
|
23
21
|
export { MidwayMockService } from './service/mockService';
|
|
22
|
+
export { RouterInfo, DynamicRouterInfo, RouterPriority, RouterCollectorOptions, MidwayWebRouterService, WebRouterCollector, } from './service/webRouterService';
|
|
24
23
|
export * from './service/pipelineService';
|
|
25
24
|
export * from './util/contextUtil';
|
|
26
25
|
export * from './common/serviceFactory';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.MidwayFrameworkType = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.createConfiguration = exports.extend = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
17
|
+
exports.MidwayFrameworkType = exports.WebRouterCollector = exports.MidwayWebRouterService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.createConfiguration = exports.extend = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.safeRequire = exports.safelyGet = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
18
18
|
__exportStar(require("./interface"), exports);
|
|
19
19
|
__exportStar(require("./context/container"), exports);
|
|
20
20
|
var requestContainer_1 = require("./context/requestContainer");
|
|
@@ -39,8 +39,6 @@ var extend_1 = require("./util/extend");
|
|
|
39
39
|
Object.defineProperty(exports, "extend", { enumerable: true, get: function () { return extend_1.extend; } });
|
|
40
40
|
__exportStar(require("./util/pathFileUtil"), exports);
|
|
41
41
|
__exportStar(require("./util/webRouterParam"), exports);
|
|
42
|
-
__exportStar(require("./common/webRouterCollector"), exports);
|
|
43
|
-
__exportStar(require("./common/triggerCollector"), exports);
|
|
44
42
|
var configuration_1 = require("./functional/configuration");
|
|
45
43
|
Object.defineProperty(exports, "createConfiguration", { enumerable: true, get: function () { return configuration_1.createConfiguration; } });
|
|
46
44
|
var configService_1 = require("./service/configService");
|
|
@@ -63,6 +61,9 @@ var decoratorService_1 = require("./service/decoratorService");
|
|
|
63
61
|
Object.defineProperty(exports, "MidwayDecoratorService", { enumerable: true, get: function () { return decoratorService_1.MidwayDecoratorService; } });
|
|
64
62
|
var mockService_1 = require("./service/mockService");
|
|
65
63
|
Object.defineProperty(exports, "MidwayMockService", { enumerable: true, get: function () { return mockService_1.MidwayMockService; } });
|
|
64
|
+
var webRouterService_1 = require("./service/webRouterService");
|
|
65
|
+
Object.defineProperty(exports, "MidwayWebRouterService", { enumerable: true, get: function () { return webRouterService_1.MidwayWebRouterService; } });
|
|
66
|
+
Object.defineProperty(exports, "WebRouterCollector", { enumerable: true, get: function () { return webRouterService_1.WebRouterCollector; } });
|
|
66
67
|
__exportStar(require("./service/pipelineService"), exports);
|
|
67
68
|
__exportStar(require("./util/contextUtil"), exports);
|
|
68
69
|
__exportStar(require("./common/serviceFactory"), exports);
|
|
@@ -50,11 +50,11 @@ let MidwayFrameworkService = class MidwayFrameworkService {
|
|
|
50
50
|
return new pipelineService_1.MidwayPipelineService((_b = (_a = instance[interface_1.REQUEST_OBJ_CTX_KEY]) === null || _a === void 0 ? void 0 : _a.requestContext) !== null && _b !== void 0 ? _b : this.applicationContext, meta.valves);
|
|
51
51
|
});
|
|
52
52
|
// register @App decorator handler
|
|
53
|
-
this.decoratorService.registerPropertyHandler(decorator_1.APPLICATION_KEY, (propertyName,
|
|
54
|
-
if (
|
|
55
|
-
const framework = this.applicationManager.getApplication(
|
|
53
|
+
this.decoratorService.registerPropertyHandler(decorator_1.APPLICATION_KEY, (propertyName, meta) => {
|
|
54
|
+
if (meta.type) {
|
|
55
|
+
const framework = this.applicationManager.getApplication(meta.type);
|
|
56
56
|
if (!framework) {
|
|
57
|
-
throw new error_1.MidwayCommonError(`Framework ${
|
|
57
|
+
throw new error_1.MidwayCommonError(`Framework ${meta.type} not Found`);
|
|
58
58
|
}
|
|
59
59
|
return framework;
|
|
60
60
|
}
|
|
@@ -62,8 +62,9 @@ let MidwayFrameworkService = class MidwayFrameworkService {
|
|
|
62
62
|
return this.getMainApp();
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
|
-
this.decoratorService.registerPropertyHandler(decorator_1.PLUGIN_KEY, (
|
|
66
|
-
|
|
65
|
+
this.decoratorService.registerPropertyHandler(decorator_1.PLUGIN_KEY, (propertyName, meta) => {
|
|
66
|
+
var _a;
|
|
67
|
+
return this.getMainApp()[(_a = meta.identifier) !== null && _a !== void 0 ? _a : propertyName];
|
|
67
68
|
});
|
|
68
69
|
let frameworks = (0, decorator_1.listModule)(decorator_1.FRAMEWORK_KEY);
|
|
69
70
|
// filter proto
|
|
@@ -88,13 +88,17 @@ let MidwayMiddlewareService = class MidwayMiddlewareService {
|
|
|
88
88
|
return Promise.resolve(fn(context, dispatch.bind(null, i + 1), {
|
|
89
89
|
index,
|
|
90
90
|
})).then(result => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
/**
|
|
92
|
+
* 1、return 和 ctx.body,return 的优先级更高
|
|
93
|
+
* 2、如果 result 有值(非 undefined),则不管什么情况,都会覆盖当前 body,注意,这里有可能赋值 null,导致 status 为 204,会在中间件处进行修正
|
|
94
|
+
* 3、如果 result 没值,且 ctx.body 已经赋值,则向 result 赋值
|
|
95
|
+
*/
|
|
96
|
+
if (result !== undefined) {
|
|
96
97
|
context['body'] = result;
|
|
97
98
|
}
|
|
99
|
+
else if (context['body'] !== undefined) {
|
|
100
|
+
result = context['body'];
|
|
101
|
+
}
|
|
98
102
|
return result;
|
|
99
103
|
});
|
|
100
104
|
}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { ControllerOption } from '@midwayjs/decorator';
|
|
1
2
|
export interface RouterInfo {
|
|
2
3
|
/**
|
|
3
4
|
* uuid
|
|
4
5
|
*/
|
|
5
|
-
id
|
|
6
|
+
id?: string;
|
|
6
7
|
/**
|
|
7
8
|
* router prefix from controller
|
|
8
9
|
*/
|
|
9
|
-
prefix
|
|
10
|
+
prefix?: string;
|
|
10
11
|
/**
|
|
11
12
|
* router alias name
|
|
12
13
|
*/
|
|
13
|
-
routerName
|
|
14
|
+
routerName?: string;
|
|
14
15
|
/**
|
|
15
16
|
* router path, without prefix
|
|
16
17
|
*/
|
|
@@ -22,40 +23,40 @@ export interface RouterInfo {
|
|
|
22
23
|
/**
|
|
23
24
|
* invoke function method
|
|
24
25
|
*/
|
|
25
|
-
method: string;
|
|
26
|
+
method: string | ((...args: any[]) => void);
|
|
26
27
|
/**
|
|
27
28
|
* router description
|
|
28
29
|
*/
|
|
29
|
-
description
|
|
30
|
-
summary
|
|
30
|
+
description?: string;
|
|
31
|
+
summary?: string;
|
|
31
32
|
/**
|
|
32
33
|
* router handler function key,for IoC container load
|
|
33
34
|
*/
|
|
34
|
-
handlerName
|
|
35
|
+
handlerName?: string;
|
|
35
36
|
/**
|
|
36
37
|
* serverless func load key
|
|
37
38
|
*/
|
|
38
|
-
funcHandlerName
|
|
39
|
+
funcHandlerName?: string;
|
|
39
40
|
/**
|
|
40
41
|
* controller provideId
|
|
41
42
|
*/
|
|
42
|
-
controllerId
|
|
43
|
+
controllerId?: string;
|
|
43
44
|
/**
|
|
44
45
|
* router middleware
|
|
45
46
|
*/
|
|
46
|
-
middleware
|
|
47
|
+
middleware?: any[];
|
|
47
48
|
/**
|
|
48
49
|
* controller middleware in this router
|
|
49
50
|
*/
|
|
50
|
-
controllerMiddleware
|
|
51
|
+
controllerMiddleware?: any[];
|
|
51
52
|
/**
|
|
52
53
|
* request args metadata
|
|
53
54
|
*/
|
|
54
|
-
requestMetadata
|
|
55
|
+
requestMetadata?: any[];
|
|
55
56
|
/**
|
|
56
57
|
* response data metadata
|
|
57
58
|
*/
|
|
58
|
-
responseMetadata
|
|
59
|
+
responseMetadata?: any[];
|
|
59
60
|
/**
|
|
60
61
|
* serverless function name
|
|
61
62
|
*/
|
|
@@ -73,6 +74,7 @@ export interface RouterInfo {
|
|
|
73
74
|
*/
|
|
74
75
|
functionMetadata?: any;
|
|
75
76
|
}
|
|
77
|
+
export declare type DynamicRouterInfo = Omit<RouterInfo, 'id' | 'url' | 'prefix' | 'method' | 'controllerId' | 'controllerMiddleware' | 'responseMetadata'>;
|
|
76
78
|
export interface RouterPriority {
|
|
77
79
|
prefix: string;
|
|
78
80
|
priority: number;
|
|
@@ -88,15 +90,27 @@ export interface RouterCollectorOptions {
|
|
|
88
90
|
includeFunctionRouter?: boolean;
|
|
89
91
|
globalPrefix?: string;
|
|
90
92
|
}
|
|
91
|
-
export declare class
|
|
92
|
-
|
|
93
|
+
export declare class MidwayWebRouterService {
|
|
94
|
+
readonly options: RouterCollectorOptions;
|
|
93
95
|
private isReady;
|
|
94
96
|
protected routes: Map<string, RouterInfo[]>;
|
|
95
97
|
private routesPriority;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
constructor(options?: RouterCollectorOptions);
|
|
99
|
+
private analyze;
|
|
100
|
+
/**
|
|
101
|
+
* dynamically add a controller
|
|
102
|
+
* @param controllerClz
|
|
103
|
+
* @param controllerOption
|
|
104
|
+
* @param functionMeta
|
|
105
|
+
*/
|
|
106
|
+
addController(controllerClz: any, controllerOption: ControllerOption, functionMeta?: boolean): void;
|
|
107
|
+
/**
|
|
108
|
+
* dynamically add a route to root prefix
|
|
109
|
+
* @param routerPath
|
|
110
|
+
* @param routerFunction
|
|
111
|
+
* @param routerInfoOption
|
|
112
|
+
*/
|
|
113
|
+
addRouter(routerPath: string | RegExp, routerFunction: (...args: any[]) => void, routerInfoOption: DynamicRouterInfo): void;
|
|
100
114
|
protected collectFunctionRoute(module: any, functionMeta?: boolean): void;
|
|
101
115
|
sortRouter(urlMatchList: RouterInfo[]): {
|
|
102
116
|
_pureRouter: string;
|
|
@@ -107,15 +121,15 @@ export declare class WebRouterCollector {
|
|
|
107
121
|
/**
|
|
108
122
|
* uuid
|
|
109
123
|
*/
|
|
110
|
-
id
|
|
124
|
+
id?: string;
|
|
111
125
|
/**
|
|
112
126
|
* router prefix from controller
|
|
113
127
|
*/
|
|
114
|
-
prefix
|
|
128
|
+
prefix?: string;
|
|
115
129
|
/**
|
|
116
130
|
* router alias name
|
|
117
131
|
*/
|
|
118
|
-
routerName
|
|
132
|
+
routerName?: string;
|
|
119
133
|
/**
|
|
120
134
|
* router path, without prefix
|
|
121
135
|
*/
|
|
@@ -127,40 +141,40 @@ export declare class WebRouterCollector {
|
|
|
127
141
|
/**
|
|
128
142
|
* invoke function method
|
|
129
143
|
*/
|
|
130
|
-
method: string;
|
|
144
|
+
method: string | ((...args: any[]) => void);
|
|
131
145
|
/**
|
|
132
146
|
* router description
|
|
133
147
|
*/
|
|
134
|
-
description
|
|
135
|
-
summary
|
|
148
|
+
description?: string;
|
|
149
|
+
summary?: string;
|
|
136
150
|
/**
|
|
137
151
|
* router handler function key,for IoC container load
|
|
138
152
|
*/
|
|
139
|
-
handlerName
|
|
153
|
+
handlerName?: string;
|
|
140
154
|
/**
|
|
141
155
|
* serverless func load key
|
|
142
156
|
*/
|
|
143
|
-
funcHandlerName
|
|
157
|
+
funcHandlerName?: string;
|
|
144
158
|
/**
|
|
145
159
|
* controller provideId
|
|
146
160
|
*/
|
|
147
|
-
controllerId
|
|
161
|
+
controllerId?: string;
|
|
148
162
|
/**
|
|
149
163
|
* router middleware
|
|
150
164
|
*/
|
|
151
|
-
middleware
|
|
165
|
+
middleware?: any[];
|
|
152
166
|
/**
|
|
153
167
|
* controller middleware in this router
|
|
154
168
|
*/
|
|
155
|
-
controllerMiddleware
|
|
169
|
+
controllerMiddleware?: any[];
|
|
156
170
|
/**
|
|
157
171
|
* request args metadata
|
|
158
172
|
*/
|
|
159
|
-
requestMetadata
|
|
173
|
+
requestMetadata?: any[];
|
|
160
174
|
/**
|
|
161
175
|
* response data metadata
|
|
162
176
|
*/
|
|
163
|
-
responseMetadata
|
|
177
|
+
responseMetadata?: any[];
|
|
164
178
|
/**
|
|
165
179
|
* serverless function name
|
|
166
180
|
*/
|
|
@@ -183,4 +197,17 @@ export declare class WebRouterCollector {
|
|
|
183
197
|
getFlattenRouterTable(): Promise<RouterInfo[]>;
|
|
184
198
|
private checkDuplicateAndPush;
|
|
185
199
|
}
|
|
186
|
-
|
|
200
|
+
/**
|
|
201
|
+
* @deprecated use built-in MidwayWebRouterService first
|
|
202
|
+
*/
|
|
203
|
+
export declare class WebRouterCollector {
|
|
204
|
+
private baseDir;
|
|
205
|
+
private options;
|
|
206
|
+
private proxy;
|
|
207
|
+
constructor(baseDir?: string, options?: RouterCollectorOptions);
|
|
208
|
+
protected init(): Promise<void>;
|
|
209
|
+
getRoutePriorityList(): Promise<RouterPriority[]>;
|
|
210
|
+
getRouterTable(): Promise<Map<string, RouterInfo[]>>;
|
|
211
|
+
getFlattenRouterTable(): Promise<RouterInfo[]>;
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=webRouterService.d.ts.map
|
|
@@ -1,37 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebRouterCollector = void 0;
|
|
12
|
+
exports.WebRouterCollector = exports.MidwayWebRouterService = void 0;
|
|
4
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
5
14
|
const util_1 = require("../util");
|
|
6
|
-
const container_1 = require("../context/container");
|
|
7
|
-
const fileDetector_1 = require("./fileDetector");
|
|
8
|
-
const util = require("util");
|
|
9
15
|
const error_1 = require("../error");
|
|
16
|
+
const util = require("util");
|
|
17
|
+
const contextUtil_1 = require("../util/contextUtil");
|
|
18
|
+
const container_1 = require("../context/container");
|
|
19
|
+
const fileDetector_1 = require("../common/fileDetector");
|
|
10
20
|
const debug = util.debuglog('midway:debug');
|
|
11
|
-
class
|
|
12
|
-
constructor(
|
|
21
|
+
let MidwayWebRouterService = class MidwayWebRouterService {
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.options = options;
|
|
13
24
|
this.isReady = false;
|
|
14
25
|
this.routes = new Map();
|
|
15
26
|
this.routesPriority = [];
|
|
16
|
-
this.baseDir = baseDir;
|
|
17
|
-
this.options = options;
|
|
18
27
|
}
|
|
19
28
|
async analyze() {
|
|
20
|
-
if (this.baseDir) {
|
|
21
|
-
const container = new container_1.MidwayContainer();
|
|
22
|
-
container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
|
|
23
|
-
loadDir: this.baseDir,
|
|
24
|
-
}));
|
|
25
|
-
await container.ready();
|
|
26
|
-
}
|
|
27
29
|
const controllerModules = (0, decorator_1.listModule)(decorator_1.CONTROLLER_KEY);
|
|
28
30
|
for (const module of controllerModules) {
|
|
29
|
-
|
|
31
|
+
const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, module);
|
|
32
|
+
this.addController(module, controllerOption, this.options.includeFunctionRouter);
|
|
30
33
|
}
|
|
31
34
|
if (this.options.includeFunctionRouter) {
|
|
32
35
|
const fnModules = (0, decorator_1.listModule)(decorator_1.FUNC_KEY);
|
|
33
36
|
for (const module of fnModules) {
|
|
34
|
-
this.collectFunctionRoute(module);
|
|
37
|
+
this.collectFunctionRoute(module, this.options.includeFunctionRouter);
|
|
35
38
|
}
|
|
36
39
|
}
|
|
37
40
|
// filter empty prefix
|
|
@@ -54,13 +57,37 @@ class WebRouterCollector {
|
|
|
54
57
|
this.routesPriority = this.routesPriority.sort((routeA, routeB) => {
|
|
55
58
|
return routeB.prefix.length - routeA.prefix.length;
|
|
56
59
|
});
|
|
60
|
+
// format function router meta
|
|
61
|
+
if (this.options.includeFunctionRouter) {
|
|
62
|
+
// requestMethod all transform to other method
|
|
63
|
+
for (const routerInfo of this.routes.values()) {
|
|
64
|
+
for (const info of routerInfo) {
|
|
65
|
+
if (info.requestMethod === 'all') {
|
|
66
|
+
info.functionTriggerMetadata.method = [
|
|
67
|
+
'get',
|
|
68
|
+
'post',
|
|
69
|
+
'put',
|
|
70
|
+
'delete',
|
|
71
|
+
'head',
|
|
72
|
+
'patch',
|
|
73
|
+
'options',
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
57
79
|
}
|
|
58
|
-
|
|
80
|
+
/**
|
|
81
|
+
* dynamically add a controller
|
|
82
|
+
* @param controllerClz
|
|
83
|
+
* @param controllerOption
|
|
84
|
+
* @param functionMeta
|
|
85
|
+
*/
|
|
86
|
+
addController(controllerClz, controllerOption, functionMeta = false) {
|
|
59
87
|
var _a;
|
|
60
|
-
const controllerId = (0, decorator_1.getProviderName)(
|
|
88
|
+
const controllerId = (0, decorator_1.getProviderName)(controllerClz);
|
|
61
89
|
debug(`[core]: Found Controller ${controllerId}.`);
|
|
62
|
-
const id = (0, decorator_1.getProviderUUId)(
|
|
63
|
-
const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, module);
|
|
90
|
+
const id = (0, decorator_1.getProviderUUId)(controllerClz);
|
|
64
91
|
let priority;
|
|
65
92
|
// implement middleware in controller
|
|
66
93
|
const middleware = controllerOption.routerOptions.middleware;
|
|
@@ -83,7 +110,7 @@ class WebRouterCollector {
|
|
|
83
110
|
middleware,
|
|
84
111
|
routerOptions: controllerOption.routerOptions,
|
|
85
112
|
controllerId,
|
|
86
|
-
routerModule:
|
|
113
|
+
routerModule: controllerClz,
|
|
87
114
|
});
|
|
88
115
|
}
|
|
89
116
|
// set ignorePrefix
|
|
@@ -95,14 +122,14 @@ class WebRouterCollector {
|
|
|
95
122
|
middleware,
|
|
96
123
|
routerOptions: controllerOption.routerOptions,
|
|
97
124
|
controllerId,
|
|
98
|
-
routerModule:
|
|
125
|
+
routerModule: controllerClz,
|
|
99
126
|
});
|
|
100
127
|
}
|
|
101
|
-
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.WEB_ROUTER_KEY,
|
|
128
|
+
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.WEB_ROUTER_KEY, controllerClz);
|
|
102
129
|
if (webRouterInfo && typeof webRouterInfo[Symbol.iterator] === 'function') {
|
|
103
130
|
for (const webRouter of webRouterInfo) {
|
|
104
|
-
const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY,
|
|
105
|
-
const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY,
|
|
131
|
+
const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, controllerClz, webRouter.method) || [];
|
|
132
|
+
const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, controllerClz, webRouter.method) || [];
|
|
106
133
|
const data = {
|
|
107
134
|
id,
|
|
108
135
|
prefix: webRouter.ignoreGlobalPrefix ? ignorePrefix : prefix,
|
|
@@ -136,6 +163,18 @@ class WebRouterCollector {
|
|
|
136
163
|
}
|
|
137
164
|
}
|
|
138
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* dynamically add a route to root prefix
|
|
168
|
+
* @param routerPath
|
|
169
|
+
* @param routerFunction
|
|
170
|
+
* @param routerInfoOption
|
|
171
|
+
*/
|
|
172
|
+
addRouter(routerPath, routerFunction, routerInfoOption) {
|
|
173
|
+
this.checkDuplicateAndPush('/', Object.assign(routerInfoOption, {
|
|
174
|
+
method: routerFunction,
|
|
175
|
+
url: routerPath,
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
139
178
|
collectFunctionRoute(module, functionMeta = false) {
|
|
140
179
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
141
180
|
// serverlessTrigger metadata
|
|
@@ -323,9 +362,56 @@ class WebRouterCollector {
|
|
|
323
362
|
}
|
|
324
363
|
prefixList.push(routerInfo);
|
|
325
364
|
}
|
|
326
|
-
}
|
|
327
|
-
|
|
365
|
+
};
|
|
366
|
+
MidwayWebRouterService = __decorate([
|
|
367
|
+
(0, decorator_1.Provide)(),
|
|
368
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
|
|
369
|
+
__metadata("design:paramtypes", [Object])
|
|
370
|
+
], MidwayWebRouterService);
|
|
371
|
+
exports.MidwayWebRouterService = MidwayWebRouterService;
|
|
328
372
|
function createFunctionName(target, functionName) {
|
|
329
373
|
return (0, decorator_1.getProviderName)(target).replace(/[:#]/g, '-') + '-' + functionName;
|
|
330
374
|
}
|
|
331
|
-
|
|
375
|
+
/**
|
|
376
|
+
* @deprecated use built-in MidwayWebRouterService first
|
|
377
|
+
*/
|
|
378
|
+
class WebRouterCollector {
|
|
379
|
+
constructor(baseDir = '', options = {}) {
|
|
380
|
+
this.baseDir = baseDir;
|
|
381
|
+
this.options = options;
|
|
382
|
+
}
|
|
383
|
+
async init() {
|
|
384
|
+
if (!this.proxy) {
|
|
385
|
+
if (this.baseDir) {
|
|
386
|
+
const container = new container_1.MidwayContainer();
|
|
387
|
+
(0, decorator_1.bindContainer)(container);
|
|
388
|
+
container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
|
|
389
|
+
loadDir: this.baseDir,
|
|
390
|
+
}));
|
|
391
|
+
await container.ready();
|
|
392
|
+
}
|
|
393
|
+
if ((0, contextUtil_1.getCurrentMainFramework)()) {
|
|
394
|
+
this.proxy = await (0, contextUtil_1.getCurrentMainFramework)()
|
|
395
|
+
.getApplicationContext()
|
|
396
|
+
.getAsync(MidwayWebRouterService, [this.options]);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
this.proxy = new MidwayWebRouterService(this.options);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
async getRoutePriorityList() {
|
|
404
|
+
await this.init();
|
|
405
|
+
return this.proxy.getRoutePriorityList();
|
|
406
|
+
}
|
|
407
|
+
async getRouterTable() {
|
|
408
|
+
await this.init();
|
|
409
|
+
return this.proxy.getRouterTable();
|
|
410
|
+
}
|
|
411
|
+
async getFlattenRouterTable() {
|
|
412
|
+
await this.init();
|
|
413
|
+
return this.proxy.getFlattenRouterTable();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
exports.WebRouterCollector = WebRouterCollector;
|
|
417
|
+
//# sourceMappingURL=webRouterService.js.map
|
package/dist/setup.js
CHANGED
|
@@ -95,6 +95,7 @@ function prepareGlobalApplicationContext(globalOptions) {
|
|
|
95
95
|
applicationContext.bindClass(_1.MidwayMiddlewareService);
|
|
96
96
|
applicationContext.bindClass(_1.MidwayLifeCycleService);
|
|
97
97
|
applicationContext.bindClass(_1.MidwayMockService);
|
|
98
|
+
applicationContext.bindClass(_1.MidwayWebRouterService);
|
|
98
99
|
// bind preload module
|
|
99
100
|
if (globalOptions.preloadModules && globalOptions.preloadModules.length) {
|
|
100
101
|
for (const preloadModule of globalOptions.preloadModules) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/core",
|
|
3
|
-
"version": "3.4.0-beta.
|
|
3
|
+
"version": "3.4.0-beta.2",
|
|
4
4
|
"description": "midway core",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@midwayjs/decorator": "^3.4.0-beta.
|
|
24
|
+
"@midwayjs/decorator": "^3.4.0-beta.2",
|
|
25
25
|
"koa": "2.13.4",
|
|
26
26
|
"midway-test-component": "*",
|
|
27
27
|
"mm": "3.2.0",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">=12"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "a61721d3946b30fd4cac183265edcd99b31ac72b"
|
|
49
49
|
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { RouterInfo, WebRouterCollector } from './webRouterCollector';
|
|
2
|
-
export declare class ServerlessTriggerCollector extends WebRouterCollector {
|
|
3
|
-
protected analyze(): Promise<void>;
|
|
4
|
-
protected collectRoute(module: any): void;
|
|
5
|
-
protected collectFunctionRoute(module: any): void;
|
|
6
|
-
getFunctionList(): Promise<RouterInfo[]>;
|
|
7
|
-
}
|
|
8
|
-
//# sourceMappingURL=triggerCollector.d.ts.map
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ServerlessTriggerCollector = void 0;
|
|
4
|
-
const webRouterCollector_1 = require("./webRouterCollector");
|
|
5
|
-
class ServerlessTriggerCollector extends webRouterCollector_1.WebRouterCollector {
|
|
6
|
-
async analyze() {
|
|
7
|
-
this.options.includeFunctionRouter = true;
|
|
8
|
-
await super.analyze();
|
|
9
|
-
// requestMethod all transform to other method
|
|
10
|
-
for (const routerInfo of this.routes.values()) {
|
|
11
|
-
for (const info of routerInfo) {
|
|
12
|
-
if (info.requestMethod === 'all') {
|
|
13
|
-
info.functionTriggerMetadata.method = [
|
|
14
|
-
'get',
|
|
15
|
-
'post',
|
|
16
|
-
'put',
|
|
17
|
-
'delete',
|
|
18
|
-
'head',
|
|
19
|
-
'patch',
|
|
20
|
-
'options',
|
|
21
|
-
];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
collectRoute(module) {
|
|
27
|
-
super.collectRoute(module, true);
|
|
28
|
-
}
|
|
29
|
-
collectFunctionRoute(module) {
|
|
30
|
-
super.collectFunctionRoute(module, true);
|
|
31
|
-
}
|
|
32
|
-
async getFunctionList() {
|
|
33
|
-
return this.getFlattenRouterTable();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.ServerlessTriggerCollector = ServerlessTriggerCollector;
|
|
37
|
-
//# sourceMappingURL=triggerCollector.js.map
|