@midwayjs/web 4.0.0-beta.1 → 4.0.0-beta.11
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/base.js +6 -4
- package/dist/configuration.js +4 -0
- package/dist/decorator.js +2 -3
- package/dist/framework/lifecycle.js +4 -1
- package/dist/framework/web.d.ts +2 -0
- package/dist/framework/web.js +83 -6
- package/dist/index.js +2 -2
- package/dist/interface.d.ts +41 -2
- package/dist/logger.d.ts +1 -1
- package/dist/logger.js +2 -0
- package/dist/utils.js +6 -6
- package/package.json +9 -9
package/README.md
CHANGED
package/dist/base.js
CHANGED
|
@@ -35,10 +35,12 @@ function getFramework() {
|
|
|
35
35
|
const createAppWorkerLoader = () => {
|
|
36
36
|
const AppWorkerLoader = require(getFramework())?.AppWorkerLoader || require('egg').AppWorkerLoader;
|
|
37
37
|
class EggAppWorkerLoader extends AppWorkerLoader {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
app;
|
|
39
|
+
framework;
|
|
40
|
+
bootstrap;
|
|
41
|
+
useEggSocketIO = false;
|
|
42
|
+
applicationContext;
|
|
43
|
+
lifecycleService;
|
|
42
44
|
getEggPaths() {
|
|
43
45
|
if (!this.appDir) {
|
|
44
46
|
// 这里的逻辑是为了兼容老 cluster 模式
|
package/dist/configuration.js
CHANGED
|
@@ -13,6 +13,10 @@ exports.EggConfiguration = void 0;
|
|
|
13
13
|
const core_1 = require("@midwayjs/core");
|
|
14
14
|
const path_1 = require("path");
|
|
15
15
|
let EggConfiguration = class EggConfiguration {
|
|
16
|
+
baseDir;
|
|
17
|
+
appDir;
|
|
18
|
+
app;
|
|
19
|
+
decoratorService;
|
|
16
20
|
init() {
|
|
17
21
|
this.decoratorService.registerParameterHandler(core_1.WEB_ROUTER_PARAM_KEY, options => {
|
|
18
22
|
return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1]);
|
package/dist/decorator.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.AgentApp = AgentApp;
|
|
4
|
+
exports.RunInEggAgent = RunInEggAgent;
|
|
4
5
|
const core_1 = require("@midwayjs/core");
|
|
5
6
|
const interface_1 = require("./interface");
|
|
6
7
|
function AgentApp() {
|
|
7
8
|
return core_1.DecoratorManager.createCustomPropertyDecorator(interface_1.EGG_AGENT_APP_KEY, {});
|
|
8
9
|
}
|
|
9
|
-
exports.AgentApp = AgentApp;
|
|
10
10
|
function RunInEggAgent() {
|
|
11
11
|
return function (target) {
|
|
12
12
|
core_1.DecoratorManager.saveModule(interface_1.RUN_IN_AGENT_KEY, target);
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
exports.RunInEggAgent = RunInEggAgent;
|
|
16
15
|
//# sourceMappingURL=decorator.js.map
|
|
@@ -15,9 +15,12 @@ const functional_1 = require("@midwayjs/core/functional");
|
|
|
15
15
|
const util_1 = require("util");
|
|
16
16
|
const debug = (0, util_1.debuglog)('midway:debug');
|
|
17
17
|
let MidwayWebLifeCycleService = class MidwayWebLifeCycleService {
|
|
18
|
+
applicationContext;
|
|
19
|
+
frameworkService;
|
|
20
|
+
configService;
|
|
21
|
+
lifecycleInstanceList = [];
|
|
18
22
|
constructor(applicationContext) {
|
|
19
23
|
this.applicationContext = applicationContext;
|
|
20
|
-
this.lifecycleInstanceList = [];
|
|
21
24
|
}
|
|
22
25
|
async init() {
|
|
23
26
|
// run lifecycle
|
package/dist/framework/web.d.ts
CHANGED
|
@@ -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
|
package/dist/framework/web.js
CHANGED
|
@@ -15,8 +15,11 @@ const router_1 = require("@eggjs/router");
|
|
|
15
15
|
const logger_1 = require("@midwayjs/logger");
|
|
16
16
|
const path_1 = require("path");
|
|
17
17
|
const util_1 = require("util");
|
|
18
|
+
const net_1 = require("net");
|
|
18
19
|
const debug = (0, util_1.debuglog)('midway:debug');
|
|
19
20
|
class EggControllerGenerator extends core_1.WebControllerGenerator {
|
|
21
|
+
app;
|
|
22
|
+
webRouterService;
|
|
20
23
|
constructor(app, webRouterService) {
|
|
21
24
|
super(app, webRouterService);
|
|
22
25
|
this.app = app;
|
|
@@ -32,10 +35,13 @@ class EggControllerGenerator extends core_1.WebControllerGenerator {
|
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
loggers;
|
|
39
|
+
generator;
|
|
40
|
+
server;
|
|
41
|
+
agent;
|
|
42
|
+
isClusterMode = false;
|
|
43
|
+
webRouterService;
|
|
44
|
+
appDir;
|
|
39
45
|
configure() {
|
|
40
46
|
process.env.EGG_TYPESCRIPT = 'true';
|
|
41
47
|
if (process.env['EGG_CLUSTER_MODE'] === 'true') {
|
|
@@ -79,9 +85,15 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
79
85
|
throw new core_1.httpError.NotFoundError(`${ctx.path} Not Found`);
|
|
80
86
|
}
|
|
81
87
|
};
|
|
88
|
+
const applyMiddlewares = [midwayRouterNotFound];
|
|
89
|
+
const versioningConfig = this.configurationOptions.versioning;
|
|
90
|
+
// 如果启用版本控制,添加版本处理中间件
|
|
91
|
+
if (versioningConfig?.enabled) {
|
|
92
|
+
applyMiddlewares.push(this.createVersioningMiddleware(versioningConfig));
|
|
93
|
+
}
|
|
82
94
|
// insert error handler
|
|
83
95
|
const midwayRootMiddleware = async (ctx, next) => {
|
|
84
|
-
await (await this.applyMiddleware(
|
|
96
|
+
await (await this.applyMiddleware(applyMiddlewares))(ctx, next);
|
|
85
97
|
};
|
|
86
98
|
this.app.use(midwayRootMiddleware);
|
|
87
99
|
this.webRouterService = await this.applicationContext.getAsync(core_1.MidwayWebRouterService, [
|
|
@@ -91,6 +103,7 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
91
103
|
]);
|
|
92
104
|
this.generator = new EggControllerGenerator(this.app, this.webRouterService);
|
|
93
105
|
this.overwriteApplication('app');
|
|
106
|
+
this.app.loader.app = this.app;
|
|
94
107
|
this.app.loader.loadOrigin();
|
|
95
108
|
// 这里拦截 app.use 方法,让他可以加到 midway 的 middlewareManager 中
|
|
96
109
|
this.app.originUse = this.app.use;
|
|
@@ -200,7 +213,10 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
200
213
|
this.getApplicationContext().registerObject(core_1.HTTP_SERVER_KEY, this.server);
|
|
201
214
|
const eggConfig = this.configService.getConfiguration('egg');
|
|
202
215
|
if (!this.isClusterMode && eggConfig) {
|
|
203
|
-
|
|
216
|
+
let customPort = process.env.MIDWAY_HTTP_PORT ?? eggConfig.port;
|
|
217
|
+
if (customPort === 0) {
|
|
218
|
+
customPort = await getFreePort();
|
|
219
|
+
}
|
|
204
220
|
if (customPort) {
|
|
205
221
|
new Promise(resolve => {
|
|
206
222
|
const args = [customPort];
|
|
@@ -251,6 +267,52 @@ let MidwayWebFramework = class MidwayWebFramework extends core_1.BaseFramework {
|
|
|
251
267
|
setServer(server) {
|
|
252
268
|
this.server = server;
|
|
253
269
|
}
|
|
270
|
+
createVersioningMiddleware(config) {
|
|
271
|
+
return async (ctx, next) => {
|
|
272
|
+
// 提取版本信息
|
|
273
|
+
const version = this.extractVersion(ctx, config);
|
|
274
|
+
ctx.apiVersion = version;
|
|
275
|
+
// 对于 URI 版本控制,重写路径
|
|
276
|
+
if (config.type === 'URI' && version) {
|
|
277
|
+
const versionPrefix = `/${config.prefix || 'v'}${version}`;
|
|
278
|
+
if (ctx.path.startsWith(versionPrefix)) {
|
|
279
|
+
ctx.originalPath = ctx.path;
|
|
280
|
+
ctx.path = ctx.path.replace(versionPrefix, '') || '/';
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
await next();
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
extractVersion(ctx, config) {
|
|
287
|
+
// 自定义提取函数优先
|
|
288
|
+
if (config.extractVersionFn) {
|
|
289
|
+
return config.extractVersionFn(ctx);
|
|
290
|
+
}
|
|
291
|
+
const type = config.type || 'URI';
|
|
292
|
+
switch (type) {
|
|
293
|
+
case 'HEADER': {
|
|
294
|
+
const headerName = config.header || 'x-api-version';
|
|
295
|
+
const headerValue = ctx.headers[headerName];
|
|
296
|
+
if (typeof headerValue === 'string') {
|
|
297
|
+
return headerValue.replace(/^v/, '');
|
|
298
|
+
}
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
case 'MEDIA_TYPE': {
|
|
302
|
+
const accept = ctx.headers.accept;
|
|
303
|
+
const paramName = config.mediaTypeParam || 'version';
|
|
304
|
+
const match = accept?.match(new RegExp(`${paramName}=(\\\\d+)`));
|
|
305
|
+
return match ? match[1] : undefined;
|
|
306
|
+
}
|
|
307
|
+
case 'URI': {
|
|
308
|
+
const prefix = config.prefix || 'v';
|
|
309
|
+
const uriMatch = ctx.path.match(new RegExp(`^/${prefix}(\\\\d+)`));
|
|
310
|
+
return uriMatch ? uriMatch[1] : undefined;
|
|
311
|
+
}
|
|
312
|
+
default:
|
|
313
|
+
return config.defaultVersion;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
254
316
|
};
|
|
255
317
|
exports.MidwayWebFramework = MidwayWebFramework;
|
|
256
318
|
__decorate([
|
|
@@ -260,4 +322,19 @@ __decorate([
|
|
|
260
322
|
exports.MidwayWebFramework = MidwayWebFramework = __decorate([
|
|
261
323
|
(0, core_1.Framework)()
|
|
262
324
|
], MidwayWebFramework);
|
|
325
|
+
async function getFreePort() {
|
|
326
|
+
return new Promise((resolve, reject) => {
|
|
327
|
+
const server = (0, net_1.createServer)();
|
|
328
|
+
server.listen(0, () => {
|
|
329
|
+
try {
|
|
330
|
+
const port = server.address().port;
|
|
331
|
+
server.close();
|
|
332
|
+
resolve(port);
|
|
333
|
+
}
|
|
334
|
+
catch (err) {
|
|
335
|
+
reject(err);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
}
|
|
263
340
|
//# sourceMappingURL=web.js.map
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.Configuration = exports.Agent = exports.Application = exports.createAgentWorkerLoader = exports.createAppWorkerLoader = exports.createEggAgent = exports.createEggApplication = exports.Framework = void 0;
|
|
18
|
+
exports.startCluster = startCluster;
|
|
18
19
|
const egg_cluster_1 = require("egg-cluster");
|
|
19
20
|
const path_1 = require("path");
|
|
20
21
|
__exportStar(require("./interface"), exports);
|
|
@@ -41,5 +42,4 @@ function startCluster(serverConfig, callback) {
|
|
|
41
42
|
serverConfig['require'].push((0, path_1.join)(__dirname, 'cluster'));
|
|
42
43
|
return (0, egg_cluster_1.startCluster)(serverConfig, callback);
|
|
43
44
|
}
|
|
44
|
-
exports.startCluster = startCluster;
|
|
45
45
|
//# sourceMappingURL=index.js.map
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import { Context as EggContext, Application as EggApplication } from 'egg';
|
|
4
2
|
import { IMidwayContainer, IMidwayContext, IMidwayApplication, IConfigurationOptions, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
5
3
|
import { DefaultState, Middleware } from 'koa';
|
|
@@ -61,6 +59,14 @@ export interface Context<ResponseBodyT = unknown> extends IMidwayWebContext<Resp
|
|
|
61
59
|
[_: string]: any;
|
|
62
60
|
};
|
|
63
61
|
state: State;
|
|
62
|
+
/**
|
|
63
|
+
* API version extracted from request
|
|
64
|
+
*/
|
|
65
|
+
apiVersion?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Original path before version processing
|
|
68
|
+
*/
|
|
69
|
+
originalPath?: string;
|
|
64
70
|
}
|
|
65
71
|
/**
|
|
66
72
|
* @deprecated since version 3.0.0
|
|
@@ -120,6 +126,39 @@ export interface IMidwayWebConfigurationOptions extends IConfigurationOptions {
|
|
|
120
126
|
* https/https/http2 server options
|
|
121
127
|
*/
|
|
122
128
|
serverOptions?: Record<string, any>;
|
|
129
|
+
/**
|
|
130
|
+
* API versioning configuration
|
|
131
|
+
*/
|
|
132
|
+
versioning?: {
|
|
133
|
+
/**
|
|
134
|
+
* Enable versioning
|
|
135
|
+
*/
|
|
136
|
+
enabled: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Versioning type
|
|
139
|
+
*/
|
|
140
|
+
type?: 'URI' | 'HEADER' | 'MEDIA_TYPE' | 'CUSTOM';
|
|
141
|
+
/**
|
|
142
|
+
* Default version if none is specified
|
|
143
|
+
*/
|
|
144
|
+
defaultVersion?: string;
|
|
145
|
+
/**
|
|
146
|
+
* Version prefix for URI versioning (default: 'v')
|
|
147
|
+
*/
|
|
148
|
+
prefix?: string;
|
|
149
|
+
/**
|
|
150
|
+
* Header name for HEADER versioning (default: 'x-api-version')
|
|
151
|
+
*/
|
|
152
|
+
header?: string;
|
|
153
|
+
/**
|
|
154
|
+
* Media type parameter name for MEDIA_TYPE versioning (default: 'version')
|
|
155
|
+
*/
|
|
156
|
+
mediaTypeParam?: string;
|
|
157
|
+
/**
|
|
158
|
+
* Custom version extraction function
|
|
159
|
+
*/
|
|
160
|
+
extractVersionFn?: (ctx: Context) => string | undefined;
|
|
161
|
+
};
|
|
123
162
|
}
|
|
124
163
|
/**
|
|
125
164
|
* @deprecated since version 3.0.0
|
package/dist/logger.d.ts
CHANGED
|
@@ -34,6 +34,6 @@ declare class MidwayLoggers extends Map<string, ILogger> {
|
|
|
34
34
|
};
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
export declare const createLoggers: (app: Application, processType:
|
|
37
|
+
export declare const createLoggers: (app: Application, processType: "agent" | "app") => MidwayLoggers;
|
|
38
38
|
export {};
|
|
39
39
|
//# sourceMappingURL=logger.d.ts.map
|
package/dist/logger.js
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getCurrentDateString = exports.findLernaRoot = exports.parseNormalDir = void 0;
|
|
4
|
+
exports.initializeAgentApplicationContext = initializeAgentApplicationContext;
|
|
4
5
|
const path_1 = require("path");
|
|
5
6
|
const find_up_1 = require("find-up");
|
|
6
7
|
const fs_1 = require("fs");
|
|
@@ -64,10 +65,10 @@ async function initializeAgentApplicationContext(agent) {
|
|
|
64
65
|
agentFramework['logger'] = agent.coreLogger;
|
|
65
66
|
agentFramework['appLogger'] = agent.logger;
|
|
66
67
|
agentFramework.app = agent;
|
|
67
|
-
agentFramework
|
|
68
|
-
agentFramework
|
|
69
|
-
agentFramework
|
|
70
|
-
agentFramework
|
|
68
|
+
agentFramework['configService'] = applicationContext.get(core_1.MidwayConfigService);
|
|
69
|
+
agentFramework['environmentService'] = applicationContext.get(core_1.MidwayEnvironmentService);
|
|
70
|
+
agentFramework['loggerService'] = applicationContext.get(core_1.MidwayLoggerService);
|
|
71
|
+
agentFramework['informationService'] = applicationContext.get(core_1.MidwayInformationService);
|
|
71
72
|
agentFramework.overwriteApplication('agent');
|
|
72
73
|
// init decorator service
|
|
73
74
|
const decoratorService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayDecoratorService);
|
|
@@ -115,5 +116,4 @@ async function initializeAgentApplicationContext(agent) {
|
|
|
115
116
|
}
|
|
116
117
|
return applicationContext;
|
|
117
118
|
}
|
|
118
|
-
exports.initializeAgentApplicationContext = initializeAgentApplicationContext;
|
|
119
119
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/web",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.11",
|
|
4
4
|
"description": "Midway Web Framework for Egg.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -28,17 +28,17 @@
|
|
|
28
28
|
],
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@midwayjs/logger": "^
|
|
32
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
31
|
+
"@midwayjs/logger": "^4.0.0",
|
|
32
|
+
"@midwayjs/mock": "^4.0.0-beta.11",
|
|
33
33
|
"@types/koa": "2.15.0",
|
|
34
|
-
"dayjs": "1.11.
|
|
34
|
+
"dayjs": "1.11.19",
|
|
35
35
|
"egg-logger": "3.6.1",
|
|
36
36
|
"egg-mock": "4.2.1",
|
|
37
37
|
"egg-scripts": "3.1.0",
|
|
38
38
|
"egg-socket.io": "4.1.6",
|
|
39
39
|
"egg-view-nunjucks": "2.3.0",
|
|
40
40
|
"fake-egg": "1.0.0",
|
|
41
|
-
"fs-extra": "11.3.
|
|
41
|
+
"fs-extra": "11.3.3",
|
|
42
42
|
"is-type-of": "^2.1.0",
|
|
43
43
|
"mm": "3.4.0",
|
|
44
44
|
"mz-modules": "^2.1.0",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@eggjs/router": "^2.0.0",
|
|
52
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
52
|
+
"@midwayjs/core": "^4.0.0-beta.11",
|
|
53
53
|
"egg": "^2.28.0",
|
|
54
54
|
"egg-cluster": "^1.27.1",
|
|
55
55
|
"egg-path-matching": "^2.1.0",
|
|
56
56
|
"find-up": "5.0.0",
|
|
57
57
|
"mkdirp": "1.0.4",
|
|
58
|
-
"qs": "6.14.
|
|
58
|
+
"qs": "6.14.1"
|
|
59
59
|
},
|
|
60
60
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
61
61
|
"repository": {
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"url": "https://github.com/midwayjs/midway.git"
|
|
64
64
|
},
|
|
65
65
|
"engines": {
|
|
66
|
-
"node": ">=
|
|
66
|
+
"node": ">=20"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "6ef05719ca6e900f1ec34aff7a5c5a9614358c50"
|
|
69
69
|
}
|