@tsed/config 8.9.0-beta.4 → 8.9.0-beta.6

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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Injects a specific configuration source by its name.
3
+ *
4
+ * @param {string} name - The name of the configuration source to inject.
5
+ * @return {Function} A decorator that resolves the configuration source dynamically.
6
+ */
7
+ export declare function InjectConfigSource(name: string): any;
@@ -0,0 +1,10 @@
1
+ import { type InvokeOptions } from "@tsed/di";
2
+ import type { ConfigSource } from "../interfaces/ConfigSource.js";
3
+ /**
4
+ * Injects a configuration source by name and returns it as the specified type.
5
+ *
6
+ * @param {string} name - The name of the configuration source to retrieve.
7
+ * @param {Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>} [opts] - Optional invocation options to customize the behavior of the injection.
8
+ * @return {T} The configuration source cast to the specified type.
9
+ */
10
+ export declare function injectConfigSource<T extends ConfigSource = ConfigSource>(name: string, opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>): T;
@@ -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.6",
6
6
  "keywords": [
7
7
  "Ts.ED",
8
8
  "Configuration",
@@ -36,6 +36,18 @@
36
36
  "types": "./lib/types/providers/json/index.d.ts",
37
37
  "import": "./lib/esm/providers/json/index.js",
38
38
  "default": "./lib/esm/providers/json/index.js"
39
+ },
40
+ "./fn/*": {
41
+ "tsed-source": "./src/fn/*.ts",
42
+ "types": "./lib/types/fn/*.ts",
43
+ "import": "./lib/esm/fn/*.js",
44
+ "default": "./lib/esm/fn/*.js"
45
+ },
46
+ "./decorators/*": {
47
+ "tsed-source": "./src/decorators/*.ts",
48
+ "types": "./lib/types/decorators/*.ts",
49
+ "import": "./lib/esm/decorators/*.js",
50
+ "default": "./lib/esm/decorators/*.js"
39
51
  }
40
52
  },
41
53
  "scripts": {
@@ -49,11 +61,11 @@
49
61
  "tslib": "2.7.0"
50
62
  },
51
63
  "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",
64
+ "@tsed/ajv": "8.9.0-beta.6",
65
+ "@tsed/barrels": "8.9.0-beta.6",
66
+ "@tsed/core": "8.9.0-beta.6",
67
+ "@tsed/di": "8.9.0-beta.6",
68
+ "@tsed/typescript": "8.9.0-beta.6",
57
69
  "@types/js-yaml": "4.0.9",
58
70
  "dotenv": "^16.5.0",
59
71
  "dotenv-expand": "^12.0.2",
package/readme.md CHANGED
@@ -319,6 +319,53 @@ export class MyCustomConfigSource implements ConfigSource<MyCustomConfigSourceOp
319
319
  }
