npm package: @deboxsoft/config
Description: For setting a global config object managed as a requirement
Latest version: 1.0.5

# React config

<!-- [![Build Status](https://travis-ci.org/deboxsoft/react-config.svg?branch=master)](https://travis-ci.org/deboxsoft/react-config) -->


## Installation

```bash
$ npm install @deboxsoft/react-config
```

## API

__set( configuration [, options] )__

````js
import config from '@deboxsoft/react-config';

config.set({ 
    foo: 'bar',
    bar: {
        baz: 'qux'
    },
    baz: ['qux']
});
````

- __configuration__ whatever you want to be made available when subsequently importing / requiring get function `@deboxsoft/react-config`.
- __options__ object optionally containing the following:
    - __options.freeze__ _default: true_ - used to prevent the [freezing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) of the configuration object.
    - __options.assign__ _default: false_ - causes the passed configuration object to have its properties [assigned](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) to the existing configuration, rather than replacing it.

__get( [key], [default] )__

````js
import config from '@deboxsoft/react-config';

config.get('foo'); //'bar'
config.get('bar'); //{ baz: 'qux' }
config.get('bar.baz'); //'qux'
config.get('baz'); //['qux']
config.get('baz.0'); //'qux'
````

- __key__ key to the setting you want to recover. If you do not put this key you recover all settings.
- __default__ default value if not exists the setting with the specified key. If you do not put this parameter you get `null` value by default.

__serialize()__

````js
import config from '@deboxsoft/react-config';

config.serialize(); //"{foo:'bar',bar:{baz:'qux'},baz:['qux']}"
````

Serialize configuration to a superset of JSON.

__reset()__

````js
import reset from '@deboxsoft/react-config/reset';

reset();
````

This is a testing utility that removes the existing configuration from the require cache. By calling this, calling `config.set(configuration)` and then re-requiring any target file, that target file will then be returned from require with the new `configuration` applied.

## Example Usage

### Server Side

__config.js__ (global configuration file)
````js
const config = {
    foo: 'bar' 
};

export default config;
````

__server.js__ (initiation of server side process)
````js
import config from '@deboxsoft/react-config';
import configuration from './config';
import App from './app';

config.set(configuration);

new App();
````

__render.js__ (render of server side process)
````js
import config from 'react-config';

export renderScripts = () => 
    `
        <script>
            window.__INITIAL_CONFIG__ = ${config.serialize()};
        </script>
    `;
````

### Client Side

__client.js__ (initiation of client side js, assume compiled via browserify / webpack / similar)
````js
import React from 'react';
import config from '@deboxsoft/react-config';
import App from './app';

(function clientJS() {
    config.set(window.__INITIAL_CONFIG__);
    React.render(<App/>, document);
}());
````

### React 

__component.js__ (somewhere inside the client side app)
````js
import React from 'react';
import config from 'react-config';

class Component extends React.Component {
    render() {
        return (
            <div>{ config.get('foo') }</div>
        );
    }
};

export default Component;
````

### Testing
__appLogic.test.js__
````js
import reset from '@deboxsoft/react-config/reset';
import assert from 'assert';

describe('appLogic', () => {
    it('should return foo from configuration', () => {
        import config from '@deboxsoft/react-config';
    
        const foos = [ 'alpha', 'beta', 'gamma' ];
        foos.forEach((foo) => {
            // This only works because `freeze: false` was set the first time set was called (in gulp/test.js).
            config.set({ foo: foo });
            const appLogic = require('./appLogic');
            assert(appLogic() === foo);
        });
    });

    afterEach(() => {
        reset();
    });
});
````

## Thanks

React global configuration was initially inspired by global-configuration. Many thanks to Josh-a-e.