@midwayjs/core 3.4.0-beta.3 → 3.4.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common/dataSourceManager.d.ts +8 -1
- package/dist/common/dataSourceManager.js +61 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.js +6 -3
- package/dist/service/configService.js +10 -4
- package/dist/service/slsFunctionService.d.ts +25 -0
- package/dist/service/slsFunctionService.js +240 -0
- package/dist/service/webRouterService.d.ts +5 -17
- package/dist/service/webRouterService.js +7 -161
- package/dist/setup.js +2 -0
- package/package.json +3 -3
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export declare abstract class DataSourceManager<T> {
|
|
2
2
|
protected dataSource: Map<string, T>;
|
|
3
3
|
protected options: {};
|
|
4
|
-
protected
|
|
4
|
+
protected modelMapping: WeakMap<object, any>;
|
|
5
|
+
protected initDataSource(options: any, appDir: string): Promise<void>;
|
|
5
6
|
/**
|
|
6
7
|
* get a data source instance
|
|
7
8
|
* @param dataSourceName
|
|
@@ -19,10 +20,16 @@ export declare abstract class DataSourceManager<T> {
|
|
|
19
20
|
*/
|
|
20
21
|
isConnected(dataSourceName: string): Promise<boolean>;
|
|
21
22
|
createInstance(config: any, clientName: any): Promise<T | void>;
|
|
23
|
+
/**
|
|
24
|
+
* get data source name by model or repository
|
|
25
|
+
* @param modelOrRepository
|
|
26
|
+
*/
|
|
27
|
+
getDataSourceNameByModel(modelOrRepository: any): string | undefined;
|
|
22
28
|
abstract getName(): string;
|
|
23
29
|
protected abstract createDataSource(config: any, dataSourceName: string): Promise<T | void> | (T | void);
|
|
24
30
|
protected abstract checkConnected(dataSource: T): Promise<boolean>;
|
|
25
31
|
protected destroyDataSource(dataSource: T): Promise<void>;
|
|
26
32
|
stop(): Promise<void>;
|
|
27
33
|
}
|
|
34
|
+
export declare function globModels(globString: string, appDir: string): any[];
|
|
28
35
|
//# sourceMappingURL=dataSourceManager.d.ts.map
|
|
@@ -1,22 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DataSourceManager = void 0;
|
|
3
|
+
exports.globModels = exports.DataSourceManager = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* 数据源管理器实现
|
|
6
6
|
*/
|
|
7
7
|
const extend_1 = require("../util/extend");
|
|
8
8
|
const error_1 = require("../error");
|
|
9
|
+
const glob_1 = require("@midwayjs/glob");
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
12
|
+
const DEFAULT_PATTERN = ['**/**.ts', '**/**.js'];
|
|
9
13
|
class DataSourceManager {
|
|
10
14
|
constructor() {
|
|
11
15
|
this.dataSource = new Map();
|
|
12
16
|
this.options = {};
|
|
17
|
+
this.modelMapping = new WeakMap();
|
|
13
18
|
}
|
|
14
|
-
async initDataSource(options
|
|
19
|
+
async initDataSource(options, appDir) {
|
|
15
20
|
this.options = options;
|
|
16
21
|
if (options.dataSource) {
|
|
17
22
|
for (const dataSourceName in options.dataSource) {
|
|
23
|
+
const dataSourceOptions = options.dataSource[dataSourceName];
|
|
24
|
+
if (dataSourceOptions['entities']) {
|
|
25
|
+
const entities = new Set();
|
|
26
|
+
// loop entities and glob files to model
|
|
27
|
+
for (const entity of dataSourceOptions['entities']) {
|
|
28
|
+
if (typeof entity === 'string') {
|
|
29
|
+
// string will be glob file
|
|
30
|
+
const models = globModels(entity, appDir);
|
|
31
|
+
for (const model of models) {
|
|
32
|
+
entities.add(model);
|
|
33
|
+
this.modelMapping.set(model, dataSourceName);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// model will be add to array
|
|
38
|
+
entities.add(entity);
|
|
39
|
+
this.modelMapping.set(entity, dataSourceName);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
dataSourceOptions['entities'] = Array.from(entities);
|
|
43
|
+
}
|
|
18
44
|
// create data source
|
|
19
|
-
await this.createInstance(
|
|
45
|
+
await this.createInstance(dataSourceOptions, dataSourceName);
|
|
20
46
|
}
|
|
21
47
|
}
|
|
22
48
|
else {
|
|
@@ -58,12 +84,44 @@ class DataSourceManager {
|
|
|
58
84
|
return client;
|
|
59
85
|
}
|
|
60
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* get data source name by model or repository
|
|
89
|
+
* @param modelOrRepository
|
|
90
|
+
*/
|
|
91
|
+
getDataSourceNameByModel(modelOrRepository) {
|
|
92
|
+
return this.modelMapping.get(modelOrRepository);
|
|
93
|
+
}
|
|
61
94
|
async destroyDataSource(dataSource) { }
|
|
62
95
|
async stop() {
|
|
63
96
|
for (const value of this.dataSource.values()) {
|
|
64
97
|
await this.destroyDataSource(value);
|
|
65
98
|
}
|
|
99
|
+
this.dataSource.clear();
|
|
66
100
|
}
|
|
67
101
|
}
|
|
68
102
|
exports.DataSourceManager = DataSourceManager;
|
|
103
|
+
function globModels(globString, appDir) {
|
|
104
|
+
const cwd = (0, path_1.join)(appDir, globString);
|
|
105
|
+
const models = [];
|
|
106
|
+
// string will be glob file
|
|
107
|
+
const files = (0, glob_1.run)(DEFAULT_PATTERN, {
|
|
108
|
+
cwd,
|
|
109
|
+
});
|
|
110
|
+
for (const file of files) {
|
|
111
|
+
const exports = require(file);
|
|
112
|
+
if (decorator_1.Types.isClass(exports)) {
|
|
113
|
+
models.push(exports);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
for (const m in exports) {
|
|
117
|
+
const module = exports[m];
|
|
118
|
+
if (decorator_1.Types.isClass(module)) {
|
|
119
|
+
models.push(module);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return models;
|
|
125
|
+
}
|
|
126
|
+
exports.globModels = globModels;
|
|
69
127
|
//# sourceMappingURL=dataSourceManager.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -19,11 +19,12 @@ export { MidwayLifeCycleService } from './service/lifeCycleService';
|
|
|
19
19
|
export { MidwayMiddlewareService } from './service/middlewareService';
|
|
20
20
|
export { MidwayDecoratorService } from './service/decoratorService';
|
|
21
21
|
export { MidwayMockService } from './service/mockService';
|
|
22
|
-
export { RouterInfo, DynamicRouterInfo, RouterPriority, RouterCollectorOptions, MidwayWebRouterService,
|
|
22
|
+
export { RouterInfo, DynamicRouterInfo, RouterPriority, RouterCollectorOptions, MidwayWebRouterService, } from './service/webRouterService';
|
|
23
|
+
export { MidwayServerlessFunctionService, WebRouterCollector, } from './service/slsFunctionService';
|
|
24
|
+
export { DataSourceManager } from './common/dataSourceManager';
|
|
23
25
|
export * from './service/pipelineService';
|
|
24
26
|
export * from './util/contextUtil';
|
|
25
27
|
export * from './common/serviceFactory';
|
|
26
|
-
export * from './common/dataSourceManager';
|
|
27
28
|
export * from './common/dataListener';
|
|
28
29
|
export * from './common/fileDetector';
|
|
29
30
|
export * from './common/webGenerator';
|
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.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;
|
|
17
|
+
exports.MidwayFrameworkType = exports.DataSourceManager = exports.WebRouterCollector = exports.MidwayServerlessFunctionService = 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");
|
|
@@ -63,11 +63,14 @@ var mockService_1 = require("./service/mockService");
|
|
|
63
63
|
Object.defineProperty(exports, "MidwayMockService", { enumerable: true, get: function () { return mockService_1.MidwayMockService; } });
|
|
64
64
|
var webRouterService_1 = require("./service/webRouterService");
|
|
65
65
|
Object.defineProperty(exports, "MidwayWebRouterService", { enumerable: true, get: function () { return webRouterService_1.MidwayWebRouterService; } });
|
|
66
|
-
|
|
66
|
+
var slsFunctionService_1 = require("./service/slsFunctionService");
|
|
67
|
+
Object.defineProperty(exports, "MidwayServerlessFunctionService", { enumerable: true, get: function () { return slsFunctionService_1.MidwayServerlessFunctionService; } });
|
|
68
|
+
Object.defineProperty(exports, "WebRouterCollector", { enumerable: true, get: function () { return slsFunctionService_1.WebRouterCollector; } });
|
|
69
|
+
var dataSourceManager_1 = require("./common/dataSourceManager");
|
|
70
|
+
Object.defineProperty(exports, "DataSourceManager", { enumerable: true, get: function () { return dataSourceManager_1.DataSourceManager; } });
|
|
67
71
|
__exportStar(require("./service/pipelineService"), exports);
|
|
68
72
|
__exportStar(require("./util/contextUtil"), exports);
|
|
69
73
|
__exportStar(require("./common/serviceFactory"), exports);
|
|
70
|
-
__exportStar(require("./common/dataSourceManager"), exports);
|
|
71
74
|
__exportStar(require("./common/dataListener"), exports);
|
|
72
75
|
__exportStar(require("./common/fileDetector"), exports);
|
|
73
76
|
__exportStar(require("./common/webGenerator"), exports);
|
|
@@ -175,11 +175,17 @@ let MidwayConfigService = class MidwayConfigService {
|
|
|
175
175
|
let exports = typeof configFilename === 'string'
|
|
176
176
|
? require(configFilename)
|
|
177
177
|
: configFilename;
|
|
178
|
-
if
|
|
179
|
-
|
|
180
|
-
|
|
178
|
+
// if es module
|
|
179
|
+
if (exports && exports.__esModule) {
|
|
180
|
+
if (exports && exports.default) {
|
|
181
|
+
if (Object.keys(exports).length > 1) {
|
|
182
|
+
throw new error_1.MidwayInvalidConfigError(`${configFilename} should not have both a default export and named export`);
|
|
183
|
+
}
|
|
184
|
+
exports = exports.default;
|
|
181
185
|
}
|
|
182
|
-
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
console.log(exports);
|
|
183
189
|
}
|
|
184
190
|
return exports;
|
|
185
191
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { FaaSMetadata } from '@midwayjs/decorator';
|
|
2
|
+
import { MidwayWebRouterService, RouterCollectorOptions, RouterInfo, RouterPriority } from './webRouterService';
|
|
3
|
+
export declare class MidwayServerlessFunctionService extends MidwayWebRouterService {
|
|
4
|
+
readonly options: RouterCollectorOptions;
|
|
5
|
+
constructor(options?: RouterCollectorOptions);
|
|
6
|
+
protected analyze(): Promise<void>;
|
|
7
|
+
protected analyzeFunction(): void;
|
|
8
|
+
protected collectFunctionRoute(module: any): void;
|
|
9
|
+
getFunctionList(): Promise<RouterInfo[]>;
|
|
10
|
+
addServerlessFunction(func: (...args: any[]) => Promise<any>, triggerOptions: FaaSMetadata.TriggerMetadata, functionOptions?: FaaSMetadata.ServerlessFunctionOptions): void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated use built-in MidwayWebRouterService first
|
|
14
|
+
*/
|
|
15
|
+
export declare class WebRouterCollector {
|
|
16
|
+
private baseDir;
|
|
17
|
+
private options;
|
|
18
|
+
private proxy;
|
|
19
|
+
constructor(baseDir?: string, options?: RouterCollectorOptions);
|
|
20
|
+
protected init(): Promise<void>;
|
|
21
|
+
getRoutePriorityList(): Promise<RouterPriority[]>;
|
|
22
|
+
getRouterTable(): Promise<Map<string, RouterInfo[]>>;
|
|
23
|
+
getFlattenRouterTable(): Promise<RouterInfo[]>;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=slsFunctionService.d.ts.map
|
|
@@ -0,0 +1,240 @@
|
|
|
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
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.WebRouterCollector = exports.MidwayServerlessFunctionService = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const webRouterService_1 = require("./webRouterService");
|
|
15
|
+
const container_1 = require("../context/container");
|
|
16
|
+
const fileDetector_1 = require("../common/fileDetector");
|
|
17
|
+
const contextUtil_1 = require("../util/contextUtil");
|
|
18
|
+
let MidwayServerlessFunctionService = class MidwayServerlessFunctionService extends webRouterService_1.MidwayWebRouterService {
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
super(Object.assign({}, options, {
|
|
21
|
+
includeFunctionRouter: true,
|
|
22
|
+
}));
|
|
23
|
+
this.options = options;
|
|
24
|
+
}
|
|
25
|
+
async analyze() {
|
|
26
|
+
this.analyzeController();
|
|
27
|
+
this.analyzeFunction();
|
|
28
|
+
this.sortPrefixAndRouter();
|
|
29
|
+
// requestMethod all transform to other method
|
|
30
|
+
for (const routerInfo of this.routes.values()) {
|
|
31
|
+
for (const info of routerInfo) {
|
|
32
|
+
if (info.requestMethod === 'all') {
|
|
33
|
+
info.functionTriggerMetadata.method = [
|
|
34
|
+
'get',
|
|
35
|
+
'post',
|
|
36
|
+
'put',
|
|
37
|
+
'delete',
|
|
38
|
+
'head',
|
|
39
|
+
'patch',
|
|
40
|
+
'options',
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
analyzeFunction() {
|
|
47
|
+
const fnModules = (0, decorator_1.listModule)(decorator_1.FUNC_KEY);
|
|
48
|
+
for (const module of fnModules) {
|
|
49
|
+
this.collectFunctionRoute(module);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
collectFunctionRoute(module) {
|
|
53
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
54
|
+
// serverlessTrigger metadata
|
|
55
|
+
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.FUNC_KEY, module);
|
|
56
|
+
const controllerId = (0, decorator_1.getProviderName)(module);
|
|
57
|
+
const id = (0, decorator_1.getProviderUUId)(module);
|
|
58
|
+
const prefix = '/';
|
|
59
|
+
if (!this.routes.has(prefix)) {
|
|
60
|
+
this.routes.set(prefix, []);
|
|
61
|
+
this.routesPriority.push({
|
|
62
|
+
prefix,
|
|
63
|
+
priority: -999,
|
|
64
|
+
middleware: [],
|
|
65
|
+
routerOptions: {},
|
|
66
|
+
controllerId,
|
|
67
|
+
routerModule: module,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
for (const webRouter of webRouterInfo) {
|
|
71
|
+
// 新的 @ServerlessTrigger 写法
|
|
72
|
+
if ((_a = webRouter['metadata']) === null || _a === void 0 ? void 0 : _a['path']) {
|
|
73
|
+
const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter['methodName']) || [];
|
|
74
|
+
const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter['methodName']) || [];
|
|
75
|
+
// 新 http/api gateway 函数
|
|
76
|
+
const data = {
|
|
77
|
+
id,
|
|
78
|
+
prefix,
|
|
79
|
+
routerName: '',
|
|
80
|
+
url: webRouter['metadata']['path'],
|
|
81
|
+
requestMethod: (_c = (_b = webRouter['metadata']) === null || _b === void 0 ? void 0 : _b['method']) !== null && _c !== void 0 ? _c : 'get',
|
|
82
|
+
method: webRouter['methodName'],
|
|
83
|
+
description: '',
|
|
84
|
+
summary: '',
|
|
85
|
+
handlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
86
|
+
funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
87
|
+
controllerId,
|
|
88
|
+
middleware: ((_d = webRouter['metadata']) === null || _d === void 0 ? void 0 : _d['middleware']) || [],
|
|
89
|
+
controllerMiddleware: [],
|
|
90
|
+
requestMetadata: routeArgsInfo,
|
|
91
|
+
responseMetadata: routerResponseData,
|
|
92
|
+
};
|
|
93
|
+
const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
|
|
94
|
+
const functionName = (_f = (_e = functionMeta['functionName']) !== null && _e !== void 0 ? _e : webRouter['functionName']) !== null && _f !== void 0 ? _f : createFunctionName(module, webRouter['methodName']);
|
|
95
|
+
data.functionName = functionName;
|
|
96
|
+
data.functionTriggerName = webRouter['type'];
|
|
97
|
+
data.functionTriggerMetadata = webRouter['metadata'];
|
|
98
|
+
data.functionMetadata = {
|
|
99
|
+
functionName,
|
|
100
|
+
...functionMeta,
|
|
101
|
+
};
|
|
102
|
+
this.checkDuplicateAndPush(prefix, data);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
|
|
106
|
+
const functionName = (_h = (_g = functionMeta['functionName']) !== null && _g !== void 0 ? _g : webRouter['functionName']) !== null && _h !== void 0 ? _h : createFunctionName(module, webRouter['methodName']);
|
|
107
|
+
// 其他类型的函数
|
|
108
|
+
this.checkDuplicateAndPush(prefix, {
|
|
109
|
+
id,
|
|
110
|
+
prefix,
|
|
111
|
+
routerName: '',
|
|
112
|
+
url: '',
|
|
113
|
+
requestMethod: '',
|
|
114
|
+
method: webRouter['methodName'],
|
|
115
|
+
description: '',
|
|
116
|
+
summary: '',
|
|
117
|
+
handlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
118
|
+
funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
119
|
+
controllerId,
|
|
120
|
+
middleware: ((_j = webRouter['metadata']) === null || _j === void 0 ? void 0 : _j['middleware']) || [],
|
|
121
|
+
controllerMiddleware: [],
|
|
122
|
+
requestMetadata: [],
|
|
123
|
+
responseMetadata: [],
|
|
124
|
+
functionName,
|
|
125
|
+
functionTriggerName: webRouter['type'],
|
|
126
|
+
functionTriggerMetadata: webRouter['metadata'],
|
|
127
|
+
functionMetadata: {
|
|
128
|
+
functionName,
|
|
129
|
+
...functionMeta,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async getFunctionList() {
|
|
136
|
+
return this.getFlattenRouterTable();
|
|
137
|
+
}
|
|
138
|
+
addServerlessFunction(func, triggerOptions, functionOptions = {}) {
|
|
139
|
+
var _a, _b;
|
|
140
|
+
const prefix = '/';
|
|
141
|
+
if (!this.routes.has(prefix)) {
|
|
142
|
+
this.routes.set(prefix, []);
|
|
143
|
+
this.routesPriority.push({
|
|
144
|
+
prefix,
|
|
145
|
+
priority: -999,
|
|
146
|
+
middleware: [],
|
|
147
|
+
routerOptions: {},
|
|
148
|
+
controllerId: undefined,
|
|
149
|
+
routerModule: module,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const functionName = (_a = triggerOptions.functionName) !== null && _a !== void 0 ? _a : functionOptions.functionName;
|
|
153
|
+
this.checkDuplicateAndPush(prefix, {
|
|
154
|
+
id: null,
|
|
155
|
+
method: func,
|
|
156
|
+
url: triggerOptions.metadata['path'] || '',
|
|
157
|
+
requestMethod: triggerOptions.metadata['method'] || '',
|
|
158
|
+
description: '',
|
|
159
|
+
summary: '',
|
|
160
|
+
handlerName: '',
|
|
161
|
+
funcHandlerName: triggerOptions.handlerName || functionOptions.handlerName,
|
|
162
|
+
controllerId: '',
|
|
163
|
+
middleware: ((_b = triggerOptions.metadata) === null || _b === void 0 ? void 0 : _b.middleware) || [],
|
|
164
|
+
controllerMiddleware: [],
|
|
165
|
+
requestMetadata: [],
|
|
166
|
+
responseMetadata: [],
|
|
167
|
+
functionName,
|
|
168
|
+
functionTriggerName: triggerOptions.metadata.name,
|
|
169
|
+
functionTriggerMetadata: triggerOptions.metadata,
|
|
170
|
+
functionMetadata: {
|
|
171
|
+
functionName,
|
|
172
|
+
...functionOptions,
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
MidwayServerlessFunctionService = __decorate([
|
|
178
|
+
(0, decorator_1.Provide)(),
|
|
179
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
|
|
180
|
+
__metadata("design:paramtypes", [Object])
|
|
181
|
+
], MidwayServerlessFunctionService);
|
|
182
|
+
exports.MidwayServerlessFunctionService = MidwayServerlessFunctionService;
|
|
183
|
+
function createFunctionName(target, functionName) {
|
|
184
|
+
return (0, decorator_1.getProviderName)(target).replace(/[:#]/g, '-') + '-' + functionName;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* @deprecated use built-in MidwayWebRouterService first
|
|
188
|
+
*/
|
|
189
|
+
class WebRouterCollector {
|
|
190
|
+
constructor(baseDir = '', options = {}) {
|
|
191
|
+
this.baseDir = baseDir;
|
|
192
|
+
this.options = options;
|
|
193
|
+
}
|
|
194
|
+
async init() {
|
|
195
|
+
if (!this.proxy) {
|
|
196
|
+
if (this.baseDir) {
|
|
197
|
+
const container = new container_1.MidwayContainer();
|
|
198
|
+
(0, decorator_1.bindContainer)(container);
|
|
199
|
+
container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
|
|
200
|
+
loadDir: this.baseDir,
|
|
201
|
+
}));
|
|
202
|
+
await container.ready();
|
|
203
|
+
}
|
|
204
|
+
if (this.options.includeFunctionRouter) {
|
|
205
|
+
if ((0, contextUtil_1.getCurrentMainFramework)()) {
|
|
206
|
+
this.proxy = await (0, contextUtil_1.getCurrentMainFramework)()
|
|
207
|
+
.getApplicationContext()
|
|
208
|
+
.getAsync(MidwayServerlessFunctionService, [this.options]);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
this.proxy = new MidwayServerlessFunctionService(this.options);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
if ((0, contextUtil_1.getCurrentMainFramework)()) {
|
|
216
|
+
this.proxy = await (0, contextUtil_1.getCurrentMainFramework)()
|
|
217
|
+
.getApplicationContext()
|
|
218
|
+
.getAsync(webRouterService_1.MidwayWebRouterService, [this.options]);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
this.proxy = new webRouterService_1.MidwayWebRouterService(this.options);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
async getRoutePriorityList() {
|
|
227
|
+
await this.init();
|
|
228
|
+
return this.proxy.getRoutePriorityList();
|
|
229
|
+
}
|
|
230
|
+
async getRouterTable() {
|
|
231
|
+
await this.init();
|
|
232
|
+
return this.proxy.getRouterTable();
|
|
233
|
+
}
|
|
234
|
+
async getFlattenRouterTable() {
|
|
235
|
+
await this.init();
|
|
236
|
+
return this.proxy.getFlattenRouterTable();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.WebRouterCollector = WebRouterCollector;
|
|
240
|
+
//# sourceMappingURL=slsFunctionService.js.map
|
|
@@ -94,9 +94,11 @@ export declare class MidwayWebRouterService {
|
|
|
94
94
|
readonly options: RouterCollectorOptions;
|
|
95
95
|
private isReady;
|
|
96
96
|
protected routes: Map<string, RouterInfo[]>;
|
|
97
|
-
|
|
97
|
+
protected routesPriority: RouterPriority[];
|
|
98
98
|
constructor(options?: RouterCollectorOptions);
|
|
99
|
-
|
|
99
|
+
protected analyze(): Promise<void>;
|
|
100
|
+
protected analyzeController(): void;
|
|
101
|
+
protected sortPrefixAndRouter(): void;
|
|
100
102
|
/**
|
|
101
103
|
* dynamically add a controller
|
|
102
104
|
* @param controllerClz
|
|
@@ -111,7 +113,6 @@ export declare class MidwayWebRouterService {
|
|
|
111
113
|
* @param routerInfoOption
|
|
112
114
|
*/
|
|
113
115
|
addRouter(routerPath: string | RegExp, routerFunction: (...args: any[]) => void, routerInfoOption: DynamicRouterInfo): void;
|
|
114
|
-
protected collectFunctionRoute(module: any, functionMeta?: boolean): void;
|
|
115
116
|
sortRouter(urlMatchList: RouterInfo[]): {
|
|
116
117
|
_pureRouter: string;
|
|
117
118
|
_level: number;
|
|
@@ -195,19 +196,6 @@ export declare class MidwayWebRouterService {
|
|
|
195
196
|
getRoutePriorityList(): Promise<RouterPriority[]>;
|
|
196
197
|
getRouterTable(): Promise<Map<string, RouterInfo[]>>;
|
|
197
198
|
getFlattenRouterTable(): Promise<RouterInfo[]>;
|
|
198
|
-
|
|
199
|
-
}
|
|
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[]>;
|
|
199
|
+
protected checkDuplicateAndPush(prefix: any, routerInfo: RouterInfo): void;
|
|
212
200
|
}
|
|
213
201
|
//# sourceMappingURL=webRouterService.d.ts.map
|
|
@@ -9,14 +9,11 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.MidwayWebRouterService = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const util_1 = require("../util");
|
|
15
15
|
const error_1 = require("../error");
|
|
16
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");
|
|
20
17
|
const debug = util.debuglog('midway:debug');
|
|
21
18
|
let MidwayWebRouterService = class MidwayWebRouterService {
|
|
22
19
|
constructor(options = {}) {
|
|
@@ -26,17 +23,17 @@ let MidwayWebRouterService = class MidwayWebRouterService {
|
|
|
26
23
|
this.routesPriority = [];
|
|
27
24
|
}
|
|
28
25
|
async analyze() {
|
|
26
|
+
this.analyzeController();
|
|
27
|
+
this.sortPrefixAndRouter();
|
|
28
|
+
}
|
|
29
|
+
analyzeController() {
|
|
29
30
|
const controllerModules = (0, decorator_1.listModule)(decorator_1.CONTROLLER_KEY);
|
|
30
31
|
for (const module of controllerModules) {
|
|
31
32
|
const controllerOption = (0, decorator_1.getClassMetadata)(decorator_1.CONTROLLER_KEY, module);
|
|
32
33
|
this.addController(module, controllerOption, this.options.includeFunctionRouter);
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
for (const module of fnModules) {
|
|
37
|
-
this.collectFunctionRoute(module, this.options.includeFunctionRouter);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
35
|
+
}
|
|
36
|
+
sortPrefixAndRouter() {
|
|
40
37
|
// filter empty prefix
|
|
41
38
|
this.routesPriority = this.routesPriority.filter(item => {
|
|
42
39
|
const prefixList = this.routes.get(item.prefix);
|
|
@@ -57,25 +54,6 @@ let MidwayWebRouterService = class MidwayWebRouterService {
|
|
|
57
54
|
this.routesPriority = this.routesPriority.sort((routeA, routeB) => {
|
|
58
55
|
return routeB.prefix.length - routeA.prefix.length;
|
|
59
56
|
});
|
|
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
|
-
}
|
|
79
57
|
}
|
|
80
58
|
/**
|
|
81
59
|
* dynamically add a controller
|
|
@@ -184,93 +162,6 @@ let MidwayWebRouterService = class MidwayWebRouterService {
|
|
|
184
162
|
url: routerPath,
|
|
185
163
|
}));
|
|
186
164
|
}
|
|
187
|
-
collectFunctionRoute(module, functionMeta = false) {
|
|
188
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
189
|
-
// serverlessTrigger metadata
|
|
190
|
-
const webRouterInfo = (0, decorator_1.getClassMetadata)(decorator_1.FUNC_KEY, module);
|
|
191
|
-
const controllerId = (0, decorator_1.getProviderName)(module);
|
|
192
|
-
const id = (0, decorator_1.getProviderUUId)(module);
|
|
193
|
-
const prefix = '/';
|
|
194
|
-
if (!this.routes.has(prefix)) {
|
|
195
|
-
this.routes.set(prefix, []);
|
|
196
|
-
this.routesPriority.push({
|
|
197
|
-
prefix,
|
|
198
|
-
priority: -999,
|
|
199
|
-
middleware: [],
|
|
200
|
-
routerOptions: {},
|
|
201
|
-
controllerId,
|
|
202
|
-
routerModule: module,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
for (const webRouter of webRouterInfo) {
|
|
206
|
-
// 新的 @ServerlessTrigger 写法
|
|
207
|
-
if ((_a = webRouter['metadata']) === null || _a === void 0 ? void 0 : _a['path']) {
|
|
208
|
-
const routeArgsInfo = (0, decorator_1.getPropertyDataFromClass)(decorator_1.WEB_ROUTER_PARAM_KEY, module, webRouter['methodName']) || [];
|
|
209
|
-
const routerResponseData = (0, decorator_1.getPropertyMetadata)(decorator_1.WEB_RESPONSE_KEY, module, webRouter['methodName']) || [];
|
|
210
|
-
// 新 http/api gateway 函数
|
|
211
|
-
const data = {
|
|
212
|
-
id,
|
|
213
|
-
prefix,
|
|
214
|
-
routerName: '',
|
|
215
|
-
url: webRouter['metadata']['path'],
|
|
216
|
-
requestMethod: (_c = (_b = webRouter['metadata']) === null || _b === void 0 ? void 0 : _b['method']) !== null && _c !== void 0 ? _c : 'get',
|
|
217
|
-
method: webRouter['methodName'],
|
|
218
|
-
description: '',
|
|
219
|
-
summary: '',
|
|
220
|
-
handlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
221
|
-
funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
222
|
-
controllerId,
|
|
223
|
-
middleware: ((_d = webRouter['metadata']) === null || _d === void 0 ? void 0 : _d['middleware']) || [],
|
|
224
|
-
controllerMiddleware: [],
|
|
225
|
-
requestMetadata: routeArgsInfo,
|
|
226
|
-
responseMetadata: routerResponseData,
|
|
227
|
-
};
|
|
228
|
-
if (functionMeta) {
|
|
229
|
-
const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
|
|
230
|
-
const functionName = (_f = (_e = functionMeta['functionName']) !== null && _e !== void 0 ? _e : webRouter['functionName']) !== null && _f !== void 0 ? _f : createFunctionName(module, webRouter['methodName']);
|
|
231
|
-
data.functionName = functionName;
|
|
232
|
-
data.functionTriggerName = webRouter['type'];
|
|
233
|
-
data.functionTriggerMetadata = webRouter['metadata'];
|
|
234
|
-
data.functionMetadata = {
|
|
235
|
-
functionName,
|
|
236
|
-
...functionMeta,
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
this.checkDuplicateAndPush(prefix, data);
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
if (functionMeta) {
|
|
243
|
-
const functionMeta = (0, decorator_1.getPropertyMetadata)(decorator_1.SERVERLESS_FUNC_KEY, module, webRouter['methodName']) || {};
|
|
244
|
-
const functionName = (_h = (_g = functionMeta['functionName']) !== null && _g !== void 0 ? _g : webRouter['functionName']) !== null && _h !== void 0 ? _h : createFunctionName(module, webRouter['methodName']);
|
|
245
|
-
// 其他类型的函数
|
|
246
|
-
this.checkDuplicateAndPush(prefix, {
|
|
247
|
-
id,
|
|
248
|
-
prefix,
|
|
249
|
-
routerName: '',
|
|
250
|
-
url: '',
|
|
251
|
-
requestMethod: '',
|
|
252
|
-
method: webRouter['methodName'],
|
|
253
|
-
description: '',
|
|
254
|
-
summary: '',
|
|
255
|
-
handlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
256
|
-
funcHandlerName: `${controllerId}.${webRouter['methodName']}`,
|
|
257
|
-
controllerId,
|
|
258
|
-
middleware: ((_j = webRouter['metadata']) === null || _j === void 0 ? void 0 : _j['middleware']) || [],
|
|
259
|
-
controllerMiddleware: [],
|
|
260
|
-
requestMetadata: [],
|
|
261
|
-
responseMetadata: [],
|
|
262
|
-
functionName,
|
|
263
|
-
functionTriggerName: webRouter['type'],
|
|
264
|
-
functionTriggerMetadata: webRouter['metadata'],
|
|
265
|
-
functionMetadata: {
|
|
266
|
-
functionName,
|
|
267
|
-
...functionMeta,
|
|
268
|
-
},
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
165
|
sortRouter(urlMatchList) {
|
|
275
166
|
// 1. 绝对路径规则优先级最高如 /ab/cb/e
|
|
276
167
|
// 2. 星号只能出现最后且必须在/后面,如 /ab/cb/**
|
|
@@ -378,49 +269,4 @@ MidwayWebRouterService = __decorate([
|
|
|
378
269
|
__metadata("design:paramtypes", [Object])
|
|
379
270
|
], MidwayWebRouterService);
|
|
380
271
|
exports.MidwayWebRouterService = MidwayWebRouterService;
|
|
381
|
-
function createFunctionName(target, functionName) {
|
|
382
|
-
return (0, decorator_1.getProviderName)(target).replace(/[:#]/g, '-') + '-' + functionName;
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* @deprecated use built-in MidwayWebRouterService first
|
|
386
|
-
*/
|
|
387
|
-
class WebRouterCollector {
|
|
388
|
-
constructor(baseDir = '', options = {}) {
|
|
389
|
-
this.baseDir = baseDir;
|
|
390
|
-
this.options = options;
|
|
391
|
-
}
|
|
392
|
-
async init() {
|
|
393
|
-
if (!this.proxy) {
|
|
394
|
-
if (this.baseDir) {
|
|
395
|
-
const container = new container_1.MidwayContainer();
|
|
396
|
-
(0, decorator_1.bindContainer)(container);
|
|
397
|
-
container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
|
|
398
|
-
loadDir: this.baseDir,
|
|
399
|
-
}));
|
|
400
|
-
await container.ready();
|
|
401
|
-
}
|
|
402
|
-
if ((0, contextUtil_1.getCurrentMainFramework)()) {
|
|
403
|
-
this.proxy = await (0, contextUtil_1.getCurrentMainFramework)()
|
|
404
|
-
.getApplicationContext()
|
|
405
|
-
.getAsync(MidwayWebRouterService, [this.options]);
|
|
406
|
-
}
|
|
407
|
-
else {
|
|
408
|
-
this.proxy = new MidwayWebRouterService(this.options);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
async getRoutePriorityList() {
|
|
413
|
-
await this.init();
|
|
414
|
-
return this.proxy.getRoutePriorityList();
|
|
415
|
-
}
|
|
416
|
-
async getRouterTable() {
|
|
417
|
-
await this.init();
|
|
418
|
-
return this.proxy.getRouterTable();
|
|
419
|
-
}
|
|
420
|
-
async getFlattenRouterTable() {
|
|
421
|
-
await this.init();
|
|
422
|
-
return this.proxy.getFlattenRouterTable();
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
exports.WebRouterCollector = WebRouterCollector;
|
|
426
272
|
//# sourceMappingURL=webRouterService.js.map
|
package/dist/setup.js
CHANGED
|
@@ -7,6 +7,7 @@ const decorator_1 = require("@midwayjs/decorator");
|
|
|
7
7
|
const util = require("util");
|
|
8
8
|
const path_1 = require("path");
|
|
9
9
|
const logger_1 = require("@midwayjs/logger");
|
|
10
|
+
const slsFunctionService_1 = require("./service/slsFunctionService");
|
|
10
11
|
const debug = util.debuglog('midway:debug');
|
|
11
12
|
/**
|
|
12
13
|
* midway framework main entry, this method bootstrap all service and framework.
|
|
@@ -96,6 +97,7 @@ function prepareGlobalApplicationContext(globalOptions) {
|
|
|
96
97
|
applicationContext.bindClass(_1.MidwayLifeCycleService);
|
|
97
98
|
applicationContext.bindClass(_1.MidwayMockService);
|
|
98
99
|
applicationContext.bindClass(_1.MidwayWebRouterService);
|
|
100
|
+
applicationContext.bindClass(slsFunctionService_1.MidwayServerlessFunctionService);
|
|
99
101
|
// bind preload module
|
|
100
102
|
if (globalOptions.preloadModules && globalOptions.preloadModules.length) {
|
|
101
103
|
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.4",
|
|
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.4",
|
|
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": "3b686101f7802f87d47b8c9d25561339d556e573"
|
|
49
49
|
}
|