Skip to content

French-Exception/node-config-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm version

About

Configuration Library is inspired by service-container2 and Symfony Dependency Injection Container

Purpose here is not to provide a full feature mapping from both libs.

Installation

npm i --save @frenchex/config-lib

Usage

Basic JSON file structure

export interface ConfigurationDeclarationInterface {
    imports?: Array<string>
    ns?: string
    $: object
}

Example :

{
  "imports": ["config_%env%.json"],
  "ns": "example",
  "$": {
    "foo": {
      "bar": "foobar",
      "foobar": "%foo.bar%%foo.bar%"
    }
  }
}

Loading from File

const loader = new ConfigurationLoader();
const request = (() => {
    const requestBuilder = new ConfigurationLoaderFromFileRequestBuilder();

    requestBuilder
        .withFile(path.normalize(path.join(__dirname, '..', '..', 'test-res', 'js.js')))
        .withRoot(path.normalize(path.join(__dirname, '..', '..', 'test-res')))
        .withEnv({env: 'dev'})

    return requestBuilder.build();
})();

const config: ConfigurationInterface = await loader.fromFile(request);
const foobar : string = await config.get<string>('foo.bar'); // return Maybe.just('foobar');

Usages examples (from tests)

Usage 1

const c = new Configuration({
    $: {
        foo: {
            bar: 'foobar',
            foobar: '%foo.bar%%foo.bar%'
        }
    }
});

const foobar = await c.get<string>('foo.bar');
const foobar2 = await c.get<string>('foo.foobar');
const foo = await c.get<object>('foo');

expect(foobar).to.be.equal('foobar');
expect(foobar2).to.be.equal('foobarfoobar');
expect(foo).to.be.deep.equal({bar: 'foobar', foobar: 'foobarfoobar'});

Usage 2

 const loader = new ConfigurationLoader();

const request = await (async () => {
    const requestBuilder = new ConfigurationLoaderFromFileRequestBuilder();

    requestBuilder
        .withFile(path.normalize(path.join(__dirname, '..', '..', 'test-res', 'js.js')))
        .withRoot(path.normalize(path.join(__dirname, '..', '..', 'test-res')))
        .withEnv({env: 'dev'})

    const request = await requestBuilder.build();

    return request;
})();

const config: ConfigurationInterface = await loader.fromFile(request);

const foobar = await config.get<string>('foo.bar');
const foobar2 = await config.get<string>('foo.foobar');
const foo = await config.get<object>('foo');
const promise = await config.get<object>('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');