@fengcart/nestjs-get-config
v1.17.0
Published
NestJS Get Config Loader Module
Downloads
4
Readme
@fengcart/nestjs-get-config
NestJS module for loading configuration from simple getConfig()
factory functions.
Installation
$ yarn add @fengcart/nestjs-get-config
Usage
Each NestJS module in your application can load its own distinct config.
Use module globally
GetConfigModule.register({ isGlobal: true, load: () => ({ foo: 'bar' }) });
load
singleton-scoped
// config.factory.ts
export interface IConfig {
// ...
}
export function getConfig(): IConfig {
// return the configuration object for this module
}
// example.module.ts
import { GetConfigModule } from '@fengcart/nestjs-get-config';
import { getConfig } From './config.factory.ts';
@Module({
imports: [
GetConfigModule.register({ load: getConfig }),
],
providers: [ExampleService],
})
class ExampleModule {}
// example.service.ts
import { CONFIG } from '@fengcart/nestjs-get-config';
import { IConfig } from './config.factory.ts';
@Injectable()
class ExampleService {
constructor(@Inject(CONFIG) private readonly config: IConfig) {}
}
loadWithRequest
request-scoped
Use loadWithRequest
if your module's config factory function requires some context
from the HTTP request.
// config.factory.ts
export interface IConfig {
// singleton-scoped configuration
}
export interface IConfigRequest extends IConfig {
// singleton-scoped & request-scoped configuration
}
export function getConfig(): IBaseConfig {
// return singleton-scoped module configuration
}
export function getConfigRequest(request: Request, config: IConfig): IConfigRequest {
// return config w/ additional request-scoped module configuration
}
// example.module.ts
import { GetConfigModule } from '@fengcart/nestjs-get-config';
import { getConfig, getRequestConfig } From './config.factory.ts';
@Module({
imports: [
GetConfigModule.register({
load: getConfig,
loadWithRequest: getConfigRequest,
}),
],
providers: [ExampleService],
})
class ExampleModule {}
// example.service.ts
import { CONFIG_REQUEST } from '@fengcart/nestjs-get-config';
import { IConfigRequest } from './config.factory.ts';
@Injectable()
class ExampleService {
constructor(@Inject(CONFIG_REQUEST) private readonly config: IConfigRequest) {}
}