@midwayjs/koa 3.0.0-beta.2 → 3.0.0-beta.6

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/CHANGELOG.md CHANGED
@@ -3,6 +3,49 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [3.0.0-beta.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
12
+
13
+
14
+
15
+
16
+
17
+ # [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
18
+
19
+ **Note:** Version bump only for package @midwayjs/koa
20
+
21
+
22
+
23
+
24
+
25
+ # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
26
+
27
+
28
+ ### Features
29
+
30
+ * add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
31
+ * auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
32
+ * support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
33
+
34
+
35
+
36
+
37
+
38
+ # [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
39
+
40
+
41
+ ### Features
42
+
43
+ * add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
44
+
45
+
46
+
47
+
48
+
6
49
  # [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
7
50
 
8
51
  **Note:** Version bump only for package @midwayjs/koa
@@ -1,10 +1,7 @@
1
- import { MidwayKoaFramework } from './framework';
2
1
  import { MidwayDecoratorService } from '@midwayjs/core';
3
2
  export declare class KoaConfiguration {
4
- framework: MidwayKoaFramework;
5
3
  decoratorService: MidwayDecoratorService;
6
4
  init(): void;
7
5
  onReady(): Promise<void>;
8
- onServerReady(): Promise<void>;
9
6
  }
10
7
  //# sourceMappingURL=configuration.d.ts.map
@@ -11,23 +11,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.KoaConfiguration = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
- const framework_1 = require("./framework");
15
14
  const core_1 = require("@midwayjs/core");
16
15
  let KoaConfiguration = class KoaConfiguration {
17
16
  init() {
18
17
  this.decoratorService.registerParameterHandler(decorator_1.WEB_ROUTER_PARAM_KEY, options => {
19
- return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData)(options.originArgs[0], options.originArgs[1]);
18
+ return (0, core_1.extractKoaLikeValue)(options.metadata.type, options.metadata.propertyData, options.originParamType)(options.originArgs[0], options.originArgs[1]);
20
19
  });
21
20
  }
22
21
  async onReady() { }
23
- async onServerReady() {
24
- await this.framework.run();
25
- }
26
22
  };
