config-factory.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var _ = require('lodash');
  2. var url = require('url');
  3. var logger = require('./logger').getInstance();
  4. module.exports = {
  5. createConfig: createConfig
  6. };
  7. function createConfig(context, opts) {
  8. // structure of config object to be returned
  9. var config = {
  10. context: undefined,
  11. options: {}
  12. };
  13. // app.use('/api', proxy({target:'http://localhost:9000'}));
  14. if (isContextless(context, opts)) {
  15. config.context = '/';
  16. config.options = _.assign(config.options, context);
  17. }
  18. // app.use('/api', proxy('http://localhost:9000'));
  19. // app.use(proxy('http://localhost:9000/api'));
  20. else if (isStringShortHand(context)) {
  21. var oUrl = url.parse(context);
  22. var target = [oUrl.protocol, '//', oUrl.host].join('');
  23. config.context = oUrl.pathname || '/';
  24. config.options = _.assign(config.options, {target: target}, opts);
  25. if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
  26. config.options.ws = true;
  27. }
  28. // app.use('/api', proxy({target:'http://localhost:9000'}));
  29. } else {
  30. config.context = context;
  31. config.options = _.assign(config.options, opts);
  32. }
  33. configureLogger(config.options);
  34. if (!config.options.target) {
  35. throw new Error('[HPM] Missing "target" option. Example: {target: "http://www.example.org"}');
  36. }
  37. // Legacy option.proxyHost
  38. config.options = mapLegacyProxyHostOption(config.options);
  39. // Legacy option.proxyTable > option.router
  40. config.options = mapLegacyProxyTableOption(config.options);
  41. return config;
  42. }
  43. /**
  44. * Checks if a String only target/config is provided.
  45. * This can be just the host or with the optional path.
  46. *
  47. * @example
  48. * app.use('/api', proxy('http://localhost:9000'));
  49. app.use(proxy('http://localhost:9000/api'));
  50. *
  51. * @param {String} context [description]
  52. * @return {Boolean} [description]
  53. */
  54. function isStringShortHand(context) {
  55. if (_.isString(context)) {
  56. return (url.parse(context).host) ? true : false;
  57. }
  58. }
  59. /**
  60. * Checks if a Object only config is provided, without a context.
  61. * In this case the all paths will be proxied.
  62. *
  63. * @example
  64. * app.use('/api', proxy({target:'http://localhost:9000'}));
  65. *
  66. * @param {Object} context [description]
  67. * @param {*} opts [description]
  68. * @return {Boolean} [description]
  69. */
  70. function isContextless(context, opts) {
  71. return (_.isPlainObject(context) && _.isEmpty(opts));
  72. }
  73. function mapLegacyProxyHostOption(options) {
  74. // set options.headers.host when option.proxyHost is provided
  75. if (options.proxyHost) {
  76. logger.warn('*************************************');
  77. logger.warn('[HPM] Deprecated "option.proxyHost"');
  78. logger.warn(' Use "option.changeOrigin" or "option.headers.host" instead');
  79. logger.warn(' "option.proxyHost" will be removed in future release.');
  80. logger.warn('*************************************');
  81. options.headers = options.headers || {};
  82. options.headers.host = options.proxyHost;
  83. }
  84. return options;
  85. }
  86. // Warn deprecated proxyTable api usage
  87. function mapLegacyProxyTableOption(options) {
  88. if (options.proxyTable) {
  89. logger.warn('*************************************');
  90. logger.warn('[HPM] Deprecated "option.proxyTable"');
  91. logger.warn(' Use "option.router" instead');
  92. logger.warn(' "option.proxyTable" will be removed in future release.');
  93. logger.warn('*************************************');
  94. options.router = _.clone(options.proxyTable);
  95. _.omit(options, 'proxyTable');
  96. }
  97. return options;
  98. }
  99. function configureLogger(options) {
  100. if (options.logLevel) {
  101. logger.setLevel(options.logLevel);
  102. }
  103. if (options.logProvider) {
  104. logger.setProvider(options.logProvider);
  105. }
  106. }