Repository URL to install this package:
|
Version:
3.6.0 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/**
* @file get a config property for the current environment (server | client)
*
*
* @example
* import config from '../config';
* config('welcomeMessage'); // => "Hello World!"
*/
const get_1 = tslib_1.__importDefault(require("lodash/get"));
exports.get = get_1.default;
const PROPERTIES_1 = tslib_1.__importDefault(require("./PROPERTIES"));
exports.cache = PROPERTIES_1.default;
/**
* 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, fallback = undefined) {
const configObjForEnv = PROPERTIES_1.default;
validateAccess(configObjForEnv, path);
const result = get_1.default(configObjForEnv, path);
if (result === undefined) {
return fallback;
}
else {
return result;
}
}
exports.getConfig = getConfigForEnv;
exports.getConfigForEnv = getConfigForEnv;
const IS_DEV_CLIENT = process.env.NODE_ENV === 'development' &&
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 = PROPERTIES_1.default;
exports.default = getConfigForEnv;
//# sourceMappingURL=getConfigForEnv.js.map