1const dns = require('dns')
2
3const conditionalHeaders = [
4  'if-modified-since',
5  'if-none-match',
6  'if-unmodified-since',
7  'if-match',
8  'if-range',
9]
10
11const configureOptions = (opts) => {
12  const { strictSSL, ...options } = { ...opts }
13  options.method = options.method ? options.method.toUpperCase() : 'GET'
14  options.rejectUnauthorized = strictSSL !== false
15
16  if (!options.retry) {
17    options.retry = { retries: 0 }
18  } else if (typeof options.retry === 'string') {
19    const retries = parseInt(options.retry, 10)
20    if (isFinite(retries)) {
21      options.retry = { retries }
22    } else {
23      options.retry = { retries: 0 }
24    }
25  } else if (typeof options.retry === 'number') {
26    options.retry = { retries: options.retry }
27  } else {
28    options.retry = { retries: 0, ...options.retry }
29  }
30
31  options.dns = { ttl: 5 * 60 * 1000, lookup: dns.lookup, ...options.dns }
32
33  options.cache = options.cache || 'default'
34  if (options.cache === 'default') {
35    const hasConditionalHeader = Object.keys(options.headers || {}).some((name) => {
36      return conditionalHeaders.includes(name.toLowerCase())
37    })
38    if (hasConditionalHeader) {
39      options.cache = 'no-store'
40    }
41  }
42
43  options.cacheAdditionalHeaders = options.cacheAdditionalHeaders || []
44
45  // cacheManager is deprecated, but if it's set and
46  // cachePath is not we should copy it to the new field
47  if (options.cacheManager && !options.cachePath) {
48    options.cachePath = options.cacheManager
49  }
50
51  return options
52}
53
54module.exports = configureOptions
55