1.0.3 • Published 5 years ago

flk-config v1.0.3

Weekly downloads
15
License
-
Repository
github
Last release
5 years ago

Configurations

Most of the framework packages support developer default configurations to be used allover the called packages|components to be much easier on usage.

All of the configurations lies on file in the app directory config.js which should be located in src/appName/modules/config.js.

The Config class handles configurations in the runtime, thus it has nothing to do with the framework configurations in the config.json file in the root directory.

Table of contents

Usage

Now let's see how to use it.

Using the Config.extend method makes it more convenient to set bulk of configurations in one time.

config.js file

// runtime configurations
Config.extend({
    http: {
        endpoint: {
            baseUrl: '',
            apiKey: '', // used with the `key` for Authorization header 
        }
    }
});

Now let's see how to get any values from that config file.

Getting values from the config file

The Config class has Config.get method which makes it easy for you to get any value from it.

Get

get(key: string, defaultValue: any = null): any

This method is used to get any value from the config.js file using the object dot notation syntax.

The object dot notation syntax means that the configurations is marked as namespaces which means you can get the object value of a primary key in the configurations.

Let's see what does it mean in action.

let httpOptions = Config.get('http');
// the returned value will be: 
httpOptions = {
    endpoint: {
        baseUrl: '',
        apiKey: ''
    }

Now let's say that you want only the value of the apiKey, here we will use the object dot notation syntax

let endPointApiKey = Config.get('http.endpoint.apiKey');

That's it!, so if we want to get a certain value for a key from any of configurations you can write the key name like this mainKey.subKey.subSubKey.key.

Has

static has(key: string): boolean

To check if the given key exists in the configurations file.

if (Config.has('some.value')) {
    // do something
}

Set

static set(key: string, value: mixed): void

Set/Replace the value for the given key

Config.set('app.name', 'My New App Name!');

Precompiled configurations

There are some precompiled configurations that is added to the config handler, it all relies under the app scope.

For example: Config.get('app.name'); // Blog

List of precompiled configurations

Here is the available options under the app scope:

keyValueDescription
nameDynamicBased on current application name, the value will be set.
directionltr/rtlThe current direction of the application, defaults to the base direction of the app in the root config.json file.
localeCodeDynamicThe current locale code of the application, defaults to the base direction of the app in the root config.json file.
envdevelopment/productionCurrent environment mode of the application, on the flk serve it will be development.
baseUrlDynamicThe base url set in the root config config.json file.
scriptPathDynamicThe script path of the current application, defaults to / for base app.
scriptUrlDynamicThe script full url of the current application, defaults to the baseUrl combined with scriptPath value.
1.0.3

5 years ago

1.0.1

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago