config-factory.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var _ = require('lodash')
  2. var url = require('url')
  3. var ERRORS = require('./errors')
  4. var logger = require('./logger').getInstance()
  5. module.exports = {
  6. createConfig: createConfig
  7. }
  8. function createConfig(context, opts) {
  9. // structure of config object to be returned
  10. var config = {
  11. context: undefined,
  12. options: {}
  13. }
  14. // app.use('/api', proxy({target:'http://localhost:9000'}));
  15. if (isContextless(context, opts)) {
  16. config.context = '/'
  17. config.options = _.assign(config.options, context)
  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(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING)
  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
  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(
  79. ' Use "option.changeOrigin" or "option.headers.host" instead'
  80. )
  81. logger.warn(' "option.proxyHost" will be removed in future release.')
  82. logger.warn('*************************************')
  83. options.headers = options.headers || {}
  84. options.headers.host = options.proxyHost
  85. }
  86. return options
  87. }
  88. // Warn deprecated proxyTable api usage
  89. function mapLegacyProxyTableOption(options) {
  90. if (options.proxyTable) {
  91. logger.warn('*************************************')
  92. logger.warn('[HPM] Deprecated "option.proxyTable"')
  93. logger.warn(' Use "option.router" instead')
  94. logger.warn(' "option.proxyTable" will be removed in future release.')
  95. logger.warn('*************************************')
  96. options.router = _.clone(options.proxyTable)
  97. _.omit(options, 'proxyTable')
  98. }
  99. return options
  100. }
  101. function configureLogger(options) {
  102. if (options.logLevel) {
  103. logger.setLevel(options.logLevel)
  104. }
  105. if (options.logProvider) {
  106. logger.setProvider(options.logProvider)
  107. }
  108. }