Nestjs module for processing uniconfig-based configs.
yarn add @qiwi/nestjs-enterprise-config
Static import
import { Module } from '@nestjs/common'
import { ConfigModule } from "@qiwi/nestjs-enterprise-config"
@Module({
imports: [
ConfigModule,
// and so on
],
})
export class AppModule {}
Import as dynamic module.
@Module({
imports: [
ConfigModule.register({
// Absolute or relative path to the config file
path: '/custom/config/path.json'
// use <root> or <cwd> tag to form the path.
// path: '<root>/custom/config/path.json'
}),
]
})
export class AppModule {}
@Injectable()
export class MyService {
constructor(@Inject('IConfigService') config: IConfigService) {}
myMethod() {
return this.config.get('name')
}
}
read json file
{
"data": {
"test": "$testFile:"
},
"sources": {
"testFile": {
"data": ["config/ci.json"],
"pipeline": "file>json"
}
}
}
read json from url
{
"data": {
"test": "$testWeb:"
},
"sources": {
"testWeb": {
"data": "URL",
"pipeline": "http>json"
}
}
}
read args
{
"data": {
"param": "$test:"
},
"sources": {
"test": {
"pipeline": "argv"
}
}
}
//node target/es6/main --foo=bar
config.get('param') // { _: [], foo: 'bar', '$0': 'target/es6/main' }
{
"data": {
"test": "$test1:"
},
"sources": {
"test1": {
"data": {
"test.test": "test1",
"test.foo": {
"bar":"baz"
}
}
}
}
}
config.get('test') // { 'test.test': 'test1', 'test.foo': { bar: 'baz' } }
use doT template
{
"data": {
"test": "$test1:"
},
"sources": {
"test1": {
"data": {
"data": {
"data": {
"foo": "FOO",
"baz": "BAZ"
},
"template": "{{=it.foo}}-bar-{{=it.baz}}"
}
},
"pipeline": "datatree>dot"
}
}
}
config.get('test') // FOO-bar-BAZ
read ENV variables
{
"data": {
"test": "$env:LOCAL"
},
"sources": {
"env": {
"pipeline": "env"
}
}
}
//LOCAL=true node target/es6/main
config.get('test') // true
IP/host resolver
{
"data": {
"test": "$host:"
},
"sources": {
"host": {
"pipeline": "ip"
}
}
}
config.get('test') // 192.168.3.5
read package.json
{
"data": {
"test": "$pkg:"
},
"sources": {
"pkg": {
"pipeline": "pkg"
}
}
}
config.get('test') // { name: '', version: '0.0.0', description: '' ...etc }
ConfigService
tries to read config file from <cwd>/config/${process.env.ENVIRONMENT_PROFILE_NAME}.json
.
If it does not exist, then module will read from <cwd>/config/kube.json
(DEFAULT_KUBE_CONFIG_PATH
).
If process.env.LOCAL
is truthy, then service will read from <cwd>/config/local.json
(DEFAULT_LOCAL_CONFIG_PATH
).
Exports ConfigService
with token IConfigService
<cwd>/config/kube.json
<cwd>/config/local.json