UNPKG

qcobjects-docs

Version:

The official app and website for documentation of QCObjects

63 lines (46 loc) 2.66 kB
### CONFIG CONFIG is a smart class that manages the global settings of your application. You can get the properties either from a config.json or from the memory previously saved by a set() call. #### Usage from memory: 1.- In your initial code set the CONFIG initial values: ```javascript CONFIG.set('someSettingProperty','some initial value'); ``` 2.- Then you can access it from anywhere in your code by using the get method: ```javascript var someSettingProperty = CONFIG.get('someSettingProperty'); ``` #### Usage from config.json: 1.- You need to indicate first that you are using a config.json file by setting the "useConfigService" value to true ```javascript CONFIG.set('useConfigService',true); // using config.json for custom settings config ``` 2.- Once you have set the value above QCObjects will know and look to the next CONFIG settings into the file config.json in the basePath folder of your application. #### Usage from an encrypted config.json: There is also a way to use an encrypted config.json file in order to protect your settings robots that can steal unprotected data from your web application (like API keys web crawlers). To encrypt your json file go to https://config.qcobjects.dev, put your domain and the config.json content. The tool will encrypt your json and you can copy the encrypted content to insert it in your config.json file. QCObjects will know the data is encrypted and the process to decode the data will be transparent for you. #### Dynamic CONFIG Settings Sometimes you will need to set a value from a source that isn't static, like the ENV vars or another custom source of dynamic data. To get a value using CONFIG from a dynamic source you have to use a processor. There are common processors predefined like $ENV (only available on CLI, Collab and Node) and $config (available on all environments). Processors are called as a meta value either into the config.json file or in the CONFIG Class. ```json // file: config.json { "domain":"localhost", "env1":"$ENV(ENV1)", "customSettings":{ "value1":"$config(domain)" } } ``` ```javascript let value1 = CONFIG.get("customSettings").value1; // value1 = "localhost"; let env1 = CONFIG.get("env1"); //env1 = (environment variable ENV1) ``` ```javascript // sets the key "api_key" of the CONFIG settings to a dynamic processor $ENV that recovers the value of API_KEY from the environment variables CONFIG.set("api_key","$ENV(API_KEY)"); let api_key = CONFIG.get("api_key"); // api_key will contain the value of the system API_KEY environment var // ($ENV processor returns a valid value only on Node.js , QCObjects CLI and QCObjects Collab engine) ```