320
320
  ```
321
321
 
322
+ ## Inject configuration source
323
+
324
+ Given a configuration source, you can inject it in any service or controller using his name.
325
+
326
+ ```typescript
327
+ import {Configuration} from "@tsed/di";
328
+ import "@tsed/config";
329
+ import {withOptions} from "@tsed/config";
330
+ import {MyCustomConfigSource} from "./configs/MyCustomConfigSource.js";
331
+
332
+ @Configuration({
333
+ extends: [
334
+ withOptions(MyCustomConfigSource, {
335
+ name: "myCustomConfig"
336
+ })
337
+ ]
338
+ })
339
+ export class Server {}
340
+ ```
341
+
342
+ Then you can inject it in your service or controller using the `@InjectConfigSource` decorator:
343
+
344
+ ```typescript
345
+ import {Injectable, Inject} from "@tsed/di";
346
+ import {InjectConfigSource} from "@tsed/config/decorators/injectConfigSource.js";
347
+ import {MyCustomConfigSource} from "./configs/MyCustomConfigSource.js";
348
+
349
+ @Injectable()
350
+ export class MyService {
351
+ @InjectConfigSource("myCustomConfig")
352
+ configSource: MyCustomConfigSource;
353
+ }
354
+ ```
355
+
356
+ Or using function `injectConfigSource`:
357
+
358
+ ```typescript
359
+ import {Injectable} from "@tsed/di";
360
+ import {injectConfigSource} from "@tsed/config/fn/injectConfigSource.js";
361
+ import {MyCustomConfigSource} from "./configs/MyCustomConfigSource.js";
362
+
363
+ @Injectable()
364
+ export class MyService {
365
+ configSource = injectConfigSource<MyCustomConfigSource>("myCustomConfig");
366
+ }
367
+ ```
368
+
322
369
  ## Contributors
323
370
 
324
371
  <a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
@@ -0,0 +1,43 @@
1
+ import "../hooks/afterResolveConfiguration.js";
2
+
3
+ import {DITest, inject, Injectable} from "@tsed/di";
4
+
5
+ import type {ConfigSource} from "../interfaces/ConfigSource.js";
6
+ import {InjectConfigSource} from "./injectConfigSource.js";
7
+
8
+ class TestConfigSource implements ConfigSource {
9
+ options: {
10
+ hello?: string;
11
+ };
12
+
13
+ getAll() {
14
+ return Promise.resolve({
15
+ value: "string",
16
+ value2: "string-3",
17
+ ...this.options
18
+ });
19
+ }
20
+ }
21
+
22
+ @Injectable()
23
+ class MyService {
24
+ @InjectConfigSource("test")
25
+ config: TestConfigSource;
26
+ }
27
+
28
+ describe("@InjectConfigSource", () => {
29
+ describe("without options", () => {
30
+ beforeEach(() =>
31
+ DITest.create({
32
+ extends: [TestConfigSource]
33
+ })
34
+ );
35
+ afterEach(() => DITest.reset());
36
+
37
+ it("should inject configuration source", () => {
38
+ const service = inject(MyService);
39
+
40
+ expect(service.config).toBeInstanceOf(TestConfigSource);
41
+ });
42
+ });
43
+ });
@@ -0,0 +1,15 @@
1
+ import {Inject} from "@tsed/di";
2
+
3
+ import {CONFIG_SOURCES} from "../constants/constants.js";
4
+
5
+ /**
6
+ * Injects a specific configuration source by its name.
7
+ *
8
+ * @param {string} name - The name of the configuration source to inject.
9
+ * @return {Function} A decorator that resolves the configuration source dynamically.
10
+ */
11
+ export function InjectConfigSource(name: string) {
12
+ return Inject(CONFIG_SOURCES, (configs: CONFIG_SOURCES) => {
13
+ return configs[name];
14
+ });
15
+ }
@@ -0,0 +1,42 @@
1
+ import "../hooks/afterResolveConfiguration.js";
2
+
3
+ import {DITest, inject, Injectable} from "@tsed/di";
4
+
5
+ import type {ConfigSource} from "../interfaces/ConfigSource.js";
6
+ import {injectConfigSource} from "./injectConfigSource.js";
7
+
8
+ class TestConfigSource implements ConfigSource {
9
+ options: {
10
+ hello?: string;
11
+ };
12
+
13
+ getAll() {
14
+ return Promise.resolve({
15
+ value: "string",
16
+ value2: "string-3",
17
+ ...this.options
18
+ });
19
+ }
20
+ }
21
+
22
+ @Injectable()
23
+ class MyService {
24
+ config = injectConfigSource<TestConfigSource>("test");
25
+ }
26
+
27
+ describe("injectConfigSource()", () => {
28
+ describe("without options", () => {
29
+ beforeEach(() =>
30
+ DITest.create({
31
+ extends: [TestConfigSource]
32
+ })
33
+ );
34
+ afterEach(() => DITest.reset());
35
+
36
+ it("should inject configuration source", () => {
37
+ const service = inject(MyService);
38
+
39
+ expect(service.config).toBeInstanceOf(TestConfigSource);
40
+ });
41
+ });
42
+ });
@@ -0,0 +1,20 @@
1
+ import {inject, type InvokeOptions} from "@tsed/di";
2
+
3
+ import {CONFIG_SOURCES} from "../constants/constants.js";
4
+ import type {ConfigSource} from "../interfaces/ConfigSource.js";
5
+
6
+ /**
7
+ * Injects a configuration source by name and returns it as the specified type.
8
+ *
9
+ * @param {string} name - The name of the configuration source to retrieve.
10
+ * @param {Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>} [opts] - Optional invocation options to customize the behavior of the injection.
11
+ * @return {T} The configuration source cast to the specified type.
12
+ */
13
+ export function injectConfigSource<T extends ConfigSource = ConfigSource>(
14
+ name: string,
15
+ opts?: Partial<Pick<InvokeOptions, "useOpts" | "rebuild" | "locals">>
16
+ ): T {
17
+ const configs = inject<CONFIG_SOURCES>(CONFIG_SOURCES, opts);
18
+
19
+ return configs[name] as T;
20
+ }
@@ -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)[];