@tsed/config 8.9.0-beta.3 → 8.9.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/lib/esm/hooks/afterResolveConfiguration.js +17 -4
- package/lib/esm/hooks/afterResolveConfiguration.js.map +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/interfaces/ConfigSource.d.ts +3 -1
- package/package.json +6 -6
- package/src/hooks/afterResolveConfiguration.ts +23 -4
- package/src/interfaces/ConfigSource.ts +3 -1
|
@@ -48,10 +48,12 @@ export type ConfigSourceOnChangeCB = () => void;
|
|
|
48
48
|
*/
|
|
49
49
|
export interface ConfigSource<Opts = any> {
|
|
50
50
|
options: Opts;
|
|
51
|
+
$onInit?(): Promise<void> | void;
|
|
52
|
+
$onDestroy?(): Promise<void> | void;
|
|
51
53
|
/**
|
|
52
54
|
* Load configuration from the source.
|
|
53
55
|
*/
|
|
54
56
|
getAll(): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
55
|
-
watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB
|
|
57
|
+
watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB | void | Promise<void>;
|
|
56
58
|
}
|
|
57
59
|
export type ConfigurationExtends = (Type<ConfigSource> | ConfigSourceOptions)[];
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/config",
|
|
3
3
|
"description": "Configuration management for Ts.ED",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.9.0-beta.
|
|
5
|
+
"version": "8.9.0-beta.5",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"Ts.ED",
|
|
8
8
|
"Configuration",
|
|
@@ -49,11 +49,11 @@
|
|
|
49
49
|
"tslib": "2.7.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@tsed/ajv": "8.9.0-beta.
|
|
53
|
-
"@tsed/barrels": "8.9.0-beta.
|
|
54
|
-
"@tsed/core": "8.9.0-beta.
|
|
55
|
-
"@tsed/di": "8.9.0-beta.
|
|
56
|
-
"@tsed/typescript": "8.9.0-beta.
|
|
52
|
+
"@tsed/ajv": "8.9.0-beta.5",
|
|
53
|
+
"@tsed/barrels": "8.9.0-beta.5",
|
|
54
|
+
"@tsed/core": "8.9.0-beta.5",
|
|
55
|
+
"@tsed/di": "8.9.0-beta.5",
|
|
56
|
+
"@tsed/typescript": "8.9.0-beta.5",
|
|
57
57
|
"@types/js-yaml": "4.0.9",
|
|
58
58
|
"dotenv": "^16.5.0",
|
|
59
59
|
"dotenv-expand": "^12.0.2",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "@tsed/ajv";
|
|
2
2
|
|
|
3
|
-
import {configuration, constant, injectable, logger} from "@tsed/di";
|
|
3
|
+
import {configuration, constant, inject, injectable, logger} from "@tsed/di";
|
|
4
4
|
import {$on} from "@tsed/hooks";
|
|
5
5
|
|
|
6
6
|
import {CONFIG_SOURCES} from "../constants/constants.js";
|
|
@@ -12,10 +12,16 @@ export async function afterResolveConfiguration() {
|
|
|
12
12
|
const sources = getConfigSources(constant<ConfigurationExtends>("extends", []));
|
|
13
13
|
|
|
14
14
|
for (const source of sources.values()) {
|
|
15
|
-
const {instance,
|
|
15
|
+
const {instance, watch, refreshOn, name, validationSchema} = source;
|
|
16
|
+
|
|
17
|
+
if (instance?.$onInit) {
|
|
18
|
+
await instance.$onInit();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const getAll = instance.getAll;
|
|
16
22
|
|
|
17
23
|
const refresh = async () => {
|
|
18
|
-
let data = await
|
|
24
|
+
let data = await getAll.apply(instance);
|
|
19
25
|
|
|
20
26
|
if (validationSchema) {
|
|
21
27
|
data = await validate(name!, data, validationSchema);
|
|
@@ -23,15 +29,19 @@ export async function afterResolveConfiguration() {
|
|
|
23
29
|
|
|
24
30
|
source.data = data;
|
|
25
31
|
configuration().set(data);
|
|
32
|
+
|
|
33
|
+
return data;
|
|
26
34
|
};
|
|
27
35
|
|
|
36
|
+
instance.getAll = refresh as any;
|
|
37
|
+
|
|
28
38
|
await refresh();
|
|
29
39
|
|
|
30
40
|
// manage watcher
|
|
31
41
|
if (watch && instance?.watch) {
|
|
32
42
|
const closer = await instance.watch(refresh);
|
|
33
43
|
|
|
34
|
-
$on("$onDestroy", closer);
|
|
44
|
+
closer && $on("$onDestroy", closer);
|
|
35
45
|
}
|
|
36
46
|
|
|
37
47
|
// manager refresh on request
|
|
@@ -75,3 +85,12 @@ export async function afterResolveConfiguration() {
|
|
|
75
85
|
}
|
|
76
86
|
|
|
77
87
|
$on("$afterResolveConfiguration", afterResolveConfiguration);
|
|
88
|
+
$on("$onDestroy", () => {
|
|
89
|
+
const configs = inject<CONFIG_SOURCES>(CONFIG_SOURCES);
|
|
90
|
+
|
|
91
|
+
const promises = Object.values(configs).map((config) => {
|
|
92
|
+
return config.$onDestroy?.();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return Promise.all(promises);
|
|
96
|
+
});
|
|
@@ -54,12 +54,14 @@ export type ConfigSourceOnChangeCB = () => void;
|
|
|
54
54
|
export interface ConfigSource<Opts = any> {
|
|
55
55
|
options: Opts;
|
|
56
56
|
|
|
57
|
+
$onInit?(): Promise<void> | void;
|
|
58
|
+
$onDestroy?(): Promise<void> | void;
|
|
57
59
|
/**
|
|
58
60
|
* Load configuration from the source.
|
|
59
61
|
*/
|
|
60
62
|
getAll(): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
61
63
|
|
|
62
|
-
watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB
|
|
64
|
+
watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB | void | Promise<void>;
|
|
63
65
|
}
|
|
64
66
|
|
|
65
67
|
export type ConfigurationExtends = (Type<ConfigSource> | ConfigSourceOptions)[];
|