UNPKG

1.94 kBJavaScriptView Raw
1module.exports = parseOptions
2
3const defaults = require('lodash/defaults')
4const pick = require('lodash/pick')
5
6const deprecate = require('./deprecate')
7const getRequestAgent = require('./get-request-agent')
8const DEFAULTS = require('./defaults')
9const OPTION_NAMES = [
10 'timeout',
11 'baseUrl',
12 'agent',
13 'headers'
14]
15
16function parseOptions (userOptions) {
17 if (!userOptions) {
18 userOptions = {}
19 }
20
21 if ('followRedirects' in userOptions) {
22 deprecate('followRedirects option is no longer supported. All redirects are followed correctly')
23 }
24
25 if ('protocol' in userOptions) {
26 deprecate('protocol option is no longer supported')
27 }
28
29 if ('host' in userOptions) {
30 deprecate('host option is no longer supported')
31 }
32
33 if ('port' in userOptions) {
34 deprecate('port option is no longer supported')
35 }
36
37 if ('pathPrefix' in userOptions) {
38 deprecate('pathPrefix option is no longer supported')
39 }
40
41 if ('Promise' in userOptions) {
42 deprecate('Promise option is no longer supported. The native Promise API is used')
43 }
44
45 const options = defaults(pick(userOptions, OPTION_NAMES), DEFAULTS)
46
47 const clientDefaults = {
48 baseUrl: options.baseUrl,
49 headers: options.headers,
50 request: {
51 timeout: options.timeout
52 }
53 }
54 if (userOptions.protocol) {
55 clientDefaults.baseUrl = `${userOptions.protocol}://${userOptions.host}`
56
57 /* istanbul ignore else */
58 if (userOptions.port) {
59 clientDefaults.baseUrl += `:${userOptions.port}`
60 }
61
62 // Check if a prefix is passed in the options and strip any leading or trailing slashes from it.
63 /* istanbul ignore else */
64 if (userOptions.pathPrefix) {
65 clientDefaults.baseUrl += '/' + userOptions.pathPrefix.replace(/(^[/]+|[/]+$)/g, '')
66 }
67 }
68 /* istanbul ignore else */
69
70 if (!process.browser) {
71 clientDefaults.request.agent = getRequestAgent(clientDefaults.baseUrl, userOptions)
72 }
73
74 return clientDefaults
75}