@midwayjs/web 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 CHANGED
@@ -9,4 +9,4 @@ Document: [https://midwayjs.org](https://midwayjs.org)
9
9
 
10
10
  ## License
11
11
 
12
- [MIT]((https://github.com/midwayjs/midway/blob/master/LICENSE))
12
+ [MIT](https://github.com/midwayjs/midway/blob/master/LICENSE)
@@ -22,5 +22,7 @@ export declare class MidwayWebFramework extends BaseFramework<Application, Conte
22
22
  generateMiddleware(middlewareId: any): Promise<any>;
23
23
  beforeStop(): Promise<void>;
24
24
  setServer(server: any): void;
25
+ private createVersioningMiddleware;
26
+ private extractVersion;
25
27
  }
26
28
  //# sourceMappingURL=web.d.ts.map
@@ -80,9 +80,15 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
80
80
  throw new core_1.httpError.NotFoundError(`${ctx.path} Not Found`);
81
81
  }
82
82
  };
83
+ const applyMiddlewares = [midwayRouterNotFound];
84
+ const versioningConfig = this.configurationOptions.versioning;
85
+ // 如果启用版本控制,添加版本处理中间件
86
+ if (versioningConfig?.enabled) {
87
+ applyMiddlewares.push(this.createVersioningMiddleware(versioningConfig));
88
+ }
83
89
  // insert error handler
84
90
  const midwayRootMiddleware = async (ctx, next) => {
85
- await (await this.applyMiddleware(midwayRouterNotFound))(ctx, next);
91
+ await (await this.applyMiddleware(applyMiddlewares))(ctx, next);
86
92
  };
87
93
  this.app.use(midwayRootMiddleware);
88
94
  this.webRouterService = await this.applicationContext.getAsync(core_1.MidwayWebRouterService, [
@@ -255,6 +261,52 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
255
261
  setServer(server) {
256
262
  this.server = server;
257
263
  }
264
+ createVersioningMiddleware(config) {
265
+ return async (ctx, next) => {
266
+ // 提取版本信息
267
+ const version = this.extractVersion(ctx, config);
268
+ ctx.apiVersion = version;
269
+ // 对于 URI 版本控制,重写路径
270
+ if (config.type === 'URI' && version) {
271
+ const versionPrefix = `/${config.prefix || 'v'}${version}`;
272
+ if (ctx.path.startsWith(versionPrefix)) {
273
+ ctx.originalPath = ctx.path;
274
+ ctx.path = ctx.path.replace(versionPrefix, '') || '/';
275
+ }
276
+ }
277
+ await next();
278
+ };
279
+ }
280
+ extractVersion(ctx, config) {
281
+ // 自定义提取函数优先
282
+ if (config.extractVersionFn) {
283
+ return config.extractVersionFn(ctx);
284
+ }
285
+ const type = config.type || 'URI';
286
+ switch (type) {
287
+ case 'HEADER': {
288
+ const headerName = config.header || 'x-api-version';
289
+ const headerValue = ctx.headers[headerName];
290
+ if (typeof headerValue === 'string') {
291
+ return headerValue.replace(/^v/, '');
292
+ }
293
+ return undefined;
294
+ }
295
+ case 'MEDIA_TYPE': {
296
+ const accept = ctx.headers.accept;
297
+ const paramName = config.mediaTypeParam || 'version';
298
+ const match = accept?.match(new RegExp(`${paramName}=(\\\\d+)`));
299
+ return match ? match[1] : undefined;
300
+ }
301
+ case 'URI': {
302
+ const prefix = config.prefix || 'v';
303
+ const uriMatch = ctx.path.match(new RegExp(`^/${prefix}(\\\\d+)`));
304
+ return uriMatch ? uriMatch[1] : undefined;
305
+ }
306
+ default:
307
+ return config.defaultVersion;
308
+ }
309
+ }
258
310
  };
259
311
  exports.MidwayWebFramework = MidwayWebFramework;
260
312
  __decorate([
@@ -61,6 +61,14 @@ export interface Context<ResponseBodyT = unknown> extends IMidwayWebContext<Resp
61
61
  [_: string]: any;
62
62
  };
63
63
  state: State;
64
+ /**
65
+ * API version extracted from request
66
+ */
67
+ apiVersion?: string;
68
+ /**
69
+ * Original path before version processing
70
+ */
71
+ originalPath?: string;
64
72
  }
65
73
  /**
66
74
  * @deprecated since version 3.0.0
@@ -120,6 +128,39 @@ export interface IMidwayWebConfigurationOptions extends IConfigurationOptions {
120
128
  * https/https/http2 server options
121
129
  */
122
130
  serverOptions?: Record<string, any>;
131
+ /**
132
+ * API versioning configuration
133
+ */
134
+ versioning?: {
135
+ /**
136
+ * Enable versioning
137
+ */
138
+ enabled: boolean;
139
+ /**
140
+ * Versioning type
141
+ */
142
+ type?: 'URI' | 'HEADER' | 'MEDIA_TYPE' | 'CUSTOM';
143
+ /**
144
+ * Default version if none is specified
145
+ */
146
+ defaultVersion?: string;
147
+ /**
148
+ * Version prefix for URI versioning (default: 'v')
149
+ */
150
+ prefix?: string;
151
+ /**
152
+ * Header name for HEADER versioning (default: 'x-api-version')
153
+ */
154
+ header?: string;
155
+ /**
156
+ * Media type parameter name for MEDIA_TYPE versioning (default: 'version')
157
+ */
158
+ mediaTypeParam?: string;
159
+ /**
160
+ * Custom version extraction function
161
+ */
162
+ extractVersionFn?: (ctx: Context) => string | undefined;
163
+ };
123
164
  }
124
165
  /**
125
166
  * @deprecated since version 3.0.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/web",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.3",
4
4
  "description": "Midway Web Framework for Egg.js",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "license": "MIT",
30
30
  "devDependencies": {
31
31
  "@midwayjs/logger": "^3.0.0",
32
- "@midwayjs/mock": "^4.0.0-beta.2",
32
+ "@midwayjs/mock": "^4.0.0-beta.3",
33
33
  "@types/koa": "2.15.0",
34
34
  "dayjs": "1.11.13",
35
35
  "egg-logger": "3.6.1",
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "@eggjs/router": "^2.0.0",
52
- "@midwayjs/core": "^4.0.0-beta.2",
52
+ "@midwayjs/core": "^4.0.0-beta.3",
53
53
  "egg": "^2.28.0",
54
54
  "egg-cluster": "^1.27.1",
55
55
  "egg-path-matching": "^2.1.0",
@@ -65,5 +65,5 @@
65
65
  "engines": {
66
66
  "node": ">=20"
67
67
  },
68
- "gitHead": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
68
+ "gitHead": "b5b7dfaafb81823cdba5fd10ad2709b3e495c6b4"
69
69
  }