options.js 1.4 KB

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