all files / lib/strategy/ EnrichIntegrationConfigStrategy.js

100% Statements 20/20
100% Branches 10/10
100% Functions 3/3
100% Lines 20/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                                      10×     10×         10×              
'use strict';
 
var extend = require('../helpers/clone-extend').extend;
 
/**
 * Represents a strategy that enriches the configuration (post loading).
 *
 * @class
 */
function EnrichIntegrationConfigStrategy (userConfig) {
  this.userConfig = userConfig;
}
 
EnrichIntegrationConfigStrategy.prototype.process = function (config, callback) {
  var webFeaturesToEnable = [];
 
  // If a user enables a boolean configuration option named `website`, this
  // means the user is building a website and we should automatically enable
  // certain features in the library meant for users developing websites.  This
  // is a simpler way of handling configuration than forcing users to specify
  // all nested JSON configuration options themselves.
  if (config.website) {
    webFeaturesToEnable.push('register');
    webFeaturesToEnable.push('login');
    webFeaturesToEnable.push('logout');
    webFeaturesToEnable.push('me');
  }
 
  // If a user enables a boolean configuration option named `api`, this means
  // the user is building an API service, and we should automatically enable
  // certain features in the library meant for users developing API services --
  // namely, our OAuth2 token endpoint (/oauth/token).  This allows users
  // building APIs to easily provision OAuth2 tokens without specifying any
  // nested JSON configuration options.
  if (config.api) {
    webFeaturesToEnable.push('oauth2');
  }
 
  var userConfig = this.userConfig;
 
  webFeaturesToEnable.forEach(function (feature) {
    var webFeatures = {};
 
    // Only turn on features that haven't already been configured by the user.
    if (!(userConfig && userConfig.web && (feature in userConfig.web) && ('enabled' in userConfig.web[feature]))) {
      webFeatures[feature] = {
        enabled: true
      };
    }
 
    extend(config, {
      web: webFeatures
    });
  });
 
  callback(null, config);
};
 
module.exports = EnrichIntegrationConfigStrategy;