|
1 | | -import { Injectable } from '@nestjs/common'; |
2 | | -import { ConfigService } from '@nestjs/config'; |
| 1 | +import { Inject, Injectable } from '../foundation'; |
| 2 | +import { DotNotation, GetNestedPropertyType } from '../type-helpers'; |
| 3 | +import { Obj } from '../utils'; |
| 4 | +import { CONFIG_FACTORY } from './constant'; |
| 5 | +import { ConfigMap, NamespacedConfigMapValues } from './options'; |
| 6 | + |
| 7 | +type ConfigPaths<T> = DotNotation<T>; |
3 | 8 |
|
4 | 9 | @Injectable() |
5 | | -export class IntentConfig { |
6 | | - private static cachedConfig: Map<string, any>; |
7 | | - private static config: ConfigService; |
| 10 | +export class ConfigService<G = undefined> { |
| 11 | + private static cachedConfig = new Map<string, any>(); |
| 12 | + private static config: ConfigMap; |
8 | 13 |
|
9 | | - constructor(private config: ConfigService) { |
10 | | - IntentConfig.cachedConfig = new Map<string, any>(); |
11 | | - IntentConfig.config = config; |
| 14 | + constructor(@Inject(CONFIG_FACTORY) private config: ConfigMap) { |
| 15 | + ConfigService.cachedConfig = new Map<ConfigPaths<G>, any>(); |
| 16 | + ConfigService.config = this.config; |
12 | 17 | } |
13 | 18 |
|
14 | | - get<T = any>(key: string): T { |
15 | | - if (IntentConfig.cachedConfig.has(key)) |
16 | | - return IntentConfig.cachedConfig.get(key); |
17 | | - const value = this.config.get<T>(key); |
18 | | - IntentConfig.cachedConfig.set(key, value); |
19 | | - return value; |
| 19 | + get<P extends string = ConfigPaths<G>, F = any>( |
| 20 | + key: P, |
| 21 | + ): GetNestedPropertyType<G, P, F> | Promise<GetNestedPropertyType<G, P, F>> { |
| 22 | + return ConfigService.get<G, P>(key); |
20 | 23 | } |
21 | 24 |
|
22 | | - static get<T = any>(key: string): T { |
23 | | - if (this.cachedConfig.has(key)) return this.cachedConfig.get(key); |
24 | | - const value = this.config.get<T>(key); |
25 | | - this.cachedConfig.set(key, value); |
26 | | - return value; |
| 25 | + static get<C = undefined, P extends string = ConfigPaths<C>, F = any>( |
| 26 | + key: P, |
| 27 | + ): GetNestedPropertyType<C, P, F> | Promise<GetNestedPropertyType<C, P, F>> { |
| 28 | + const cachedValue = ConfigService.cachedConfig.get(key); |
| 29 | + if (cachedValue) return cachedValue; |
| 30 | + |
| 31 | + const [namespace, ...paths] = (key as string).split('.'); |
| 32 | + const nsConfig = this.config.get(namespace); |
| 33 | + /** |
| 34 | + * Returns a null value if the namespace doesn't exist. |
| 35 | + */ |
| 36 | + if (!nsConfig) return null; |
| 37 | + |
| 38 | + if (!paths.length) return nsConfig.get('static') as any; |
| 39 | + |
| 40 | + const staticValues = nsConfig.get('static') as Omit< |
| 41 | + NamespacedConfigMapValues, |
| 42 | + 'function' |
| 43 | + >; |
| 44 | + |
| 45 | + const valueOnPath = Obj.get<any>(staticValues, paths.join('.')); |
| 46 | + if (valueOnPath) { |
| 47 | + this.cachedConfig.set(key as string, valueOnPath); |
| 48 | + } |
| 49 | + return valueOnPath; |
27 | 50 | } |
28 | 51 | } |
0 commit comments