@midwayjs/express 4.0.0-beta.2 → 4.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/framework.d.ts +2 -0
- package/dist/framework.js +53 -0
- package/dist/interface.d.ts +41 -0
- package/package.json +6 -6
package/README.md
CHANGED
package/dist/framework.d.ts
CHANGED
|
@@ -31,5 +31,7 @@ export declare class MidwayExpressFramework extends BaseFramework<IMidwayExpress
|
|
|
31
31
|
getServer(): Server;
|
|
32
32
|
getPort(): string;
|
|
33
33
|
getFrameworkName(): string;
|
|
34
|
+
private createVersioningMiddleware;
|
|
35
|
+
private extractVersion;
|
|
34
36
|
}
|
|
35
37
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -31,6 +31,12 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
31
31
|
ctx.requestContext.registerObject('res', res);
|
|
32
32
|
next();
|
|
33
33
|
});
|
|
34
|
+
// 版本控制配置
|
|
35
|
+
const versioningConfig = this.configurationOptions.versioning;
|
|
36
|
+
// 如果启用版本控制,添加版本处理中间件
|
|
37
|
+
if (versioningConfig?.enabled) {
|
|
38
|
+
this.app.use(this.createVersioningMiddleware(versioningConfig));
|
|
39
|
+
}
|
|
34
40
|
this.defineApplicationProperties({
|
|
35
41
|
useMiddleware: (routerPath, ...middleware) => {
|
|
36
42
|
if (typeof routerPath === 'string' && middleware) {
|
|
@@ -294,6 +300,53 @@ let MidwayExpressFramework = class MidwayExpressFramework extends core_1.BaseFra
|
|
|
294
300
|
getFrameworkName() {
|
|
295
301
|
return 'express';
|
|
296
302
|
}
|
|
303
|
+
createVersioningMiddleware(config) {
|
|
304
|
+
return (req, res, next) => {
|
|
305
|
+
// 提取版本信息
|
|
306
|
+
const version = this.extractVersion(req, config);
|
|
307
|
+
req.apiVersion = version;
|
|
308
|
+
// 对于 URI 版本控制,重写路径
|
|
309
|
+
if (config.type === 'URI' && version) {
|
|
310
|
+
const versionPrefix = `/${config.prefix || 'v'}${version}`;
|
|
311
|
+
if (req.path.startsWith(versionPrefix)) {
|
|
312
|
+
req.originalPath = req.path;
|
|
313
|
+
// Express 中需要修改 url 而不是 path
|
|
314
|
+
req.url = req.url.replace(versionPrefix, '') || '/';
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
next();
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
extractVersion(req, config) {
|
|
321
|
+
// 自定义提取函数优先
|
|
322
|
+
if (config.extractVersionFn) {
|
|
323
|
+
return config.extractVersionFn(req);
|
|
324
|
+
}
|
|
325
|
+
const type = config.type || 'URI';
|
|
326
|
+
switch (type) {
|
|
327
|
+
case 'HEADER': {
|
|
328
|
+
const headerName = config.header || 'x-api-version';
|
|
329
|
+
const headerValue = req.headers[headerName];
|
|
330
|
+
if (typeof headerValue === 'string') {
|
|
331
|
+
return headerValue.replace(/^v/, '');
|
|
332
|
+
}
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
case 'MEDIA_TYPE': {
|
|
336
|
+
const accept = req.headers.accept;
|
|
337
|
+
const paramName = config.mediaTypeParam || 'version';
|
|
338
|
+
const match = accept?.match(new RegExp(`${paramName}=(\\\\d+)`));
|
|
339
|
+
return match ? match[1] : undefined;
|
|
340
|
+
}
|
|
341
|
+
case 'URI': {
|
|
342
|
+
const prefix = config.prefix || 'v';
|
|
343
|
+
const uriMatch = req.path.match(new RegExp(`^/${prefix}(\\\\d+)`));
|
|
344
|
+
return uriMatch ? uriMatch[1] : undefined;
|
|
345
|
+
}
|
|
346
|
+
default:
|
|
347
|
+
return config.defaultVersion;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
297
350
|
};
|
|
298
351
|
exports.MidwayExpressFramework = MidwayExpressFramework;
|
|
299
352
|
exports.MidwayExpressFramework = MidwayExpressFramework = __decorate([
|
package/dist/interface.d.ts
CHANGED
|
@@ -8,6 +8,14 @@ type Request = IMidwayContext<ExpressRequest>;
|
|
|
8
8
|
export type Response = ExpressResponse;
|
|
9
9
|
export type NextFunction = ExpressNextFunction;
|
|
10
10
|
export interface Context extends Request {
|
|
11
|
+
/**
|
|
12
|
+
* API version extracted from request
|
|
13
|
+
*/
|
|
14
|
+
apiVersion?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Original path before version processing
|
|
17
|
+
*/
|
|
18
|
+
originalPath?: string;
|
|
11
19
|
}
|
|
12
20
|
/**
|
|
13
21
|
* @deprecated use Context
|
|
@@ -76,6 +84,39 @@ export interface IMidwayExpressConfigurationOptions extends IConfigurationOption
|
|
|
76
84
|
* listen options
|
|
77
85
|
*/
|
|
78
86
|
listenOptions?: ListenOptions;
|
|
87
|
+
/**
|
|
88
|
+
* API versioning configuration
|
|
89
|
+
*/
|
|
90
|
+
versioning?: {
|
|
91
|
+
/**
|
|
92
|
+
* Enable versioning
|
|
93
|
+
*/
|
|
94
|
+
enabled: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Versioning type
|
|
97
|
+
*/
|
|
98
|
+
type?: 'URI' | 'HEADER' | 'MEDIA_TYPE' | 'CUSTOM';
|
|
99
|
+
/**
|
|
100
|
+
* Default version if none is specified
|
|
101
|
+
*/
|
|
102
|
+
defaultVersion?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Version prefix for URI versioning (default: 'v')
|
|
105
|
+
*/
|
|
106
|
+
prefix?: string;
|
|
107
|
+
/**
|
|
108
|
+
* Header name for HEADER versioning (default: 'x-api-version')
|
|
109
|
+
*/
|
|
110
|
+
header?: string;
|
|
111
|
+
/**
|
|
112
|
+
* Media type parameter name for MEDIA_TYPE versioning (default: 'version')
|
|
113
|
+
*/
|
|
114
|
+
mediaTypeParam?: string;
|
|
115
|
+
/**
|
|
116
|
+
* Custom version extraction function
|
|
117
|
+
*/
|
|
118
|
+
extractVersionFn?: (ctx: Context) => string | undefined;
|
|
119
|
+
};
|
|
79
120
|
}
|
|
80
121
|
export type Application = IMidwayExpressApplication;
|
|
81
122
|
export {};
|
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.4",
|
|
4
4
|
"description": "Midway Web Framework for Express",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,14 +24,14 @@
|
|
|
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.
|
|
27
|
+
"@midwayjs/core": "^4.0.0-beta.4",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.4",
|
|
29
|
+
"@types/body-parser": "1.19.6",
|
|
30
30
|
"@types/express": "4.17.23",
|
|
31
31
|
"fs-extra": "11.3.0"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@midwayjs/express-session": "^4.0.0-beta.
|
|
34
|
+
"@midwayjs/express-session": "^4.0.0-beta.4",
|
|
35
35
|
"body-parser": "1.20.3",
|
|
36
36
|
"cookie-parser": "^1.4.6",
|
|
37
37
|
"express": "4.21.2"
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=20"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "64b0cc5d7f67134b1bc75959233e9048e796f8b2"
|
|
48
48
|
}
|