@midwayjs/express 3.0.0-alpha.9 → 3.0.0-beta.12
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 +122 -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 +10 -0
- package/dist/configuration.js +84 -0
- package/dist/framework.d.ts +9 -18
- package/dist/framework.js +141 -78
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -1
- package/dist/interface.d.ts +51 -26
- package/dist/logger.js +1 -1
- package/dist/middlewareService.d.ts +14 -0
- package/dist/middlewareService.js +134 -0
- package/package.json +13 -6
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,128 @@
|
|
|
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.12](https://github.com/midwayjs/midway/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-12-28)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* custom error code & add @Files/@Fields ([#1438](https://github.com/midwayjs/midway/issues/1438)) ([b0032af](https://github.com/midwayjs/midway/commit/b0032afd2fa9ea0416fe69f4bd0c1a58bea5314e))
|
|
12
|
+
* support throw err status ([#1440](https://github.com/midwayjs/midway/issues/1440)) ([7b98110](https://github.com/midwayjs/midway/commit/7b98110d65c5287a8fcb3eb5356dea2d7a32cee9))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# [3.0.0-beta.11](https://github.com/midwayjs/midway/compare/v3.0.0-beta.10...v3.0.0-beta.11) (2021-12-21)
|
|
19
|
+
|
|
20
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# [3.0.0-beta.10](https://github.com/midwayjs/midway/compare/v3.0.0-beta.9...v3.0.0-beta.10) (2021-12-20)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
* 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))
|
|
32
|
+
* implement i18n for validate ([#1426](https://github.com/midwayjs/midway/issues/1426)) ([4c7ed2f](https://github.com/midwayjs/midway/commit/4c7ed2ff2e7ccf10f88f62abad230f92f5e76b97))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# [3.0.0-beta.9](https://github.com/midwayjs/midway/compare/v3.0.0-beta.8...v3.0.0-beta.9) (2021-12-09)
|
|
39
|
+
|
|
40
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
# [3.0.0-beta.8](https://github.com/midwayjs/midway/compare/v3.0.0-beta.7...v3.0.0-beta.8) (2021-12-08)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
### Bug Fixes
|
|
50
|
+
|
|
51
|
+
* 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))
|
|
52
|
+
* typeorm EntityView missing connectionName ([#1403](https://github.com/midwayjs/midway/issues/1403)) ([30b2b37](https://github.com/midwayjs/midway/commit/30b2b3711db485cb85d825d56aeabf53b1374cae))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# [3.0.0-beta.7](https://github.com/midwayjs/midway/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2021-12-03)
|
|
59
|
+
|
|
60
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
### Bug Fixes
|
|
70
|
+
|
|
71
|
+
* class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
|
|
78
|
+
|
|
79
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
# [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### Features
|
|
89
|
+
|
|
90
|
+
* add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
|
|
91
|
+
* auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
|
|
92
|
+
* support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
### Features
|
|
102
|
+
|
|
103
|
+
* add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
|
|
110
|
+
|
|
111
|
+
**Note:** Version bump only for package @midwayjs/express
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
### Features
|
|
121
|
+
|
|
122
|
+
* add http2 support ([#1242](https://github.com/midwayjs/midway/issues/1242)) ([6cda27e](https://github.com/midwayjs/midway/commit/6cda27e9e22689e46ace543326b43ae21b134911))
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
6
128
|
## [2.12.3](https://github.com/midwayjs/midway/compare/v2.12.2...v2.12.3) (2021-08-09)
|
|
7
129
|
|
|
8
130
|
**Note:** Version bump only for package @midwayjs/express
|
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
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MidwayConfigService, MidwayDecoratorService } from '@midwayjs/core';
|
|
2
|
+
import { MidwayExpressFramework } from './framework';
|
|
3
|
+
export declare class ExpressConfiguration {
|
|
4
|
+
decoratorService: MidwayDecoratorService;
|
|
5
|
+
expressFramework: MidwayExpressFramework;
|
|
6
|
+
configService: MidwayConfigService;
|
|
7
|
+
init(): void;
|
|
8
|
+
onReady(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,84 @@
|
|
|
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.ExpressConfiguration = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
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");
|
|
20
|
+
let ExpressConfiguration = class ExpressConfiguration {
|
|
21
|
+
init() {
|
|
22
|
+
this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
|
|
23
|
+
return (0, core_1.extractExpressLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1], options.originArgs[2]);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
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
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
__decorate([
|
|
55
|
+
(0, decorator_1.Inject)(),
|
|
56
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
57
|
+
], ExpressConfiguration.prototype, "decoratorService", void 0);
|
|
58
|
+
__decorate([
|
|
59
|
+
(0, decorator_1.Inject)(),
|
|
60
|
+
__metadata("design:type", framework_1.MidwayExpressFramework)
|
|
61
|
+
], ExpressConfiguration.prototype, "expressFramework", void 0);
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, decorator_1.Inject)(),
|
|
64
|
+
__metadata("design:type", core_1.MidwayConfigService)
|
|
65
|
+
], ExpressConfiguration.prototype, "configService", void 0);
|
|
66
|
+
__decorate([
|
|
67
|
+
(0, decorator_1.Init)(),
|
|
68
|
+
__metadata("design:type", Function),
|
|
69
|
+
__metadata("design:paramtypes", []),
|
|
70
|
+
__metadata("design:returntype", void 0)
|
|
71
|
+
], ExpressConfiguration.prototype, "init", null);
|
|
72
|
+
ExpressConfiguration = __decorate([
|
|
73
|
+
(0, decorator_1.Configuration)({
|
|
74
|
+
namespace: 'express',
|
|
75
|
+
imports: [session],
|
|
76
|
+
importConfigs: [
|
|
77
|
+
{
|
|
78
|
+
default: DefaultConfig,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
})
|
|
82
|
+
], ExpressConfiguration);
|
|
83
|
+
exports.ExpressConfiguration = ExpressConfiguration;
|
|
84
|
+
//# sourceMappingURL=configuration.js.map
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,42 +1,33 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { BaseFramework, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
|
|
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
|
-
import type { IRouter, IRouterHandler } from 'express';
|
|
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
|
-
prioritySortRouters: Array<{
|
|
12
|
-
priority: number;
|
|
13
|
-
router: IRouter;
|
|
14
|
-
prefix: string;
|
|
15
|
-
}>;
|
|
16
9
|
private server;
|
|
10
|
+
private expressMiddlewareService;
|
|
11
|
+
configure(): IMidwayExpressConfigurationOptions;
|
|
17
12
|
applicationInitialize(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
|
|
18
|
-
protected afterContainerReady(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
|
|
19
13
|
run(): Promise<void>;
|
|
20
14
|
getFrameworkType(): MidwayFrameworkType;
|
|
21
15
|
/**
|
|
22
16
|
* wrap controller string to middleware function
|
|
23
|
-
* @param controllerMapping like FooController.index
|
|
24
|
-
* @param routeArgsInfo
|
|
25
|
-
* @param routerResponseData
|
|
26
17
|
*/
|
|
27
|
-
generateController(
|
|
18
|
+
protected generateController(routeInfo: RouterInfo): IRouterHandler<any>;
|
|
28
19
|
loadMidwayController(): Promise<void>;
|
|
29
|
-
generateMiddleware(middlewareId: string): Promise<import("./interface").Middleware>;
|
|
30
20
|
/**
|
|
31
|
-
* @param
|
|
21
|
+
* @param routerOptions
|
|
32
22
|
*/
|
|
33
23
|
protected createRouter(routerOptions: {
|
|
34
24
|
sensitive: any;
|
|
35
25
|
}): IRouter;
|
|
36
|
-
|
|
26
|
+
applyMiddleware<Response, NextFunction>(): Promise<MiddlewareRespond<IMidwayExpressContext, Response, NextFunction>>;
|
|
37
27
|
beforeStop(): Promise<void>;
|
|
38
28
|
getServer(): Server;
|
|
39
29
|
getFrameworkName(): string;
|
|
40
30
|
getDefaultContextLoggerClass(): typeof MidwayExpressContextLogger;
|
|
31
|
+
protected sendData(res: any, data: any): void;
|
|
41
32
|
}
|
|
42
33
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -1,54 +1,120 @@
|
|
|
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
|
+
};
|
|
2
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
9
|
exports.MidwayExpressFramework = void 0;
|
|
4
10
|
const core_1 = require("@midwayjs/core");
|
|
5
11
|
const decorator_1 = require("@midwayjs/decorator");
|
|
6
12
|
const express = require("express");
|
|
7
13
|
const logger_1 = require("./logger");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
const middlewareService_1 = require("./middlewareService");
|
|
15
|
+
const util_1 = require("util");
|
|
16
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
17
|
+
let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFramework {
|
|
18
|
+
configure() {
|
|
19
|
+
return this.configService.getConfiguration('express');
|
|
13
20
|
}
|
|
14
21
|
async applicationInitialize(options) {
|
|
22
|
+
this.expressMiddlewareService = await this.applicationContext.getAsync(middlewareService_1.MidwayExpressMiddlewareService, [this.applicationContext]);
|
|
23
|
+
debug('[express]: create express app');
|
|
15
24
|
this.app = express();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return this.generateController(controllerMapping);
|
|
19
|
-
},
|
|
20
|
-
generateMiddleware: async (middlewareId) => {
|
|
21
|
-
return this.generateMiddleware(middlewareId);
|
|
22
|
-
},
|
|
23
|
-
});
|
|
25
|
+
debug('[express]: use root middleware');
|
|
26
|
+
// use root middleware
|
|
24
27
|
this.app.use((req, res, next) => {
|
|
25
|
-
const ctx =
|
|
28
|
+
const ctx = req;
|
|
26
29
|
this.app.createAnonymousContext(ctx);
|
|
27
30
|
req.requestContext = ctx.requestContext;
|
|
28
31
|
ctx.requestContext.registerObject('req', req);
|
|
29
32
|
ctx.requestContext.registerObject('res', res);
|
|
30
33
|
next();
|
|
31
34
|
});
|
|
35
|
+
this.defineApplicationProperties();
|
|
36
|
+
// hack use method
|
|
37
|
+
this.app.originUse = this.app.use;
|
|
38
|
+
this.app.use = this.app.useMiddleware;
|
|
39
|
+
}
|
|
40
|
+
async run() {
|
|
41
|
+
var _a;
|
|
42
|
+
debug(`[express]: use middlewares = "${this.getMiddleware().getNames()}"`);
|
|
43
|
+
// restore use method
|
|
44
|
+
this.app.use = this.app.originUse;
|
|
45
|
+
// use global middleware
|
|
46
|
+
const globalMiddleware = await this.applyMiddleware();
|
|
47
|
+
debug('[express]: use and apply all framework and global middleware');
|
|
48
|
+
this.app.use(globalMiddleware);
|
|
49
|
+
debug('[express]: use user router middleware');
|
|
50
|
+
// load controller
|
|
51
|
+
await this.loadMidwayController();
|
|
52
|
+
debug('[express]: use global error handler middleware');
|
|
53
|
+
// use global error handler
|
|
54
|
+
this.app.use((err, req, res, next) => {
|
|
55
|
+
this.filterManager
|
|
56
|
+
.runErrorFilter(err, req, res, next)
|
|
57
|
+
.then(data => {
|
|
58
|
+
var _a, _b;
|
|
59
|
+
const { result, error } = data;
|
|
60
|
+
if (error) {
|
|
61
|
+
const status = (_b = (_a = error.status) !== null && _a !== void 0 ? _a : res.statusCode) !== null && _b !== void 0 ? _b : 500;
|
|
62
|
+
// 5xx
|
|
63
|
+
if (status >= 500) {
|
|
64
|
+
try {
|
|
65
|
+
req.logger.error(err);
|
|
66
|
+
}
|
|
67
|
+
catch (ex) {
|
|
68
|
+
this.logger.error(err);
|
|
69
|
+
this.logger.error(ex);
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
// 4xx
|
|
74
|
+
try {
|
|
75
|
+
req.logger.warn(err);
|
|
76
|
+
}
|
|
77
|
+
catch (ex) {
|
|
78
|
+
this.logger.warn(err);
|
|
79
|
+
this.logger.error(ex);
|
|
80
|
+
}
|
|
81
|
+
res.status(status);
|
|
82
|
+
next(error);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.sendData(res, result);
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.catch(err => {
|
|
89
|
+
next(err);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
32
92
|
// https config
|
|
33
93
|
if (this.configurationOptions.key && this.configurationOptions.cert) {
|
|
34
94
|
this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
|
|
35
95
|
this.configurationOptions.cert = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.cert);
|
|
36
96
|
this.configurationOptions.ca = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.ca);
|
|
37
|
-
|
|
97
|
+
if (this.configurationOptions.http2) {
|
|
98
|
+
this.server = require('http2').createSecureServer(this.configurationOptions, this.app);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
this.server = require('https').createServer(this.configurationOptions, this.app);
|
|
102
|
+
}
|
|
38
103
|
}
|
|
39
104
|
else {
|
|
40
|
-
|
|
105
|
+
if (this.configurationOptions.http2) {
|
|
106
|
+
this.server = require('http2').createServer(this.app);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
this.server = require('http').createServer(this.app);
|
|
110
|
+
}
|
|
41
111
|
}
|
|
42
112
|
// register httpServer to applicationContext
|
|
43
113
|
this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
await this.loadMidwayController();
|
|
47
|
-
}
|
|
48
|
-
async run() {
|
|
49
|
-
if (this.configurationOptions.port) {
|
|
114
|
+
const customPort = (_a = process.env.MIDWAY_HTTP_PORT) !== null && _a !== void 0 ? _a : this.configurationOptions.port;
|
|
115
|
+
if (customPort) {
|
|
50
116
|
new Promise(resolve => {
|
|
51
|
-
const args = [
|
|
117
|
+
const args = [customPort];
|
|
52
118
|
if (this.configurationOptions.hostname) {
|
|
53
119
|
args.push(this.configurationOptions.hostname);
|
|
54
120
|
}
|
|
@@ -56,6 +122,7 @@ class MidwayExpressFramework extends core_1.BaseFramework {
|
|
|
56
122
|
resolve();
|
|
57
123
|
});
|
|
58
124
|
this.server.listen(...args);
|
|
125
|
+
process.env.MIDWAY_HTTP_PORT = String(customPort);
|
|
59
126
|
});
|
|
60
127
|
}
|
|
61
128
|
}
|
|
@@ -64,22 +131,11 @@ class MidwayExpressFramework extends core_1.BaseFramework {
|
|
|
64
131
|
}
|
|
65
132
|
/**
|
|
66
133
|
* wrap controller string to middleware function
|
|
67
|
-
* @param controllerMapping like FooController.index
|
|
68
|
-
* @param routeArgsInfo
|
|
69
|
-
* @param routerResponseData
|
|
70
134
|
*/
|
|
71
|
-
generateController(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
if (Array.isArray(routeArgsInfo)) {
|
|
76
|
-
await Promise.all(routeArgsInfo.map(async ({ index, type, propertyData }) => {
|
|
77
|
-
args[index] = await core_1.extractExpressLikeValue(type, propertyData)(req, res, next);
|
|
78
|
-
}));
|
|
79
|
-
}
|
|
80
|
-
const controller = await req.requestContext.getAsync(controllerId);
|
|
81
|
-
// eslint-disable-next-line prefer-spread
|
|
82
|
-
const result = await controller[methodName].apply(controller, args);
|
|
135
|
+
generateController(routeInfo) {
|
|
136
|
+
return (0, middlewareService_1.wrapAsyncHandler)(async (req, res, next) => {
|
|
137
|
+
const controller = await req.requestContext.getAsync(routeInfo.id);
|
|
138
|
+
const result = await controller[routeInfo.method].call(controller, req, res, next);
|
|
83
139
|
if (res.headersSent) {
|
|
84
140
|
// return when response send
|
|
85
141
|
return;
|
|
@@ -88,8 +144,9 @@ class MidwayExpressFramework extends core_1.BaseFramework {
|
|
|
88
144
|
res.status(204);
|
|
89
145
|
}
|
|
90
146
|
// implement response decorator
|
|
91
|
-
if (Array.isArray(
|
|
92
|
-
|
|
147
|
+
if (Array.isArray(routeInfo.responseMetadata) &&
|
|
148
|
+
routeInfo.responseMetadata.length) {
|
|
149
|
+
for (const routerRes of routeInfo.responseMetadata) {
|
|
93
150
|
switch (routerRes.type) {
|
|
94
151
|
case decorator_1.WEB_RESPONSE_HTTP_CODE:
|
|
95
152
|
res.status(routerRes.code);
|
|
@@ -106,66 +163,61 @@ class MidwayExpressFramework extends core_1.BaseFramework {
|
|
|
106
163
|
}
|
|
107
164
|
}
|
|
108
165
|
}
|
|
109
|
-
|
|
110
|
-
|
|
166
|
+
const { result: returnValue, error } = await this.filterManager.runResultFilter(result, req, res, next);
|
|
167
|
+
if (error) {
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
this.sendData(res, returnValue);
|
|
171
|
+
});
|
|
111
172
|
}
|
|
112
173
|
async loadMidwayController() {
|
|
113
|
-
|
|
174
|
+
var _a, _b;
|
|
175
|
+
const collector = new core_1.WebRouterCollector('', {
|
|
176
|
+
globalPrefix: this.configurationOptions.globalPrefix,
|
|
177
|
+
});
|
|
114
178
|
const routerTable = await collector.getRouterTable();
|
|
115
179
|
const routerList = await collector.getRoutePriorityList();
|
|
116
180
|
for (const routerInfo of routerList) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
throw new Error(`Controller identifier [${providerId}] already exists!`);
|
|
121
|
-
}
|
|
122
|
-
this.controllerIds.push(providerId);
|
|
123
|
-
this.logger.debug(`Load Controller "${providerId}", prefix=${routerInfo.prefix}`);
|
|
181
|
+
// bind controller first
|
|
182
|
+
this.getApplicationContext().bindClass(routerInfo.routerModule);
|
|
183
|
+
this.logger.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
124
184
|
// new router
|
|
125
185
|
const newRouter = this.createRouter(routerInfo.routerOptions);
|
|
186
|
+
routerInfo.middleware = (_a = routerInfo.middleware) !== null && _a !== void 0 ? _a : [];
|
|
126
187
|
// add router middleware
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
188
|
+
if (routerInfo.middleware.length) {
|
|
189
|
+
const routerMiddlewareFn = await this.expressMiddlewareService.compose(routerInfo.middleware, this.app);
|
|
190
|
+
newRouter.use(routerMiddlewareFn);
|
|
191
|
+
}
|
|
130
192
|
// add route
|
|
131
193
|
const routes = routerTable.get(routerInfo.prefix);
|
|
132
194
|
for (const routeInfo of routes) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
195
|
+
const routeMiddlewareList = [];
|
|
196
|
+
// routeInfo middleware
|
|
197
|
+
routeInfo.middleware = (_b = routeInfo.middleware) !== null && _b !== void 0 ? _b : [];
|
|
198
|
+
if (routeInfo.middleware.length) {
|
|
199
|
+
const routeMiddlewareFn = await this.expressMiddlewareService.compose(routeInfo.middleware, this.app);
|
|
200
|
+
routeMiddlewareList.push(routeMiddlewareFn);
|
|
201
|
+
}
|
|
137
202
|
this.logger.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
138
203
|
// apply controller from request context
|
|
139
|
-
newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, this.generateController(routeInfo
|
|
204
|
+
newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
|
|
140
205
|
}
|
|
141
206
|
this.app.use(routerInfo.prefix, newRouter);
|
|
142
207
|
}
|
|
143
208
|
}
|
|
144
|
-
async generateMiddleware(middlewareId) {
|
|
145
|
-
const mwIns = await this.getApplicationContext().getAsync(middlewareId);
|
|
146
|
-
return mwIns.resolve();
|
|
147
|
-
}
|
|
148
209
|
/**
|
|
149
|
-
* @param
|
|
210
|
+
* @param routerOptions
|
|
150
211
|
*/
|
|
151
212
|
createRouter(routerOptions) {
|
|
152
213
|
return express.Router({ caseSensitive: routerOptions.sensitive });
|
|
153
214
|
}
|
|
154
|
-
async
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
// web function middleware
|
|
159
|
-
handlerCallback(middleware);
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
const middlewareImpl = await this.getApplicationContext().getAsync(middleware);
|
|
163
|
-
if (middlewareImpl && typeof middlewareImpl.resolve === 'function') {
|
|
164
|
-
handlerCallback(middlewareImpl.resolve());
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
215
|
+
async applyMiddleware() {
|
|
216
|
+
if (!this.composeMiddleware) {
|
|
217
|
+
this.composeMiddleware = await this.expressMiddlewareService.compose(this.middlewareManager, this.app);
|
|
218
|
+
await this.filterManager.init(this.applicationContext);
|
|
168
219
|
}
|
|
220
|
+
return this.composeMiddleware;
|
|
169
221
|
}
|
|
170
222
|
async beforeStop() {
|
|
171
223
|
this.server.close();
|
|
@@ -179,6 +231,17 @@ class MidwayExpressFramework extends core_1.BaseFramework {
|
|
|
179
231
|
getDefaultContextLoggerClass() {
|
|
180
232
|
return logger_1.MidwayExpressContextLogger;
|
|
181
233
|
}
|
|
182
|
-
|
|
234
|
+
sendData(res, data) {
|
|
235
|
+
if (typeof data === 'number') {
|
|
236
|
+
res.status(res.statusCode).send('' + data);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
res.status(res.statusCode).send(data);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
MidwayExpressFramework = __decorate([
|
|
244
|
+
(0, decorator_1.Framework)()
|
|
245
|
+
], MidwayExpressFramework);
|
|
183
246
|
exports.MidwayExpressFramework = MidwayExpressFramework;
|
|
184
247
|
//# sourceMappingURL=framework.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export * from './interface';
|
|
2
2
|
export { MidwayExpressFramework as Framework } from './framework';
|
|
3
|
+
export { ExpressConfiguration as Configuration } from './configuration';
|
|
4
|
+
export * from './middlewareService';
|
|
5
|
+
export * from './logger';
|
|
3
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -10,8 +10,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.Framework = void 0;
|
|
13
|
+
exports.Configuration = exports.Framework = void 0;
|
|
14
14
|
__exportStar(require("./interface"), exports);
|
|
15
15
|
var framework_1 = require("./framework");
|
|
16
16
|
Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.MidwayExpressFramework; } });
|
|
17
|
+
var configuration_1 = require("./configuration");
|
|
18
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.ExpressConfiguration; } });
|
|
19
|
+
__exportStar(require("./middlewareService"), exports);
|
|
20
|
+
__exportStar(require("./logger"), exports);
|
|
17
21
|
//# sourceMappingURL=index.js.map
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { IConfigurationOptions, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
3
|
-
import { Application as ExpressApplication,
|
|
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
|
+
export declare type IMidwayExpressContext = IMidwayContext<Request>;
|
|
5
6
|
/**
|
|
6
|
-
* @deprecated use
|
|
7
|
+
* @deprecated use IMidwayExpressContext
|
|
7
8
|
*/
|
|
8
|
-
export declare type IMidwayExpressRequest =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
export declare type IMidwayExpressApplication = IMidwayApplication<IMidwayExpressContext, ExpressApplication & {
|
|
22
|
-
generateController(controllerMapping: string, routeArgsInfo?: RouterParamValue[], routerResponseData?: any[]): Middleware;
|
|
23
|
-
generateMiddleware(middlewareId: string): Promise<Middleware>;
|
|
24
|
-
}>;
|
|
9
|
+
export declare type IMidwayExpressRequest = IMidwayExpressContext;
|
|
10
|
+
export declare type IMidwayExpressMiddleware = IMiddleware<IMidwayExpressContext, Response, ExpressNextFunction>;
|
|
11
|
+
export interface IMidwayExpressApplication extends IMidwayApplication<IMidwayExpressContext, ExpressApplication> {
|
|
12
|
+
/**
|
|
13
|
+
* add global middleware to app
|
|
14
|
+
* @param Middleware
|
|
15
|
+
*/
|
|
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
|
+
}
|
|
25
22
|
export interface IMidwayExpressConfigurationOptions extends IConfigurationOptions {
|
|
23
|
+
/**
|
|
24
|
+
* session or cookie secret key
|
|
25
|
+
*/
|
|
26
|
+
keys?: string | string[];
|
|
26
27
|
/**
|
|
27
28
|
* application http port
|
|
28
29
|
*/
|
|
@@ -43,13 +44,37 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
|
|
|
43
44
|
* https ca
|
|
44
45
|
*/
|
|
45
46
|
ca?: string | Buffer | Array<string | Buffer>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
/**
|
|
48
|
+
* http2 support
|
|
49
|
+
*/
|
|
50
|
+
http2?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* http global prefix
|
|
53
|
+
*/
|
|
54
|
+
globalPrefix?: string;
|
|
51
55
|
}
|
|
52
56
|
export declare type Application = IMidwayExpressApplication;
|
|
57
|
+
export declare type NextFunction = ExpressNextFunction;
|
|
53
58
|
export interface Context extends IMidwayExpressContext {
|
|
54
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
|
+
}
|
|
55
80
|
//# sourceMappingURL=interface.d.ts.map
|
package/dist/logger.js
CHANGED
|
@@ -5,7 +5,7 @@ const logger_1 = require("@midwayjs/logger");
|
|
|
5
5
|
class MidwayExpressContextLogger extends logger_1.MidwayContextLogger {
|
|
6
6
|
formatContextLabel() {
|
|
7
7
|
var _a;
|
|
8
|
-
const req = this.ctx
|
|
8
|
+
const req = this.ctx;
|
|
9
9
|
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
10
10
|
const userId = ((_a = req === null || req === void 0 ? void 0 : req['session']) === null || _a === void 0 ? void 0 : _a['userId']) || '-';
|
|
11
11
|
const traceId = '-';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IMidwayContainer, CommonMiddleware, FunctionMiddleware } from '@midwayjs/core';
|
|
2
|
+
import { IMidwayExpressContext, Application } from './interface';
|
|
3
|
+
import { NextFunction, Response } from 'express';
|
|
4
|
+
export declare function wrapAsyncHandler(fn: any): any;
|
|
5
|
+
export declare class MidwayExpressMiddlewareService {
|
|
6
|
+
readonly applicationContext: IMidwayContainer;
|
|
7
|
+
constructor(applicationContext: IMidwayContainer);
|
|
8
|
+
compose(middleware: Array<CommonMiddleware<IMidwayExpressContext, Response, NextFunction> | string>, app: Application, name?: string): Promise<{
|
|
9
|
+
(req: IMidwayExpressContext, res: Response, nextFunction: NextFunction): any;
|
|
10
|
+
_name: string;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
export declare function wrapMiddleware(mw: FunctionMiddleware<any, any, any>, options: any): (context: any, next: any, options?: any) => any;
|
|
14
|
+
//# sourceMappingURL=middlewareService.d.ts.map
|
|
@@ -0,0 +1,134 @@
|
|
|
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.wrapMiddleware = exports.MidwayExpressMiddlewareService = exports.wrapAsyncHandler = void 0;
|
|
13
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
+
const core_1 = require("@midwayjs/core");
|
|
15
|
+
function wrapAsyncHandler(fn) {
|
|
16
|
+
if ((0, decorator_1.isAsyncFunction)(fn)) {
|
|
17
|
+
return (req, res, next) => {
|
|
18
|
+
return fn(req, res, next).catch(err => {
|
|
19
|
+
next(err);
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return fn;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.wrapAsyncHandler = wrapAsyncHandler;
|
|
28
|
+
let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
29
|
+
constructor(applicationContext) {
|
|
30
|
+
this.applicationContext = applicationContext;
|
|
31
|
+
}
|
|
32
|
+
async compose(middleware, app, name) {
|
|
33
|
+
if (!Array.isArray(middleware)) {
|
|
34
|
+
throw new core_1.MidwayCommonError('Middleware stack must be an array');
|
|
35
|
+
}
|
|
36
|
+
const newMiddlewareArr = [];
|
|
37
|
+
for (let fn of middleware) {
|
|
38
|
+
if ((0, decorator_1.isClass)(fn) || typeof fn === 'string') {
|
|
39
|
+
if (typeof fn === 'string' &&
|
|
40
|
+
!this.applicationContext.hasDefinition(fn)) {
|
|
41
|
+
throw new core_1.MidwayCommonError('Middleware definition not found in midway container');
|
|
42
|
+
}
|
|
43
|
+
const classMiddleware = await this.applicationContext.getAsync(fn);
|
|
44
|
+
if (classMiddleware) {
|
|
45
|
+
fn = classMiddleware.resolve(app);
|
|
46
|
+
// wrap async middleware
|
|
47
|
+
fn = wrapAsyncHandler(fn);
|
|
48
|
+
if (!classMiddleware.match && !classMiddleware.ignore) {
|
|
49
|
+
fn._name = classMiddleware.constructor.name;
|
|
50
|
+
// just got fn
|
|
51
|
+
newMiddlewareArr.push(fn);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// wrap ignore and match
|
|
55
|
+
const mw = fn;
|
|
56
|
+
const match = (0, core_1.pathMatching)({
|
|
57
|
+
match: classMiddleware.match,
|
|
58
|
+
ignore: classMiddleware.ignore,
|
|
59
|
+
});
|
|
60
|
+
fn = (req, res, next) => {
|
|
61
|
+
if (!match(req))
|
|
62
|
+
return next();
|
|
63
|
+
return mw(req, res, next);
|
|
64
|
+
};
|
|
65
|
+
fn._name = classMiddleware.constructor.name;
|
|
66
|
+
newMiddlewareArr.push(fn);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw new core_1.MidwayCommonError('Middleware must have resolve method!');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// wrap async middleware
|
|
75
|
+
fn = wrapAsyncHandler(fn);
|
|
76
|
+
newMiddlewareArr.push(fn);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const composeFn = (req, res, nextFunction) => {
|
|
80
|
+
let index = -1;
|
|
81
|
+
function dispatch(pos, err) {
|
|
82
|
+
const handler = newMiddlewareArr[pos];
|
|
83
|
+
index = pos;
|
|
84
|
+
if (err || index === newMiddlewareArr.length) {
|
|
85
|
+
return nextFunction(err);
|
|
86
|
+
}
|
|
87
|
+
function next(err) {
|
|
88
|
+
if (pos < index) {
|
|
89
|
+
throw new TypeError('`next()` called multiple times');
|
|
90
|
+
}
|
|
91
|
+
return dispatch(pos + 1, err);
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
return handler(req, res, next);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
// Avoid future errors that could diverge stack execution.
|
|
98
|
+
if (index > pos)
|
|
99
|
+
throw err;
|
|
100
|
+
return next(err);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return dispatch(0);
|
|
104
|
+
};
|
|
105
|
+
if (name) {
|
|
106
|
+
composeFn._name = name;
|
|
107
|
+
}
|
|
108
|
+
return composeFn;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
MidwayExpressMiddlewareService = __decorate([
|
|
112
|
+
(0, decorator_1.Provide)(),
|
|
113
|
+
(0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton),
|
|
114
|
+
__metadata("design:paramtypes", [Object])
|
|
115
|
+
], MidwayExpressMiddlewareService);
|
|
116
|
+
exports.MidwayExpressMiddlewareService = MidwayExpressMiddlewareService;
|
|
117
|
+
function wrapMiddleware(mw, options) {
|
|
118
|
+
// support options.enable
|
|
119
|
+
if (options.enable === false)
|
|
120
|
+
return null;
|
|
121
|
+
// support options.match and options.ignore
|
|
122
|
+
if (!options.match && !options.ignore)
|
|
123
|
+
return mw;
|
|
124
|
+
const match = (0, core_1.pathMatching)(options);
|
|
125
|
+
const fn = (req, res, next) => {
|
|
126
|
+
if (!match(req))
|
|
127
|
+
return next();
|
|
128
|
+
return mw(req, res, next);
|
|
129
|
+
};
|
|
130
|
+
fn._name = mw._name + 'middlewareWrapper';
|
|
131
|
+
return fn;
|
|
132
|
+
}
|
|
133
|
+
exports.wrapMiddleware = wrapMiddleware;
|
|
134
|
+
//# sourceMappingURL=middlewareService.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/express",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-beta.12",
|
|
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-
|
|
27
|
-
"@midwayjs/
|
|
26
|
+
"@midwayjs/decorator": "^3.0.0-beta.12",
|
|
27
|
+
"@midwayjs/logger": "^3.0.0-beta.12",
|
|
28
|
+
"@midwayjs/mock": "^3.0.0-beta.12",
|
|
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-
|
|
33
|
-
"@midwayjs/
|
|
34
|
+
"@midwayjs/core": "^3.0.0-beta.12",
|
|
35
|
+
"@midwayjs/express-session": "^3.0.0-beta.12",
|
|
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>",
|
|
@@ -38,5 +42,8 @@
|
|
|
38
42
|
"type": "git",
|
|
39
43
|
"url": "http://github.com/midwayjs/midway.git"
|
|
40
44
|
},
|
|
41
|
-
"
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=12"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "1c46e53eb934248007eeb7fe3920f5ac24e272c6"
|
|
42
49
|
}
|