UNPKG

1.31 kBJavaScriptView Raw
1module.exports = parseOptions
2
3const _ = require('lodash')
4
5const DEFAULTS = require('./defaults')
6const OPTION_NAMES = [
7 'timeout',
8 'host',
9 'pathPrefix',
10 'protocol',
11 'port',
12 'proxy',
13 'ca',
14 'headers',
15 'requestMedia',
16 'rejectUnauthorized',
17 'family'
18]
19
20function parseOptions (options) {
21 if (!options) {
22 options = {}
23 }
24
25 if ('followRedirects' in options) {
26 console.warn('DEPRECATED: followRedirects option is no longer supported. All redirects are followed correctly')
27 }
28
29 if ('Promise' in options) {
30 console.warn('DEPRECATED: Promise option is no longer supported. The native Promise API is used')
31 }
32
33 options = _.defaults(_.pick(options, OPTION_NAMES), DEFAULTS)
34
35 const defaults = {
36 headers: options.headers,
37 request: {
38 ca: options.ca,
39 family: options.family,
40 proxy: options.proxy,
41 rejectUnauthorized: options.rejectUnauthorized,
42 timeout: options.timeout
43 }
44 }
45
46 defaults.baseUrl = `${options.protocol}://${options.host}`
47
48 if (options.port) {
49 defaults.baseUrl += `:${options.port}`
50 }
51
52 // Check if a prefix is passed in the options and strip any leading or trailing slashes from it.
53 if (options.pathPrefix) {
54 defaults.baseUrl += '/' + options.pathPrefix.replace(/(^[/]+|[/]+$)/g, '')
55 }
56
57 return defaults
58}