1 | module.exports = parseOptions
|
2 |
|
3 | const defaults = require('lodash/defaults')
|
4 | const pick = require('lodash/pick')
|
5 |
|
6 | const deprecate = require('./deprecate')
|
7 | const getRequestAgent = require('./get-request-agent')
|
8 | const DEFAULTS = require('./defaults')
|
9 | const OPTION_NAMES = [
|
10 | 'timeout',
|
11 | 'baseUrl',
|
12 | 'agent',
|
13 | 'headers'
|
14 | ]
|
15 |
|
16 | function 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 |
|
58 | if (userOptions.port) {
|
59 | clientDefaults.baseUrl += `:${userOptions.port}`
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | if (userOptions.pathPrefix) {
|
65 | clientDefaults.baseUrl += '/' + userOptions.pathPrefix.replace(/(^[/]+|[/]+$)/g, '')
|
66 | }
|
67 | }
|
68 |
|
69 |
|
70 | if (!process.browser) {
|
71 | clientDefaults.request.agent = getRequestAgent(clientDefaults.baseUrl, userOptions)
|
72 | }
|
73 |
|
74 | return clientDefaults
|
75 | }
|