Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment Variables #421

Merged
merged 4 commits into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const _ = require('lodash'),
db = require('./services/db'),
log = require('./services/log').withStandardPrefix(__filename),
composer = require('./services/composer'),
mapLayoutToPageData = require('./utils/layout-to-page-data');
mapLayoutToPageData = require('./utils/layout-to-page-data'),
control = require('./control');

/**
* Check the renderers for the request extension. If the
Expand All @@ -37,6 +38,16 @@ function registerRenderers(renderObj) {
module.exports.renderers = renderers;
}

/**
* Register env variables to be referenced
*
* @param {Array} envArray
*/
function registerEnv(envArray) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this API documented?

// Export the renderers object
module.exports.envVars = envArray;
}

/**
*
* @param {string} ref
Expand Down Expand Up @@ -65,6 +76,22 @@ function transfer(from, to, fromProp, toProp) {
}
}

/**
* Look through env variables array and return
* the variables from the `process.env` bject
*
* @return {Object}
*/
function resolveEnvVars() {
var obj = {};

_.map(module.exports.envVars, function (val) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map is for transforming an array, but this just side-effects. You could use Array.prototype.forEach, _.each or some other _ method to better-communicate intent

obj[val] = process.env[val];
});

return obj;
}

/**
* Add state to result, which adds functionality and information about this request to the templates.
* @param {object} options
Expand All @@ -81,6 +108,12 @@ function applyOptions(options, locals) {
locals
};

// If we're in edit mode then we want to send across the
// environment variables that model.js files might need.
if (locals.edit) {
state._envVars = module.exports.resolveEnvVars();
}

// Get an array of components that are included in the request. This
// includes the root component (layout or other) that is at the base
// of the request and its children.
Expand All @@ -92,7 +125,8 @@ function applyOptions(options, locals) {
transfer(options, state, 'pageRef', '_self');
transfer(options, state, 'pageData', '_pageData');
transfer(options, state, 'version', '_version');
transfer(options, state, 'layoutRef', '_layoutRef'); // TODO: collapse layoutRef and pageRef into the same prop name?
transfer(options, state, 'layoutRef', '_layoutRef');


// Add the list of components to the payload
_.set(state, '_components', componentList);
Expand Down Expand Up @@ -388,6 +422,7 @@ module.exports.renderPage = renderPage;
module.exports.renderUri = renderUri;
module.exports.formDataForRenderer = formDataForRenderer;
module.exports.applyOptions = applyOptions;
module.exports.resolveEnvVars = control.memoize(resolveEnvVars);

// Render Utils
module.exports.rendererExists = rendererExists;
Expand All @@ -397,7 +432,9 @@ module.exports.transfer = transfer;

// Setup
module.exports.renderers = {}; // Overriden when a user registers a render object
module.exports.envVars = [];
module.exports.registerRenderers = registerRenderers;
module.exports.registerEnv = registerEnv;

// For Testing
module.exports.resetUriRouteHandlers = resetUriRouteHandlers;
Expand Down
18 changes: 18 additions & 0 deletions lib/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ describe(_.startCase(filename), function () {
describe('applyOptions', function () {
const fn = lib[this.title];

lib.registerEnv(['foo', 'bar']);

it('consolidates all request information to be sent to the rendererer', function () {
const locals = {
site: {
Expand All @@ -371,8 +373,24 @@ describe(_.startCase(filename), function () {
expect(returnVal._media).to.have.property('styles');
expect(returnVal._media).to.have.property('styles');
});

it('calls `resolveEnvVars` if the request is for edit mode', function () {
const locals = {
site: {
slug: 'site'
},
edit: 'true'
},
callback = fn({}, locals),
returnVal = callback({});

sandbox.stub(lib, 'resolveEnvVars');
expect(returnVal._envVars).to.eql({ foo: undefined, bar: undefined });
});
});



describe('transfer', function () {
const fn = lib[this.title];

Expand Down
4 changes: 3 additions & 1 deletion lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ bluebird.config({
* @returns {Promise}
*/
module.exports = function (options) {
let app, providers, router, sessionStore, renderers, appPlugins;
let app, providers, router, sessionStore, renderers, appPlugins, env;

options = options || {};
app = options.app || express(); // use new express app if not passed in
providers = options.providers || [];
sessionStore = options.sessionStore;
renderers = options.renderers; // TODO: DOCUMENT THIS
appPlugins = options.plugins; // TODO: DOCUMENT THIS
env = options.env || []; // TODO: DOCUMENT THIS

// Init plugins
if (appPlugins) {
Expand All @@ -42,6 +43,7 @@ module.exports = function (options) {
// if engines were passed in, send them to the renderer
if (renderers) {
render.registerRenderers(renderers);
render.registerEnv(env);
}

// look for bootstraps in components
Expand Down
7 changes: 7 additions & 0 deletions lib/setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe(_.startCase(filename), function () {
sandbox = sinon.sandbox.create();
sandbox.stub(plugins, 'registerPlugins');
sandbox.stub(render, 'registerRenderers');
sandbox.stub(render, 'registerEnv');
});

afterEach(function () {
Expand Down Expand Up @@ -49,4 +50,10 @@ describe(_.startCase(filename), function () {
sinon.assert.calledOnce(render.registerRenderers);
});
});

it('registers env vars', function () {
return lib({ renderers: { default: 'html', html: _.noop } }).then(function () {
sinon.assert.calledOnce(render.registerEnv);
});
});
});