27
- __decorate([
28
- (0, decorator_1.Inject)(),
29
- __metadata("design:type", framework_1.MidwayKoaFramework)
30
- ], KoaConfiguration.prototype, "framework", void 0);
31
23
  __decorate([
32
24
  (0, decorator_1.Inject)(),
33
25
  __metadata("design:type", core_1.MidwayDecoratorService)
@@ -1,10 +1,9 @@
1
1
  /// <reference types="node" />
2
- import { BaseFramework, IMidwayBootstrapOptions, MidwayFrameworkType } from '@midwayjs/core';
3
- import { RouterParamValue } from '@midwayjs/decorator';
2
+ import { BaseFramework, CommonFilterUnion, CommonMiddlewareUnion, IMidwayBootstrapOptions, MidwayFrameworkType, RouterInfo } from '@midwayjs/core';
4
3
  import { IMidwayKoaApplication, IMidwayKoaConfigurationOptions, IMidwayKoaContext } from './interface';
5
- import type { DefaultState, Middleware } from 'koa';
4
+ import type { DefaultState, Middleware, Next } from 'koa';
6
5
  import { Server } from 'net';
7
- export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplication, IMidwayKoaContext, IMidwayKoaConfigurationOptions> {
6
+ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplication, IMidwayKoaContext, IMidwayKoaConfigurationOptions, Next> {
8
7
  private server;
9
8
  private generator;
10
9
  configure(): IMidwayKoaConfigurationOptions;
@@ -12,11 +11,8 @@ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplicat
12
11
  loadMidwayController(): Promise<void>;
13
12
  /**
14
13
  * wrap controller string to middleware function
15
- * @param controllerMapping like FooController.index
16
- * @param routeArgsInfo
17
- * @param routerResponseData
18
14
  */
19
- generateController(controllerMapping: string, routeArgsInfo?: RouterParamValue[], routerResponseData?: any[]): Middleware<DefaultState, IMidwayKoaContext>;
15
+ generateController(routeInfo: RouterInfo): Middleware<DefaultState, IMidwayKoaContext>;
20
16
  /**
21
17
  * @deprecated
22
18
  * @param middlewareId
@@ -27,5 +23,7 @@ export declare class MidwayKoaFramework extends BaseFramework<IMidwayKoaApplicat
27
23
  getFrameworkType(): MidwayFrameworkType;
28
24
  getFrameworkName(): string;
29
25
  getServer(): Server;
26
+ useMiddleware(Middleware: CommonMiddlewareUnion<IMidwayKoaContext, Next, unknown>): void;
27
+ useFilter(Filter: CommonFilterUnion<IMidwayKoaContext, Next, unknown>): void;
30
28
  }
31
29
  //# sourceMappingURL=framework.d.ts.map
package/dist/framework.js CHANGED
@@ -11,6 +11,7 @@ const core_1 = require("@midwayjs/core");
11
11
  const decorator_1 = require("@midwayjs/decorator");
12
12
  const Router = require("@koa/router");
13
13
  const koa = require("koa");
14
+ const onerror = require("koa-onerror");
14
15
  class KoaControllerGenerator extends core_1.WebControllerGenerator {
15
16
  constructor(app, applicationContext, logger) {
16
17
  super(applicationContext, core_1.MidwayFrameworkType.WEB_KOA, logger);
@@ -23,8 +24,8 @@ class KoaControllerGenerator extends core_1.WebControllerGenerator {
23
24
  router.prefix(routerOptions.prefix);
24
25
  return router;
25
26
  }
26
- generateController(controllerMapping, routeArgsInfo, routerResponseData) {
27
- return this.generateKoaController(controllerMapping, routeArgsInfo, routerResponseData);
27
+ generateController(routeInfo) {
28
+ return this.generateKoaController(routeInfo);
28
29
  }
29
30
  }
30
31
  let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
@@ -33,6 +34,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
33
34
  }
34
35
  async applicationInitialize(options) {
35
36
  this.app = new koa();
37
+ onerror(this.app, this.configurationOptions.onerror);
36
38
  this.app.use(async (ctx, next) => {
37
39
  this.app.createAnonymousContext(ctx);
38
40
  const { result, error } = await (await this.getMiddleware())(ctx, next);
@@ -44,57 +46,21 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
44
46
  }
45
47
  });
46
48
  this.generator = new KoaControllerGenerator(this.app, this.applicationContext, this.appLogger);
47
- this.defineApplicationProperties({
48
- generateController: (controllerMapping) => {
49
- return this.generateController(controllerMapping);
50
- },
51
- /**
52
- * @deprecated
53
- * @param middlewareId
54
- */
55
- generateMiddleware: async (middlewareId) => {
56
- return this.generateMiddleware(middlewareId);
57
- },
58
- });
49
+ this.defineApplicationProperties();
59
50
  // hack use method
60
51
  this.app.originUse = this.app.use;
61
52
  this.app.use = this.app.useMiddleware;
62
- // https config
63
- if (this.configurationOptions.key && this.configurationOptions.cert) {
64
- this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
65
- this.configurationOptions.cert = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.cert);
66
- this.configurationOptions.ca = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.ca);
67
- if (this.configurationOptions.http2) {
68
- this.server = require('http2').createSecureServer(this.configurationOptions, this.app.callback());
69
- }
70
- else {
71
- this.server = require('https').createServer(this.configurationOptions, this.app.callback());
72
- }
73
- }
74
- else {
75
- if (this.configurationOptions.http2) {
76
- this.server = require('http2').createServer(this.app.callback());
77
- }
78
- else {
79
- this.server = require('http').createServer(this.app.callback());
80
- }
81
- }
82
- // register httpServer to applicationContext
83
- this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
84
53
  }
