You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const { FetchError, Headers, Request, Response } = require('minipass-fetch')
  2. const configureOptions = require('./options.js')
  3. const fetch = require('./fetch.js')
  4. const makeFetchHappen = (url, opts) => {
  5. const options = configureOptions(opts)
  6. const request = new Request(url, options)
  7. return fetch(request, options)
  8. }
  9. makeFetchHappen.defaults = (defaultUrl, defaultOptions = {}, wrappedFetch = makeFetchHappen) => {
  10. if (typeof defaultUrl === 'object') {
  11. defaultOptions = defaultUrl
  12. defaultUrl = null
  13. }
  14. const defaultedFetch = (url, options = {}) => {
  15. const finalUrl = url || defaultUrl
  16. const finalOptions = {
  17. ...defaultOptions,
  18. ...options,
  19. headers: {
  20. ...defaultOptions.headers,
  21. ...options.headers,
  22. },
  23. }
  24. return wrappedFetch(finalUrl, finalOptions)
  25. }
  26. defaultedFetch.defaults = (defaultUrl1, defaultOptions1 = {}) =>
  27. makeFetchHappen.defaults(defaultUrl1, defaultOptions1, defaultedFetch)
  28. return defaultedFetch
  29. }
  30. module.exports = makeFetchHappen
  31. module.exports.FetchError = FetchError
  32. module.exports.Headers = Headers
  33. module.exports.Request = Request
  34. module.exports.Response = Response