koatty 3.6.5 → 3.6.8-0

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.
@@ -14,12 +14,28 @@
14
14
  "ts-node/register"
15
15
  ],
16
16
  "env": {
17
- "NODE_ENV": "development",
17
+ "NODE_ENV": "development"
18
18
  },
19
19
  "sourceMaps": true,
20
20
  "cwd": "${workspaceRoot}",
21
21
  "protocol": "inspector",
22
22
  "internalConsoleOptions": "neverOpen"
23
+ },
24
+ {
25
+ "type": "node",
26
+ "name": "vscode-jest-tests",
27
+ "request": "launch",
28
+ "console": "integratedTerminal",
29
+ "internalConsoleOptions": "neverOpen",
30
+ "disableOptimisticBPs": true,
31
+ "program": "${workspaceFolder}/node_modules/.bin/jest",
32
+ "cwd": "${workspaceFolder}",
33
+ "args": [
34
+ "--config",
35
+ "jest.config.js",
36
+ "--runInBand",
37
+ "--watchAll=false"
38
+ ]
23
39
  }
24
40
  ]
25
41
  }
package/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [3.6.8-0](https://github.com/thinkkoa/koatty/compare/v3.6.7...v3.6.8-0) (2022-03-14)
6
+
7
+ ### [3.6.8-0](https://github.com/thinkkoa/koatty/compare/v3.6.7...v3.6.8-0) (2022-03-14)
8
+
9
+ ### [3.6.7](https://github.com/thinkkoa/koatty/compare/v3.6.6...v3.6.7) (2022-03-09)
10
+
11
+ ### [3.6.6](https://github.com/thinkkoa/koatty/compare/v3.6.5...v3.6.6) (2022-03-02)
12
+
5
13
  ### [3.6.5](https://github.com/thinkkoa/koatty/compare/v3.6.4...v3.6.5) (2022-03-02)
6
14
 
7
15
  ### [3.6.4](https://github.com/thinkkoa/koatty/compare/v3.6.3...v3.6.4) (2022-02-25)
package/README.md CHANGED
@@ -103,22 +103,26 @@ export class IndexController extends BaseController {
103
103
  ```javascript
104
104
  import request from 'supertest';
105
105
  import { ExecBootStrap } from 'koatty';
106
-
107
106
  import { App } from '../src/App';
108
107
 
109
108
  describe('UT example', () => {
110
109
 
111
- let server;
110
+ let server: any;
112
111
  beforeAll(async () => {
112
+ jest.useFakeTimers();
113
113
  const appInstance = await ExecBootStrap()(App);
114
- server = appInstance.callback();
114
+ server = await appInstance.listen();
115
115
  });
116
116
 
117
- it('request', async (done) => {
118
- const rsp = await request(server).get('/path/to/server');
119
- expect(rsp.status).toBe(200);
117
+ afterAll(done => {
118
+ server.close();
120
119
  done();
121
120
  });
121
+
122
+ it('request', async () => {
123
+ const rsp = await request(server).get('/');
124
+ expect(rsp.status).toBe(200);
125
+ });
122
126
  });
123
127
 
124
128
  ```
package/dist/README.md CHANGED
@@ -103,22 +103,26 @@ export class IndexController extends BaseController {
103
103
  ```javascript
104
104
  import request from 'supertest';
105
105
  import { ExecBootStrap } from 'koatty';
106
-
107
106
  import { App } from '../src/App';
108
107
 
109
108
  describe('UT example', () => {
110
109
 
111
- let server;
110
+ let server: any;
112
111
  beforeAll(async () => {
112
+ jest.useFakeTimers();
113
113
  const appInstance = await ExecBootStrap()(App);
114
- server = appInstance.callback();
114
+ server = await appInstance.listen();
115
115
  });
116
116
 
117
- it('request', async (done) => {
118
- const rsp = await request(server).get('/path/to/server');
119
- expect(rsp.status).toBe(200);
117
+ afterAll(done => {
118
+ server.close();
120
119
  done();
121
120
  });
121
+
122
+ it('request', async () => {
123
+ const rsp = await request(server).get('/');
124
+ expect(rsp.status).toBe(200);
125
+ });
122
126
  });
123
127
 
124
128
  ```
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-03-02 15:39:00
3
+ * @Date: 2022-03-14 18:27:53
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -171,7 +171,7 @@ export declare function Controller(path?: string): ClassDecorator;
171
171
  * @param {Function} [bootFunc] callback function
172
172
  * @returns
173
173
  */
174
- export declare function ExecBootStrap(bootFunc?: Function): (target: any) => Promise<void>;
174
+ export declare function ExecBootStrap(bootFunc?: Function): (target: any) => Promise<Koatty>;
175
175
 
176
176
  export { Helper }
177
177
 
@@ -225,7 +225,7 @@ export declare class HttpController extends BaseController {
225
225
  * @returns
226
226
  * @memberof HttpController
227
227
  */
228
- param(name?: string): any;
228
+ param(name?: string): Promise<any>;
229
229
  /**
230
230
  * Set response content-type
231
231
  *
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-03-02 15:38:40
3
+ * @Date: 2022-03-14 18:27:34
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -195,6 +195,7 @@ class BaseController {
195
195
  */
196
196
  fail(msg, data, code = 1) {
197
197
  const obj = formatApiData(msg, data, code);
198
+ this.ctx.body = obj.data;
198
199
  this.ctx.throw(obj.message, obj.code, 200);
199
200
  }
200
201
  }
@@ -490,13 +491,14 @@ class Loader {
490
491
  * @memberof Loader
491
492
  */
492
493
  static initialize(app) {
493
- const env = (process.execArgv ?? []).join(",");
494
+ var _a, _b;
495
+ const env = ((_a = process.execArgv) !== null && _a !== void 0 ? _a : []).join(",");
494
496
  if (env.indexOf('ts-node') > -1 || env.indexOf('--debug') > -1) {
495
497
  app.appDebug = true;
496
498
  }
497
499
  // app.env
498
500
  app.env = process.env.KOATTY_ENV || process.env.NODE_ENV;
499
- if ((env.indexOf('--production') > -1) || ((app.env ?? '').indexOf('pro') > -1)) {
501
+ if ((env.indexOf('--production') > -1) || (((_b = app.env) !== null && _b !== void 0 ? _b : '').indexOf('pro') > -1)) {
500
502
  app.appDebug = false;
501
503
  }
502
504
  if (app.appDebug) {
@@ -600,7 +602,8 @@ class Loader {
600
602
  * @memberof Loader
601
603
  */
602
604
  static SetLogger(app) {
603
- const configs = app.getMetaData("_configs") ?? {};
605
+ var _a;
606
+ const configs = (_a = app.getMetaData("_configs")) !== null && _a !== void 0 ? _a : {};
604
607
  //Logger
605
608
  if (configs.config) {
606
609
  const opt = configs.config;
@@ -676,6 +679,7 @@ class Loader {
676
679
  * @memberof Loader
677
680
  */
678
681
  static async LoadMiddlewares(app, loadPath) {
682
+ var _a;
679
683
  let middlewareConf = app.config(undefined, "middleware");
680
684
  if (koatty_lib.Helper.isEmpty(middlewareConf)) {
681
685
  middlewareConf = { config: {}, list: [] };
@@ -684,11 +688,12 @@ class Loader {
684
688
  koatty_loader.Load(loadPath || ["./middleware"], app.thinkPath);
685
689
  //Mount application middleware
686
690
  // const middleware: any = {};
687
- const appMiddleware = koatty_container.IOCContainer.listClass("MIDDLEWARE") ?? [];
691
+ const appMiddleware = (_a = koatty_container.IOCContainer.listClass("MIDDLEWARE")) !== null && _a !== void 0 ? _a : [];
688
692
  appMiddleware.push({ id: "TraceMiddleware", target: TraceMiddleware });
689
693
  appMiddleware.push({ id: "PayloadMiddleware", target: PayloadMiddleware });
690
694
  appMiddleware.forEach((item) => {
691
- item.id = (item.id ?? "").replace("MIDDLEWARE:", "");
695
+ var _a;
696
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("MIDDLEWARE:", "");
692
697
  if (item.id && koatty_lib.Helper.isClass(item.target)) {
693
698
  koatty_container.IOCContainer.reg(item.id, item.target, { scope: "Prototype", type: "MIDDLEWARE", args: [] });
694
699
  }
@@ -744,9 +749,10 @@ class Loader {
744
749
  */
745
750
  static LoadControllers(app) {
746
751
  const controllerList = koatty_container.IOCContainer.listClass("CONTROLLER");
747
- const controllers = {};
752
+ const controllers = [];
748
753
  controllerList.forEach((item) => {
749
- item.id = (item.id ?? "").replace("CONTROLLER:", "");
754
+ var _a;
755
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("CONTROLLER:", "");
750
756
  if (item.id && koatty_lib.Helper.isClass(item.target)) {
751
757
  Logger.Debug(`Load controller: ${item.id}`);
752
758
  // registering to IOC
@@ -755,10 +761,10 @@ class Loader {
755
761
  if (!(ctl instanceof BaseController)) {
756
762
  throw new Error(`class ${item.id} does not inherit from BaseController`);
757
763
  }
758
- controllers[item.id] = 1;
764
+ controllers.push(item.id);
759
765
  }
760
766
  });
761
- app.setMetaData("_controllers", controllers);
767
+ return controllers;
762
768
  }
763
769
  /**
764
770
  * Load services
@@ -770,7 +776,8 @@ class Loader {
770
776
  static LoadServices(app) {
771
777
  const serviceList = koatty_container.IOCContainer.listClass("SERVICE");
772
778
  serviceList.forEach((item) => {
773
- item.id = (item.id ?? "").replace("SERVICE:", "");
779
+ var _a;
780
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("SERVICE:", "");
774
781
  if (item.id && koatty_lib.Helper.isClass(item.target)) {
775
782
  Logger.Debug(`Load service: ${item.id}`);
776
783
  // registering to IOC
@@ -788,7 +795,8 @@ class Loader {
788
795
  static LoadComponents(app) {
789
796
  const componentList = koatty_container.IOCContainer.listClass("COMPONENT");
790
797
  componentList.forEach((item) => {
791
- item.id = (item.id ?? "").replace("COMPONENT:", "");
798
+ var _a;
799
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("COMPONENT:", "");
792
800
  if (item.id && !(item.id).endsWith("Plugin") && koatty_lib.Helper.isClass(item.target)) {
793
801
  Logger.Debug(`Load component: ${item.id}`);
794
802
  // registering to IOC
@@ -804,6 +812,7 @@ class Loader {
804
812
  * @memberof Loader
805
813
  */
806
814
  static async LoadPlugins(app) {
815
+ var _a;
807
816
  const componentList = koatty_container.IOCContainer.listClass("COMPONENT");
808
817
  let pluginsConf = app.config(undefined, "plugin");
809
818
  if (koatty_lib.Helper.isEmpty(pluginsConf)) {
@@ -811,7 +820,8 @@ class Loader {
811
820
  }
812
821
  const pluginList = [];
813
822
  componentList.forEach(async (item) => {
814
- item.id = (item.id ?? "").replace("COMPONENT:", "");
823
+ var _a;
824
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("COMPONENT:", "");
815
825
  if (item.id && (item.id).endsWith("Plugin") && koatty_lib.Helper.isClass(item.target)) {
816
826
  // registering to IOC
817
827
  koatty_container.IOCContainer.reg(item.id, item.target, { scope: "Singleton", type: "COMPONENT", args: [] });
@@ -832,12 +842,12 @@ class Loader {
832
842
  continue;
833
843
  }
834
844
  // sync exec
835
- await handle.run(pluginsConf.config[key] ?? {}, app);
845
+ await handle.run((_a = pluginsConf.config[key]) !== null && _a !== void 0 ? _a : {}, app);
836
846
  }
837
847
  }
838
848
  }
839
849
 
840
- var version = "3.6.5";
850
+ var version = "3.6.8-0";
841
851
  var engines = {
842
852
  node: ">12.0.0"
843
853
  };
@@ -948,6 +958,10 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
948
958
  Loader.LoadAppReadyHooks(app, target);
949
959
  // New router
950
960
  const KoattyRouter = newRouter(app);
961
+ koatty_lib.Helper.define(app, "router", KoattyRouter);
962
+ // Create Server
963
+ app.server = newServe(app);
964
+ koatty_lib.Helper.define(app, "listenCallback", listenCallback(app));
951
965
  // Load Middleware
952
966
  Logger.Log('think', '', 'Load Middlewares ...');
953
967
  await Loader.LoadMiddlewares(app);
@@ -959,24 +973,17 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
959
973
  Loader.LoadServices(app);
960
974
  // Load Controllers
961
975
  Logger.Log('think', '', 'Load Controllers ...');
962
- Loader.LoadControllers(app);
976
+ const controllers = Loader.LoadControllers(app);
977
+ // Load Routers
978
+ Logger.Log('think', '', 'Load Routers ...');
979
+ KoattyRouter.LoadRouter(controllers);
963
980
  // Emit app ready event
964
981
  Logger.Log('think', '', 'Emit App Ready ...');
965
- // app.emit("appReady");
966
982
  await asyncEvent(app, 'appReady');
967
- // Load Routers
968
- Logger.Log('think', '', 'Load Routers ...');
969
- KoattyRouter.LoadRouter(app.getMetaData("_controllers"));
970
- Logger.Log('think', '', '====================================');
971
- // Start server
972
- const server = newServe(app);
973
- app.listen(server, listenCallback(app, server.options));
974
- // app.emit("appStart");
975
- // Emit app started event
976
- Logger.Log('think', '', 'Emit App Start ...');
977
- asyncEvent(app, 'appStart');
978
- // binding event "appStop"
979
- koatty_serve.BindProcessEvent(app, 'appStop');
983
+ if (!isUTRuntime) {
984
+ app.listen();
985
+ }
986
+ return app;
980
987
  }
981
988
  catch (err) {
982
989
  Logger.Error(err);
@@ -991,26 +998,34 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
991
998
  * @returns {*}
992
999
  */
993
1000
  const newRouter = function (app) {
1001
+ var _a;
994
1002
  const protocol = app.config("protocol") || "http";
995
- const options = app.config(undefined, 'router') ?? {};
1003
+ const options = (_a = app.config(undefined, 'router')) !== null && _a !== void 0 ? _a : {};
996
1004
  const router = koatty_router.NewRouter(app, options, protocol);
997
- koatty_lib.Helper.define(app, "router", router);
998
1005
  return router;
999
1006
  };
1000
1007
  /**
1001
1008
  * Listening callback function
1002
1009
  *
1003
1010
  * @param {Koatty} app
1004
- * @param {ListeningOptions} options
1005
1011
  * @returns {*}
1006
1012
  */
1007
- const listenCallback = (app, options) => {
1013
+ const listenCallback = (app) => {
1014
+ const options = app.server.options;
1008
1015
  return function () {
1016
+ // Emit app started event
1017
+ Logger.Log('think', '', 'Emit App Start ...');
1018
+ asyncEvent(app, 'appStart');
1019
+ Logger.Log('think', '', '====================================');
1009
1020
  Logger.Log("think", "", `Nodejs Version: ${process.version}`);
1010
1021
  Logger.Log("think", "", `Koatty Version: v${app.version}`);
1011
1022
  Logger.Log("think", "", `App Environment: ${app.env}`);
1023
+ Logger.Log('think', '', `Server Protocol: ${(options.protocol).toUpperCase()}`);
1012
1024
  Logger.Log("think", "", `Server running at ${options.protocol === "http2" ? "https" : options.protocol}://${options.hostname || '127.0.0.1'}:${options.port}/`);
1013
1025
  Logger.Log("think", "", "====================================");
1026
+ // binding event "appStop"
1027
+ Logger.Log('think', '', 'Bind App Stop event ...');
1028
+ koatty_serve.BindProcessEvent(app, 'appStop');
1014
1029
  // tslint:disable-next-line: no-unused-expression
1015
1030
  app.appDebug && Logger.Warn(`Running in debug mode.`);
1016
1031
  };
@@ -1022,11 +1037,12 @@ const listenCallback = (app, options) => {
1022
1037
  * @returns {*}
1023
1038
  */
1024
1039
  const newServe = function (app) {
1040
+ var _a, _b, _c;
1025
1041
  const protocol = app.config("protocol") || "http";
1026
1042
  const port = process.env.PORT || process.env.APP_PORT ||
1027
1043
  app.config('app_port') || 3000;
1028
1044
  const hostname = process.env.IP ||
1029
- process.env.HOSTNAME?.replace(/-/g, '.') || app.config('app_host') || '127.0.0.1';
1045
+ ((_a = process.env.HOSTNAME) === null || _a === void 0 ? void 0 : _a.replace(/-/g, '.')) || app.config('app_host') || '127.0.0.1';
1030
1046
  const options = {
1031
1047
  hostname,
1032
1048
  port,
@@ -1039,8 +1055,8 @@ const newServe = function (app) {
1039
1055
  };
1040
1056
  const pm = new Set(["https", "http2", "wss"]);
1041
1057
  if (pm.has(options.protocol)) {
1042
- const keyFile = app.config("key_file") ?? "";
1043
- const crtFile = app.config("crt_file") ?? "";
1058
+ const keyFile = (_b = app.config("key_file")) !== null && _b !== void 0 ? _b : "";
1059
+ const crtFile = (_c = app.config("crt_file")) !== null && _c !== void 0 ? _c : "";
1044
1060
  options.ext.key = fs__default["default"].readFileSync(keyFile).toString();
1045
1061
  options.ext.cert = fs__default["default"].readFileSync(crtFile).toString();
1046
1062
  }
@@ -1051,9 +1067,7 @@ const newServe = function (app) {
1051
1067
  const proto = app.config("protoFile", "router");
1052
1068
  options.ext.protoFile = proto;
1053
1069
  }
1054
- const server = koatty_serve.Serve(app, options);
1055
- koatty_lib.Helper.define(app, "server", server);
1056
- return server;
1070
+ return koatty_serve.Serve(app, options);
1057
1071
  };
1058
1072
  /**
1059
1073
  * Execute event as async
@@ -1113,7 +1127,7 @@ function ComponentScan(scanPath) {
1113
1127
  if (!(target.prototype instanceof koatty_core.Koatty)) {
1114
1128
  throw new Error(`class does not inherit from Koatty`);
1115
1129
  }
1116
- scanPath = scanPath ?? '';
1130
+ scanPath = scanPath !== null && scanPath !== void 0 ? scanPath : '';
1117
1131
  koatty_container.IOCContainer.saveClassMetadata(koatty_container.TAGGED_CLS, COMPONENT_SCAN, scanPath, target);
1118
1132
  };
1119
1133
  }
@@ -1129,7 +1143,7 @@ function ConfigurationScan(scanPath) {
1129
1143
  if (!(target.prototype instanceof koatty_core.Koatty)) {
1130
1144
  throw new Error(`class does not inherit from Koatty`);
1131
1145
  }
1132
- scanPath = scanPath ?? '';
1146
+ scanPath = scanPath !== null && scanPath !== void 0 ? scanPath : '';
1133
1147
  koatty_container.IOCContainer.saveClassMetadata(koatty_container.TAGGED_CLS, CONFIGURATION_SCAN, scanPath, target);
1134
1148
  };
1135
1149
  }
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-03-02 15:38:40
3
+ * @Date: 2022-03-14 18:27:34
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -15,7 +15,7 @@ import { NewRouter, CONTROLLER_ROUTER } from 'koatty_router';
15
15
  export * from 'koatty_router';
16
16
  import { IOCContainer, TAGGED_CLS } from 'koatty_container';
17
17
  export * from 'koatty_container';
18
- import { BindProcessEvent, Serve } from 'koatty_serve';
18
+ import { Serve, BindProcessEvent } from 'koatty_serve';
19
19
  export * from 'koatty_serve';
20
20
  import * as path from 'path';
21
21
  import { Load } from 'koatty_loader';
@@ -175,6 +175,7 @@ class BaseController {
175
175
  */
176
176
  fail(msg, data, code = 1) {
177
177
  const obj = formatApiData(msg, data, code);
178
+ this.ctx.body = obj.data;
178
179
  this.ctx.throw(obj.message, obj.code, 200);
179
180
  }
180
181
  }
@@ -470,13 +471,14 @@ class Loader {
470
471
  * @memberof Loader
471
472
  */
472
473
  static initialize(app) {
473
- const env = (process.execArgv ?? []).join(",");
474
+ var _a, _b;
475
+ const env = ((_a = process.execArgv) !== null && _a !== void 0 ? _a : []).join(",");
474
476
  if (env.indexOf('ts-node') > -1 || env.indexOf('--debug') > -1) {
475
477
  app.appDebug = true;
476
478
  }
477
479
  // app.env
478
480
  app.env = process.env.KOATTY_ENV || process.env.NODE_ENV;
479
- if ((env.indexOf('--production') > -1) || ((app.env ?? '').indexOf('pro') > -1)) {
481
+ if ((env.indexOf('--production') > -1) || (((_b = app.env) !== null && _b !== void 0 ? _b : '').indexOf('pro') > -1)) {
480
482
  app.appDebug = false;
481
483
  }
482
484
  if (app.appDebug) {
@@ -580,7 +582,8 @@ class Loader {
580
582
  * @memberof Loader
581
583
  */
582
584
  static SetLogger(app) {
583
- const configs = app.getMetaData("_configs") ?? {};
585
+ var _a;
586
+ const configs = (_a = app.getMetaData("_configs")) !== null && _a !== void 0 ? _a : {};
584
587
  //Logger
585
588
  if (configs.config) {
586
589
  const opt = configs.config;
@@ -656,6 +659,7 @@ class Loader {
656
659
  * @memberof Loader
657
660
  */
658
661
  static async LoadMiddlewares(app, loadPath) {
662
+ var _a;
659
663
  let middlewareConf = app.config(undefined, "middleware");
660
664
  if (Helper.isEmpty(middlewareConf)) {
661
665
  middlewareConf = { config: {}, list: [] };
@@ -664,11 +668,12 @@ class Loader {
664
668
  Load(loadPath || ["./middleware"], app.thinkPath);
665
669
  //Mount application middleware
666
670
  // const middleware: any = {};
667
- const appMiddleware = IOCContainer.listClass("MIDDLEWARE") ?? [];
671
+ const appMiddleware = (_a = IOCContainer.listClass("MIDDLEWARE")) !== null && _a !== void 0 ? _a : [];
668
672
  appMiddleware.push({ id: "TraceMiddleware", target: TraceMiddleware });
669
673
  appMiddleware.push({ id: "PayloadMiddleware", target: PayloadMiddleware });
670
674
  appMiddleware.forEach((item) => {
671
- item.id = (item.id ?? "").replace("MIDDLEWARE:", "");
675
+ var _a;
676
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("MIDDLEWARE:", "");
672
677
  if (item.id && Helper.isClass(item.target)) {
673
678
  IOCContainer.reg(item.id, item.target, { scope: "Prototype", type: "MIDDLEWARE", args: [] });
674
679
  }
@@ -724,9 +729,10 @@ class Loader {
724
729
  */
725
730
  static LoadControllers(app) {
726
731
  const controllerList = IOCContainer.listClass("CONTROLLER");
727
- const controllers = {};
732
+ const controllers = [];
728
733
  controllerList.forEach((item) => {
729
- item.id = (item.id ?? "").replace("CONTROLLER:", "");
734
+ var _a;
735
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("CONTROLLER:", "");
730
736
  if (item.id && Helper.isClass(item.target)) {
731
737
  Logger.Debug(`Load controller: ${item.id}`);
732
738
  // registering to IOC
@@ -735,10 +741,10 @@ class Loader {
735
741
  if (!(ctl instanceof BaseController)) {
736
742
  throw new Error(`class ${item.id} does not inherit from BaseController`);
737
743
  }
738
- controllers[item.id] = 1;
744
+ controllers.push(item.id);
739
745
  }
740
746
  });
741
- app.setMetaData("_controllers", controllers);
747
+ return controllers;
742
748
  }
743
749
  /**
744
750
  * Load services
@@ -750,7 +756,8 @@ class Loader {
750
756
  static LoadServices(app) {
751
757
  const serviceList = IOCContainer.listClass("SERVICE");
752
758
  serviceList.forEach((item) => {
753
- item.id = (item.id ?? "").replace("SERVICE:", "");
759
+ var _a;
760
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("SERVICE:", "");
754
761
  if (item.id && Helper.isClass(item.target)) {
755
762
  Logger.Debug(`Load service: ${item.id}`);
756
763
  // registering to IOC
@@ -768,7 +775,8 @@ class Loader {
768
775
  static LoadComponents(app) {
769
776
  const componentList = IOCContainer.listClass("COMPONENT");
770
777
  componentList.forEach((item) => {
771
- item.id = (item.id ?? "").replace("COMPONENT:", "");
778
+ var _a;
779
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("COMPONENT:", "");
772
780
  if (item.id && !(item.id).endsWith("Plugin") && Helper.isClass(item.target)) {
773
781
  Logger.Debug(`Load component: ${item.id}`);
774
782
  // registering to IOC
@@ -784,6 +792,7 @@ class Loader {
784
792
  * @memberof Loader
785
793
  */
786
794
  static async LoadPlugins(app) {
795
+ var _a;
787
796
  const componentList = IOCContainer.listClass("COMPONENT");
788
797
  let pluginsConf = app.config(undefined, "plugin");
789
798
  if (Helper.isEmpty(pluginsConf)) {
@@ -791,7 +800,8 @@ class Loader {
791
800
  }
792
801
  const pluginList = [];
793
802
  componentList.forEach(async (item) => {
794
- item.id = (item.id ?? "").replace("COMPONENT:", "");
803
+ var _a;
804
+ item.id = ((_a = item.id) !== null && _a !== void 0 ? _a : "").replace("COMPONENT:", "");
795
805
  if (item.id && (item.id).endsWith("Plugin") && Helper.isClass(item.target)) {
796
806
  // registering to IOC
797
807
  IOCContainer.reg(item.id, item.target, { scope: "Singleton", type: "COMPONENT", args: [] });
@@ -812,12 +822,12 @@ class Loader {
812
822
  continue;
813
823
  }
814
824
  // sync exec
815
- await handle.run(pluginsConf.config[key] ?? {}, app);
825
+ await handle.run((_a = pluginsConf.config[key]) !== null && _a !== void 0 ? _a : {}, app);
816
826
  }
817
827
  }
818
828
  }
819
829
 
820
- var version = "3.6.5";
830
+ var version = "3.6.8-0";
821
831
  var engines = {
822
832
  node: ">12.0.0"
823
833
  };
@@ -928,6 +938,10 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
928
938
  Loader.LoadAppReadyHooks(app, target);
929
939
  // New router
930
940
  const KoattyRouter = newRouter(app);
941
+ Helper.define(app, "router", KoattyRouter);
942
+ // Create Server
943
+ app.server = newServe(app);
944
+ Helper.define(app, "listenCallback", listenCallback(app));
931
945
  // Load Middleware
932
946
  Logger.Log('think', '', 'Load Middlewares ...');
933
947
  await Loader.LoadMiddlewares(app);
@@ -939,24 +953,17 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
939
953
  Loader.LoadServices(app);
940
954
  // Load Controllers
941
955
  Logger.Log('think', '', 'Load Controllers ...');
942
- Loader.LoadControllers(app);
956
+ const controllers = Loader.LoadControllers(app);
957
+ // Load Routers
958
+ Logger.Log('think', '', 'Load Routers ...');
959
+ KoattyRouter.LoadRouter(controllers);
943
960
  // Emit app ready event
944
961
  Logger.Log('think', '', 'Emit App Ready ...');
945
- // app.emit("appReady");
946
962
  await asyncEvent(app, 'appReady');
947
- // Load Routers
948
- Logger.Log('think', '', 'Load Routers ...');
949
- KoattyRouter.LoadRouter(app.getMetaData("_controllers"));
950
- Logger.Log('think', '', '====================================');
951
- // Start server
952
- const server = newServe(app);
953
- app.listen(server, listenCallback(app, server.options));
954
- // app.emit("appStart");
955
- // Emit app started event
956
- Logger.Log('think', '', 'Emit App Start ...');
957
- asyncEvent(app, 'appStart');
958
- // binding event "appStop"
959
- BindProcessEvent(app, 'appStop');
963
+ if (!isUTRuntime) {
964
+ app.listen();
965
+ }
966
+ return app;
960
967
  }
961
968
  catch (err) {
962
969
  Logger.Error(err);
@@ -971,26 +978,34 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
971
978
  * @returns {*}
972
979
  */
973
980
  const newRouter = function (app) {
981
+ var _a;
974
982
  const protocol = app.config("protocol") || "http";
975
- const options = app.config(undefined, 'router') ?? {};
983
+ const options = (_a = app.config(undefined, 'router')) !== null && _a !== void 0 ? _a : {};
976
984
  const router = NewRouter(app, options, protocol);
977
- Helper.define(app, "router", router);
978
985
  return router;
979
986
  };
980
987
  /**
981
988
  * Listening callback function
982
989
  *
983
990
  * @param {Koatty} app
984
- * @param {ListeningOptions} options
985
991
  * @returns {*}
986
992
  */
987
- const listenCallback = (app, options) => {
993
+ const listenCallback = (app) => {
994
+ const options = app.server.options;
988
995
  return function () {
996
+ // Emit app started event
997
+ Logger.Log('think', '', 'Emit App Start ...');
998
+ asyncEvent(app, 'appStart');
999
+ Logger.Log('think', '', '====================================');
989
1000
  Logger.Log("think", "", `Nodejs Version: ${process.version}`);
990
1001
  Logger.Log("think", "", `Koatty Version: v${app.version}`);
991
1002
  Logger.Log("think", "", `App Environment: ${app.env}`);
1003
+ Logger.Log('think', '', `Server Protocol: ${(options.protocol).toUpperCase()}`);
992
1004
  Logger.Log("think", "", `Server running at ${options.protocol === "http2" ? "https" : options.protocol}://${options.hostname || '127.0.0.1'}:${options.port}/`);
993
1005
  Logger.Log("think", "", "====================================");
1006
+ // binding event "appStop"
1007
+ Logger.Log('think', '', 'Bind App Stop event ...');
1008
+ BindProcessEvent(app, 'appStop');
994
1009
  // tslint:disable-next-line: no-unused-expression
995
1010
  app.appDebug && Logger.Warn(`Running in debug mode.`);
996
1011
  };
@@ -1002,11 +1017,12 @@ const listenCallback = (app, options) => {
1002
1017
  * @returns {*}
1003
1018
  */
1004
1019
  const newServe = function (app) {
1020
+ var _a, _b, _c;
1005
1021
  const protocol = app.config("protocol") || "http";
1006
1022
  const port = process.env.PORT || process.env.APP_PORT ||
1007
1023
  app.config('app_port') || 3000;
1008
1024
  const hostname = process.env.IP ||
1009
- process.env.HOSTNAME?.replace(/-/g, '.') || app.config('app_host') || '127.0.0.1';
1025
+ ((_a = process.env.HOSTNAME) === null || _a === void 0 ? void 0 : _a.replace(/-/g, '.')) || app.config('app_host') || '127.0.0.1';
1010
1026
  const options = {
1011
1027
  hostname,
1012
1028
  port,
@@ -1019,8 +1035,8 @@ const newServe = function (app) {
1019
1035
  };
1020
1036
  const pm = new Set(["https", "http2", "wss"]);
1021
1037
  if (pm.has(options.protocol)) {
1022
- const keyFile = app.config("key_file") ?? "";
1023
- const crtFile = app.config("crt_file") ?? "";
1038
+ const keyFile = (_b = app.config("key_file")) !== null && _b !== void 0 ? _b : "";
1039
+ const crtFile = (_c = app.config("crt_file")) !== null && _c !== void 0 ? _c : "";
1024
1040
  options.ext.key = fs.readFileSync(keyFile).toString();
1025
1041
  options.ext.cert = fs.readFileSync(crtFile).toString();
1026
1042
  }
@@ -1031,9 +1047,7 @@ const newServe = function (app) {
1031
1047
  const proto = app.config("protoFile", "router");
1032
1048
  options.ext.protoFile = proto;
1033
1049
  }
1034
- const server = Serve(app, options);
1035
- Helper.define(app, "server", server);
1036
- return server;
1050
+ return Serve(app, options);
1037
1051
  };
1038
1052
  /**
1039
1053
  * Execute event as async
@@ -1093,7 +1107,7 @@ function ComponentScan(scanPath) {
1093
1107
  if (!(target.prototype instanceof Koatty)) {
1094
1108
  throw new Error(`class does not inherit from Koatty`);
1095
1109
  }
1096
- scanPath = scanPath ?? '';
1110
+ scanPath = scanPath !== null && scanPath !== void 0 ? scanPath : '';
1097
1111
  IOCContainer.saveClassMetadata(TAGGED_CLS, COMPONENT_SCAN, scanPath, target);
1098
1112
  };
1099
1113
  }
@@ -1109,7 +1123,7 @@ function ConfigurationScan(scanPath) {
1109
1123
  if (!(target.prototype instanceof Koatty)) {
1110
1124
  throw new Error(`class does not inherit from Koatty`);
1111
1125
  }
1112
- scanPath = scanPath ?? '';
1126
+ scanPath = scanPath !== null && scanPath !== void 0 ? scanPath : '';
1113
1127
  IOCContainer.saveClassMetadata(TAGGED_CLS, CONFIGURATION_SCAN, scanPath, target);
1114
1128
  };
1115
1129
  }
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty",
3
- "version": "3.6.5",
3
+ "version": "3.6.8-0",
4
4
  "description": "Koa2 + Typescript = koatty. Use Typescript's decorator implement auto injection.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -50,6 +50,8 @@
50
50
  }
51
51
  ],
52
52
  "devDependencies": {
53
+ "@commitlint/cli": "^12.x.x",
54
+ "@commitlint/config-conventional": "^15.x.x",
53
55
  "@microsoft/api-documenter": "^7.x.x",
54
56
  "@microsoft/api-extractor": "^7.x.x",
55
57
  "@rollup/plugin-json": "^4.x.x",
@@ -57,11 +59,10 @@
57
59
  "@types/koa": "^2.x.x",
58
60
  "@types/koa__router": "^8.x.x",
59
61
  "@types/node": "^16.x.x",
62
+ "@types/supertest": "^2.x.x",
60
63
  "@types/ws": "^8.x.x",
61
64
  "@typescript-eslint/eslint-plugin": "^5.x.x",
62
65
  "@typescript-eslint/parser": "^5.x.x",
63
- "commitlint": "^11.x.x",
64
- "commitlint-config-gitmoji": "^2.x.x",
65
66
  "conventional-changelog-cli": "^2.x.x",
66
67
  "copyfiles": "^2.x.x",
67
68
  "del-cli": "^4.x.x",
@@ -70,9 +71,14 @@
70
71
  "husky": "^4.x.x",
71
72
  "jest": "^27.x.x",
72
73
  "jest-html-reporters": "^2.x.x",
74
+ "koatty_cacheable": "^1.x.x",
75
+ "koatty_schedule": "^1.x.x",
76
+ "koatty_store": "^1.x.x",
77
+ "koatty_validation": "^1.x.x",
73
78
  "rollup": "^2.x.x",
74
79
  "rollup-plugin-typescript2": "^0.x.x",
75
80
  "standard-version": "^9.x.x",
81
+ "supertest": "^6.x.x",
76
82
  "ts-jest": "^27.x.x",
77
83
  "ts-node": "^10.x.x",
78
84
  "typescript": "^4.x.x"
@@ -81,17 +87,16 @@
81
87
  "koa": "2.13.4",
82
88
  "koatty_config": "1.1.2",
83
89
  "koatty_container": "1.7.7",
84
- "koatty_core": "1.6.1",
85
- "koatty_exception": "1.2.4",
90
+ "koatty_core": "1.6.5",
91
+ "koatty_exception": "1.2.6",
86
92
  "koatty_lib": "1.2.10",
87
93
  "koatty_loader": "1.0.2",
88
94
  "koatty_logger": "1.3.12",
89
- "koatty_payload": "1.3.14",
95
+ "koatty_payload": "1.3.16",
90
96
  "koatty_proto": "1.1.6",
91
- "koatty_router": "1.7.2",
92
- "koatty_serve": "1.4.8",
93
- "koatty_trace": "1.6.4",
94
- "koatty_validation": "1.2.2",
97
+ "koatty_router": "1.7.5",
98
+ "koatty_serve": "1.4.9",
99
+ "koatty_trace": "1.6.6",
95
100
  "reflect-metadata": "0.1.13",
96
101
  "tslib": "2.3.1"
97
102
  }
@@ -1 +1 @@
1
- window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":0,"numPassedTests":0,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":0,"numTotalTests":0,"startTime":1646206717841,"success":false,"testResults":[],"wasInterrupted":false,"config":{"bail":0,"changedFilesWithAncestor":false,"collectCoverage":true,"collectCoverageFrom":[],"coverageDirectory":"coverage","coverageProvider":"babel","coverageReporters":["html","lcov","json","text","clover","text-summary"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"passWithNoTests":true,"projects":[],"reporters":[["default",{}],["/Users/richen/Workspace/nodejs/koatty/node_modules/jest-html-reporters/index.js",{}]],"rootDir":"/Users/richen/Workspace/nodejs/koatty","runTestsByPath":false,"skipFilter":false,"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/Users/richen/Workspace/nodejs/koatty/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true},"endTime":1646206718115,"_reporterOptions":{"publicPath":"/Users/richen/Workspace/nodejs/koatty","filename":"jest_html_reporters.html","expand":false,"pageTitle":"","hideIcon":false,"testCommand":"npx jest","openReport":false,"failureMessageOnly":false,"enableMergeData":false,"dataMergeLevel":1},"attachInfos":{}})
1
+ window.jest_html_reporters_callback__({"numFailedTestSuites":0,"numFailedTests":0,"numPassedTestSuites":0,"numPassedTests":0,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTodoTests":0,"numTotalTestSuites":0,"numTotalTests":0,"startTime":1647253651754,"success":false,"testResults":[],"wasInterrupted":false,"config":{"bail":0,"changedFilesWithAncestor":false,"collectCoverage":true,"collectCoverageFrom":[],"coverageDirectory":"coverage","coverageProvider":"babel","coverageReporters":["html","lcov","json","text","clover","text-summary"],"detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"expand":false,"findRelatedTests":false,"forceExit":false,"json":false,"lastCommit":false,"listTests":false,"logHeapUsage":false,"maxConcurrency":5,"maxWorkers":7,"noStackTrace":false,"nonFlagArgs":[],"notify":false,"notifyMode":"failure-change","onlyChanged":false,"onlyFailures":false,"passWithNoTests":true,"projects":[],"reporters":[["default",{}],["/Users/richen/Workspace/nodejs/koatty/node_modules/jest-html-reporters/index.js",{}]],"rootDir":"/Users/richen/Workspace/nodejs/koatty","runTestsByPath":false,"skipFilter":false,"testFailureExitCode":1,"testPathPattern":"","testSequencer":"/Users/richen/Workspace/nodejs/koatty/node_modules/@jest/test-sequencer/build/index.js","updateSnapshot":"new","useStderr":false,"watch":false,"watchAll":false,"watchman":true},"endTime":1647253651795,"_reporterOptions":{"publicPath":"/Users/richen/Workspace/nodejs/koatty","filename":"jest_html_reporters.html","expand":false,"pageTitle":"","hideIcon":false,"testCommand":"npx jest","openReport":false,"failureMessageOnly":false,"enableMergeData":false,"dataMergeLevel":1},"attachInfos":{}})
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty",
3
- "version": "3.6.5",
3
+ "version": "3.6.8-0",
4
4
  "description": "Koa2 + Typescript = koatty. Use Typescript's decorator implement auto injection.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -50,6 +50,8 @@
50
50
  }
51
51
  ],
52
52
  "devDependencies": {
53
+ "@commitlint/cli": "^12.x.x",
54
+ "@commitlint/config-conventional": "^15.x.x",
53
55
  "@microsoft/api-documenter": "^7.x.x",
54
56
  "@microsoft/api-extractor": "^7.x.x",
55
57
  "@rollup/plugin-json": "^4.x.x",
@@ -57,11 +59,10 @@
57
59
  "@types/koa": "^2.x.x",
58
60
  "@types/koa__router": "^8.x.x",
59
61
  "@types/node": "^16.x.x",
62
+ "@types/supertest": "^2.x.x",
60
63
  "@types/ws": "^8.x.x",
61
64
  "@typescript-eslint/eslint-plugin": "^5.x.x",
62
65
  "@typescript-eslint/parser": "^5.x.x",
63
- "commitlint": "^11.x.x",
64
- "commitlint-config-gitmoji": "^2.x.x",
65
66
  "conventional-changelog-cli": "^2.x.x",
66
67
  "copyfiles": "^2.x.x",
67
68
  "del-cli": "^4.x.x",
@@ -70,9 +71,14 @@
70
71
  "husky": "^4.x.x",
71
72
  "jest": "^27.x.x",
72
73
  "jest-html-reporters": "^2.x.x",
74
+ "koatty_cacheable": "^1.x.x",
75
+ "koatty_schedule": "^1.x.x",
76
+ "koatty_store": "^1.x.x",
77
+ "koatty_validation": "^1.x.x",
73
78
  "rollup": "^2.x.x",
74
79
  "rollup-plugin-typescript2": "^0.x.x",
75
80
  "standard-version": "^9.x.x",
81
+ "supertest": "^6.x.x",
76
82
  "ts-jest": "^27.x.x",
77
83
  "ts-node": "^10.x.x",
78
84
  "typescript": "^4.x.x"
@@ -81,17 +87,16 @@
81
87
  "koa": "2.13.4",
82
88
  "koatty_config": "1.1.2",
83
89
  "koatty_container": "1.7.7",
84
- "koatty_core": "1.6.1",
85
- "koatty_exception": "1.2.4",
90
+ "koatty_core": "1.6.5",
91
+ "koatty_exception": "1.2.6",
86
92
  "koatty_lib": "1.2.10",
87
93
  "koatty_loader": "1.0.2",
88
94
  "koatty_logger": "1.3.12",
89
- "koatty_payload": "1.3.14",
95
+ "koatty_payload": "1.3.16",
90
96
  "koatty_proto": "1.1.6",
91
- "koatty_router": "1.7.2",
92
- "koatty_serve": "1.4.8",
93
- "koatty_trace": "1.6.4",
94
- "koatty_validation": "1.2.2",
97
+ "koatty_router": "1.7.5",
98
+ "koatty_serve": "1.4.9",
99
+ "koatty_trace": "1.6.6",
95
100
  "reflect-metadata": "0.1.13",
96
101
  "tslib": "2.3.1"
97
102
  }
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "types": [
5
+ "jest",
6
+ "node"
7
+ ]
8
+ },
9
+ "exclude": [
10
+ "node_modules"
11
+ ],
12
+ "include": [
13
+ "test/*.test.ts"
14
+ ]
15
+ }
@@ -1,3 +0,0 @@
1
- {
2
- "debug.javascript.unmapMissingSources": true
3
- }