Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): add asProvider helper method to config namespaces
- Loading branch information
1 parent
a3d48e0
commit e76d212
Showing
4 changed files
with
37 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,47 @@ | ||
import { ConfigModule } from '..'; | ||
import { | ||
AS_PROVIDER_METHOD_KEY, | ||
PARTIAL_CONFIGURATION_KEY, | ||
PARTIAL_CONFIGURATION_PROPNAME, | ||
} from '../config.constants'; | ||
import { ConfigFactory } from '../interfaces'; | ||
import { ConfigObject } from '../types'; | ||
import { getConfigToken } from './get-config-token.util'; | ||
|
||
export type ConfigFactoryKeyHost = { KEY: string }; | ||
export interface ConfigFactoryKeyHost<T = unknown> { | ||
KEY: string; | ||
asProvider(): { | ||
imports: [ReturnType<typeof ConfigModule.forFeature>]; | ||
useFactory: (config: T) => T; | ||
inject: [string]; | ||
}; | ||
} | ||
|
||
/** | ||
* Registers the configuration object behind a specified token. | ||
*/ | ||
export function registerAs< | ||
TConfig extends ConfigObject, | ||
TFactory extends ConfigFactory = ConfigFactory<TConfig> | ||
>(token: string, configFactory: TFactory): TFactory & ConfigFactoryKeyHost { | ||
Object.defineProperty(configFactory, PARTIAL_CONFIGURATION_KEY, { | ||
configurable: false, | ||
enumerable: false, | ||
value: token, | ||
writable: false, | ||
}); | ||
Object.defineProperty(configFactory, PARTIAL_CONFIGURATION_PROPNAME, { | ||
configurable: false, | ||
enumerable: false, | ||
value: getConfigToken(token), | ||
writable: false, | ||
}); | ||
TFactory extends ConfigFactory = ConfigFactory<TConfig>, | ||
>( | ||
token: string, | ||
configFactory: TFactory, | ||
): TFactory & ConfigFactoryKeyHost<ReturnType<TFactory>> { | ||
const defineProperty = (key: string, value: unknown) => { | ||
Object.defineProperty(configFactory, key, { | ||
configurable: false, | ||
enumerable: false, | ||
value, | ||
writable: false, | ||
}); | ||
}; | ||
|
||
return configFactory as TFactory & ConfigFactoryKeyHost; | ||
defineProperty(PARTIAL_CONFIGURATION_KEY, token); | ||
defineProperty(PARTIAL_CONFIGURATION_PROPNAME, getConfigToken(token)); | ||
defineProperty(AS_PROVIDER_METHOD_KEY, () => ({ | ||
imports: [ConfigModule.forFeature(configFactory)], | ||
useFactory: (config: unknown) => config, | ||
inject: [getConfigToken(token)], | ||
})); | ||
return configFactory as TFactory & ConfigFactoryKeyHost<ReturnType<TFactory>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters