options.js 1.3 KB

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