@tsed/config 8.9.0-beta.4 → 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.
@@ -49,10 +49,11 @@ export type ConfigSourceOnChangeCB = () => void;
49
49
  export interface ConfigSource<Opts = any> {
50
50
  options: Opts;
51
51
  $onInit?(): Promise<void> | void;
52
+ $onDestroy?(): Promise<void> | void;
52
53
  /**
53
54
  * Load configuration from the source.
54
55
  */
55
56
  getAll(): Promise<Record<string, unknown>> | Record<string, unknown>;
56
- watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB;
57
+ watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB | void | Promise<void>;
57
58
  }
58
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.4",
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.4",
53
- "@tsed/barrels": "8.9.0-beta.4",
54
- "@tsed/core": "8.9.0-beta.4",
55
- "@tsed/di": "8.9.0-beta.4",
56
- "@tsed/typescript": "8.9.0-beta.4",
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";
@@ -18,8 +18,10 @@ export async function afterResolveConfiguration() {
18
18
  await instance.$onInit();
19
19
  }
20
20
 
21
+ const getAll = instance.getAll;
22
+
21
23
  const refresh = async () => {
22
- let data = await instance.getAll();
24
+ let data = await getAll.apply(instance);
23
25
 
24
26
  if (validationSchema) {
25
27
  data = await validate(name!, data, validationSchema);
@@ -27,15 +29,19 @@ export async function afterResolveConfiguration() {
27
29
 
28
30
  source.data = data;
29
31
  configuration().set(data);
32
+
33
+ return data;
30
34
  };
31
35
 
36
+ instance.getAll = refresh as any;
37
+
32
38
  await refresh();
33
39
 
34
40
  // manage watcher
35
41
  if (watch && instance?.watch) {
36
42
  const closer = await instance.watch(refresh);
37
43
 
38
- $on("$onDestroy", closer);
44
+ closer && $on("$onDestroy", closer);
39
45
  }
40
46
 
41
47
  // manager refresh on request
@@ -79,3 +85,12 @@ export async function afterResolveConfiguration() {
79
85
  }
80
86
 
81
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
+ });
@@ -55,12 +55,13 @@ export interface ConfigSource<Opts = any> {
55
55
  options: Opts;
56
56
 
57
57
  $onInit?(): Promise<void> | void;
58
+ $onDestroy?(): Promise<void> | void;
58
59
  /**
59
60
  * Load configuration from the source.
60
61
  */
61
62
  getAll(): Promise<Record<string, unknown>> | Record<string, unknown>;
62
63
 
63
- watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB;
64
+ watch?(onChange?: ConfigSourceOnChangeCB): Promise<ConfigSourceWatchCloseCB> | ConfigSourceWatchCloseCB | void | Promise<void>;
64
65
  }
65
66
 
66
67
  export type ConfigurationExtends = (Type<ConfigSource> | ConfigSourceOptions)[];