conev-source-jsonfile
v1.1.0
Published
conev-source-jsonfile is an implementation of conev's source to get configuration from json file.
Downloads
2
Readme
conev-source-jsonfile
conev-source-jsonfile is an implementation of conev's source to get configuration from json file.
Install
# with npm
npm install conev-source-jsonfile
# or with Yarn
yarn add conev-source-jsonfile
Usage
Get ConfigBuilder from conev and Sources to use.
import { ConfigBuilder } from 'conev';
import ProcessEnvSource from 'conev-source-jsonfile';
And create Source and set up.
const jsonFileSource = new ProcessEnvSource();
jsonFileSource
.setConfig('basic', './basic.json') // basic is JSON
.setConfig('dev', './dev.json') // dev is JSON
.setConfig('prd', './prd.json'); // prd is JSON
Create ConfigBuilder and set Environment, add source. (highest priority is added first).
const builder = new ConfigBuilder();
builder
.setEnv('dev', 'basic')
.addSource(jsonFileSource);
Build configuration
const config = await builder.build(); // This is the result of combining dev and basic.
Use configuration
config.get() // The whole configuration created comes out
config.get('a.b.c'); // Is same as config.get().a.b.c
Json File Source
class JsonFileSource {
constructor(fileMap?: Map<string, string>);
setConfig(env: string, ...filenames: string[]): JsonFileSource;
addConfig(env: string, ...filename: string[]): JsonFileSource;
removeConfig(env: string): JsonFileSource;
export(): Promise<Map<string, object>>;
}
ProcessEnvSource
defines the source from JSON file. Use setConfig
to add a configuration for a new environment or removeConfig
to delete a configuration. Map is returned as the result value of export
. The key of this map is environment and the value is the configuration when environment.