source: trip-planner-front/node_modules/http-proxy-middleware/lib/config-factory.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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