@midwayjs/express 4.0.0-beta.1 → 4.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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/configuration.js +3 -0
- package/dist/framework.d.ts +2 -2
- package/dist/framework.js +79 -12
- package/dist/interface.d.ts +46 -2
- package/dist/middlewareService.js +4 -3
- package/dist/util.d.ts +1 -0
- package/dist/util.js +18 -2
- package/package.json +11 -11
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 - Now midwayjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
package/dist/configuration.js
CHANGED
|
@@ -17,6 +17,9 @@ const cookieParser = require("cookie-parser");
|
|
|
17
17
|
const DefaultConfig = require("./config/config.default");
|
|
18
18
|
const framework_1 = require("./framework");
|
|
19
19
|
let ExpressConfiguration = class ExpressConfiguration {
|
|
20
|
+
decoratorService;
|
|
21
|
+
expressFramework;
|
|
22
|
+
configService;
|
|
20
23
|
init() {
|
|
21
24
|
this.decoratorService.registerParameterHandler(core_1.WEB_ROUTER_PARAM_KEY, options => {
|
|
22
25
|
return (0, core_1.extractExpressLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1], options.originArgs[2]);
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { BaseFramework, IMidwayBootstrapOptions, MiddlewareRespond, RouterInfo } from '@midwayjs/core';
|
|
3
2
|
import { IMidwayExpressApplication, IMidwayExpressConfigurationOptions, Context } from './interface';
|
|
4
3
|
import type { IRouter, IRouterHandler, Response, NextFunction } from 'express';
|
|
5
4
|
import { Server } from 'net';
|
|
6
5
|
export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpressApplication, Context, IMidwayExpressConfigurationOptions, Response, NextFunction> {
|
|
7
|
-
app: IMidwayExpressApplication;
|
|
8
6
|
private server;
|
|
9
7
|
private expressMiddlewareService;
|
|
10
8
|
private webRouterService;
|
|
@@ -31,5 +29,7 @@ export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpress
|
|
|
31
29
|
getServer(): Server;
|
|
32
30
|
getPort(): string;
|
|
33
31
|
getFrameworkName(): string;
|
|
32
|
+
private createVersioningMiddleware;
|
|
33
|
+
private extractVersion;
|
|
34
34
|
}
|
|
35
35
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -14,6 +14,9 @@ const util_1 = require("util");
|
|
|
14
14
|
const util_2 = require("./util");
|
|
15
15
|
const debug = (0, util_1.debuglog)('midway:debug');
|
|
16
16
|
let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFramework {
|
|
17
|
+
server;
|
|
18
|
+
expressMiddlewareService;
|
|
19
|
+
webRouterService;
|
|
17
20
|
configure() {
|
|
18
21
|
return this.configService.getConfiguration('express');
|
|
19
22
|
}
|
|
@@ -31,6 +34,12 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
31
34
|
ctx.requestContext.registerObject('res', res);
|
|
32
35
|
next();
|
|
33
36
|
});
|
|
37
|
+
// 版本控制配置
|
|
38
|
+
const versioningConfig = this.configurationOptions.versioning;
|
|
39
|
+
// 如果启用版本控制,添加版本处理中间件
|
|
40
|
+
if (versioningConfig?.enabled) {
|
|
41
|
+
this.app.use(this.createVersioningMiddleware(versioningConfig));
|
|
42
|
+
}
|
|
34
43
|
this.defineApplicationProperties({
|
|
35
44
|
useMiddleware: (routerPath, ...middleware) => {
|
|
36
45
|
if (typeof routerPath === 'string' && middleware) {
|
|
@@ -139,18 +148,27 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
139
148
|
}
|
|
140
149
|
// register httpServer to applicationContext
|
|
141
150
|
this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
|
|
142
|
-
|
|
143
|
-
|
|
151
|
+
this.configurationOptions.listenOptions = {
|
|
152
|
+
port: this.configurationOptions.port,
|
|
153
|
+
host: this.configurationOptions.hostname,
|
|
154
|
+
...this.configurationOptions.listenOptions,
|
|
155
|
+
};
|
|
156
|
+
// set port and listen server
|
|
157
|
+
let customPort = process.env.MIDWAY_HTTP_PORT ||
|
|
158
|
+
this.configurationOptions.listenOptions.port;
|
|
159
|
+
if (customPort === 0 || customPort === '0') {
|
|
160
|
+
customPort = await (0, util_2.getFreePort)();
|
|
161
|
+
this.logger.info(`[midway:express] detect available port: ${customPort}`);
|
|
162
|
+
}
|
|
163
|
+
this.configurationOptions.listenOptions.port = Number(customPort);
|
|
164
|
+
if (this.configurationOptions.listenOptions.port) {
|
|
144
165
|
new Promise(resolve => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
args.push(this.configurationOptions.hostname);
|
|
148
|
-
}
|
|
149
|
-
args.push(() => {
|
|
166
|
+
// 使用 ListenOptions 对象启动服务器
|
|
167
|
+
this.server.listen(this.configurationOptions.listenOptions, () => {
|
|
150
168
|
resolve();
|
|
151
169
|
});
|
|
152
|
-
this.
|
|
153
|
-
|
|
170
|
+
process.env.MIDWAY_HTTP_PORT = String(this.configurationOptions.listenOptions.port);
|
|
171
|
+
this.logger.debug(`[midway:express] Server listening on http://${this.configurationOptions.hostname || 'localhost'}:${customPort}`);
|
|
154
172
|
});
|
|
155
173
|
}
|
|
156
174
|
}
|
|
@@ -221,7 +239,7 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
221
239
|
for (const routerInfo of routerList) {
|
|
222
240
|
// bind controller first
|
|
223
241
|
this.getApplicationContext().bindClass(routerInfo.routerModule);
|
|
224
|
-
this.logger.debug(`Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
242
|
+
this.logger.debug(`[midway:express] Load Controller "${routerInfo.controllerId}", prefix=${routerInfo.prefix}`);
|
|
225
243
|
// new router
|
|
226
244
|
const newRouter = this.createRouter(routerInfo.routerOptions);
|
|
227
245
|
routerInfo.middleware = routerInfo.middleware ?? [];
|
|
@@ -240,9 +258,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
240
258
|
const routeMiddlewareFn = await this.expressMiddlewareService.compose(routeInfo.middleware, this.app);
|
|
241
259
|
routeMiddlewareList.push(routeMiddlewareFn);
|
|
242
260
|
}
|
|
243
|
-
this.logger.debug(`Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
261
|
+
this.logger.debug(`[midway:express] Load Router "${routeInfo.requestMethod.toUpperCase()} ${routeInfo.url}"`);
|
|
244
262
|
// apply controller from request context
|
|
245
|
-
newRouter[routeInfo.requestMethod].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
|
|
263
|
+
newRouter[routeInfo.requestMethod.toLowerCase()].call(newRouter, routeInfo.url, ...routeMiddlewareList, this.generateController(routeInfo));
|
|
246
264
|
}
|
|
247
265
|
routerMiddlewares.push({
|
|
248
266
|
prefix: routerInfo.prefix,
|
|
@@ -271,7 +289,9 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
271
289
|
if (this.server) {
|
|
272
290
|
new Promise(resolve => {
|
|
273
291
|
this.server.close(resolve);
|
|
292
|
+
process.env.MIDWAY_HTTP_PORT = '';
|
|
274
293
|
});
|
|
294
|
+
this.logger.debug('[midway:express] server close');
|
|
275
295
|
}
|
|
276
296
|
}
|
|
277
297
|
getServer() {
|
|
@@ -283,6 +303,53 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
283
303
|
getFrameworkName() {
|
|
284
304
|
return 'express';
|
|
285
305
|
}
|
|
306
|
+
createVersioningMiddleware(config) {
|
|
307
|
+
return (req, res, next) => {
|
|
308
|
+
// 提取版本信息
|
|
309
|
+
const version = this.extractVersion(req, config);
|
|
310
|
+
req.apiVersion = version;
|
|
311
|
+
// 对于 URI 版本控制,重写路径
|
|
312
|
+
if (config.type === 'URI' && version) {
|
|
313
|
+
const versionPrefix = `/${config.prefix || 'v'}${version}`;
|
|
314
|
+
if (req.path.startsWith(versionPrefix)) {
|
|
315
|
+
req.originalPath = req.path;
|
|
316
|
+
// Express 中需要修改 url 而不是 path
|
|
317
|
+
req.url = req.url.replace(versionPrefix, '') || '/';
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
next();
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
extractVersion(req, config) {
|
|
324
|
+
// 自定义提取函数优先
|
|
325
|
+
if (config.extractVersionFn) {
|
|
326
|
+
return config.extractVersionFn(req);
|
|
327
|
+
}
|
|
328
|
+
const type = config.type || 'URI';
|
|
329
|
+
switch (type) {
|
|
330
|
+
case 'HEADER': {
|
|
331
|
+
const headerName = config.header || 'x-api-version';
|
|
332
|
+
const headerValue = req.headers[headerName];
|
|
333
|
+
if (typeof headerValue === 'string') {
|
|
334
|
+
return headerValue.replace(/^v/, '');
|
|
335
|
+
}
|
|
336
|
+
return undefined;
|
|
337
|
+
}
|
|
338
|
+
case 'MEDIA_TYPE': {
|
|
339
|
+
const accept = req.headers.accept;
|
|
340
|
+
const paramName = config.mediaTypeParam || 'version';
|
|
341
|
+
const match = accept?.match(new RegExp(`${paramName}=(\\\\d+)`));
|
|
342
|
+
return match ? match[1] : undefined;
|
|
343
|
+
}
|
|
344
|
+
case 'URI': {
|
|
345
|
+
const prefix = config.prefix || 'v';
|
|
346
|
+
const uriMatch = req.path.match(new RegExp(`^/${prefix}(\\\\d+)`));
|
|
347
|
+
return uriMatch ? uriMatch[1] : undefined;
|
|
348
|
+
}
|
|
349
|
+
default:
|
|
350
|
+
return config.defaultVersion;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
286
353
|
};
|
|
287
354
|
exports.MidwayExpressFramework = MidwayExpressFramework;
|
|
288
355
|
exports.MidwayExpressFramework = MidwayExpressFramework = __decorate([
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import { CommonMiddlewareUnion, ContextMiddlewareManager, FunctionMiddleware, IConfigurationOptions, IMiddleware, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
4
2
|
import { Application as ExpressApplication, NextFunction as ExpressNextFunction, Request as ExpressRequest, Response as ExpressResponse } from 'express';
|
|
3
|
+
import { ListenOptions } from 'net';
|
|
5
4
|
type Request = IMidwayContext<ExpressRequest>;
|
|
6
5
|
export type Response = ExpressResponse;
|
|
7
6
|
export type NextFunction = ExpressNextFunction;
|
|
8
7
|
export interface Context extends Request {
|
|
8
|
+
/**
|
|
9
|
+
* API version extracted from request
|
|
10
|
+
*/
|
|
11
|
+
apiVersion?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Original path before version processing
|
|
14
|
+
*/
|
|
15
|
+
originalPath?: string;
|
|
9
16
|
}
|
|
10
17
|
/**
|
|
11
18
|
* @deprecated use Context
|
|
@@ -70,6 +77,43 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
|
|
|
70
77
|
* https/https/http2 server options
|
|
71
78
|
*/
|
|
72
79
|
serverOptions?: Record<string, any>;
|
|
80
|
+
/**
|
|
81
|
+
* listen options
|
|
82
|
+
*/
|
|
83
|
+
listenOptions?: ListenOptions;
|
|
84
|
+
/**
|
|
85
|
+
* API versioning configuration
|
|
86
|
+
*/
|
|
87
|
+
versioning?: {
|
|
88
|
+
/**
|
|
89
|
+
* Enable versioning
|
|
90
|
+
*/
|
|
91
|
+
enabled: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Versioning type
|
|
94
|
+
*/
|
|
95
|
+
type?: 'URI' | 'HEADER' | 'MEDIA_TYPE' | 'CUSTOM';
|
|
96
|
+
/**
|
|
97
|
+
* Default version if none is specified
|
|
98
|
+
*/
|
|
99
|
+
defaultVersion?: string;
|
|
100
|
+
/**
|
|
101
|
+
* Version prefix for URI versioning (default: 'v')
|
|
102
|
+
*/
|
|
103
|
+
prefix?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Header name for HEADER versioning (default: 'x-api-version')
|
|
106
|
+
*/
|
|
107
|
+
header?: string;
|
|
108
|
+
/**
|
|
109
|
+
* Media type parameter name for MEDIA_TYPE versioning (default: 'version')
|
|
110
|
+
*/
|
|
111
|
+
mediaTypeParam?: string;
|
|
112
|
+
/**
|
|
113
|
+
* Custom version extraction function
|
|
114
|
+
*/
|
|
115
|
+
extractVersionFn?: (ctx: Context) => string | undefined;
|
|
116
|
+
};
|
|
73
117
|
}
|
|
74
118
|
export type Application = IMidwayExpressApplication;
|
|
75
119
|
export {};
|
|
@@ -9,7 +9,9 @@ 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.MidwayExpressMiddlewareService = void 0;
|
|
13
|
+
exports.wrapAsyncHandler = wrapAsyncHandler;
|
|
14
|
+
exports.wrapMiddleware = wrapMiddleware;
|
|
13
15
|
const core_1 = require("@midwayjs/core");
|
|
14
16
|
const util_1 = require("./util");
|
|
15
17
|
function wrapAsyncHandler(fn) {
|
|
@@ -24,8 +26,8 @@ function wrapAsyncHandler(fn) {
|
|
|
24
26
|
return fn;
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
|
-
exports.wrapAsyncHandler = wrapAsyncHandler;
|
|
28
29
|
let MidwayExpressMiddlewareService = class MidwayExpressMiddlewareService {
|
|
30
|
+
applicationContext;
|
|
29
31
|
constructor(applicationContext) {
|
|
30
32
|
this.applicationContext = applicationContext;
|
|
31
33
|
}
|
|
@@ -139,5 +141,4 @@ function wrapMiddleware(mw, options) {
|
|
|
139
141
|
fn._name = mw._name + 'middlewareWrapper';
|
|
140
142
|
return fn;
|
|
141
143
|
}
|
|
142
|
-
exports.wrapMiddleware = wrapMiddleware;
|
|
143
144
|
//# sourceMappingURL=middlewareService.js.map
|
package/dist/util.d.ts
CHANGED
package/dist/util.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sendData =
|
|
3
|
+
exports.sendData = sendData;
|
|
4
|
+
exports.getFreePort = getFreePort;
|
|
5
|
+
const net_1 = require("net");
|
|
4
6
|
function sendData(res, data) {
|
|
5
7
|
if (typeof data === 'number') {
|
|
6
8
|
res.status(res.statusCode).send('' + data);
|
|
@@ -9,5 +11,19 @@ function sendData(res, data) {
|
|
|
9
11
|
res.status(res.statusCode).send(data);
|
|
10
12
|
}
|
|
11
13
|
}
|
|
12
|
-
|
|
14
|
+
async function getFreePort() {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const server = (0, net_1.createServer)();
|
|
17
|
+
server.listen(0, () => {
|
|
18
|
+
try {
|
|
19
|
+
const port = server.address().port;
|
|
20
|
+
server.close();
|
|
21
|
+
resolve(port);
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
reject(err);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
13
29
|
//# sourceMappingURL=util.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/express",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.10",
|
|
4
4
|
"description": "Midway Web Framework for Express",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,17 +24,17 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
28
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
29
|
-
"@types/body-parser": "1.19.
|
|
30
|
-
"@types/express": "4.17.
|
|
31
|
-
"fs-extra": "11.3.
|
|
27
|
+
"@midwayjs/core": "^4.0.0-beta.10",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.10",
|
|
29
|
+
"@types/body-parser": "1.19.6",
|
|
30
|
+
"@types/express": "4.17.25",
|
|
31
|
+
"fs-extra": "11.3.3"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@midwayjs/express-session": "^4.0.0-beta.
|
|
35
|
-
"body-parser": "
|
|
34
|
+
"@midwayjs/express-session": "^4.0.0-beta.10",
|
|
35
|
+
"body-parser": "2.2.2",
|
|
36
36
|
"cookie-parser": "^1.4.6",
|
|
37
|
-
"express": "4.
|
|
37
|
+
"express": "4.22.1"
|
|
38
38
|
},
|
|
39
39
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
40
40
|
"repository": {
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"url": "https://github.com/midwayjs/midway.git"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
|
-
"node": ">=
|
|
45
|
+
"node": ">=20"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
|
|
48
48
|
}
|