-
Notifications
You must be signed in to change notification settings - Fork 67
/
parse-client-options.js
72 lines (57 loc) · 1.88 KB
/
parse-client-options.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
64
65
66
67
68
69
70
71
72
module.exports = parseOptions
const defaults = require('lodash/defaults')
const pick = require('lodash/pick')
const getRequestAgent = require('./get-request-agent')
const DEFAULTS = require('./defaults')
const OPTION_NAMES = [
'timeout',
'baseUrl',
'agent',
'headers'
]
function parseOptions (userOptions) {
if (!userOptions) {
userOptions = {}
}
if ('followRedirects' in userOptions) {
console.warn('DEPRECATED: followRedirects option is no longer supported. All redirects are followed correctly')
}
if ('protocol' in userOptions) {
console.warn('DEPRECATED: protocol option is no longer supported')
}
if ('host' in userOptions) {
console.warn('DEPRECATED: host option is no longer supported')
}
if ('port' in userOptions) {
console.warn('DEPRECATED: port option is no longer supported')
}
if ('pathPrefix' in userOptions) {
console.warn('DEPRECATED: pathPrefix option is no longer supported')
}
if ('Promise' in userOptions) {
console.warn('DEPRECATED: Promise option is no longer supported. The native Promise API is used')
}
const options = defaults(pick(userOptions, OPTION_NAMES), DEFAULTS)
const clientDefaults = {
baseUrl: options.baseUrl,
headers: options.headers,
request: {
timeout: options.timeout
}
}
if (userOptions.protocol) {
clientDefaults.baseUrl = `${userOptions.protocol}://${userOptions.host}`
if (userOptions.port) {
clientDefaults.baseUrl += `:${userOptions.port}`
}
// Check if a prefix is passed in the options and strip any leading or trailing slashes from it.
if (userOptions.pathPrefix) {
clientDefaults.baseUrl += '/' + userOptions.pathPrefix.replace(/(^[/]+|[/]+$)/g, '')
}
}
/* istanbul ignore else */
if (!process.browser) {
clientDefaults.request.agent = getRequestAgent(clientDefaults.baseUrl, userOptions)
}
return clientDefaults
}