Load Config from any place and merge it into one.
npm install @ez-hex/configLoad Config from any place and merge it into one.
`` bash`
npm install --save @ez-hex/configor
yarn add @ez-hex/config
` typescript
interface MyConfig {
// defined your config struct
keyA: string
keyB: number
keyC: boolean
}
// config is Partial
const config = loadConfig
keyA: 'default A',
keyC: false,
}), jsonLoader('./simple.json'), envLoader('AAA_'))
// config is MyConfig
const config = loadConfigWithDefault
keyA: 'default A',
keyB: 1,
keyC: false,
}, jsonLoader('./simple.json'), envLoader('AAA_'))
`
There are two type: SyncLoader and AsyncLoader
` typescript`
type Loader
type SyncLoader
type AsyncLoader
If all args of loadConfig or loadConfigWithDefault is SyncLoader, then return the value directly, else your will get a Promise.
` typescript
function load_with_async_fs
return () => {
return new Promise
readFile(path, 'utf-8', (err, data) => {
try {
resolve(JSON.parse(data))
} catch (err) {
reject(err)
}
})
})
}
}
// config is Promise
const config = loadConfig
// config2 is Promise
const config2 = loadConfigWithDefault
keyA: 'default A',
keyB: 1,
keyC: false,
}, jsonLoader('./simple.json'), envLoader('AAA_'), load_with_async_fs
`
#### defaultLoader
> SyncLoader
It just return the argument
#### envLoaderr
> SyncLoader
It will load all env which has special prefix and parse the value with JSON.parse
` typescript`
// env.js
const result = envLoaderr('A_')()
console.log(result)
` bash`
A_KEYA=aaa node env.js{KEYA: "aaa"}
#### jsonLoaderr
> SyncLoader
load the file and parse the content with JSON.parse`