@tsed/config 8.9.0-beta.5 → 8.9.0-beta.7
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/decorators/injectConfigSource.js +14 -0
- package/lib/esm/decorators/injectConfigSource.js.map +1 -0
- package/lib/esm/fn/injectConfigSource.js +14 -0
- package/lib/esm/fn/injectConfigSource.js.map +1 -0
- package/lib/esm/hooks/afterResolveConfiguration.js +1 -4
- package/lib/esm/hooks/afterResolveConfiguration.js.map +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/types/decorators/injectConfigSource.d.ts +7 -0
- package/lib/types/fn/injectConfigSource.d.ts +10 -0
- package/package.json +18 -6
- package/readme.md +47 -0
- package/src/decorators/injectConfigSource.integration.spec.ts +43 -0
- package/src/decorators/injectConfigSource.ts +15 -0
- package/src/fn/injectConfigSource.integration.spec.ts +42 -0
- package/src/fn/injectConfigSource.ts +20 -0
- package/src/hooks/afterResolveConfiguration.ts +2 -10
|
@@ -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;
|
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.7",
|
|
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.
|
|
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.
|
|
64
|
+
"@tsed/ajv": "8.9.0-beta.7",
|
|
65
|
+
"@tsed/barrels": "8.9.0-beta.7",
|
|
66
|
+
"@tsed/core": "8.9.0-beta.7",
|
|
67
|
+
"@tsed/di": "8.9.0-beta.7",
|
|
68
|
+
"@tsed/typescript": "8.9.0-beta.7",
|
|
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
|
+
}
|
|
@@ -27,8 +27,8 @@ export async function afterResolveConfiguration() {
|
|
|
27
27
|
data = await validate(name!, data, validationSchema);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
source.data = data;
|
|
31
30
|
configuration().set(data);
|
|
31
|
+
configuration().set(`configs.${name}`, data);
|
|
32
32
|
|
|
33
33
|
return data;
|
|
34
34
|
};
|
|
@@ -67,20 +67,12 @@ export async function afterResolveConfiguration() {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
configuration().set(
|
|
71
|
-
"configs",
|
|
72
|
-
Object.fromEntries(
|
|
73
|
-
[...sources.entries()].map(([key, source]) => {
|
|
74
|
-
return [key, source.data];
|
|
75
|
-
})
|
|
76
|
-
)
|
|
77
|
-
);
|
|
78
|
-
|
|
79
70
|
const configs = Object.fromEntries(
|
|
80
71
|
[...sources.entries()].map(([key, source]) => {
|
|
81
72
|
return [key, source.instance];
|
|
82
73
|
})
|
|
83
74
|
);
|
|
75
|
+
|
|
84
76
|
injectable(CONFIG_SOURCES).value(configs);
|
|
85
77
|
}
|
|
86
78
|
|