-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathconfig-factory.js
63 lines (49 loc) · 1.78 KB
/
config-factory.js
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
61
62
63
var _ = require('lodash');
var url = require('url');
module.exports = {
createConfig : createConfig
};
function createConfig (context, opts) {
// structure of config object to be returned
var config = {
context : undefined,
options : {}
};
var useShortHand = isShortHand(context);
if (useShortHand) {
var oUrl = url.parse(context);
var target = [oUrl.protocol, '//', oUrl.host].join('');
config.context = oUrl.pathname || '/';
config.options = _.assign(config.options, {target:target}, opts);
if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
config.options.ws = true;
}
} else {
config.context = context;
config.options = _.assign(config.options, opts);
}
// Legacy option.proxyHost
config.options = mapLegacyProxyHostOption(config.options);
if (!config.options.target) {
throw new Error('[HPM] Missing "target" option. Example: {target: "http://www.example.org"}');
}
return config;
};
function isShortHand (context) {
if (_.isString(context)) {
return (url.parse(context).host) ? true : false;
}
}
function mapLegacyProxyHostOption (options) {
// set options.headers.host when option.proxyHost is provided
if (options.proxyHost) {
console.log('*************************************');
console.log('[HPM] Deprecated "option.proxyHost"');
console.log(' Use "option.changeOrigin" or "option.headers.host" instead');
console.log(' "option.proxyHost" will be removed in future release.');
console.log('*************************************');
options.headers = options.headers || {};
options.headers.host = options.proxyHost;
}
return options;
}