Configuration API let you use Configuration Lib with your own code.
It helps create and load User-scoped, Global-scoped and Directory-scoped Configuration files.
Configuration files can be .json
or .js
files returning plain JSON or
npm i --save @frenchex/config-api
Example loading from file (from tests) :
import * as api from "@frenchex/config-api"
const config = await api.fromFile({
env: {env: 'dev'},
file: path.join(testResourcesRootPath, 'js.js'),
root: testResourcesRootPath
})
const foobar = await config.get<string>('foo.bar');
const foobar2 = await config.get<string>('foo.foobar');
const foo = await config.get<{ bar: string, foobar: string }>('foo');
const promise = await config.get<string>('promise');
expect(foobar).to.be.equal('foobar');
expect(foobar2).to.be.equal('foobarfoobar');
expect(foo).to.be.deep.equal({bar: 'foobar', foobar: 'foobarfoobar'});
expect(promise).to.be.deep.equal('resolved');
Example loading config from object (from tests):
const config = await api.fromDeclaration({
$: {
foo: {
bar: 'foobar'
}
}
})
const bar = await config.get<string>('foo.bar');
expect(bar).to.be.equal('foobar');
You can also do both
const config = await api.fromFile({
env: {env: 'dev'},
file: path.join(testResourcesRootPath, 'js.js'),
root: testResourcesRootPath,
$: {
"imports": ['my_file_%env%.json'],
}
})