@midwayjs/express 3.0.0-beta.1 → 3.0.0-beta.10
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 +91 -0
- package/README.md +1 -1
- package/dist/config/config.default.d.ts +22 -0
- package/dist/config/config.default.js +27 -0
- package/dist/configuration.d.ts +3 -3
- package/dist/configuration.js +45 -8
- package/dist/framework.d.ts +5 -16
- package/dist/framework.js +68 -84
- package/dist/interface.d.ts +46 -9
- package/dist/middlewareService.d.ts +4 -4
- package/dist/middlewareService.js +18 -38
- package/package.json +10 -6
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,97 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* default add session & bodyparser support for koa/express/faas ([#1420](https://github.com/midwayjs/midway/issues/1420)) ([cdaff31](https://github.com/midwayjs/midway/commit/cdaff317c3e862a95494a167995a28280af639bf))
|
|
12
|
+
* implement i18n for validate ([#1426](https://github.com/midwayjs/midway/issues/1426)) ([4c7ed2f](https://github.com/midwayjs/midway/commit/4c7ed2ff2e7ccf10f88f62abad230f92f5e76b97))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# [3.0.0-beta.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
|
|
19
|
+
|
|
20
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Bug Fixes
|
|
30
|
+
|
|
31
|
+
* express routing middleware takes effect at the controller level ([#1364](https://github.com/midwayjs/midway/issues/1364)) ([b9272e0](https://github.com/midwayjs/midway/commit/b9272e0971003443304b0c53815be31a0061b4bd))
|
|
32
|
+
* typeorm EntityView missing connectionName ([#1403](https://github.com/midwayjs/midway/issues/1403)) ([30b2b37](https://github.com/midwayjs/midway/commit/30b2b3711db485cb85d825d56aeabf53b1374cae))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
|
|
39
|
+
|
|
40
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### Bug Fixes
|
|
50
|
+
|
|
51
|
+
* class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
|
|
58
|
+
|
|
59
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
### Features
|
|
69
|
+
|
|
70
|
+
* add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
|
|
71
|
+
* auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
|
|
72
|
+
* support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
### Features
|
|
82
|
+
|
|
83
|
+
* add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
|
|
90
|
+
|
|
91
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
6
97
|
# [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
|
|
7
98
|
|
|
8
99
|
|
package/README.md
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Options, OptionsJson, OptionsText, OptionsUrlencoded } from 'body-parser';
|
|
2
|
+
import { CookieOptions } from 'express';
|
|
3
|
+
export declare const cookieParser: {
|
|
4
|
+
secret?: string | string[];
|
|
5
|
+
options?: CookieOptions;
|
|
6
|
+
};
|
|
7
|
+
export declare const bodyParser: {
|
|
8
|
+
enable?: boolean;
|
|
9
|
+
json?: OptionsJson & {
|
|
10
|
+
enable?: boolean;
|
|
11
|
+
};
|
|
12
|
+
raw?: Options & {
|
|
13
|
+
enable?: boolean;
|
|
14
|
+
};
|
|
15
|
+
text?: OptionsText & {
|
|
16
|
+
enable?: boolean;
|
|
17
|
+
};
|
|
18
|
+
urlencoded?: OptionsUrlencoded & {
|
|
19
|
+
enable?: boolean;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
//# sourceMappingURL=config.default.d.ts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bodyParser = exports.cookieParser = void 0;
|
|
4
|
+
exports.cookieParser = {};
|
|
5
|
+
exports.bodyParser = {
|
|
6
|
+
enable: true,
|
|
7
|
+
json: {
|
|
8
|
+
enable: true,
|
|
9
|
+
limit: '1mb',
|
|
10
|
+
strict: true,
|
|
11
|
+
},
|
|
12
|
+
raw: {
|
|
13
|
+
enable: false,
|
|
14
|
+
limit: '1mb',
|
|
15
|
+
},
|
|
16
|
+
text: {
|
|
17
|
+
enable: true,
|
|
18
|
+
limit: '1mb',
|
|
19
|
+
},
|
|
20
|
+
urlencoded: {
|
|
21
|
+
enable: true,
|
|
22
|
+
extended: false,
|
|
23
|
+
limit: '1mb',
|
|
24
|
+
parameterLimit: 1000,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=config.default.js.map
|
package/dist/configuration.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { MidwayConfigService, MidwayDecoratorService } from '@midwayjs/core';
|
|
1
2
|
import { MidwayExpressFramework } from './framework';
|
|
2
|
-
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
3
3
|
export declare class ExpressConfiguration {
|
|
4
|
-
framework: MidwayExpressFramework;
|
|
5
4
|
decoratorService: MidwayDecoratorService;
|
|
5
|
+
expressFramework: MidwayExpressFramework;
|
|
6
|
+
configService: MidwayConfigService;
|
|
6
7
|
init(): void;
|
|
7
8
|
onReady(): Promise<void>;
|
|
8
|
-
onServerReady(): Promise<void>;
|
|
9
9
|
}
|
|
10
10
|
//# sourceMappingURL=configuration.d.ts.map
|
package/dist/configuration.js
CHANGED
|
@@ -11,27 +11,58 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ExpressConfiguration = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
-
const framework_1 = require("./framework");
|
|
15
14
|
const core_1 = require("@midwayjs/core");
|
|
15
|
+
const session = require("@midwayjs/express-session");
|
|
16
|
+
const framework_1 = require("./framework");
|
|
17
|
+
const bodyParser = require("body-parser");
|
|
18
|
+
const cookieParser = require("cookie-parser");
|
|
19
|
+
const DefaultConfig = require("./config/config.default");
|
|
16
20
|
let ExpressConfiguration = class ExpressConfiguration {
|
|
17
21
|
init() {
|
|
18
22
|
this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
|
|
19
|
-
return (0, core_1.extractExpressLikeValue)(options.metadata.type, options.metadata.propertyData)(options.originArgs[0], options.originArgs[1], options.originArgs[2]);
|
|
23
|
+
return (0, core_1.extractExpressLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1], options.originArgs[2]);
|
|
20
24
|
});
|
|
21
25
|
}
|
|
22
|
-
async onReady() {
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
async onReady() {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
const keys = (_a = this.configService.getConfiguration('express.keys')) !== null && _a !== void 0 ? _a : this.configService.getConfiguration('keys');
|
|
29
|
+
const cookieParserConfig = this.configService.getConfiguration('cookieParser');
|
|
30
|
+
// add cookie parser middleware
|
|
31
|
+
this.expressFramework
|
|
32
|
+
.getMiddleware()
|
|
33
|
+
.insertFirst(cookieParser((_b = cookieParserConfig.secret) !== null && _b !== void 0 ? _b : keys, cookieParserConfig.options));
|
|
34
|
+
// add body parser
|
|
35
|
+
const bodyparserConfig = this.configService.getConfiguration('bodyParser');
|
|
36
|
+
if (bodyparserConfig.enable) {
|
|
37
|
+
// create application/json parser
|
|
38
|
+
if (bodyparserConfig.json.enable) {
|
|
39
|
+
this.expressFramework.useMiddleware(bodyParser.json(bodyparserConfig.json));
|
|
40
|
+
}
|
|
41
|
+
if (bodyparserConfig.raw.enable) {
|
|
42
|
+
this.expressFramework.useMiddleware(bodyParser.raw(bodyparserConfig.raw));
|
|
43
|
+
}
|
|
44
|
+
if (bodyparserConfig.text.enable) {
|
|
45
|
+
this.expressFramework.useMiddleware(bodyParser.text(bodyparserConfig.text));
|
|
46
|
+
}
|
|
47
|
+
// create application/x-www-form-urlencoded parser
|
|
48
|
+
if (bodyparserConfig.urlencoded.enable) {
|
|
49
|
+
this.expressFramework.useMiddleware(bodyParser.urlencoded(bodyparserConfig.urlencoded));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
25
52
|
}
|
|
26
53
|
};
|
|
54
|
+
__decorate([
|
|
55
|
+
(0, decorator_1.Inject)(),
|
|
56
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
57
|
+
], ExpressConfiguration.prototype, "decoratorService", void 0);
|
|
27
58
|
__decorate([
|
|
28
59
|
(0, decorator_1.Inject)(),
|
|
29
60
|
__metadata("design:type", framework_1.MidwayExpressFramework)
|
|
30
|
-
], ExpressConfiguration.prototype, "
|
|
61
|
+
], ExpressConfiguration.prototype, "expressFramework", void 0);
|
|
31
62
|
__decorate([
|
|
32
63
|
(0, decorator_1.Inject)(),
|
|
33
|
-
__metadata("design:type", core_1.
|
|
34
|
-
], ExpressConfiguration.prototype, "
|
|
64
|
+
__metadata("design:type", core_1.MidwayConfigService)
|
|
65
|
+
], ExpressConfiguration.prototype, "configService", void 0);
|
|
35
66
|
__decorate([
|
|
36
67
|
(0, decorator_1.Init)(),
|
|
37
68
|
__metadata("design:type", Function),
|
|
@@ -41,6 +72,12 @@ __decorate([
|
|
|
41
72
|
ExpressConfiguration = __decorate([
|
|
42
73
|
(0, decorator_1.Configuration)({
|
|
43
74
|
namespace: 'express',
|
|
75
|
+
imports: [session],
|
|
76
|
+
importConfigs: [
|
|
77
|
+
{
|
|
78
|
+
default: DefaultConfig,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
44
81
|
})
|
|
45
82
|
], ExpressConfiguration);
|
|
46
83
|
exports.ExpressConfiguration = ExpressConfiguration;
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { BaseFramework,
|
|
3
|
-
import { RouterParamValue } from '@midwayjs/decorator';
|
|
2
|
+
import { BaseFramework, IMidwayBootstrapOptions, MiddlewareRespond, MidwayFrameworkType, RouterInfo } from '@midwayjs/core';
|
|
4
3
|
import { IMidwayExpressApplication, IMidwayExpressConfigurationOptions, IMidwayExpressContext } from './interface';
|
|
5
4
|
import type { IRouter, IRouterHandler, Response, NextFunction } from 'express';
|
|
6
5
|
import { Server } from 'net';
|
|
7
6
|
import { MidwayExpressContextLogger } from './logger';
|
|
8
|
-
export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpressApplication, IMidwayExpressContext, IMidwayExpressConfigurationOptions> {
|
|
7
|
+
export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpressApplication, IMidwayExpressContext, IMidwayExpressConfigurationOptions, Response, NextFunction> {
|
|
9
8
|
app: IMidwayExpressApplication;
|
|
10
|
-
private controllerIds;
|
|
11
9
|
private server;
|
|
12
|
-
protected middlewareManager: ContextMiddlewareManager<IMidwayExpressContext, Response<any, Record<string, any>>, NextFunction>;
|
|
13
|
-
protected exceptionFilterManager: ExceptionFilterManager<IMidwayExpressContext, Response<any, Record<string, any>>, NextFunction>;
|
|
14
10
|
private expressMiddlewareService;
|
|
15
11
|
configure(): IMidwayExpressConfigurationOptions;
|
|
16
12
|
applicationInitialize(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
|
|
@@ -18,27 +14,20 @@ export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpress
|
|
|
18
14
|
getFrameworkType(): MidwayFrameworkType;
|
|
19
15
|
/**
|
|
20
16
|
* wrap controller string to middleware function
|
|
21
|
-
* @param controllerMapping like FooController.index
|
|
22
|
-
* @param routeArgsInfo
|
|
23
|
-
* @param routerResponseData
|
|
24
17
|
*/
|
|
25
|
-
generateController(
|
|
18
|
+
protected generateController(routeInfo: RouterInfo): IRouterHandler<any>;
|
|
26
19
|
loadMidwayController(): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* @deprecated
|
|
29
|
-
*/
|
|
30
|
-
generateMiddleware(middlewareId: string): Promise<FunctionMiddleware<IMidwayExpressContext, Response<any, Record<string, any>>, NextFunction>>;
|
|
31
20
|
/**
|
|
32
21
|
* @param routerOptions
|
|
33
22
|
*/
|
|
34
23
|
protected createRouter(routerOptions: {
|
|
35
24
|
sensitive: any;
|
|
36
25
|
}): IRouter;
|
|
37
|
-
|
|
38
|
-
getMiddleware<Response, NextFunction>(): Promise<MiddlewareRespond<IMidwayExpressContext, Response, NextFunction>>;
|
|
26
|
+
applyMiddleware<Response, NextFunction>(): Promise<MiddlewareRespond<IMidwayExpressContext, Response, NextFunction>>;
|
|
39
27
|
beforeStop(): Promise<void>;
|
|
40
28
|
getServer(): Server;
|
|
41
29
|
getFrameworkName(): string;
|
|
42
30
|
getDefaultContextLoggerClass(): typeof MidwayExpressContextLogger;
|
|
31
|
+
protected sendData(res: any, data: any): void;
|
|
43
32
|
}
|
|
44
33
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -5,9 +5,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
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
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.MidwayExpressFramework = void 0;
|
|
13
10
|
const core_1 = require("@midwayjs/core");
|
|
@@ -16,28 +13,12 @@ const express = require("express");
|
|
|
16
13
|
const logger_1 = require("./logger");
|
|
17
14
|
const middlewareService_1 = require("./middlewareService");
|
|
18
15
|
let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFramework {
|
|
19
|
-
constructor() {
|
|
20
|
-
super(...arguments);
|
|
21
|
-
this.controllerIds = [];
|
|
22
|
-
this.middlewareManager = new core_1.ContextMiddlewareManager();
|
|
23
|
-
this.exceptionFilterManager = new core_1.ExceptionFilterManager();
|
|
24
|
-
}
|
|
25
16
|
configure() {
|
|
26
17
|
return this.configService.getConfiguration('express');
|
|
27
18
|
}
|
|
28
19
|
async applicationInitialize(options) {
|
|
20
|
+
this.expressMiddlewareService = await this.applicationContext.getAsync(middlewareService_1.MidwayExpressMiddlewareService, [this.applicationContext]);
|
|
29
21
|
this.app = express();
|
|
30
|
-
this.defineApplicationProperties({
|
|
31
|
-
generateController: (controllerMapping) => {
|
|
32
|
-
return this.generateController(controllerMapping);
|
|
33
|
-
},
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated
|
|
36
|
-
*/
|
|
37
|
-
generateMiddleware: async (middlewareId) => {
|
|
38
|
-
return this.generateMiddleware(middlewareId);
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
22
|
this.app.use((req, res, next) => {
|
|
42
23
|
const ctx = req;
|
|
43
24
|
this.app.createAnonymousContext(ctx);
|
|
@@ -46,6 +27,26 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
46
27
|
ctx.requestContext.registerObject('res', res);
|
|
47
28
|
next();
|
|
48
29
|
});
|
|
30
|
+
}
|
|
31
|
+
async run() {
|
|
32
|
+
var _a;
|
|
33
|
+
// use global middleware
|
|
34
|
+
const globalMiddleware = await this.applyMiddleware();
|
|
35
|
+
this.app.use(globalMiddleware);
|
|
36
|
+
// load controller
|
|
37
|
+
await this.loadMidwayController();
|
|
38
|
+
// use global error handler
|
|
39
|
+
this.app.use(async (err, req, res, next) => {
|
|
40
|
+
if (err) {
|
|
41
|
+
const { result, error } = await this.filterManager.runErrorFilter(err, req, res, next);
|
|
42
|
+
if (error) {
|
|
43
|
+
next(error);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.sendData(res, result);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
49
50
|
// https config
|
|
50
51
|
if (this.configurationOptions.key && this.configurationOptions.cert) {
|
|
51
52
|
this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
|
|
@@ -68,26 +69,8 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
68
69
|
}
|
|
69
70
|
// register httpServer to applicationContext
|
|
70
71
|
this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
// use global middleware
|
|
74
|
-
const globalMiddleware = await this.getMiddleware();
|
|
75
|
-
this.app.use(globalMiddleware);
|
|
76
|
-
// load controller
|
|
77
|
-
await this.loadMidwayController();
|
|
78
|
-
// use global error handler
|
|
79
|
-
this.app.use(async (err, req, res, next) => {
|
|
80
|
-
if (err) {
|
|
81
|
-
const { result, error } = await this.exceptionFilterManager.run(err, req, res, next);
|
|
82
|
-
if (error) {
|
|
83
|
-
next(error);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
res.send(result);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
if (this.configurationOptions.port) {
|
|
72
|
+
const customPort = (_a = process.env.MIDWAY_HTTP_PORT) !== null && _a !== void 0 ? _a : this.configurationOptions.port;
|
|
73
|
+
if (customPort) {
|
|
91
74
|
new Promise(resolve => {
|
|
92
75
|
const args = [this.configurationOptions.port];
|
|
93
76
|
if (this.configurationOptions.hostname) {
|
|
@@ -97,6 +80,7 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
97
80
|
resolve();
|
|
98
81
|
});
|
|
99
82
|
this.server.listen(...args);
|
|
83
|
+
process.env.MIDWAY_HTTP_PORT = String(customPort);
|
|
100
84
|
});
|
|
101
85
|
}
|
|
102
86
|
}
|
|
@@ -105,15 +89,18 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
105
89
|
}
|
|
106
90
|
/**
|
|
107
91
|
* wrap controller string to middleware function
|
|
108
|
-
* @param controllerMapping like FooController.index
|
|
109
|
-
* @param routeArgsInfo
|
|
110
|
-
* @param routerResponseData
|
|
111
92
|
*/
|
|
112
|
-
generateController(
|
|
113
|
-
const [controllerId, methodName] = controllerMapping.split('.');
|
|
93
|
+
generateController(routeInfo) {
|
|
114
94
|
return async (req, res, next) => {
|
|
115
|
-
const controller = await req.requestContext.getAsync(
|
|
116
|
-
|
|
95
|
+
const controller = await req.requestContext.getAsync(routeInfo.id);
|
|
96
|
+
let result;
|
|
97
|
+
try {
|
|
98
|
+
result = await controller[routeInfo.method].call(controller, req, res, next);
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
next(err);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
117
104
|
if (res.headersSent) {
|
|
118
105
|
// return when response send
|
|
119
106
|
return;
|
|
@@ -122,8 +109,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
122
109
|
res.status(204);
|
|
123
110
|
}
|
|
124
111
|
// implement response decorator
|
|
125
|
-
if (Array.isArray(
|
|
126
|
-
|
|
112
|
+
if (Array.isArray(routeInfo.responseMetadata) &&
|
|
113
|
+
routeInfo.responseMetadata.length) {
|
|
114
|
+
for (const routerRes of routeInfo.responseMetadata) {
|
|
127
115
|
switch (routerRes.type) {
|
|
128
116
|
case decorator_1.WEB_RESPONSE_HTTP_CODE:
|
|
129
117
|
res.status(routerRes.code);
|
|
@@ -140,64 +128,56 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
140
128
|
}
|
|
141
129
|
}
|
|
142
130
|
}
|
|
143
|
-
|
|
131
|
+
const { result: returnValue, error } = await this.filterManager.runResultFilter(result, req, res, next);
|
|
132
|
+
if (error) {
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
this.sendData(res, returnValue);
|
|
144
136
|
};
|
|
145
137
|
}
|
|
146
138
|
async loadMidwayController() {
|
|
147
|
-
const collector = new core_1.WebRouterCollector(
|
|
139
|
+
const collector = new core_1.WebRouterCollector('', {
|
|
140
|
+
globalPrefix: this.configurationOptions.globalPrefix,
|
|
141
|
+
});
|
|
148
142
|
const routerTable = await collector.getRouterTable();
|
|
149
143
|
const routerList = await collector.getRoutePriorityList();
|
|
150
144
|
for (const routerInfo of routerList) {
|
|
151
145
|
// bind controller first
|
|
152
146
|
this.getApplicationContext().bindClass(routerInfo.routerModule);
|
|
153
|
-
|
|
154
|
-
// controller id check
|
|
155
|
-
if (this.controllerIds.indexOf(providerId) > -1) {
|
|
156
|
-
throw new Error(`Controller identifier [${providerId}] already exists!`);
|
|
157
|
-
}
|
|
158
|
-
this.controllerIds.push(providerId);
|
|
159
|
-
this.logger.debug(`Load Controller "${providerId}", prefix=${routerInfo.prefix}`);
|
|
147
|
+
this.logger.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
160
148
|
// new router
|
|
161
149
|
const newRouter = this.createRouter(routerInfo.routerOptions);
|
|
162
150
|
// add router middleware
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
151
|
+
if (routerInfo.middleware.length) {
|
|
152
|
+
const routerMiddlewareFn = await this.expressMiddlewareService.compose(routerInfo.middleware, this.app);
|
|
153
|
+
newRouter.use(routerMiddlewareFn);
|
|
154
|
+
}
|
|
166
155
|
// add route
|
|
167
156
|
const routes = routerTable.get(routerInfo.prefix);
|
|
168
157
|
for (const routeInfo of routes) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
158
|
+
const routeMiddlewareList = [];
|
|
159
|
+
// routeInfo middleware
|
|
160
|
+
if (routeInfo.middleware.length) {
|
|
161
|
+
const routeMiddlewareFn = await this.expressMiddlewareService.compose(routeInfo.middleware, this.app);
|
|
162
|
+
routeMiddlewareList.push(routeMiddlewareFn);
|
|
163
|
+
}
|
|
173
164
|
this.logger.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
174
165
|
// apply controller from request context
|
|
175
|
-
newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, this.generateController(routeInfo
|
|
166
|
+
newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
|
|
176
167
|
}
|
|
177
168
|
this.app.use(routerInfo.prefix, newRouter);
|
|
178
169
|
}
|
|
179
170
|
}
|
|
180
|
-
/**
|
|
181
|
-
* @deprecated
|
|
182
|
-
*/
|
|
183
|
-
async generateMiddleware(middlewareId) {
|
|
184
|
-
const mwIns = await this.getApplicationContext().getAsync(middlewareId);
|
|
185
|
-
return mwIns.resolve();
|
|
186
|
-
}
|
|
187
171
|
/**
|
|
188
172
|
* @param routerOptions
|
|
189
173
|
*/
|
|
190
174
|
createRouter(routerOptions) {
|
|
191
175
|
return express.Router({ caseSensitive: routerOptions.sensitive });
|
|
192
176
|
}
|
|
193
|
-
async
|
|
194
|
-
const fn = await this.expressMiddlewareService.compose(middlewares);
|
|
195
|
-
handlerCallback(fn);
|
|
196
|
-
}
|
|
197
|
-
async getMiddleware() {
|
|
177
|
+
async applyMiddleware() {
|
|
198
178
|
if (!this.composeMiddleware) {
|
|
199
|
-
this.composeMiddleware = await this.expressMiddlewareService.compose(this.middlewareManager);
|
|
200
|
-
await this.
|
|
179
|
+
this.composeMiddleware = await this.expressMiddlewareService.compose(this.middlewareManager, this.app);
|
|
180
|
+
await this.filterManager.init(this.applicationContext);
|
|
201
181
|
}
|
|
202
182
|
return this.composeMiddleware;
|
|
203
183
|
}
|
|
@@ -213,11 +193,15 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
213
193
|
getDefaultContextLoggerClass() {
|
|
214
194
|
return logger_1.MidwayExpressContextLogger;
|
|
215
195
|
}
|
|
196
|
+
sendData(res, data) {
|
|
197
|
+
if (typeof data === 'number') {
|
|
198
|
+
res.status(res.statusCode).send('' + data);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
res.status(res.statusCode).send(data);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
216
204
|
};
|
|
217
|
-
__decorate([
|
|
218
|
-
(0, decorator_1.Inject)(),
|
|
219
|
-
__metadata("design:type", middlewareService_1.MidwayExpressMiddlewareService)
|
|
220
|
-
], MidwayExpressFramework.prototype, "expressMiddlewareService", void 0);
|
|
221
205
|
MidwayExpressFramework = __decorate([
|
|
222
206
|
(0, decorator_1.Framework)()
|
|
223
207
|
], MidwayExpressFramework);
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { IConfigurationOptions, IMiddleware, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
3
|
-
import { Application as ExpressApplication, NextFunction, Request, Response } from 'express';
|
|
4
|
-
import {
|
|
2
|
+
import { CommonMiddlewareUnion, ContextMiddlewareManager, IConfigurationOptions, IMiddleware, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
3
|
+
import { Application as ExpressApplication, NextFunction as ExpressNextFunction, Request, Response } from 'express';
|
|
4
|
+
import { Options, OptionsJson, OptionsText, OptionsUrlencoded } from 'body-parser';
|
|
5
5
|
export declare type IMidwayExpressContext = IMidwayContext<Request>;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated use IMidwayExpressContext
|
|
8
|
+
*/
|
|
9
|
+
export declare type IMidwayExpressRequest = IMidwayExpressContext;
|
|
10
|
+
export declare type IMidwayExpressMiddleware = IMiddleware<IMidwayExpressContext, Response, ExpressNextFunction>;
|
|
11
|
+
export interface IMidwayExpressApplication extends IMidwayApplication<IMidwayExpressContext, ExpressApplication> {
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* add global middleware to app
|
|
14
|
+
* @param Middleware
|
|
11
15
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
useMiddleware<Response, NextFunction>(Middleware: CommonMiddlewareUnion<IMidwayExpressContext, Response, NextFunction>): void;
|
|
17
|
+
/**
|
|
18
|
+
* get global middleware
|
|
19
|
+
*/
|
|
20
|
+
getMiddleware<Response, NextFunction>(): ContextMiddlewareManager<IMidwayExpressContext, Response, NextFunction>;
|
|
21
|
+
}
|
|
14
22
|
export interface IMidwayExpressConfigurationOptions extends IConfigurationOptions {
|
|
23
|
+
/**
|
|
24
|
+
* session or cookie secret key
|
|
25
|
+
*/
|
|
26
|
+
keys?: string | string[];
|
|
15
27
|
/**
|
|
16
28
|
* application http port
|
|
17
29
|
*/
|
|
@@ -36,8 +48,33 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
|
|
|
36
48
|
* http2 support
|
|
37
49
|
*/
|
|
38
50
|
http2?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* http global prefix
|
|
53
|
+
*/
|
|
54
|
+
globalPrefix?: string;
|
|
39
55
|
}
|
|
40
56
|
export declare type Application = IMidwayExpressApplication;
|
|
57
|
+
export declare type NextFunction = ExpressNextFunction;
|
|
41
58
|
export interface Context extends IMidwayExpressContext {
|
|
42
59
|
}
|
|
60
|
+
declare module '@midwayjs/core/dist/interface' {
|
|
61
|
+
interface MidwayConfig {
|
|
62
|
+
express?: IMidwayExpressConfigurationOptions;
|
|
63
|
+
bodyParser?: {
|
|
64
|
+
enable?: boolean;
|
|
65
|
+
json?: OptionsJson & {
|
|
66
|
+
enable?: boolean;
|
|
67
|
+
};
|
|
68
|
+
raw?: Options & {
|
|
69
|
+
enable?: boolean;
|
|
70
|
+
};
|
|
71
|
+
text?: OptionsText & {
|
|
72
|
+
enable?: boolean;
|
|
73
|
+
};
|
|
74
|
+
urlencoded?: OptionsUrlencoded & {
|
|
75
|
+
enable?: boolean;
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
43
80
|
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { IMidwayContainer, CommonMiddleware } from '@midwayjs/core';
|
|
2
|
-
import { IMidwayExpressContext } from './interface';
|
|
1
|
+
import { IMidwayContainer, CommonMiddleware, FunctionMiddleware } from '@midwayjs/core';
|
|
2
|
+
import { IMidwayExpressContext, Application } from './interface';
|
|
3
3
|
import { NextFunction, Response } from 'express';
|
|
4
4
|
export declare class MidwayExpressMiddlewareService {
|
|
5
5
|
readonly applicationContext: IMidwayContainer;
|
|
6
6
|
constructor(applicationContext: IMidwayContainer);
|
|
7
|
-
compose(middleware: Array<CommonMiddleware<IMidwayExpressContext, Response, NextFunction> | string>, name?: string): Promise<{
|
|
7
|
+
compose(middleware: Array<CommonMiddleware<IMidwayExpressContext, Response, NextFunction> | string>, app: Application, name?: string): Promise<{
|
|
8
8
|
(req: IMidwayExpressContext, res: Response, next: NextFunction): void;
|
|
9
9
|
_name: string;
|
|
10
10
|
}>;
|
|
11
11
|
}
|
|
12
|
-
export declare function
|
|
12
|
+
export declare function wrapMiddleware(mw: FunctionMiddleware<any, any, any>, options: any): (context: any, next: any, options?: any) => any;
|
|
13
13
|
//# sourceMappingURL=middlewareService.d.ts.map
|
|
@@ -9,14 +9,14 @@ 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.wrapMiddleware = exports.MidwayExpressMiddlewareService = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const core_1 = require("@midwayjs/core");
|
|
15
15
|
let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
16
16
|
constructor(applicationContext) {
|
|
17
17
|
this.applicationContext = applicationContext;
|
|
18
18
|
}
|
|
19
|
-
async compose(middleware, name) {
|
|
19
|
+
async compose(middleware, app, name) {
|
|
20
20
|
if (!Array.isArray(middleware)) {
|
|
21
21
|
throw new core_1.MidwayCommonError('Middleware stack must be an array');
|
|
22
22
|
}
|
|
@@ -29,7 +29,7 @@ let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
|
29
29
|
}
|
|
30
30
|
const classMiddleware = await this.applicationContext.getAsync(fn);
|
|
31
31
|
if (classMiddleware) {
|
|
32
|
-
fn = classMiddleware.resolve();
|
|
32
|
+
fn = classMiddleware.resolve(app);
|
|
33
33
|
if (!classMiddleware.match && !classMiddleware.ignore) {
|
|
34
34
|
fn._name = classMiddleware.constructor.name;
|
|
35
35
|
// just got fn
|
|
@@ -38,7 +38,7 @@ let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
|
38
38
|
else {
|
|
39
39
|
// wrap ignore and match
|
|
40
40
|
const mw = fn;
|
|
41
|
-
const match = pathMatching({
|
|
41
|
+
const match = (0, core_1.pathMatching)({
|
|
42
42
|
match: classMiddleware.match,
|
|
43
43
|
ignore: classMiddleware.ignore,
|
|
44
44
|
});
|
|
@@ -79,41 +79,21 @@ MidwayExpressMiddlewareService = __decorate([
|
|
|
79
79
|
__metadata("design:paramtypes", [Object])
|
|
80
80
|
], MidwayExpressMiddlewareService);
|
|
81
81
|
exports.MidwayExpressMiddlewareService = MidwayExpressMiddlewareService;
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
if (options.
|
|
85
|
-
|
|
82
|
+
function wrapMiddleware(mw, options) {
|
|
83
|
+
// support options.enable
|
|
84
|
+
if (options.enable === false)
|
|
85
|
+
return null;
|
|
86
|
+
// support options.match and options.ignore
|
|
86
87
|
if (!options.match && !options.ignore)
|
|
87
|
-
return
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return options.match ? matched : !matched;
|
|
88
|
+
return mw;
|
|
89
|
+
const match = (0, core_1.pathMatching)(options);
|
|
90
|
+
const fn = (req, res, next) => {
|
|
91
|
+
if (!match(req))
|
|
92
|
+
return next();
|
|
93
|
+
return mw(req, res, next);
|
|
94
94
|
};
|
|
95
|
+
fn._name = mw._name + 'middlewareWrapper';
|
|
96
|
+
return fn;
|
|
95
97
|
}
|
|
96
|
-
exports.
|
|
97
|
-
function toPathMatch(pattern) {
|
|
98
|
-
if (typeof pattern === 'string') {
|
|
99
|
-
const reg = (0, core_1.pathToRegexp)(pattern, [], { end: false });
|
|
100
|
-
if (reg.global)
|
|
101
|
-
reg.lastIndex = 0;
|
|
102
|
-
return ctx => reg.test(ctx.path);
|
|
103
|
-
}
|
|
104
|
-
if (pattern instanceof RegExp) {
|
|
105
|
-
return ctx => {
|
|
106
|
-
if (pattern.global)
|
|
107
|
-
pattern.lastIndex = 0;
|
|
108
|
-
return pattern.test(ctx.path);
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
if (typeof pattern === 'function')
|
|
112
|
-
return pattern;
|
|
113
|
-
if (Array.isArray(pattern)) {
|
|
114
|
-
const matchs = pattern.map(item => toPathMatch(item));
|
|
115
|
-
return ctx => matchs.some(match => match(ctx));
|
|
116
|
-
}
|
|
117
|
-
throw new core_1.MidwayCommonError('match/ignore pattern must be RegExp, Array or String, but got ' + pattern);
|
|
118
|
-
}
|
|
98
|
+
exports.wrapMiddleware = wrapMiddleware;
|
|
119
99
|
//# sourceMappingURL=middlewareService.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/express",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.10",
|
|
4
4
|
"description": "Midway Web Framework for Express",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -23,14 +23,18 @@
|
|
|
23
23
|
],
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/decorator": "^3.0.0-beta.
|
|
27
|
-
"@midwayjs/
|
|
26
|
+
"@midwayjs/decorator": "^3.0.0-beta.10",
|
|
27
|
+
"@midwayjs/logger": "^3.0.0-beta.10",
|
|
28
|
+
"@midwayjs/mock": "^3.0.0-beta.10",
|
|
29
|
+
"@types/body-parser": "^1.19.2",
|
|
28
30
|
"@types/express": "^4.17.8",
|
|
29
31
|
"fs-extra": "^8.0.1"
|
|
30
32
|
},
|
|
31
33
|
"dependencies": {
|
|
32
|
-
"@midwayjs/core": "^3.0.0-beta.
|
|
33
|
-
"@midwayjs/
|
|
34
|
+
"@midwayjs/core": "^3.0.0-beta.10",
|
|
35
|
+
"@midwayjs/express-session": "^3.0.0-beta.10",
|
|
36
|
+
"body-parser": "^1.19.0",
|
|
37
|
+
"cookie-parser": "^1.4.6",
|
|
34
38
|
"express": "^4.17.1"
|
|
35
39
|
},
|
|
36
40
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
@@ -41,5 +45,5 @@
|
|
|
41
45
|
"engines": {
|
|
42
46
|
"node": ">=12"
|
|
43
47
|
},
|
|
44
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "153870f2e2dd6b17673ec7591e49224a6bd51b36"
|
|
45
49
|
}
|