85
54
  async loadMidwayController() {
86
- await this.generator.loadMidwayController(newRouter => {
55
+ await this.generator.loadMidwayController(this.configurationOptions.globalPrefix, newRouter => {
87
56
  this.app.use(newRouter.middleware());
88
57
  });
89
58
  }
90
59
  /**
91
60
  * wrap controller string to middleware function
92
- * @param controllerMapping like FooController.index
93
- * @param routeArgsInfo
94
- * @param routerResponseData
95
61
  */
96
- generateController(controllerMapping, routeArgsInfo, routerResponseData) {
97
- return this.generator.generateKoaController(controllerMapping, routeArgsInfo, routerResponseData);
62
+ generateController(routeInfo) {
63
+ return this.generator.generateKoaController(routeInfo);
98
64
  }
99
65
  /**
100
66
  * @deprecated
@@ -105,12 +71,36 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
105
71
  return mwIns.resolve();
106
72
  }
107
73
  async run() {
74
+ var _a;
108
75
  // load controller
109
76
  await this.loadMidwayController();
110
77
  // restore use method
111
78
  this.app.use = this.app.originUse;
79
+ // https config
80
+ if (this.configurationOptions.key && this.configurationOptions.cert) {
81
+ this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
82
+ this.configurationOptions.cert = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.cert);
83
+ this.configurationOptions.ca = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.ca);
84
+ if (this.configurationOptions.http2) {
85
+ this.server = require('http2').createSecureServer(this.configurationOptions, this.app.callback());
86
+ }
87
+ else {
88
+ this.server = require('https').createServer(this.configurationOptions, this.app.callback());
89
+ }
90
+ }
91
+ else {
92
+ if (this.configurationOptions.http2) {
93
+ this.server = require('http2').createServer(this.app.callback());
94
+ }
95
+ else {
96
+ this.server = require('http').createServer(this.app.callback());
97
+ }
98
+ }
99
+ // register httpServer to applicationContext
100
+ this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
112
101
  // set port and listen server
113
- if (this.configurationOptions.port) {
102
+ const customPort = (_a = process.env.MIDWAY_HTTP_PORT) !== null && _a !== void 0 ? _a : this.configurationOptions.port;
103
+ if (customPort) {
114
104
  new Promise(resolve => {
115
105
  const args = [this.configurationOptions.port];
116
106
  if (this.configurationOptions.hostname) {
@@ -120,6 +110,7 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
120
110
  resolve();
121
111
  });
122
112
  this.server.listen(...args);
113
+ process.env.MIDWAY_HTTP_PORT = String(customPort);
123
114
  });
124
115
  }
125
116
  }
@@ -135,6 +126,12 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
135
126
  getServer() {
136
127
  return this.server;
137
128
  }
129
+ useMiddleware(Middleware) {
130
+ this.middlewareManager.insertLast(Middleware);
131
+ }
132
+ useFilter(Filter) {
133
+ this.filterManager.useFilter(Filter);
134
+ }
138
135
  };
139
136
  MidwayKoaFramework = __decorate([
140
137
  (0, decorator_1.Framework)()
@@ -13,9 +13,7 @@ export declare type IMidwayKoaApplication = IMidwayApplication<IMidwayKoaContext
13
13
  generateMiddleware(middlewareId: string): Promise<Middleware<DefaultState, IMidwayKoaContext>>;
14
14
  }>;
15
15
  export declare type IMidwayKoaNext = Next;
16
- export interface IMidwayKoaApplicationPlus<CTX extends IMidwayContext> extends IMidwayApplication<CTX> {
17
- use(...args: any[]): any;
18
- }
16
+ export declare type NextFunction = Next;
19
17
  export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
20
18
  /**
21
19
  * application http port
@@ -41,6 +39,21 @@ export interface IMidwayKoaConfigurationOptions extends IConfigurationOptions {
41
39
  * http2 support
42
40
  */
43
41
  http2?: boolean;
42
+ /**
43
+ * http global prefix
44
+ */
45
+ globalPrefix?: string;
46
+ /**
47
+ * onerror middleware options
48
+ */
49
+ onerror?: {
50
+ text: (err: Error, ctx: IMidwayKoaContext) => void;
51
+ json: (err: Error, ctx: IMidwayKoaContext) => void;
52
+ html: (err: Error, ctx: IMidwayKoaContext) => void;
53
+ redirect?: string;
54
+ template?: string;
55
+ accepts?: (...args: any[]) => any;
56
+ };
44
57
  }
45
58
  export declare type MiddlewareParamArray = Array<Middleware<DefaultState, IMidwayKoaContext>>;
46
59
  export interface IWebMiddleware {
@@ -49,4 +62,9 @@ export interface IWebMiddleware {
49
62
  export declare type Application = IMidwayKoaApplication;
50
63
  export interface Context extends IMidwayKoaContext {
51
64
  }
65
+ declare module '@midwayjs/core/dist/interface' {
66
+ interface MidwayConfig {
67
+ koa?: IMidwayKoaConfigurationOptions;
68
+ }
69
+ }
52
70
  //# sourceMappingURL=interface.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/koa",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.0.0-beta.6",
4
4
  "description": "Midway Web Framework for KOA",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -23,9 +23,9 @@
23
23
  ],
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
- "@midwayjs/decorator": "^3.0.0-beta.2",
27
- "@midwayjs/mock": "^3.0.0-beta.2",
28
- "@midwayjs/validate": "^3.0.0-beta.2",
26
+ "@midwayjs/decorator": "^3.0.0-beta.6",
27
+ "@midwayjs/mock": "^3.0.0-beta.6",
28
+ "@midwayjs/validate": "^3.0.0-beta.6",
29
29
  "@types/koa": "^2.11.4",
30
30
  "@types/koa-router": "^7.4.1",
31
31
  "fs-extra": "^8.0.1",
@@ -34,9 +34,10 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@koa/router": "^10.0.0",
37
- "@midwayjs/core": "^3.0.0-beta.2",
38
- "@midwayjs/logger": "^3.0.0-beta.2",
39
- "koa": "^2.13.0"
37
+ "@midwayjs/core": "^3.0.0-beta.6",
38
+ "@midwayjs/logger": "^3.0.0-beta.6",
39
+ "koa": "^2.13.0",
40
+ "koa-onerror": "^4.1.0"
40
41
  },
41
42
  "author": "Harry Chen <czy88840616@gmail.com>",
42
43
  "repository": {
@@ -46,5 +47,5 @@
46
47
  "engines": {
47
48
  "node": ">=12"
48
49
  },
49
- "gitHead": "7f07de960da1155a9f7df554e1789c7a97bdd3fe"
50
+ "gitHead": "e4595d30b369e36bef21b36f2b3737d8bc2f802d"
50
51
  }