@midwayjs/koa 4.0.0-alpha.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/README.md +1 -1
- package/dist/config/config.default.d.ts +13 -1
- package/dist/config/config.default.js +34 -20
- package/dist/configuration.js +3 -0
- package/dist/framework.d.ts +2 -6
- package/dist/framework.js +95 -24
- package/dist/interface.d.ts +45 -5
- package/dist/middleware/bodyparser.middleware.js +1 -0
- package/dist/middleware/fav.middleware.js +1 -0
- package/dist/onerror.js +1 -2
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +23 -6
- package/package.json +14 -13
package/README.md
CHANGED
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
* koa server options
|
|
3
3
|
*/
|
|
4
4
|
export declare const koa: {
|
|
5
|
-
contextLoggerFormat: (info: any) => string;
|
|
6
5
|
serverTimeout: number;
|
|
7
6
|
};
|
|
7
|
+
export declare const midwayLogger: {
|
|
8
|
+
clients: {
|
|
9
|
+
appLogger: {
|
|
10
|
+
contextLoggerFormat: (info: any) => string;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* The key that signing cookies. It can contain multiple keys seperated by `,`.
|
|
10
16
|
* @member {String} Config#keys
|
|
@@ -18,6 +24,12 @@ export declare const keys = "";
|
|
|
18
24
|
* @property {Boolean} httpOnly - httpOnly property, defaults is true
|
|
19
25
|
*/
|
|
20
26
|
export declare const cookies: {};
|
|
27
|
+
/**
|
|
28
|
+
* default cookie get options
|
|
29
|
+
*/
|
|
30
|
+
export declare const cookiesExtra: {
|
|
31
|
+
defaultGetOptions: {};
|
|
32
|
+
};
|
|
21
33
|
export declare const onerror: {};
|
|
22
34
|
/**
|
|
23
35
|
* @member Config#bodyParser
|
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.siteFile = exports.bodyParser = exports.onerror = exports.cookies = exports.keys = exports.koa = void 0;
|
|
3
|
+
exports.siteFile = exports.bodyParser = exports.onerror = exports.cookiesExtra = exports.cookies = exports.keys = exports.midwayLogger = exports.koa = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* koa server options
|
|
6
6
|
*/
|
|
7
7
|
exports.koa = {
|
|
8
|
-
contextLoggerFormat: info => {
|
|
9
|
-
const ctx = info.ctx;
|
|
10
|
-
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
11
|
-
const userId = ctx.userId || '-';
|
|
12
|
-
const traceId = ctx.traceId ?? ctx.tracer?.traceId ?? '-';
|
|
13
|
-
const use = Date.now() - ctx.startTime;
|
|
14
|
-
const label = userId +
|
|
15
|
-
'/' +
|
|
16
|
-
ctx.ip +
|
|
17
|
-
'/' +
|
|
18
|
-
traceId +
|
|
19
|
-
'/' +
|
|
20
|
-
use +
|
|
21
|
-
'ms ' +
|
|
22
|
-
ctx.method +
|
|
23
|
-
' ' +
|
|
24
|
-
ctx.url;
|
|
25
|
-
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`;
|
|
26
|
-
},
|
|
27
8
|
serverTimeout: 2 * 60 * 1000,
|
|
28
9
|
};
|
|
10
|
+
exports.midwayLogger = {
|
|
11
|
+
clients: {
|
|
12
|
+
appLogger: {
|
|
13
|
+
contextLoggerFormat: info => {
|
|
14
|
+
const ctx = info.ctx;
|
|
15
|
+
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
16
|
+
const userId = ctx.userId || '-';
|
|
17
|
+
const traceId = ctx.traceId ?? ctx.tracer?.traceId ?? '-';
|
|
18
|
+
const use = Date.now() - ctx.startTime;
|
|
19
|
+
const label = userId +
|
|
20
|
+
'/' +
|
|
21
|
+
ctx.ip +
|
|
22
|
+
'/' +
|
|
23
|
+
traceId +
|
|
24
|
+
'/' +
|
|
25
|
+
use +
|
|
26
|
+
'ms ' +
|
|
27
|
+
ctx.method +
|
|
28
|
+
' ' +
|
|
29
|
+
ctx.url;
|
|
30
|
+
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${label}] ${info.message}`;
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
29
35
|
/**
|
|
30
36
|
* The key that signing cookies. It can contain multiple keys seperated by `,`.
|
|
31
37
|
* @member {String} Config#keys
|
|
@@ -42,6 +48,14 @@ exports.cookies = {
|
|
|
42
48
|
// httpOnly: true | false,
|
|
43
49
|
// sameSite: 'none|lax|strict',
|
|
44
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* default cookie get options
|
|
53
|
+
*/
|
|
54
|
+
exports.cookiesExtra = {
|
|
55
|
+
defaultGetOptions: {
|
|
56
|
+
// sign: false,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
45
59
|
exports.onerror = {};
|
|
46
60
|
/**
|
|
47
61
|
* @member Config#bodyParser
|
package/dist/configuration.js
CHANGED
|
@@ -17,6 +17,9 @@ const DefaultConfig = require("./config/config.default");
|
|
|
17
17
|
const bodyparser_middleware_1 = require("./middleware/bodyparser.middleware");
|
|
18
18
|
const fav_middleware_1 = require("./middleware/fav.middleware");
|
|
19
19
|
let KoaConfiguration = class KoaConfiguration {
|
|
20
|
+
decoratorService;
|
|
21
|
+
koaFramework;
|
|
22
|
+
configService;
|
|
20
23
|
init() {
|
|
21
24
|
// register param decorator
|
|
22
25
|
this.decoratorService.registerParameterHandler(core_1.WEB_ROUTER_PARAM_KEY, options => {
|
package/dist/framework.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { BaseFramework, CommonFilterUnion, CommonMiddlewareUnion, IMidwayBootstrapOptions, RouterInfo } from '@midwayjs/core';
|
|
3
2
|
import { IMidwayKoaApplication, IMidwayKoaConfigurationOptions, IMidwayKoaContext } from './interface';
|
|
4
3
|
import type { DefaultState, Middleware, Next } from 'koa';
|
|
@@ -14,11 +13,6 @@ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplicat
|
|
|
14
13
|
* wrap controller string to middleware function
|
|
15
14
|
*/
|
|
16
15
|
generateController(routeInfo: RouterInfo): Middleware<DefaultState, IMidwayKoaContext>;
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated
|
|
19
|
-
* @param middlewareId
|
|
20
|
-
*/
|
|
21
|
-
generateMiddleware(middlewareId: any): Promise<Middleware<DefaultState, IMidwayKoaContext, any>>;
|
|
22
16
|
run(): Promise<void>;
|
|
23
17
|
beforeStop(): Promise<void>;
|
|
24
18
|
getFrameworkName(): string;
|
|
@@ -26,5 +20,7 @@ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplicat
|
|
|
26
20
|
getPort(): string;
|
|
27
21
|
useMiddleware(Middleware: CommonMiddlewareUnion<IMidwayKoaContext, Next, unknown>): void;
|
|
28
22
|
useFilter(Filter: CommonFilterUnion<IMidwayKoaContext, Next, unknown>): void;
|
|
23
|
+
private createVersioningMiddleware;
|
|
24
|
+
private extractVersion;
|
|
29
25
|
}
|
|
30
26
|
//# sourceMappingURL=framework.d.ts.map
|
package/dist/framework.js
CHANGED
|
@@ -14,8 +14,11 @@ const koa = require("koa");
|
|
|
14
14
|
const onerror_1 = require("./onerror");
|
|
15
15
|
const qs = require("qs");
|
|
16
16
|
const querystring = require("querystring");
|
|
17
|
+
const utils_1 = require("./utils");
|
|
17
18
|
const COOKIES = Symbol('context#cookies');
|
|
18
19
|
class KoaControllerGenerator extends core_1.WebControllerGenerator {
|
|
20
|
+
app;
|
|
21
|
+
webRouterService;
|
|
19
22
|
constructor(app, webRouterService) {
|
|
20
23
|
super(app, webRouterService);
|
|
21
24
|
this.app = app;
|
|
@@ -31,6 +34,9 @@ class KoaControllerGenerator extends core_1.WebControllerGenerator {
|
|
|
31
34
|
}
|
|
32
35
|
}
|
|
33
36
|
let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
37
|
+
server;
|
|
38
|
+
generator;
|
|
39
|
+
webRouterService;
|
|
34
40
|
configure() {
|
|
35
41
|
return this.configService.getConfiguration('koa');
|
|
36
42
|
}
|
|
@@ -41,6 +47,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
41
47
|
throw new core_1.MidwayConfigMissingError('config.keys');
|
|
42
48
|
}
|
|
43
49
|
const cookieOptions = this.configService.getConfiguration('cookies');
|
|
50
|
+
const cookieGetOptions = this.configService.getConfiguration('cookiesExtra.defaultGetOptions');
|
|
44
51
|
this.app = new koa({
|
|
45
52
|
keys: [].concat(appKeys),
|
|
46
53
|
proxy: this.configurationOptions.proxy,
|
|
@@ -51,7 +58,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
51
58
|
Object.defineProperty(this.app.context, 'cookies', {
|
|
52
59
|
get() {
|
|
53
60
|
if (!this[COOKIES]) {
|
|
54
|
-
this[COOKIES] = new cookies_1.Cookies(this, this.app.keys, cookieOptions);
|
|
61
|
+
this[COOKIES] = new cookies_1.Cookies(this, this.app.keys, cookieOptions, cookieGetOptions);
|
|
55
62
|
}
|
|
56
63
|
return this[COOKIES];
|
|
57
64
|
},
|
|
@@ -108,7 +115,13 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
108
115
|
return c[str];
|
|
109
116
|
if (self.configurationOptions.queryParseMode) {
|
|
110
117
|
// use qs module to parse query
|
|
111
|
-
|
|
118
|
+
const parseOptions = {
|
|
119
|
+
...self.configurationOptions.queryParseOptions,
|
|
120
|
+
...(self.configurationOptions.queryParseMode === 'first'
|
|
121
|
+
? { duplicates: 'first' }
|
|
122
|
+
: {}),
|
|
123
|
+
};
|
|
124
|
+
c[str] = qs.parse(str, parseOptions);
|
|
112
125
|
}
|
|
113
126
|
else {
|
|
114
127
|
// use querystring to parse query by default
|
|
@@ -135,10 +148,16 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
135
148
|
throw new core_1.httpError.NotFoundError(`${ctx.path} Not Found`);
|
|
136
149
|
}
|
|
137
150
|
};
|
|
151
|
+
const applyMiddlewares = [notFound];
|
|
152
|
+
// versioning middleware
|
|
153
|
+
const versioningConfig = this.configurationOptions.versioning;
|
|
154
|
+
if (versioningConfig?.enabled) {
|
|
155
|
+
applyMiddlewares.push(this.createVersioningMiddleware(versioningConfig));
|
|
156
|
+
}
|
|
138
157
|
// root middleware
|
|
139
158
|
const midwayRootMiddleware = async (ctx, next) => {
|
|
140
159
|
this.app.createAnonymousContext(ctx);
|
|
141
|
-
await (await this.applyMiddleware(
|
|
160
|
+
await (await this.applyMiddleware(applyMiddlewares))(ctx, next);
|
|
142
161
|
if (ctx.body === undefined &&
|
|
143
162
|
!ctx.response._explicitStatus &&
|
|
144
163
|
ctx._matchedRoute) {
|
|
@@ -153,7 +172,10 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
153
172
|
},
|
|
154
173
|
]);
|
|
155
174
|
this.generator = new KoaControllerGenerator(this.app, this.webRouterService);
|
|
156
|
-
this.defineApplicationProperties(
|
|
175
|
+
this.defineApplicationProperties({
|
|
176
|
+
generateController: this.generateController.bind(this),
|
|
177
|
+
getPort: this.getPort.bind(this),
|
|
178
|
+
});
|
|
157
179
|
// hack use method
|
|
158
180
|
this.app.originUse = this.app.use;
|
|
159
181
|
this.app.use = this.app.useMiddleware;
|
|
@@ -171,14 +193,6 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
171
193
|
generateController(routeInfo) {
|
|
172
194
|
return this.generator.generateKoaController(routeInfo);
|
|
173
195
|
}
|
|
174
|
-
/**
|
|
175
|
-
* @deprecated
|
|
176
|
-
* @param middlewareId
|
|
177
|
-
*/
|
|
178
|
-
async generateMiddleware(middlewareId) {
|
|
179
|
-
const mwIns = await this.getApplicationContext().getAsync(middlewareId);
|
|
180
|
-
return mwIns.resolve();
|
|
181
|
-
}
|
|
182
196
|
async run() {
|
|
183
197
|
// load controller
|
|
184
198
|
await this.loadMidwayController();
|
|
@@ -190,9 +204,9 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
190
204
|
};
|
|
191
205
|
// https config
|
|
192
206
|
if (serverOptions.key && serverOptions.cert) {
|
|
193
|
-
serverOptions.key = core_1.
|
|
194
|
-
serverOptions.cert = core_1.
|
|
195
|
-
serverOptions.ca = core_1.
|
|
207
|
+
serverOptions.key = core_1.PathFileUtils.getFileContentSync(serverOptions.key);
|
|
208
|
+
serverOptions.cert = core_1.PathFileUtils.getFileContentSync(serverOptions.cert);
|
|
209
|
+
serverOptions.ca = core_1.PathFileUtils.getFileContentSync(serverOptions.ca);
|
|
196
210
|
process.env.MIDWAY_HTTP_SSL = 'true';
|
|
197
211
|
if (serverOptions.http2) {
|
|
198
212
|
this.server = require('http2').createSecureServer(serverOptions, this.app.callback());
|
|
@@ -215,27 +229,38 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
215
229
|
if (core_1.Types.isNumber(this.configurationOptions.serverTimeout)) {
|
|
216
230
|
this.server.setTimeout(this.configurationOptions.serverTimeout);
|
|
217
231
|
}
|
|
232
|
+
this.configurationOptions.listenOptions = {
|
|
233
|
+
port: this.configurationOptions.port,
|
|
234
|
+
host: this.configurationOptions.hostname,
|
|
235
|
+
...this.configurationOptions.listenOptions,
|
|
236
|
+
};
|
|
218
237
|
// set port and listen server
|
|
219
|
-
|
|
220
|
-
|
|
238
|
+
let customPort = process.env.MIDWAY_HTTP_PORT ||
|
|
239
|
+
this.configurationOptions.listenOptions.port;
|
|
240
|
+
if (customPort === 0 || customPort === '0') {
|
|
241
|
+
customPort = await (0, utils_1.getFreePort)();
|
|
242
|
+
this.logger.info(`[midway:koa] server has auto-assigned port ${customPort}`);
|
|
243
|
+
}
|
|
244
|
+
this.configurationOptions.listenOptions.port = Number(customPort);
|
|
245
|
+
if (this.configurationOptions.listenOptions.port) {
|
|
221
246
|
new Promise(resolve => {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
args.push(this.configurationOptions.hostname);
|
|
225
|
-
}
|
|
226
|
-
args.push(() => {
|
|
247
|
+
// 使用 ListenOptions 对象启动服务器
|
|
248
|
+
this.server.listen(this.configurationOptions.listenOptions, () => {
|
|
227
249
|
resolve();
|
|
228
250
|
});
|
|
229
|
-
|
|
230
|
-
process.env.MIDWAY_HTTP_PORT = String(
|
|
251
|
+
// 设置环境变量
|
|
252
|
+
process.env.MIDWAY_HTTP_PORT = String(this.configurationOptions.listenOptions.port);
|
|
231
253
|
});
|
|
254
|
+
this.logger.debug(`[midway:koa] server is listening on port ${customPort}`);
|
|
232
255
|
}
|
|
233
256
|
}
|
|
234
257
|
async beforeStop() {
|
|
235
258
|
if (this.server) {
|
|
236
259
|
new Promise(resolve => {
|
|
237
260
|
this.server.close(resolve);
|
|
261
|
+
process.env.MIDWAY_HTTP_PORT = '';
|
|
238
262
|
});
|
|
263
|
+
this.logger.debug('[midway:koa] server is stopped!');
|
|
239
264
|
}
|
|
240
265
|
}
|
|
241
266
|
getFrameworkName() {
|
|
@@ -253,6 +278,52 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
|
|
|
253
278
|
useFilter(Filter) {
|
|
254
279
|
this.filterManager.useFilter(Filter);
|
|
255
280
|
}
|
|
281
|
+
createVersioningMiddleware(config) {
|
|
282
|
+
return async (ctx, next) => {
|
|
283
|
+
// 提取版本信息
|
|
284
|
+
const version = this.extractVersion(ctx, config);
|
|
285
|
+
ctx.apiVersion = version;
|
|
286
|
+
// 对于 URI 版本控制,重写路径
|
|
287
|
+
if (config.type === 'URI' && version) {
|
|
288
|
+
const versionPrefix = `/${config.prefix || 'v'}${version}`;
|
|
289
|
+
if (ctx.path.startsWith(versionPrefix)) {
|
|
290
|
+
ctx.originalPath = ctx.path;
|
|
291
|
+
ctx.path = ctx.path.replace(versionPrefix, '') || '/';
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
await next();
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
extractVersion(ctx, config) {
|
|
298
|
+
// 自定义提取函数优先
|
|
299
|
+
if (config.extractVersionFn) {
|
|
300
|
+
return config.extractVersionFn(ctx);
|
|
301
|
+
}
|
|
302
|
+
const type = config.type || 'URI';
|
|
303
|
+
switch (type) {
|
|
304
|
+
case 'HEADER': {
|
|
305
|
+
const headerName = config.header || 'x-api-version';
|
|
306
|
+
const headerValue = ctx.headers[headerName];
|
|
307
|
+
if (typeof headerValue === 'string') {
|
|
308
|
+
return headerValue.replace(/^v/, '');
|
|
309
|
+
}
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
case 'MEDIA_TYPE': {
|
|
313
|
+
const accept = ctx.headers.accept;
|
|
314
|
+
const paramName = config.mediaTypeParam || 'version';
|
|
315
|
+
const match = accept?.match(new RegExp(`${paramName}=(\\d+)`));
|
|
316
|
+
return match ? match[1] : undefined;
|
|
317
|
+
}
|
|
318
|
+
case 'URI': {
|
|
319
|
+
const prefix = config.prefix || 'v';
|
|
320
|
+
const uriMatch = ctx.path.match(new RegExp(`^/${prefix}(\\d+)`));
|
|
321
|
+
return uriMatch ? uriMatch[1] : undefined;
|
|
322
|
+
}
|
|
323
|
+
default:
|
|
324
|
+
return config.defaultVersion;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
256
327
|
};
|
|
257
328
|
exports.MidwayKoaFramework = MidwayKoaFramework;
|
|
258
329
|
exports.MidwayKoaFramework = MidwayKoaFramework = __decorate([
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import { IConfigurationOptions, IMidwayApplication, IMidwayContext } from '@midwayjs/core';
|
|
4
2
|
import * as koa from 'koa';
|
|
5
3
|
import { Context as KoaContext, DefaultState, Middleware, Next } from 'koa';
|
|
6
4
|
import { RouterParamValue } from '@midwayjs/core';
|
|
7
5
|
import * as qs from 'qs';
|
|
6
|
+
import { ListenOptions } from 'net';
|
|
8
7
|
export interface State extends DefaultState {
|
|
9
8
|
}
|
|
10
9
|
export type IMidwayKoaContext = IMidwayContext<KoaContext>;
|
|
11
10
|
export type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext, koa<State, IMidwayKoaContext> & {
|
|
12
11
|
generateController(controllerMapping: string, routeArgsInfo?: RouterParamValue[], routerResponseData?: any[]): Middleware<State, IMidwayKoaContext>;
|
|
13
12
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @param middlewareId
|
|
13
|
+
* Get the port that the application is listening on
|
|
16
14
|
*/
|
|
17
|
-
|
|
15
|
+
getPort(): string;
|
|
18
16
|
}>;
|
|
19
17
|
/**
|
|
20
18
|
* @deprecated use NextFunction definition
|
|
@@ -86,7 +84,47 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
|
|
|
86
84
|
* qs options
|
|
87
85
|
*/
|
|
88
86
|
queryParseOptions?: qs.IParseOptions;
|
|
87
|
+
/**
|
|
88
|
+
* https/https/http2 server options
|
|
89
|
+
*/
|
|
89
90
|
serverOptions?: Record<string, any>;
|
|
91
|
+
/**
|
|
92
|
+
* listen options
|
|
93
|
+
*/
|
|
94
|
+
listenOptions?: ListenOptions;
|
|
95
|
+
/**
|
|
96
|
+
* 版本控制配置
|
|
97
|
+
*/
|
|
98
|
+
versioning?: {
|
|
99
|
+
/**
|
|
100
|
+
* 是否启用版本控制
|
|
101
|
+
*/
|
|
102
|
+
enabled?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* 默认版本控制类型
|
|
105
|
+
*/
|
|
106
|
+
type?: 'URI' | 'HEADER' | 'MEDIA_TYPE' | 'CUSTOM';
|
|
107
|
+
/**
|
|
108
|
+
* 默认版本
|
|
109
|
+
*/
|
|
110
|
+
defaultVersion?: string;
|
|
111
|
+
/**
|
|
112
|
+
* URI 版本前缀,默认为 'v'
|
|
113
|
+
*/
|
|
114
|
+
prefix?: string;
|
|
115
|
+
/**
|
|
116
|
+
* Header 版本控制时的 header 名称
|
|
117
|
+
*/
|
|
118
|
+
header?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Media Type 版本控制时的参数名
|
|
121
|
+
*/
|
|
122
|
+
mediaTypeParam?: string;
|
|
123
|
+
/**
|
|
124
|
+
* 自定义版本提取函数
|
|
125
|
+
*/
|
|
126
|
+
extractVersionFn?: (ctx: IMidwayKoaContext) => string | undefined;
|
|
127
|
+
};
|
|
90
128
|
}
|
|
91
129
|
export type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
|
|
92
130
|
export interface IWebMiddleware {
|
|
@@ -95,6 +133,8 @@ export interface IWebMiddleware {
|
|
|
95
133
|
export type Application = IMidwayKoaApplication;
|
|
96
134
|
export interface Context extends IMidwayKoaContext {
|
|
97
135
|
state: State;
|
|
136
|
+
apiVersion?: string;
|
|
137
|
+
originalPath?: string;
|
|
98
138
|
}
|
|
99
139
|
export interface BodyParserOptions {
|
|
100
140
|
enable?: boolean;
|
|
@@ -13,6 +13,7 @@ exports.BodyParserMiddleware = void 0;
|
|
|
13
13
|
const koaBodyParser = require("koa-bodyparser");
|
|
14
14
|
const core_1 = require("@midwayjs/core");
|
|
15
15
|
let BodyParserMiddleware = class BodyParserMiddleware {
|
|
16
|
+
bodyparserConfig;
|
|
16
17
|
resolve() {
|
|
17
18
|
// use bodyparser middleware
|
|
18
19
|
if (this.bodyparserConfig.enable) {
|
|
@@ -14,6 +14,7 @@ const core_1 = require("@midwayjs/core");
|
|
|
14
14
|
const path = require('path');
|
|
15
15
|
const MAX_AGE = 'public, max-age=2592000'; // 30 days
|
|
16
16
|
let SiteFileMiddleware = class SiteFileMiddleware {
|
|
17
|
+
siteFileConfig;
|
|
17
18
|
resolve() {
|
|
18
19
|
// use bodyparser middleware
|
|
19
20
|
if (this.siteFileConfig.enable) {
|
package/dist/onerror.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setupOnError =
|
|
3
|
+
exports.setupOnError = setupOnError;
|
|
4
4
|
const http = require("http");
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
6
|
const core_1 = require("@midwayjs/core");
|
|
@@ -182,5 +182,4 @@ function setupOnError(app, config, logger) {
|
|
|
182
182
|
this.res.end(this.body);
|
|
183
183
|
};
|
|
184
184
|
}
|
|
185
|
-
exports.setupOnError = setupOnError;
|
|
186
185
|
//# sourceMappingURL=onerror.js.map
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.tpl = void 0;
|
|
4
|
+
exports.detectStatus = detectStatus;
|
|
5
|
+
exports.accepts = accepts;
|
|
6
|
+
exports.sendToWormhole = sendToWormhole;
|
|
7
|
+
exports.isProduction = isProduction;
|
|
8
|
+
exports.escapeHtml = escapeHtml;
|
|
9
|
+
exports.getFreePort = getFreePort;
|
|
10
|
+
const net_1 = require("net");
|
|
4
11
|
function detectStatus(err) {
|
|
5
12
|
// detect status
|
|
6
13
|
let status = err.status || 500;
|
|
@@ -10,13 +17,11 @@ function detectStatus(err) {
|
|
|
10
17
|
}
|
|
11
18
|
return status;
|
|
12
19
|
}
|
|
13
|
-
exports.detectStatus = detectStatus;
|
|
14
20
|
function accepts(ctx) {
|
|
15
21
|
if (acceptJSON(ctx))
|
|
16
22
|
return 'json';
|
|
17
23
|
return 'html';
|
|
18
24
|
}
|
|
19
|
-
exports.accepts = accepts;
|
|
20
25
|
function acceptJSON(ctx) {
|
|
21
26
|
if (ctx.path.endsWith('.json'))
|
|
22
27
|
return true;
|
|
@@ -59,11 +64,9 @@ function sendToWormhole(stream) {
|
|
|
59
64
|
stream.on('error', onError);
|
|
60
65
|
});
|
|
61
66
|
}
|
|
62
|
-
exports.sendToWormhole = sendToWormhole;
|
|
63
67
|
function isProduction(app) {
|
|
64
68
|
return app.getEnv() !== 'local' && app.getEnv() !== 'unittest';
|
|
65
69
|
}
|
|
66
|
-
exports.isProduction = isProduction;
|
|
67
70
|
exports.tpl = `
|
|
68
71
|
<!DOCTYPE html>
|
|
69
72
|
<html>
|
|
@@ -152,5 +155,19 @@ function escapeHtml(string) {
|
|
|
152
155
|
}
|
|
153
156
|
return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
|
|
154
157
|
}
|
|
155
|
-
|
|
158
|
+
async function getFreePort() {
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
const server = (0, net_1.createServer)();
|
|
161
|
+
server.listen(0, () => {
|
|
162
|
+
try {
|
|
163
|
+
const port = server.address().port;
|
|
164
|
+
server.close();
|
|
165
|
+
resolve(port);
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
reject(err);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
}
|
|
156
173
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/koa",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-beta.10",
|
|
4
4
|
"description": "Midway Web Framework for KOA",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -24,20 +24,21 @@
|
|
|
24
24
|
],
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@midwayjs/core": "^4.0.0-
|
|
28
|
-
"@midwayjs/mock": "^4.0.0-
|
|
29
|
-
"@types/koa-router": "7.4.
|
|
30
|
-
"
|
|
27
|
+
"@midwayjs/core": "^4.0.0-beta.10",
|
|
28
|
+
"@midwayjs/mock": "^4.0.0-beta.10",
|
|
29
|
+
"@types/koa-router": "7.4.9",
|
|
30
|
+
"axios": "1.12.0",
|
|
31
|
+
"fs-extra": "11.3.3"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@koa/router": "^12.0.0",
|
|
34
|
-
"@midwayjs/cookies": "^1.0
|
|
35
|
-
"@midwayjs/session": "^4.0.0-
|
|
36
|
-
"@types/koa": "
|
|
37
|
-
"@types/qs": "6.9.
|
|
38
|
-
"koa": "
|
|
35
|
+
"@midwayjs/cookies": "^1.3.0",
|
|
36
|
+
"@midwayjs/session": "^4.0.0-beta.10",
|
|
37
|
+
"@types/koa": "3.0.0",
|
|
38
|
+
"@types/qs": "6.9.18",
|
|
39
|
+
"koa": "3.0.3",
|
|
39
40
|
"koa-bodyparser": "4.4.1",
|
|
40
|
-
"qs": "6.
|
|
41
|
+
"qs": "6.14.1"
|
|
41
42
|
},
|
|
42
43
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
43
44
|
"repository": {
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
"url": "https://github.com/midwayjs/midway.git"
|
|
46
47
|
},
|
|
47
48
|
"engines": {
|
|
48
|
-
"node": ">=
|
|
49
|
+
"node": ">=20"
|
|
49
50
|
},
|
|
50
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
|
|
51
52
|
}
|