koatty 3.9.4 → 3.10.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.
- package/.vscode/settings.json +4 -0
- package/CHANGELOG.md +22 -0
- package/README.md +1 -1
- package/dist/README.md +1 -1
- package/dist/index.d.ts +11 -10
- package/dist/index.js +79 -39
- package/dist/index.mjs +73 -38
- package/dist/package.json +4 -4
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,28 @@
|
|
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.10.0](https://github.com/thinkkoa/koatty/compare/v3.9.5...v3.10.0) (2023-11-08)
|
6
|
+
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* add app event bind ([66e7a48](https://github.com/thinkkoa/koatty/commit/66e7a488c2aa833e37e1418a9d17c91b0872d255))
|
11
|
+
* Custom decorator based on app events ([aa9d96c](https://github.com/thinkkoa/koatty/commit/aa9d96c8bda8364793d868c20eca5d479acb4f88))
|
12
|
+
|
13
|
+
|
14
|
+
### Bug Fixes
|
15
|
+
|
16
|
+
* key is null ([d851dc5](https://github.com/thinkkoa/koatty/commit/d851dc525933de9225ddf43bcbedc2ac29922648))
|
17
|
+
* readme ([84ed9f4](https://github.com/thinkkoa/koatty/commit/84ed9f4261d9c338c1a70d685f3b44deb35a40ee))
|
18
|
+
|
19
|
+
### [3.9.5](https://github.com/thinkkoa/koatty/compare/v3.9.4...v3.9.5) (2023-09-12)
|
20
|
+
|
21
|
+
|
22
|
+
### Bug Fixes
|
23
|
+
|
24
|
+
* import from exception ([beca23c](https://github.com/thinkkoa/koatty/commit/beca23c6b7059ad48997522feff19c716e9a084e))
|
25
|
+
* set process.env.LOGS_PATH ([ca38881](https://github.com/thinkkoa/koatty/commit/ca3888189fd5ed37c0d395f05cdb3672df95b7ff))
|
26
|
+
|
5
27
|
### [3.9.4](https://github.com/thinkkoa/koatty/compare/v3.9.3...v3.9.4) (2023-08-30)
|
6
28
|
|
7
29
|
### [3.9.3](https://github.com/thinkkoa/koatty/compare/v3.9.2...v3.9.3) (2023-08-21)
|
package/README.md
CHANGED
@@ -12,6 +12,7 @@ Use Typescript's decorator implement IOC and AOP.
|
|
12
12
|
* Support loading environment configuration, parsing command line parameters (process.argv) and environment variables (process.env)
|
13
13
|
* `@ExceptionHandler()` Register global exception handling
|
14
14
|
* graceful shutdown and pre-exit event
|
15
|
+
* custom decorator based on app events
|
15
16
|
|
16
17
|
|
17
18
|
## Documentation
|
@@ -32,7 +33,6 @@ npm i -g koatty_cli
|
|
32
33
|
```shell
|
33
34
|
kt new projectName
|
34
35
|
|
35
|
-
npm start
|
36
36
|
```
|
37
37
|
|
38
38
|
### 2. Install deps
|
package/dist/README.md
CHANGED
@@ -12,6 +12,7 @@ Use Typescript's decorator implement IOC and AOP.
|
|
12
12
|
* Support loading environment configuration, parsing command line parameters (process.argv) and environment variables (process.env)
|
13
13
|
* `@ExceptionHandler()` Register global exception handling
|
14
14
|
* graceful shutdown and pre-exit event
|
15
|
+
* custom decorator based on app events
|
15
16
|
|
16
17
|
|
17
18
|
## Documentation
|
@@ -32,7 +33,6 @@ npm i -g koatty_cli
|
|
32
33
|
```shell
|
33
34
|
kt new projectName
|
34
35
|
|
35
|
-
npm start
|
36
36
|
```
|
37
37
|
|
38
38
|
### 2. Install deps
|
package/dist/index.d.ts
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
/*!
|
2
2
|
* @Author: richen
|
3
|
-
* @Date: 2023-08
|
3
|
+
* @Date: 2023-11-08 15:28:11
|
4
4
|
* @License: BSD (3-Clause)
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
6
6
|
* @HomePage: https://koatty.org/
|
7
7
|
*/
|
8
|
+
import { AppEvent } from 'koatty_core';
|
8
9
|
import { Config } from 'koatty_config';
|
10
|
+
import { EventHookFunc } from 'koatty_core';
|
9
11
|
import { Helper } from 'koatty_lib';
|
10
12
|
import { Koatty } from 'koatty_core';
|
11
13
|
import { KoattyContext } from 'koatty_core';
|
@@ -31,8 +33,6 @@ export declare interface ApiOutput {
|
|
31
33
|
data: any;
|
32
34
|
}
|
33
35
|
|
34
|
-
export declare type AppBootHookFunc = (app: Koatty) => Promise<any>;
|
35
|
-
|
36
36
|
/**
|
37
37
|
* Base controller
|
38
38
|
*
|
@@ -100,22 +100,22 @@ export declare class BaseService implements IService {
|
|
100
100
|
}
|
101
101
|
|
102
102
|
/**
|
103
|
-
* bind
|
103
|
+
* @description: bind App event hook func
|
104
104
|
* example:
|
105
105
|
* export function TestDecorator(): ClassDecorator {
|
106
106
|
* return (target: any) => {
|
107
|
-
*
|
107
|
+
* BindEventHook(AppEvent.appBoot, (app: Koatty) => {
|
108
108
|
* // todo
|
109
109
|
* return Promise.resolve();
|
110
110
|
* }, target)
|
111
111
|
* }
|
112
112
|
* }
|
113
|
-
*
|
114
|
-
* @
|
115
|
-
* @param {
|
116
|
-
* @
|
113
|
+
* @param {AppEvent} eventName
|
114
|
+
* @param {EventHookFunc} eventFunc
|
115
|
+
* @param {any} target
|
116
|
+
* @return {*}
|
117
117
|
*/
|
118
|
-
export declare function
|
118
|
+
export declare function BindEventHook(eventName: AppEvent, eventFunc: EventHookFunc, target: any): void;
|
119
119
|
|
120
120
|
/**
|
121
121
|
* Bootstrap application
|
@@ -243,6 +243,7 @@ export { Value }
|
|
243
243
|
|
244
244
|
export * from "koatty_container";
|
245
245
|
export * from "koatty_core";
|
246
|
+
export * from "koatty_exception";
|
246
247
|
export * from "koatty_serve";
|
247
248
|
export * from "koatty_trace";
|
248
249
|
|
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/*!
|
2
2
|
* @Author: richen
|
3
|
-
* @Date: 2023-08
|
3
|
+
* @Date: 2023-11-08 15:27:56
|
4
4
|
* @License: BSD (3-Clause)
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
6
6
|
* @HomePage: https://koatty.org/
|
@@ -12,10 +12,11 @@ var koatty_core = require('koatty_core');
|
|
12
12
|
var path = require('path');
|
13
13
|
var koatty_loader = require('koatty_loader');
|
14
14
|
var koatty_config = require('koatty_config');
|
15
|
-
var koatty_logger = require('koatty_logger');
|
16
|
-
var koatty_trace = require('koatty_trace');
|
17
15
|
var koatty_lib = require('koatty_lib');
|
16
|
+
var koatty_logger = require('koatty_logger');
|
17
|
+
var koatty_exception = require('koatty_exception');
|
18
18
|
var koatty_container = require('koatty_container');
|
19
|
+
var koatty_trace = require('koatty_trace');
|
19
20
|
var koatty_payload = require('koatty_payload');
|
20
21
|
var koatty_serve = require('koatty_serve');
|
21
22
|
|
@@ -61,6 +62,8 @@ function SetLogger(app, config) {
|
|
61
62
|
koatty_logger.DefaultLogger.setLevel(config.logLevel);
|
62
63
|
}
|
63
64
|
if (config.logFilePath && !app.silent) {
|
65
|
+
koatty_lib.Helper.define(app, "logsPath", config.logFilePath);
|
66
|
+
process.env.LOGS_PATH = config.logFilePath;
|
64
67
|
koatty_logger.DefaultLogger.setLogFilePath(config.logFilePath);
|
65
68
|
}
|
66
69
|
if (config.sensFields) {
|
@@ -149,7 +152,6 @@ function formatApiData(msg, data, defaultCode) {
|
|
149
152
|
*/
|
150
153
|
const COMPONENT_SCAN = 'COMPONENT_SCAN';
|
151
154
|
const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
|
152
|
-
const APP_BOOT_HOOK = "APP_BOOT_HOOK";
|
153
155
|
// tslint:disable: no-irregular-whitespace
|
154
156
|
const LOGO = `
|
155
157
|
|
@@ -291,10 +293,10 @@ class Loader {
|
|
291
293
|
// define path
|
292
294
|
const rootPath = app.rootPath || process.cwd();
|
293
295
|
const appPath = app.appPath || path__namespace.resolve(rootPath, env.indexOf('ts-node') > -1 ? 'src' : 'dist');
|
294
|
-
const
|
296
|
+
const koattyPath = path__namespace.resolve(__dirname, '..');
|
295
297
|
koatty_lib.Helper.define(app, 'rootPath', rootPath);
|
296
298
|
koatty_lib.Helper.define(app, 'appPath', appPath);
|
297
|
-
koatty_lib.Helper.define(app, '
|
299
|
+
koatty_lib.Helper.define(app, 'koattyPath', koattyPath);
|
298
300
|
//
|
299
301
|
if (koatty_lib.Helper.isEmpty(app.name)) {
|
300
302
|
const pkg = koatty_lib.Helper.safeRequire(`${path__namespace.dirname(appPath)}/package.json`);
|
@@ -305,12 +307,11 @@ class Loader {
|
|
305
307
|
}
|
306
308
|
process.env.ROOT_PATH = rootPath;
|
307
309
|
process.env.APP_PATH = appPath;
|
308
|
-
process.env.
|
310
|
+
process.env.KOATTY_PATH = koattyPath;
|
309
311
|
// Compatible with old version, will be deprecated
|
310
|
-
koatty_lib.Helper.define(app, 'prevent',
|
311
|
-
koatty_lib.Helper.define(app, '
|
312
|
-
|
313
|
-
koatty_lib.Helper.define(app, 'think_path', thinkPath);
|
312
|
+
koatty_lib.Helper.define(app, 'prevent', koatty_exception.prevent);
|
313
|
+
koatty_lib.Helper.define(app, 'thinkPath', koattyPath);
|
314
|
+
process.env.THINK_PATH = koattyPath;
|
314
315
|
}
|
315
316
|
/**
|
316
317
|
* Get component metadata
|
@@ -408,20 +409,49 @@ class Loader {
|
|
408
409
|
}
|
409
410
|
}
|
410
411
|
/**
|
411
|
-
* Load app
|
412
|
+
* Load app event hook funcs
|
412
413
|
*
|
413
414
|
* @static
|
414
415
|
* @param {Koatty} app
|
415
416
|
* @param {*} target
|
416
417
|
* @memberof Loader
|
417
418
|
*/
|
418
|
-
static
|
419
|
-
const
|
420
|
-
|
421
|
-
funcs
|
422
|
-
|
423
|
-
|
424
|
-
|
419
|
+
static LoadAppEventHooks(app, target) {
|
420
|
+
const eventFuncs = new Map();
|
421
|
+
for (const event of koatty_core.AppEventArr) {
|
422
|
+
let funcs;
|
423
|
+
switch (event) {
|
424
|
+
case "appBoot" /* AppEvent.appBoot */:
|
425
|
+
funcs = koatty_container.IOCContainer.getClassMetadata(koatty_container.TAGGED_CLS, "appBoot" /* AppEvent.appBoot */, target);
|
426
|
+
if (koatty_lib.Helper.isArray(funcs)) {
|
427
|
+
eventFuncs.set("appBoot" /* AppEvent.appBoot */, funcs);
|
428
|
+
}
|
429
|
+
break;
|
430
|
+
case "appReady" /* AppEvent.appReady */:
|
431
|
+
funcs = koatty_container.IOCContainer.getClassMetadata(koatty_container.TAGGED_CLS, "appReady" /* AppEvent.appReady */, target);
|
432
|
+
if (koatty_lib.Helper.isArray(funcs)) {
|
433
|
+
eventFuncs.set("appReady" /* AppEvent.appReady */, funcs);
|
434
|
+
}
|
435
|
+
break;
|
436
|
+
case "appStart" /* AppEvent.appStart */:
|
437
|
+
funcs = koatty_container.IOCContainer.getClassMetadata(koatty_container.TAGGED_CLS, "appStart" /* AppEvent.appStart */, target);
|
438
|
+
if (koatty_lib.Helper.isArray(funcs)) {
|
439
|
+
eventFuncs.set("appStart" /* AppEvent.appStart */, funcs);
|
440
|
+
}
|
441
|
+
break;
|
442
|
+
case "appStop" /* AppEvent.appStop */:
|
443
|
+
funcs = koatty_container.IOCContainer.getClassMetadata(koatty_container.TAGGED_CLS, "appStop" /* AppEvent.appStop */, target);
|
444
|
+
if (koatty_lib.Helper.isArray(funcs)) {
|
445
|
+
eventFuncs.set("appStop" /* AppEvent.appStop */, funcs);
|
446
|
+
}
|
447
|
+
break;
|
448
|
+
}
|
449
|
+
}
|
450
|
+
// loop event emit
|
451
|
+
for (const [event, funcs] of eventFuncs) {
|
452
|
+
for (const func of funcs) {
|
453
|
+
app.once(event, () => func(app));
|
454
|
+
}
|
425
455
|
}
|
426
456
|
}
|
427
457
|
/**
|
@@ -435,7 +465,7 @@ class Loader {
|
|
435
465
|
static LoadConfigs(app, loadPath) {
|
436
466
|
const frameConfig = {};
|
437
467
|
// Logger.Debug(`Load configuration path: ${app.thinkPath}/config`);
|
438
|
-
koatty_loader.Load(["./config"], app.
|
468
|
+
koatty_loader.Load(["./config"], app.koattyPath, function (name, path, exp) {
|
439
469
|
frameConfig[name] = exp;
|
440
470
|
});
|
441
471
|
if (koatty_lib.Helper.isArray(loadPath)) {
|
@@ -459,7 +489,7 @@ class Loader {
|
|
459
489
|
middlewareConf = { config: {}, list: [] };
|
460
490
|
}
|
461
491
|
//Mount default middleware
|
462
|
-
koatty_loader.Load(loadPath || ["./middleware"], app.
|
492
|
+
koatty_loader.Load(loadPath || ["./middleware"], app.koattyPath);
|
463
493
|
//Mount application middleware
|
464
494
|
// const middleware: any = {};
|
465
495
|
const appMiddleware = koatty_container.IOCContainer.listClass("MIDDLEWARE") ?? [];
|
@@ -599,8 +629,12 @@ class Loader {
|
|
599
629
|
const pluginConfList = pluginsConf.list;
|
600
630
|
for (const key of pluginConfList) {
|
601
631
|
const handle = koatty_container.IOCContainer.get(key, "COMPONENT");
|
632
|
+
if (!handle) {
|
633
|
+
Logger.Error(`Plugin ${key} load error.`);
|
634
|
+
continue;
|
635
|
+
}
|
602
636
|
if (!koatty_lib.Helper.isFunction(handle.run)) {
|
603
|
-
Logger.Error(`
|
637
|
+
Logger.Error(`Plugin ${key} must be implements method 'run'.`);
|
604
638
|
continue;
|
605
639
|
}
|
606
640
|
if (pluginsConf.config[key] === false) {
|
@@ -613,7 +647,7 @@ class Loader {
|
|
613
647
|
}
|
614
648
|
}
|
615
649
|
|
616
|
-
var version = "3.
|
650
|
+
var version = "3.10.0";
|
617
651
|
var engines = {
|
618
652
|
node: ">12.0.0"
|
619
653
|
};
|
@@ -710,10 +744,10 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
710
744
|
// configuration metadata
|
711
745
|
const configurationMetas = Loader.GetConfigurationMetas(app, target);
|
712
746
|
Loader.LoadConfigs(app, configurationMetas);
|
713
|
-
// Load App
|
714
|
-
Loader.
|
715
|
-
|
716
|
-
await asyncEvent(app,
|
747
|
+
// Load App event hooks
|
748
|
+
Loader.LoadAppEventHooks(app, target);
|
749
|
+
Logger.Log('Koatty', '', 'Emit App Boot ...');
|
750
|
+
await asyncEvent(app, "appBoot" /* AppEvent.appBoot */);
|
717
751
|
// Load Plugin
|
718
752
|
Logger.Log('Koatty', '', 'Load Plugins ...');
|
719
753
|
await Loader.LoadPlugins(app);
|
@@ -737,7 +771,7 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
737
771
|
koatty_lib.Helper.define(app, "router", koatty_serve.NewRouter(app));
|
738
772
|
// Emit app ready event
|
739
773
|
Logger.Log('Koatty', '', 'Emit App Ready ...');
|
740
|
-
await asyncEvent(app,
|
774
|
+
await asyncEvent(app, "appReady" /* AppEvent.appReady */);
|
741
775
|
// Load Routers
|
742
776
|
Logger.Log('Koatty', '', 'Load Routers ...');
|
743
777
|
app.router.LoadRouter(controllers);
|
@@ -854,23 +888,23 @@ function ConfigurationScan(scanPath) {
|
|
854
888
|
};
|
855
889
|
}
|
856
890
|
/**
|
857
|
-
* bind
|
891
|
+
* @description: bind App event hook func
|
858
892
|
* example:
|
859
893
|
* export function TestDecorator(): ClassDecorator {
|
860
894
|
* return (target: any) => {
|
861
|
-
*
|
895
|
+
* BindEventHook(AppEvent.appBoot, (app: Koatty) => {
|
862
896
|
* // todo
|
863
897
|
* return Promise.resolve();
|
864
898
|
* }, target)
|
865
899
|
* }
|
866
900
|
* }
|
867
|
-
*
|
868
|
-
* @
|
869
|
-
* @param {
|
870
|
-
* @
|
901
|
+
* @param {AppEvent} eventName
|
902
|
+
* @param {EventHookFunc} eventFunc
|
903
|
+
* @param {any} target
|
904
|
+
* @return {*}
|
871
905
|
*/
|
872
|
-
function
|
873
|
-
koatty_container.IOCContainer.attachClassMetadata(koatty_container.TAGGED_CLS,
|
906
|
+
function BindEventHook(eventName, eventFunc, target) {
|
907
|
+
koatty_container.IOCContainer.attachClassMetadata(koatty_container.TAGGED_CLS, eventName, eventFunc, target);
|
874
908
|
}
|
875
909
|
|
876
910
|
/**
|
@@ -990,7 +1024,7 @@ Object.defineProperty(exports, 'Helper', {
|
|
990
1024
|
});
|
991
1025
|
exports.BaseController = BaseController;
|
992
1026
|
exports.BaseService = BaseService;
|
993
|
-
exports.
|
1027
|
+
exports.BindEventHook = BindEventHook;
|
994
1028
|
exports.Bootstrap = Bootstrap;
|
995
1029
|
exports.Component = Component;
|
996
1030
|
exports.ComponentScan = ComponentScan;
|
@@ -1007,10 +1041,10 @@ Object.keys(koatty_core).forEach(function (k) {
|
|
1007
1041
|
get: function () { return koatty_core[k]; }
|
1008
1042
|
});
|
1009
1043
|
});
|
1010
|
-
Object.keys(
|
1044
|
+
Object.keys(koatty_exception).forEach(function (k) {
|
1011
1045
|
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
1012
1046
|
enumerable: true,
|
1013
|
-
get: function () { return
|
1047
|
+
get: function () { return koatty_exception[k]; }
|
1014
1048
|
});
|
1015
1049
|
});
|
1016
1050
|
Object.keys(koatty_container).forEach(function (k) {
|
@@ -1019,6 +1053,12 @@ Object.keys(koatty_container).forEach(function (k) {
|
|
1019
1053
|
get: function () { return koatty_container[k]; }
|
1020
1054
|
});
|
1021
1055
|
});
|
1056
|
+
Object.keys(koatty_trace).forEach(function (k) {
|
1057
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
1058
|
+
enumerable: true,
|
1059
|
+
get: function () { return koatty_trace[k]; }
|
1060
|
+
});
|
1061
|
+
});
|
1022
1062
|
Object.keys(koatty_serve).forEach(function (k) {
|
1023
1063
|
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
1024
1064
|
enumerable: true,
|
package/dist/index.mjs
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
/*!
|
2
2
|
* @Author: richen
|
3
|
-
* @Date: 2023-08
|
3
|
+
* @Date: 2023-11-08 15:27:56
|
4
4
|
* @License: BSD (3-Clause)
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
6
6
|
* @HomePage: https://koatty.org/
|
7
7
|
*/
|
8
8
|
import 'reflect-metadata';
|
9
|
-
import { Koatty } from 'koatty_core';
|
9
|
+
import { AppEventArr, Koatty } from 'koatty_core';
|
10
10
|
export * from 'koatty_core';
|
11
11
|
import * as path from 'path';
|
12
12
|
import { Load } from 'koatty_loader';
|
13
13
|
import { LoadConfigs } from 'koatty_config';
|
14
14
|
export { Config, Value } from 'koatty_config';
|
15
|
-
import { DefaultLogger } from 'koatty_logger';
|
16
|
-
import { Trace, prevent } from 'koatty_trace';
|
17
|
-
export * from 'koatty_trace';
|
18
15
|
import { Helper } from 'koatty_lib';
|
19
16
|
export { Helper } from 'koatty_lib';
|
17
|
+
import { DefaultLogger } from 'koatty_logger';
|
18
|
+
import { prevent } from 'koatty_exception';
|
19
|
+
export * from 'koatty_exception';
|
20
20
|
import { IOCContainer, TAGGED_CLS } from 'koatty_container';
|
21
21
|
export * from 'koatty_container';
|
22
|
+
import { Trace } from 'koatty_trace';
|
23
|
+
export * from 'koatty_trace';
|
22
24
|
import { Payload } from 'koatty_payload';
|
23
25
|
import { NewServe, NewRouter, BindProcessEvent, CONTROLLER_ROUTER } from 'koatty_serve';
|
24
26
|
export * from 'koatty_serve';
|
@@ -46,6 +48,8 @@ function SetLogger(app, config) {
|
|
46
48
|
DefaultLogger.setLevel(config.logLevel);
|
47
49
|
}
|
48
50
|
if (config.logFilePath && !app.silent) {
|
51
|
+
Helper.define(app, "logsPath", config.logFilePath);
|
52
|
+
process.env.LOGS_PATH = config.logFilePath;
|
49
53
|
DefaultLogger.setLogFilePath(config.logFilePath);
|
50
54
|
}
|
51
55
|
if (config.sensFields) {
|
@@ -134,7 +138,6 @@ function formatApiData(msg, data, defaultCode) {
|
|
134
138
|
*/
|
135
139
|
const COMPONENT_SCAN = 'COMPONENT_SCAN';
|
136
140
|
const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
|
137
|
-
const APP_BOOT_HOOK = "APP_BOOT_HOOK";
|
138
141
|
// tslint:disable: no-irregular-whitespace
|
139
142
|
const LOGO = `
|
140
143
|
|
@@ -276,10 +279,10 @@ class Loader {
|
|
276
279
|
// define path
|
277
280
|
const rootPath = app.rootPath || process.cwd();
|
278
281
|
const appPath = app.appPath || path.resolve(rootPath, env.indexOf('ts-node') > -1 ? 'src' : 'dist');
|
279
|
-
const
|
282
|
+
const koattyPath = path.resolve(__dirname, '..');
|
280
283
|
Helper.define(app, 'rootPath', rootPath);
|
281
284
|
Helper.define(app, 'appPath', appPath);
|
282
|
-
Helper.define(app, '
|
285
|
+
Helper.define(app, 'koattyPath', koattyPath);
|
283
286
|
//
|
284
287
|
if (Helper.isEmpty(app.name)) {
|
285
288
|
const pkg = Helper.safeRequire(`${path.dirname(appPath)}/package.json`);
|
@@ -290,12 +293,11 @@ class Loader {
|
|
290
293
|
}
|
291
294
|
process.env.ROOT_PATH = rootPath;
|
292
295
|
process.env.APP_PATH = appPath;
|
293
|
-
process.env.
|
296
|
+
process.env.KOATTY_PATH = koattyPath;
|
294
297
|
// Compatible with old version, will be deprecated
|
295
298
|
Helper.define(app, 'prevent', prevent);
|
296
|
-
Helper.define(app, '
|
297
|
-
|
298
|
-
Helper.define(app, 'think_path', thinkPath);
|
299
|
+
Helper.define(app, 'thinkPath', koattyPath);
|
300
|
+
process.env.THINK_PATH = koattyPath;
|
299
301
|
}
|
300
302
|
/**
|
301
303
|
* Get component metadata
|
@@ -393,20 +395,49 @@ class Loader {
|
|
393
395
|
}
|
394
396
|
}
|
395
397
|
/**
|
396
|
-
* Load app
|
398
|
+
* Load app event hook funcs
|
397
399
|
*
|
398
400
|
* @static
|
399
401
|
* @param {Koatty} app
|
400
402
|
* @param {*} target
|
401
403
|
* @memberof Loader
|
402
404
|
*/
|
403
|
-
static
|
404
|
-
const
|
405
|
-
|
406
|
-
funcs
|
407
|
-
|
408
|
-
|
409
|
-
|
405
|
+
static LoadAppEventHooks(app, target) {
|
406
|
+
const eventFuncs = new Map();
|
407
|
+
for (const event of AppEventArr) {
|
408
|
+
let funcs;
|
409
|
+
switch (event) {
|
410
|
+
case "appBoot" /* AppEvent.appBoot */:
|
411
|
+
funcs = IOCContainer.getClassMetadata(TAGGED_CLS, "appBoot" /* AppEvent.appBoot */, target);
|
412
|
+
if (Helper.isArray(funcs)) {
|
413
|
+
eventFuncs.set("appBoot" /* AppEvent.appBoot */, funcs);
|
414
|
+
}
|
415
|
+
break;
|
416
|
+
case "appReady" /* AppEvent.appReady */:
|
417
|
+
funcs = IOCContainer.getClassMetadata(TAGGED_CLS, "appReady" /* AppEvent.appReady */, target);
|
418
|
+
if (Helper.isArray(funcs)) {
|
419
|
+
eventFuncs.set("appReady" /* AppEvent.appReady */, funcs);
|
420
|
+
}
|
421
|
+
break;
|
422
|
+
case "appStart" /* AppEvent.appStart */:
|
423
|
+
funcs = IOCContainer.getClassMetadata(TAGGED_CLS, "appStart" /* AppEvent.appStart */, target);
|
424
|
+
if (Helper.isArray(funcs)) {
|
425
|
+
eventFuncs.set("appStart" /* AppEvent.appStart */, funcs);
|
426
|
+
}
|
427
|
+
break;
|
428
|
+
case "appStop" /* AppEvent.appStop */:
|
429
|
+
funcs = IOCContainer.getClassMetadata(TAGGED_CLS, "appStop" /* AppEvent.appStop */, target);
|
430
|
+
if (Helper.isArray(funcs)) {
|
431
|
+
eventFuncs.set("appStop" /* AppEvent.appStop */, funcs);
|
432
|
+
}
|
433
|
+
break;
|
434
|
+
}
|
435
|
+
}
|
436
|
+
// loop event emit
|
437
|
+
for (const [event, funcs] of eventFuncs) {
|
438
|
+
for (const func of funcs) {
|
439
|
+
app.once(event, () => func(app));
|
440
|
+
}
|
410
441
|
}
|
411
442
|
}
|
412
443
|
/**
|
@@ -420,7 +451,7 @@ class Loader {
|
|
420
451
|
static LoadConfigs(app, loadPath) {
|
421
452
|
const frameConfig = {};
|
422
453
|
// Logger.Debug(`Load configuration path: ${app.thinkPath}/config`);
|
423
|
-
Load(["./config"], app.
|
454
|
+
Load(["./config"], app.koattyPath, function (name, path, exp) {
|
424
455
|
frameConfig[name] = exp;
|
425
456
|
});
|
426
457
|
if (Helper.isArray(loadPath)) {
|
@@ -444,7 +475,7 @@ class Loader {
|
|
444
475
|
middlewareConf = { config: {}, list: [] };
|
445
476
|
}
|
446
477
|
//Mount default middleware
|
447
|
-
Load(loadPath || ["./middleware"], app.
|
478
|
+
Load(loadPath || ["./middleware"], app.koattyPath);
|
448
479
|
//Mount application middleware
|
449
480
|
// const middleware: any = {};
|
450
481
|
const appMiddleware = IOCContainer.listClass("MIDDLEWARE") ?? [];
|
@@ -584,8 +615,12 @@ class Loader {
|
|
584
615
|
const pluginConfList = pluginsConf.list;
|
585
616
|
for (const key of pluginConfList) {
|
586
617
|
const handle = IOCContainer.get(key, "COMPONENT");
|
618
|
+
if (!handle) {
|
619
|
+
Logger.Error(`Plugin ${key} load error.`);
|
620
|
+
continue;
|
621
|
+
}
|
587
622
|
if (!Helper.isFunction(handle.run)) {
|
588
|
-
Logger.Error(`
|
623
|
+
Logger.Error(`Plugin ${key} must be implements method 'run'.`);
|
589
624
|
continue;
|
590
625
|
}
|
591
626
|
if (pluginsConf.config[key] === false) {
|
@@ -598,7 +633,7 @@ class Loader {
|
|
598
633
|
}
|
599
634
|
}
|
600
635
|
|
601
|
-
var version = "3.
|
636
|
+
var version = "3.10.0";
|
602
637
|
var engines = {
|
603
638
|
node: ">12.0.0"
|
604
639
|
};
|
@@ -695,10 +730,10 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
695
730
|
// configuration metadata
|
696
731
|
const configurationMetas = Loader.GetConfigurationMetas(app, target);
|
697
732
|
Loader.LoadConfigs(app, configurationMetas);
|
698
|
-
// Load App
|
699
|
-
Loader.
|
700
|
-
|
701
|
-
await asyncEvent(app,
|
733
|
+
// Load App event hooks
|
734
|
+
Loader.LoadAppEventHooks(app, target);
|
735
|
+
Logger.Log('Koatty', '', 'Emit App Boot ...');
|
736
|
+
await asyncEvent(app, "appBoot" /* AppEvent.appBoot */);
|
702
737
|
// Load Plugin
|
703
738
|
Logger.Log('Koatty', '', 'Load Plugins ...');
|
704
739
|
await Loader.LoadPlugins(app);
|
@@ -722,7 +757,7 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
|
|
722
757
|
Helper.define(app, "router", NewRouter(app));
|
723
758
|
// Emit app ready event
|
724
759
|
Logger.Log('Koatty', '', 'Emit App Ready ...');
|
725
|
-
await asyncEvent(app,
|
760
|
+
await asyncEvent(app, "appReady" /* AppEvent.appReady */);
|
726
761
|
// Load Routers
|
727
762
|
Logger.Log('Koatty', '', 'Load Routers ...');
|
728
763
|
app.router.LoadRouter(controllers);
|
@@ -839,23 +874,23 @@ function ConfigurationScan(scanPath) {
|
|
839
874
|
};
|
840
875
|
}
|
841
876
|
/**
|
842
|
-
* bind
|
877
|
+
* @description: bind App event hook func
|
843
878
|
* example:
|
844
879
|
* export function TestDecorator(): ClassDecorator {
|
845
880
|
* return (target: any) => {
|
846
|
-
*
|
881
|
+
* BindEventHook(AppEvent.appBoot, (app: Koatty) => {
|
847
882
|
* // todo
|
848
883
|
* return Promise.resolve();
|
849
884
|
* }, target)
|
850
885
|
* }
|
851
886
|
* }
|
852
|
-
*
|
853
|
-
* @
|
854
|
-
* @param {
|
855
|
-
* @
|
887
|
+
* @param {AppEvent} eventName
|
888
|
+
* @param {EventHookFunc} eventFunc
|
889
|
+
* @param {any} target
|
890
|
+
* @return {*}
|
856
891
|
*/
|
857
|
-
function
|
858
|
-
IOCContainer.attachClassMetadata(TAGGED_CLS,
|
892
|
+
function BindEventHook(eventName, eventFunc, target) {
|
893
|
+
IOCContainer.attachClassMetadata(TAGGED_CLS, eventName, eventFunc, target);
|
859
894
|
}
|
860
895
|
|
861
896
|
/**
|
@@ -961,4 +996,4 @@ class BaseService {
|
|
961
996
|
}
|
962
997
|
}
|
963
998
|
|
964
|
-
export { BaseController, BaseService,
|
999
|
+
export { BaseController, BaseService, BindEventHook, Bootstrap, Component, ComponentScan, ConfigurationScan, Controller, ExecBootStrap, Logger, Middleware, Plugin, Service };
|
package/dist/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "koatty",
|
3
|
-
"version": "3.
|
3
|
+
"version": "3.10.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",
|
@@ -81,14 +81,14 @@
|
|
81
81
|
"koa": "^2.14.2",
|
82
82
|
"koatty_config": "^1.1.6",
|
83
83
|
"koatty_container": "^1.8.1",
|
84
|
-
"koatty_core": "^1.
|
84
|
+
"koatty_core": "^1.8.1",
|
85
|
+
"koatty_exception": "^1.2.8",
|
85
86
|
"koatty_lib": "^1.3.4",
|
86
87
|
"koatty_loader": "^1.1.0",
|
87
88
|
"koatty_logger": "^2.1.2",
|
88
89
|
"koatty_payload": "^1.4.6",
|
89
90
|
"koatty_proto": "^1.1.12",
|
90
91
|
"koatty_serve": "^2.0.4",
|
91
|
-
"koatty_trace": "^1.
|
92
|
-
"koatty_validation": "^1.2.8"
|
92
|
+
"koatty_trace": "^1.10.0"
|
93
93
|
}
|
94
94
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "koatty",
|
3
|
-
"version": "3.
|
3
|
+
"version": "3.10.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",
|
@@ -81,14 +81,14 @@
|
|
81
81
|
"koa": "^2.14.2",
|
82
82
|
"koatty_config": "^1.1.6",
|
83
83
|
"koatty_container": "^1.8.1",
|
84
|
-
"koatty_core": "^1.
|
84
|
+
"koatty_core": "^1.8.1",
|
85
|
+
"koatty_exception": "^1.2.8",
|
85
86
|
"koatty_lib": "^1.3.4",
|
86
87
|
"koatty_loader": "^1.1.0",
|
87
88
|
"koatty_logger": "^2.1.2",
|
88
89
|
"koatty_payload": "^1.4.6",
|
89
90
|
"koatty_proto": "^1.1.12",
|
90
91
|
"koatty_serve": "^2.0.4",
|
91
|
-
"koatty_trace": "^1.
|
92
|
-
"koatty_validation": "^1.2.8"
|
92
|
+
"koatty_trace": "^1.10.0"
|
93
93
|
}
|
94
94
|
}
|