@midwayjs/koa 4.0.0-beta.2 → 4.0.0-beta.3
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 -1
- package/dist/interface.d.ts +35 -0
- package/package.json +5 -5
package/README.md
CHANGED
package/dist/framework.d.ts
CHANGED
|
@@ -26,5 +26,7 @@ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplicat
|
|
|
26
26
|
getPort(): string;
|
|
27
27
|
useMiddleware(Middleware: CommonMiddlewareUnion<IMidwayKoaContext, Next, unknown>): void;
|
|
28
28
|
useFilter(Filter: CommonFilterUnion<IMidwayKoaContext, Next, unknown>): void;
|
|
29
|
+
private createVersioningMiddleware;
|
|
30
|
+
private extractVersion;
|
|
29
31
|
}
|
|
30
32
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -137,10 +137,16 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
137
137
|
throw new core_1.httpError.NotFoundError(`${ctx.path} Not Found`);
|
|
138
138
|
}
|
|
139
139
|
};
|
|
140
|
+
const applyMiddlewares = [notFound];
|
|
141
|
+
// versioning middleware
|
|
142
|
+
const versioningConfig = this.configurationOptions.versioning;
|
|
143
|
+
if (versioningConfig?.enabled) {
|
|
144
|
+
applyMiddlewares.push(this.createVersioningMiddleware(versioningConfig));
|
|
145
|
+
}
|
|
140
146
|
// root middleware
|
|
141
147
|
const midwayRootMiddleware = async (ctx, next) => {
|
|
142
148
|
this.app.createAnonymousContext(ctx);
|
|
143
|
-
await (await this.applyMiddleware(
|
|
149
|
+
await (await this.applyMiddleware(applyMiddlewares))(ctx, next);
|
|
144
150
|
if (ctx.body === undefined &&
|
|
145
151
|
!ctx.response._explicitStatus &&
|
|
146
152
|
ctx._matchedRoute) {
|
|
@@ -266,6 +272,52 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
266
272
|
useFilter(Filter) {
|
|
267
273
|
this.filterManager.useFilter(Filter);
|
|
268
274
|
}
|
|
275
|
+
createVersioningMiddleware(config) {
|
|
276
|
+
return async (ctx, next) => {
|
|
277
|
+
// 提取版本信息
|
|
278
|
+
const version = this.extractVersion(ctx, config);
|
|
279
|
+
ctx.apiVersion = version;
|
|
280
|
+
// 对于 URI 版本控制,重写路径
|
|
281
|
+
if (config.type === 'URI' && version) {
|
|
282
|
+
const versionPrefix = `/${config.prefix || 'v'}${version}`;
|
|
283
|
+
if (ctx.path.startsWith(versionPrefix)) {
|
|
284
|
+
ctx.originalPath = ctx.path;
|
|
285
|
+
ctx.path = ctx.path.replace(versionPrefix, '') || '/';
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
await next();
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
extractVersion(ctx, config) {
|
|
292
|
+
// 自定义提取函数优先
|
|
293
|
+
if (config.extractVersionFn) {
|
|
294
|
+
return config.extractVersionFn(ctx);
|
|
295
|
+
}
|
|
296
|
+
const type = config.type || 'URI';
|
|
297
|
+
switch (type) {
|
|
298
|
+
case 'HEADER': {
|
|
299
|
+
const headerName = config.header || 'x-api-version';
|
|
300
|
+
const headerValue = ctx.headers[headerName];
|
|
301
|
+
if (typeof headerValue === 'string') {
|
|
302
|
+
return headerValue.replace(/^v/, '');
|
|
303
|
+
}
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
case 'MEDIA_TYPE': {
|
|
307
|
+
const accept = ctx.headers.accept;
|
|
308
|
+
const paramName = config.mediaTypeParam || 'version';
|
|
309
|
+
const match = accept?.match(new RegExp(`${paramName}=(\\d+)`));
|
|
310
|
+
return match ? match[1] : undefined;
|
|
311
|
+
}
|
|
312
|
+
case 'URI': {
|
|
313
|
+
const prefix = config.prefix || 'v';
|
|
314
|
+
const uriMatch = ctx.path.match(new RegExp(`^/${prefix}(\\d+)`));
|
|
315
|
+
return uriMatch ? uriMatch[1] : undefined;
|
|
316
|
+
}
|
|
317
|
+
default:
|
|
318
|
+
return config.defaultVersion;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
269
321
|
};
|
|
270
322
|
exports.MidwayKoaFramework = MidwayKoaFramework;
|
|
271
323
|
exports.MidwayKoaFramework = MidwayKoaFramework = __decorate([
|
package/dist/interface.d.ts
CHANGED
|
@@ -100,6 +100,39 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
|
|
|
100
100
|
* listen options
|
|
101
101
|
*/
|
|
102
102
|
listenOptions?: ListenOptions;
|
|
103
|
+
/**
|
|
104
|
+
* 版本控制配置
|
|
105
|
+
*/
|
|
106
|
+
versioning?: {
|
|
107
|
+
/**
|
|
108
|
+
* 是否启用版本控制
|
|
109
|
+
*/
|
|
110
|
+
enabled?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* 默认版本控制类型
|
|
113
|
+
*/
|
|
114
|
+
type?: 'URI' | 'HEADER' | 'MEDIA_TYPE' | 'CUSTOM';
|
|
115
|
+
/**
|
|
116
|
+
* 默认版本
|
|
117
|
+
*/
|
|
118
|
+
defaultVersion?: string;
|
|
119
|
+
/**
|
|
120
|
+
* URI 版本前缀,默认为 'v'
|
|
121
|
+
*/
|
|
122
|
+
prefix?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Header 版本控制时的 header 名称
|
|
125
|
+
*/
|
|
126
|
+
header?: string;
|
|
127
|
+
/**
|
|
128
|
+
* Media Type 版本控制时的参数名
|
|
129
|
+
*/
|
|
130
|
+
mediaTypeParam?: string;
|
|
131
|
+
/**
|
|
132
|
+
* 自定义版本提取函数
|
|
133
|
+
*/
|
|
134
|
+
extractVersionFn?: (ctx: IMidwayKoaContext) => string | undefined;
|
|
135
|
+
};
|
|
103
136
|
}
|
|
104
137
|
export type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
|
|
105
138
|
export interface IWebMiddleware {
|
|
@@ -108,6 +141,8 @@ export interface IWebMiddleware {
|
|
|
108
141
|
export type Application = IMidwayKoaApplication;
|
|
109
142
|
export interface Context extends IMidwayKoaContext {
|
|
110
143
|
state: State;
|
|
144
|
+
apiVersion?: string;
|
|
145
|
+
originalPath?: string;
|
|
111
146
|
}
|
|
112
147
|
export interface BodyParserOptions {
|
|
113
148
|
enable?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/koa",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.3",
|
|
4
4
|
"description": "Midway Web Framework for KOA",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
28
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
27
|
+
"@midwayjs/core": "^4.0.0-beta.3",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.3",
|
|
29
29
|
"@types/koa-router": "7.4.8",
|
|
30
30
|
"axios": "1.12.0",
|
|
31
31
|
"fs-extra": "11.3.0"
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@koa/router": "^12.0.0",
|
|
35
35
|
"@midwayjs/cookies": "^1.3.0",
|
|
36
|
-
"@midwayjs/session": "^4.0.0-beta.
|
|
36
|
+
"@midwayjs/session": "^4.0.0-beta.3",
|
|
37
37
|
"@types/koa": "3.0.0",
|
|
38
38
|
"@types/qs": "6.9.18",
|
|
39
39
|
"koa": "3.0.1",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"engines": {
|
|
49
49
|
"node": ">=20"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "b5b7dfaafb81823cdba5fd10ad2709b3e495c6b4"
|
|
52
52
|
}
|