Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/bs / src / oneConfig / getConfigForEnv.ts
Size: Mime:
/**
 * @file get a config property for the current environment (server | client)
 *
 *
 * @example
 *   import config from '../config';
 *   config('welcomeMessage'); // => "Hello World!"
 */
/**
 * @todo change this, it should import lodash/get
 */
import { get } from 'lodash'
import configCache from './PROPERTIES'

/**
 * This function wraps up the boilerplate needed to access the correct
 * configuration depending on whether your code will get executed in the
 * browser/node.
 *
 * @example
 *  - For the browser the config values are available at window.__CLIENT_CONFIG__
 *  - For a node process they are within the "<root>/config".
 *
 * To request a configuration value you must provide the repective path.
 * @example with the following configuration structure:
 *   {
 *     foo: {
 *       bar: [1, 2, 3]
 *     },
 *     bob: 'bob'
 *   }
 *
 * @example You could use this function to access "bar" like so:
 *   import config from '../config';
 *   const value = config('foo.bar');
 *
 * @example And you could access "bob" like so:
 *   import config from '../config';
 *   const value = config('bob');
 *
 *
 * --------------------------------------------------------------------------------
 *
 * @description loads the right config for browser || server
 * @see server/serverSideRendering/reactApplication
 * @extends https://lodash.com/docs/4.17.4#get
 * @example
 *    PORT=5000;
 *    config.get('port');
 *    //=> 5000
 *
 * @param {String | Array<string>} path
 * @return {String | Number | Boolean | undefined} process.env[...path] || config[...path]
 *
 * --------------------------------------------------------------------------------
 * sometimes docs are worse than no docs vvvvvvvvvvvvv
 * --------------------------------------------------------------------------------
 * This helper function allows you to use the same API in accessing configuration
 * values no matter where the code is being executed (@example browser/node).
 * This resolves the correct configuration source based on the execution
 * environment.  For node we use the standard config file, however, for browsers
 * we need to access the configuration object that would have been bound to
 * the "window" by our "reactApplication" middleware.
 * This function wraps up the boilerplate needed to access the correct
 * configuration depending on whether your code will get executed in the
 * browser/node.
 * To request a configuration value you must provide the repective path.
 * If any part of the path isn't available as a configuration key/value then
 * an error will be thrown indicating that a respective configuration value
 * could not be found at the given path.
 */
function getConfigForEnv(path: string | string[], fallback = undefined) {
  let configObjForEnv = configCache
  validateAccess(configObjForEnv, path)
  const result = get(configObjForEnv, path)

  if (result === undefined) {
    return fallback
  } else {
    return result
  }
}

const IS_DEV_CLIENT =
  process.env.BUILD_FLAG_IS_DEV === 'true' && process.env.BUILD_FLAG_IS_CLIENT === 'true'

/**
 * @private
 * @param {*} result
 * @param {Array} parts
 * @throws when accessing on client, or using a path that does not exist
 */
function validateAccess(result, parts) {
  if (result === undefined) {
    const dotPath = Array.isArray(parts) ? parts.join('.') : parts
    const errorMessage = `Failed to resolve configuration value at "${dotPath}".`

    // This "if" block gets stripped away by webpack for production builds.
    if (IS_DEV_CLIENT) {
      const clientError = `${errorMessage} We have noticed
        that you are trying to access this configuration value
        from the client bundle (i.e. code that will be executed in a browser).
        For configuration values to be exposed to the client bundle
        you must ensure that the path is added
        to the client configuration filter
        in the project configuration values file.
      `
      const clientErrorOneLine = clientError.split('\n').join(' ')
      throw new Error(clientErrorOneLine)
    } else {
      throw new Error(errorMessage)
    }
  }
}

getConfigForEnv.getConfigForEnv = getConfigForEnv
getConfigForEnv.get = getConfigForEnv
getConfigForEnv.cache = configCache

export { getConfigForEnv as getConfig, getConfigForEnv, get, configCache as cache }
export default getConfigForEnv