all files / lib/strategy/ EnrichClientConfigStrategy.js

100% Statements 13/13
100% Branches 9/9
100% Functions 2/2
100% Lines 13/13
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                                                               
'use strict';
 
var extend = require('../helpers/clone-extend').extend;
 
/**
 * Represents a strategy that enriches the API Key configuration (post loading).
 *
 * @class
 */
function EnrichClientConfigStrategy () {
}
 
EnrichClientConfigStrategy.prototype.process = function (config, callback) {
  // If we have specified an API key in the root, then copy this to our client API key.
  if (config.apiKey) {
    var extendWith = {};
 
    // Only copy if we have a value set.
    // This is to things such as NULL defaults.
    for (var key in config.apiKey) {
      if (config.apiKey[key]) {
        extendWith[key] = config.apiKey[key];
      }
    }
 
    extend(config, {
      client: {
        apiKey: extendWith
      }
    })
  }
 
  // For backwards compatibility reasons, if no API key is specified we'll try
  // to grab the API credentials out of our new format and shove it into the old
  // format.  This can go away once we cut a release and decide to no longer
  // support the old configuration formatting.
  if (!config.apiKey && config.client && config.client.apiKey) {
    config.apiKey = config.client.apiKey;
  }
 
  callback(null, config);
};
 
module.exports = EnrichClientConfigStrategy;