@midwayjs/koa 3.0.0-beta.1 → 3.0.0-beta.5

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,46 @@
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.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
7
+
8
+ **Note:** Version bump only for package @midwayjs/koa
9
+
10
+
11
+
12
+
13
+
14
+ # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
15
+
16
+
17
+ ### Features
18
+
19
+ * add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
20
+ * auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
21
+ * support global prefix url ([#1371](https://github.com/midwayjs/midway/issues/1371)) ([cc5fe44](https://github.com/midwayjs/midway/commit/cc5fe44e1d221590562dc71e1f33ae96093e0da7))
22
+
23
+
24
+
25
+
26
+
27
+ # [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
28
+
29
+
30
+ ### Features
31
+
32
+ * add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
33
+
34
+
35
+
36
+
37
+
38
+ # [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
39
+
40
+ **Note:** Version bump only for package @midwayjs/koa
41
+
42
+
43
+
44
+
45
+
6
46
  # [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
7
47
 
8
48
 
@@ -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
@@ -109,6 +75,28 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
109
75
  await this.loadMidwayController();
110
76
  // restore use method
111
77
  this.app.use = this.app.originUse;
78
+ // https config
79
+ if (this.configurationOptions.key && this.configurationOptions.cert) {
80
+ this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
81
+ this.configurationOptions.cert = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.cert);
82
+ this.configurationOptions.ca = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.ca);
83
+ if (this.configurationOptions.http2) {
84
+ this.server = require('http2').createSecureServer(this.configurationOptions, this.app.callback());
85
+ }
86
+ else {
87
+ this.server = require('https').createServer(this.configurationOptions, this.app.callback());
88
+ }
89
+ }
90
+ else {
91
+ if (this.configurationOptions.http2) {
92
+ this.server = require('http2').createServer(this.app.callback());
93
+ }
94
+ else {
95
+ this.server = require('http').createServer(this.app.callback());
96
+ }
97
+ }
98
+ // register httpServer to applicationContext
99
+ this.applicationContext.registerObject(core_1.HTTP_SERVER_KEY, this.server);
112
100
  // set port and listen server
113
101
  if (this.configurationOptions.port) {
114
102
  new Promise(resolve => {
@@ -135,6 +123,12 @@ let MidwayKoaFramework = class MidwayKoaFramework extends core_1.BaseFramework {
135
123
  getServer() {
136
124
  return this.server;
137
125
  }
126
+ useMiddleware(Middleware) {
127
+ this.middlewareManager.insertLast(Middleware);
128
+ }
129
+ useFilter(Filter) {
130
+ this.filterManager.useFilter(Filter);
131
+ }
138
132
  };
139
133
  MidwayKoaFramework = __decorate([
140
134
  (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.1",
3
+ "version": "3.0.0-beta.5",
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.1",
27
- "@midwayjs/mock": "^3.0.0-beta.1",
28
- "@midwayjs/validate": "^3.0.0-beta.1",
26
+ "@midwayjs/decorator": "^3.0.0-beta.5",
27
+ "@midwayjs/mock": "^3.0.0-beta.5",
28
+ "@midwayjs/validate": "^3.0.0-beta.5",
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.1",
38
- "@midwayjs/logger": "^3.0.0-beta.1",
39
- "koa": "^2.13.0"
37
+ "@midwayjs/core": "^3.0.0-beta.5",
38
+ "@midwayjs/logger": "^3.0.0-beta.5",
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": "72e4d20c678e65a5440c4e9a6d3b70046daa497c"
50
+ "gitHead": "ab0bf05ae6d13f6435db2f7223202be61d585a1b"
50
51
  }