@midwayjs/express 3.0.0-beta.9 → 3.0.0
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/README.md +2 -2
- package/dist/config/config.default.d.ts +22 -0
- package/dist/config/config.default.js +27 -0
- package/dist/configuration.d.ts +4 -1
- package/dist/configuration.js +46 -1
- package/dist/framework.d.ts +8 -5
- package/dist/framework.js +87 -26
- package/dist/interface.d.ts +37 -11
- package/dist/logger.d.ts +2 -2
- package/dist/middlewareService.d.ts +4 -3
- package/dist/middlewareService.js +50 -10
- package/dist/util.d.ts +2 -0
- package/dist/util.js +13 -0
- package/package.json +15 -11
- package/CHANGELOG.md +0 -993
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# midway
|
|
1
|
+
# midway for express
|
|
2
2
|
|
|
3
|
-
[](http://packagequality.com/#?package=@midwayjs/express)
|
|
4
4
|
[](https://github.com/midwayjs/midway/pulls)
|
|
5
5
|
|
|
6
6
|
this is a sub package for midway.
|
|
@@ -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,6 +1,9 @@
|
|
|
1
|
-
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
1
|
+
import { MidwayConfigService, MidwayDecoratorService } from '@midwayjs/core';
|
|
2
|
+
import { MidwayExpressFramework } from './framework';
|
|
2
3
|
export declare class ExpressConfiguration {
|
|
3
4
|
decoratorService: MidwayDecoratorService;
|
|
5
|
+
expressFramework: MidwayExpressFramework;
|
|
6
|
+
configService: MidwayConfigService;
|
|
4
7
|
init(): void;
|
|
5
8
|
onReady(): Promise<void>;
|
|
6
9
|
}
|
package/dist/configuration.js
CHANGED
|
@@ -12,18 +12,57 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.ExpressConfiguration = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
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");
|
|
15
20
|
let ExpressConfiguration = class ExpressConfiguration {
|
|
16
21
|
init() {
|
|
17
22
|
this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
|
|
18
23
|
return (0, core_1.extractExpressLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1], options.originArgs[2]);
|
|
19
24
|
});
|
|
20
25
|
}
|
|
21
|
-
async onReady() {
|
|
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
|
+
}
|
|
22
53
|
};
|
|
23
54
|
__decorate([
|
|
24
55
|
(0, decorator_1.Inject)(),
|
|
25
56
|
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
26
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);
|
|
27
66
|
__decorate([
|
|
28
67
|
(0, decorator_1.Init)(),
|
|
29
68
|
__metadata("design:type", Function),
|
|
@@ -33,6 +72,12 @@ __decorate([
|
|
|
33
72
|
ExpressConfiguration = __decorate([
|
|
34
73
|
(0, decorator_1.Configuration)({
|
|
35
74
|
namespace: 'express',
|
|
75
|
+
imports: [session],
|
|
76
|
+
importConfigs: [
|
|
77
|
+
{
|
|
78
|
+
default: DefaultConfig,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
36
81
|
})
|
|
37
82
|
], ExpressConfiguration);
|
|
38
83
|
exports.ExpressConfiguration = ExpressConfiguration;
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { BaseFramework, IMidwayBootstrapOptions, MiddlewareRespond, MidwayFrameworkType, RouterInfo } from '@midwayjs/core';
|
|
3
|
-
import { IMidwayExpressApplication, IMidwayExpressConfigurationOptions,
|
|
3
|
+
import { IMidwayExpressApplication, IMidwayExpressConfigurationOptions, Context } from './interface';
|
|
4
4
|
import type { IRouter, IRouterHandler, Response, NextFunction } from 'express';
|
|
5
5
|
import { Server } from 'net';
|
|
6
6
|
import { MidwayExpressContextLogger } from './logger';
|
|
7
|
-
export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpressApplication,
|
|
7
|
+
export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpressApplication, Context, IMidwayExpressConfigurationOptions, Response, NextFunction> {
|
|
8
8
|
app: IMidwayExpressApplication;
|
|
9
9
|
private server;
|
|
10
10
|
private expressMiddlewareService;
|
|
@@ -16,17 +16,20 @@ export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpress
|
|
|
16
16
|
* wrap controller string to middleware function
|
|
17
17
|
*/
|
|
18
18
|
protected generateController(routeInfo: RouterInfo): IRouterHandler<any>;
|
|
19
|
-
loadMidwayController(): Promise<
|
|
19
|
+
loadMidwayController(): Promise<Array<{
|
|
20
|
+
prefix: string;
|
|
21
|
+
middleware: any;
|
|
22
|
+
}>>;
|
|
20
23
|
/**
|
|
21
24
|
* @param routerOptions
|
|
22
25
|
*/
|
|
23
26
|
protected createRouter(routerOptions: {
|
|
24
27
|
sensitive: any;
|
|
25
28
|
}): IRouter;
|
|
26
|
-
|
|
27
|
-
getMiddleware<Response, NextFunction>(): Promise<MiddlewareRespond<IMidwayExpressContext, Response, NextFunction>>;
|
|
29
|
+
applyMiddleware<Response, NextFunction>(): Promise<MiddlewareRespond<Context, Response, NextFunction>>;
|
|
28
30
|
beforeStop(): Promise<void>;
|
|
29
31
|
getServer(): Server;
|
|
32
|
+
getPort(): string;
|
|
30
33
|
getFrameworkName(): string;
|
|
31
34
|
getDefaultContextLoggerClass(): typeof MidwayExpressContextLogger;
|
|
32
35
|
}
|
package/dist/framework.js
CHANGED
|
@@ -12,13 +12,19 @@ const decorator_1 = require("@midwayjs/decorator");
|
|
|
12
12
|
const express = require("express");
|
|
13
13
|
const logger_1 = require("./logger");
|
|
14
14
|
const middlewareService_1 = require("./middlewareService");
|
|
15
|
+
const util_1 = require("util");
|
|
16
|
+
const util_2 = require("./util");
|
|
17
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
15
18
|
let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFramework {
|
|
16
19
|
configure() {
|
|
17
20
|
return this.configService.getConfiguration('express');
|
|
18
21
|
}
|
|
19
22
|
async applicationInitialize(options) {
|
|
20
23
|
this.expressMiddlewareService = await this.applicationContext.getAsync(middlewareService_1.MidwayExpressMiddlewareService, [this.applicationContext]);
|
|
24
|
+
debug('[express]: create express app');
|
|
21
25
|
this.app = express();
|
|
26
|
+
debug('[express]: use root middleware');
|
|
27
|
+
// use root middleware
|
|
22
28
|
this.app.use((req, res, next) => {
|
|
23
29
|
const ctx = req;
|
|
24
30
|
this.app.createAnonymousContext(ctx);
|
|
@@ -27,25 +33,71 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
27
33
|
ctx.requestContext.registerObject('res', res);
|
|
28
34
|
next();
|
|
29
35
|
});
|
|
36
|
+
this.defineApplicationProperties();
|
|
37
|
+
// hack use method
|
|
38
|
+
this.app.originUse = this.app.use;
|
|
39
|
+
this.app.use = this.app.useMiddleware;
|
|
30
40
|
}
|
|
31
41
|
async run() {
|
|
32
42
|
var _a;
|
|
43
|
+
debug(`[express]: use middlewares = "${this.getMiddleware().getNames()}"`);
|
|
44
|
+
// restore use method
|
|
45
|
+
this.app.use = this.app.originUse;
|
|
46
|
+
debug('[express]: use user router middleware');
|
|
47
|
+
// load controller,must apply router filter here
|
|
48
|
+
const routerMiddlewares = await this.loadMidwayController();
|
|
33
49
|
// use global middleware
|
|
34
|
-
const globalMiddleware = await this.
|
|
50
|
+
const globalMiddleware = await this.applyMiddleware();
|
|
51
|
+
debug('[express]: use and apply all framework and global middleware');
|
|
35
52
|
this.app.use(globalMiddleware);
|
|
36
|
-
// load
|
|
37
|
-
|
|
53
|
+
// load router after global middleware
|
|
54
|
+
for (const info of routerMiddlewares) {
|
|
55
|
+
this.app.use(info.prefix, info.middleware);
|
|
56
|
+
}
|
|
57
|
+
debug('[express]: use 404 not found middleware');
|
|
58
|
+
// eslint-disable-next-line
|
|
59
|
+
this.app.use(function notFound(req, res, next) {
|
|
60
|
+
next(new core_1.httpError.NotFoundError());
|
|
61
|
+
});
|
|
62
|
+
debug('[express]: use global error handler middleware');
|
|
38
63
|
// use global error handler
|
|
39
|
-
this.app.use(
|
|
40
|
-
|
|
41
|
-
|
|
64
|
+
this.app.use((err, req, res, next) => {
|
|
65
|
+
this.filterManager
|
|
66
|
+
.runErrorFilter(err, req, res, next)
|
|
67
|
+
.then(data => {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
const { result, error } = data;
|
|
42
70
|
if (error) {
|
|
71
|
+
const status = (_b = (_a = error.status) !== null && _a !== void 0 ? _a : res.statusCode) !== null && _b !== void 0 ? _b : 500;
|
|
72
|
+
// 5xx
|
|
73
|
+
if (status >= 500) {
|
|
74
|
+
try {
|
|
75
|
+
req.logger.error(err);
|
|
76
|
+
}
|
|
77
|
+
catch (ex) {
|
|
78
|
+
this.logger.error(err);
|
|
79
|
+
this.logger.error(ex);
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// 4xx
|
|
84
|
+
try {
|
|
85
|
+
req.logger.warn(err);
|
|
86
|
+
}
|
|
87
|
+
catch (ex) {
|
|
88
|
+
this.logger.warn(err);
|
|
89
|
+
this.logger.error(ex);
|
|
90
|
+
}
|
|
91
|
+
res.status(status);
|
|
43
92
|
next(error);
|
|
44
93
|
}
|
|
45
94
|
else {
|
|
46
|
-
|
|
95
|
+
(0, util_2.sendData)(res, result);
|
|
47
96
|
}
|
|
48
|
-
}
|
|
97
|
+
})
|
|
98
|
+
.catch(err => {
|
|
99
|
+
next(err);
|
|
100
|
+
});
|
|
49
101
|
});
|
|
50
102
|
// https config
|
|
51
103
|
if (this.configurationOptions.key && this.configurationOptions.cert) {
|
|
@@ -72,7 +124,7 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
72
124
|
const customPort = (_a = process.env.MIDWAY_HTTP_PORT) !== null && _a !== void 0 ? _a : this.configurationOptions.port;
|
|
73
125
|
if (customPort) {
|
|
74
126
|
new Promise(resolve => {
|
|
75
|
-
const args = [
|
|
127
|
+
const args = [customPort];
|
|
76
128
|
if (this.configurationOptions.hostname) {
|
|
77
129
|
args.push(this.configurationOptions.hostname);
|
|
78
130
|
}
|
|
@@ -91,7 +143,7 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
91
143
|
* wrap controller string to middleware function
|
|
92
144
|
*/
|
|
93
145
|
generateController(routeInfo) {
|
|
94
|
-
return async (req, res, next) => {
|
|
146
|
+
return (0, middlewareService_1.wrapAsyncHandler)(async (req, res, next) => {
|
|
95
147
|
const controller = await req.requestContext.getAsync(routeInfo.id);
|
|
96
148
|
const result = await controller[routeInfo.method].call(controller, req, res, next);
|
|
97
149
|
if (res.headersSent) {
|
|
@@ -125,39 +177,49 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
125
177
|
if (error) {
|
|
126
178
|
throw error;
|
|
127
179
|
}
|
|
128
|
-
|
|
129
|
-
};
|
|
180
|
+
(0, util_2.sendData)(res, returnValue);
|
|
181
|
+
});
|
|
130
182
|
}
|
|
131
183
|
async loadMidwayController() {
|
|
184
|
+
var _a, _b;
|
|
132
185
|
const collector = new core_1.WebRouterCollector('', {
|
|
133
186
|
globalPrefix: this.configurationOptions.globalPrefix,
|
|
134
187
|
});
|
|
135
188
|
const routerTable = await collector.getRouterTable();
|
|
136
189
|
const routerList = await collector.getRoutePriorityList();
|
|
190
|
+
const routerMiddlewares = [];
|
|
137
191
|
for (const routerInfo of routerList) {
|
|
138
192
|
// bind controller first
|
|
139
193
|
this.getApplicationContext().bindClass(routerInfo.routerModule);
|
|
140
194
|
this.logger.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
141
195
|
// new router
|
|
142
196
|
const newRouter = this.createRouter(routerInfo.routerOptions);
|
|
197
|
+
routerInfo.middleware = (_a = routerInfo.middleware) !== null && _a !== void 0 ? _a : [];
|
|
143
198
|
// add router middleware
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
199
|
+
if (routerInfo.middleware.length) {
|
|
200
|
+
const routerMiddlewareFn = await this.expressMiddlewareService.compose(routerInfo.middleware, this.app);
|
|
201
|
+
newRouter.use(routerMiddlewareFn);
|
|
202
|
+
}
|
|
147
203
|
// add route
|
|
148
204
|
const routes = routerTable.get(routerInfo.prefix);
|
|
149
205
|
for (const routeInfo of routes) {
|
|
150
206
|
const routeMiddlewareList = [];
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
207
|
+
// routeInfo middleware
|
|
208
|
+
routeInfo.middleware = (_b = routeInfo.middleware) !== null && _b !== void 0 ? _b : [];
|
|
209
|
+
if (routeInfo.middleware.length) {
|
|
210
|
+
const routeMiddlewareFn = await this.expressMiddlewareService.compose(routeInfo.middleware, this.app);
|
|
211
|
+
routeMiddlewareList.push(routeMiddlewareFn);
|
|
212
|
+
}
|
|
155
213
|
this.logger.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
156
214
|
// apply controller from request context
|
|
157
215
|
newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
|
|
158
216
|
}
|
|
159
|
-
|
|
217
|
+
routerMiddlewares.push({
|
|
218
|
+
prefix: routerInfo.prefix,
|
|
219
|
+
middleware: newRouter,
|
|
220
|
+
});
|
|
160
221
|
}
|
|
222
|
+
return routerMiddlewares;
|
|
161
223
|
}
|
|
162
224
|
/**
|
|
163
225
|
* @param routerOptions
|
|
@@ -165,13 +227,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
165
227
|
createRouter(routerOptions) {
|
|
166
228
|
return express.Router({ caseSensitive: routerOptions.sensitive });
|
|
167
229
|
}
|
|
168
|
-
async
|
|
169
|
-
const fn = await this.expressMiddlewareService.compose(middlewares);
|
|
170
|
-
handlerCallback(fn);
|
|
171
|
-
}
|
|
172
|
-
async getMiddleware() {
|
|
230
|
+
async applyMiddleware() {
|
|
173
231
|
if (!this.composeMiddleware) {
|
|
174
|
-
this.composeMiddleware = await this.expressMiddlewareService.compose(this.middlewareManager);
|
|
232
|
+
this.composeMiddleware = await this.expressMiddlewareService.compose(this.middlewareManager, this.app);
|
|
175
233
|
await this.filterManager.init(this.applicationContext);
|
|
176
234
|
}
|
|
177
235
|
return this.composeMiddleware;
|
|
@@ -182,6 +240,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
182
240
|
getServer() {
|
|
183
241
|
return this.server;
|
|
184
242
|
}
|
|
243
|
+
getPort() {
|
|
244
|
+
return process.env.MIDWAY_HTTP_PORT;
|
|
245
|
+
}
|
|
185
246
|
getFrameworkName() {
|
|
186
247
|
return 'midway:express';
|
|
187
248
|
}
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,24 +1,37 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { CommonMiddlewareUnion, ContextMiddlewareManager, IConfigurationOptions, IMiddleware, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
3
|
-
import { Application as ExpressApplication, NextFunction as ExpressNextFunction, Request, Response } from 'express';
|
|
4
|
-
|
|
3
|
+
import { Application as ExpressApplication, NextFunction as ExpressNextFunction, Request as ExpressRequest, Response as ExpressResponse } from 'express';
|
|
4
|
+
import { Options, OptionsJson, OptionsText, OptionsUrlencoded } from 'body-parser';
|
|
5
|
+
declare type Request = IMidwayContext<ExpressRequest>;
|
|
6
|
+
export declare type Response = ExpressResponse;
|
|
7
|
+
export declare type NextFunction = ExpressNextFunction;
|
|
8
|
+
export interface Context extends Request {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated use Context
|
|
12
|
+
*/
|
|
13
|
+
export declare type IMidwayExpressRequest = Context;
|
|
5
14
|
/**
|
|
6
|
-
* @deprecated use
|
|
15
|
+
* @deprecated use Context
|
|
7
16
|
*/
|
|
8
|
-
export declare type
|
|
9
|
-
export declare type IMidwayExpressMiddleware = IMiddleware<
|
|
10
|
-
export interface IMidwayExpressApplication extends IMidwayApplication<
|
|
17
|
+
export declare type IMidwayExpressContext = Context;
|
|
18
|
+
export declare type IMidwayExpressMiddleware = IMiddleware<Context, ExpressResponse, ExpressNextFunction>;
|
|
19
|
+
export interface IMidwayExpressApplication extends IMidwayApplication<Context, ExpressApplication> {
|
|
11
20
|
/**
|
|
12
21
|
* add global middleware to app
|
|
13
22
|
* @param Middleware
|
|
14
23
|
*/
|
|
15
|
-
useMiddleware<Response, NextFunction>(Middleware: CommonMiddlewareUnion<
|
|
24
|
+
useMiddleware<Response, NextFunction>(Middleware: CommonMiddlewareUnion<Context, Response, NextFunction>): void;
|
|
16
25
|
/**
|
|
17
26
|
* get global middleware
|
|
18
27
|
*/
|
|
19
|
-
getMiddleware<Response, NextFunction>(): ContextMiddlewareManager<
|
|
28
|
+
getMiddleware<Response, NextFunction>(): ContextMiddlewareManager<Context, Response, NextFunction>;
|
|
20
29
|
}
|
|
21
30
|
export interface IMidwayExpressConfigurationOptions extends IConfigurationOptions {
|
|
31
|
+
/**
|
|
32
|
+
* session or cookie secret key
|
|
33
|
+
*/
|
|
34
|
+
keys?: string | string[];
|
|
22
35
|
/**
|
|
23
36
|
* application http port
|
|
24
37
|
*/
|
|
@@ -49,12 +62,25 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
|
|
|
49
62
|
globalPrefix?: string;
|
|
50
63
|
}
|
|
51
64
|
export declare type Application = IMidwayExpressApplication;
|
|
52
|
-
export declare type NextFunction = ExpressNextFunction;
|
|
53
|
-
export interface Context extends IMidwayExpressContext {
|
|
54
|
-
}
|
|
55
65
|
declare module '@midwayjs/core/dist/interface' {
|
|
56
66
|
interface MidwayConfig {
|
|
57
67
|
express?: IMidwayExpressConfigurationOptions;
|
|
68
|
+
bodyParser?: {
|
|
69
|
+
enable?: boolean;
|
|
70
|
+
json?: OptionsJson & {
|
|
71
|
+
enable?: boolean;
|
|
72
|
+
};
|
|
73
|
+
raw?: Options & {
|
|
74
|
+
enable?: boolean;
|
|
75
|
+
};
|
|
76
|
+
text?: OptionsText & {
|
|
77
|
+
enable?: boolean;
|
|
78
|
+
};
|
|
79
|
+
urlencoded?: OptionsUrlencoded & {
|
|
80
|
+
enable?: boolean;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
58
83
|
}
|
|
59
84
|
}
|
|
85
|
+
export {};
|
|
60
86
|
//# sourceMappingURL=interface.d.ts.map
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MidwayContextLogger } from '@midwayjs/logger';
|
|
2
|
-
import {
|
|
3
|
-
export declare class MidwayExpressContextLogger extends MidwayContextLogger<
|
|
2
|
+
import { Context } from './interface';
|
|
3
|
+
export declare class MidwayExpressContextLogger extends MidwayContextLogger<Context> {
|
|
4
4
|
formatContextLabel(): string;
|
|
5
5
|
}
|
|
6
6
|
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { IMidwayContainer, CommonMiddleware, FunctionMiddleware } from '@midwayjs/core';
|
|
2
|
-
import {
|
|
2
|
+
import { Context, Application } from './interface';
|
|
3
3
|
import { NextFunction, Response } from 'express';
|
|
4
|
+
export declare function wrapAsyncHandler(fn: any): any;
|
|
4
5
|
export declare class MidwayExpressMiddlewareService {
|
|
5
6
|
readonly applicationContext: IMidwayContainer;
|
|
6
7
|
constructor(applicationContext: IMidwayContainer);
|
|
7
|
-
compose(middleware: Array<CommonMiddleware<
|
|
8
|
-
(req:
|
|
8
|
+
compose(middleware: Array<CommonMiddleware<Context, Response, NextFunction> | string>, app: Application, name?: string): Promise<{
|
|
9
|
+
(req: Context, res: Response, nextFunction: NextFunction): any;
|
|
9
10
|
_name: string;
|
|
10
11
|
}>;
|
|
11
12
|
}
|
|
@@ -9,27 +9,43 @@ 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.wrapMiddleware = exports.MidwayExpressMiddlewareService = void 0;
|
|
12
|
+
exports.wrapMiddleware = exports.MidwayExpressMiddlewareService = exports.wrapAsyncHandler = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const core_1 = require("@midwayjs/core");
|
|
15
|
+
const util_1 = require("./util");
|
|
16
|
+
function wrapAsyncHandler(fn) {
|
|
17
|
+
if (decorator_1.Types.isAsyncFunction(fn)) {
|
|
18
|
+
return (req, res, next) => {
|
|
19
|
+
return fn(req, res, next).catch(err => {
|
|
20
|
+
next(err);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return fn;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.wrapAsyncHandler = wrapAsyncHandler;
|
|
15
29
|
let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
16
30
|
constructor(applicationContext) {
|
|
17
31
|
this.applicationContext = applicationContext;
|
|
18
32
|
}
|
|
19
|
-
async compose(middleware, name) {
|
|
33
|
+
async compose(middleware, app, name) {
|
|
20
34
|
if (!Array.isArray(middleware)) {
|
|
21
35
|
throw new core_1.MidwayCommonError('Middleware stack must be an array');
|
|
22
36
|
}
|
|
23
37
|
const newMiddlewareArr = [];
|
|
24
38
|
for (let fn of middleware) {
|
|
25
|
-
if (
|
|
39
|
+
if (decorator_1.Types.isClass(fn) || typeof fn === 'string') {
|
|
26
40
|
if (typeof fn === 'string' &&
|
|
27
41
|
!this.applicationContext.hasDefinition(fn)) {
|
|
28
42
|
throw new core_1.MidwayCommonError('Middleware definition not found in midway container');
|
|
29
43
|
}
|
|
30
44
|
const classMiddleware = await this.applicationContext.getAsync(fn);
|
|
31
45
|
if (classMiddleware) {
|
|
32
|
-
fn = classMiddleware.resolve();
|
|
46
|
+
fn = await classMiddleware.resolve(app);
|
|
47
|
+
// wrap async middleware
|
|
48
|
+
fn = wrapAsyncHandler(fn);
|
|
33
49
|
if (!classMiddleware.match && !classMiddleware.ignore) {
|
|
34
50
|
fn._name = classMiddleware.constructor.name;
|
|
35
51
|
// just got fn
|
|
@@ -56,16 +72,40 @@ let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
|
56
72
|
}
|
|
57
73
|
}
|
|
58
74
|
else {
|
|
75
|
+
// wrap async middleware
|
|
76
|
+
fn = wrapAsyncHandler(fn);
|
|
59
77
|
newMiddlewareArr.push(fn);
|
|
60
78
|
}
|
|
61
79
|
}
|
|
62
|
-
const composeFn = (req, res,
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
const composeFn = (req, res, nextFunction) => {
|
|
81
|
+
let index = -1;
|
|
82
|
+
function dispatch(pos, err) {
|
|
83
|
+
const handler = newMiddlewareArr[pos];
|
|
84
|
+
index = pos;
|
|
85
|
+
if (err || index === newMiddlewareArr.length) {
|
|
86
|
+
return nextFunction(err);
|
|
66
87
|
}
|
|
67
|
-
|
|
68
|
-
|
|
88
|
+
function next(err) {
|
|
89
|
+
if (pos < index) {
|
|
90
|
+
throw new TypeError('`next()` called multiple times');
|
|
91
|
+
}
|
|
92
|
+
return dispatch(pos + 1, err);
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
Promise.resolve(handler(req, res, next)).then(result => {
|
|
96
|
+
if (result) {
|
|
97
|
+
(0, util_1.sendData)(res, result);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
// Avoid future errors that could diverge stack execution.
|
|
103
|
+
if (index > pos)
|
|
104
|
+
throw err;
|
|
105
|
+
return next(err);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return dispatch(0);
|
|
69
109
|
};
|
|
70
110
|
if (name) {
|
|
71
111
|
composeFn._name = name;
|
package/dist/util.d.ts
ADDED
package/dist/util.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sendData = void 0;
|
|
4
|
+
function sendData(res, data) {
|
|
5
|
+
if (typeof data === 'number') {
|
|
6
|
+
res.status(res.statusCode).send('' + data);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
res.status(res.statusCode).send(data);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.sendData = sendData;
|
|
13
|
+
//# sourceMappingURL=util.js.map
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/express",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Midway Web Framework for Express",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
|
-
"test": "node --require=ts-node/register ../../node_modules/.bin/jest",
|
|
10
|
-
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --coverage --forceExit",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
11
|
"ci": "npm run test"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
@@ -23,15 +23,19 @@
|
|
|
23
23
|
],
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@midwayjs/decorator": "^3.0.0
|
|
27
|
-
"@midwayjs/
|
|
28
|
-
"@
|
|
29
|
-
"
|
|
26
|
+
"@midwayjs/decorator": "^3.0.0",
|
|
27
|
+
"@midwayjs/logger": "2.14.0",
|
|
28
|
+
"@midwayjs/mock": "^3.0.0",
|
|
29
|
+
"@types/body-parser": "1.19.2",
|
|
30
|
+
"@types/express": "4.17.13",
|
|
31
|
+
"fs-extra": "10.0.0"
|
|
30
32
|
},
|
|
31
33
|
"dependencies": {
|
|
32
|
-
"@midwayjs/core": "^3.0.0
|
|
33
|
-
"@midwayjs/
|
|
34
|
-
"
|
|
34
|
+
"@midwayjs/core": "^3.0.0",
|
|
35
|
+
"@midwayjs/express-session": "^3.0.0",
|
|
36
|
+
"body-parser": "1.19.1",
|
|
37
|
+
"cookie-parser": "^1.4.6",
|
|
38
|
+
"express": "4.17.2"
|
|
35
39
|
},
|
|
36
40
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
37
41
|
"repository": {
|
|
@@ -41,5 +45,5 @@
|
|
|
41
45
|
"engines": {
|
|
42
46
|
"node": ">=12"
|
|
43
47
|
},
|
|
44
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "55c26029bccf7bbb739fa1597e8f418dafa2caa0"
|
|
45
49
|
}
|