How to use the minipass-fetch.FetchError function in minipass-fetch

To help you get started, we’ve selected a few minipass-fetch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github npm / make-fetch-happen / index.js View on Github external
}
          if (opts.redirect === 'manual') {
            return res
          }
          // if (!fetch.isRedirect(res.status) || opts.redirect === 'manual') {
          //   return res
          // }

          // handle redirects - matches behavior of fetch: https://github.com/bitinn/node-fetch
          if (opts.redirect === 'error') {
            const err = new fetch.FetchError(`redirect mode is set to error: ${uri}`, 'no-redirect', { code: 'ENOREDIRECT' })
            throw err
          }

          if (!res.headers.get('location')) {
            const err = new fetch.FetchError(`redirect location header missing at: ${uri}`, 'no-location', { code: 'EINVALIDREDIRECT' })
            throw err
          }

          if (req.counter >= req.follow) {
            const err = new fetch.FetchError(`maximum redirect reached at: ${uri}`, 'max-redirect', { code: 'EMAXREDIRECT' })
            throw err
          }

          const resolvedUrlParsed = new url.URL(res.headers.get('location'), req.url)
          const resolvedUrl = url.format(resolvedUrlParsed)
          const redirectURL = (isURL.test(res.headers.get('location')))
            ? new url.URL(res.headers.get('location'))
            : resolvedUrlParsed

          // Remove authorization if changing hostnames (but not if just
          // changing ports or protocols).  This matches the behavior of request:
github npm / make-fetch-happen / index.js View on Github external
//   return res
          // }

          // handle redirects - matches behavior of fetch: https://github.com/bitinn/node-fetch
          if (opts.redirect === 'error') {
            const err = new fetch.FetchError(`redirect mode is set to error: ${uri}`, 'no-redirect', { code: 'ENOREDIRECT' })
            throw err
          }

          if (!res.headers.get('location')) {
            const err = new fetch.FetchError(`redirect location header missing at: ${uri}`, 'no-location', { code: 'EINVALIDREDIRECT' })
            throw err
          }

          if (req.counter >= req.follow) {
            const err = new fetch.FetchError(`maximum redirect reached at: ${uri}`, 'max-redirect', { code: 'EMAXREDIRECT' })
            throw err
          }

          const resolvedUrlParsed = new url.URL(res.headers.get('location'), req.url)
          const resolvedUrl = url.format(resolvedUrlParsed)
          const redirectURL = (isURL.test(res.headers.get('location')))
            ? new url.URL(res.headers.get('location'))
            : resolvedUrlParsed

          // Remove authorization if changing hostnames (but not if just
          // changing ports or protocols).  This matches the behavior of request:
          // https://github.com/request/request/blob/b12a6245/lib/redirect.js#L134-L138
          if (new url.URL(req.url).hostname !== redirectURL.hostname) {
            req.headers.delete('authorization')
          }
github npm / make-fetch-happen / index.js View on Github external
return retryHandler(res)
          }

          if (!fetch.isRedirect(res.status)) {
            return res
          }
          if (opts.redirect === 'manual') {
            return res
          }
          // if (!fetch.isRedirect(res.status) || opts.redirect === 'manual') {
          //   return res
          // }

          // handle redirects - matches behavior of fetch: https://github.com/bitinn/node-fetch
          if (opts.redirect === 'error') {
            const err = new fetch.FetchError(`redirect mode is set to error: ${uri}`, 'no-redirect', { code: 'ENOREDIRECT' })
            throw err
          }

          if (!res.headers.get('location')) {
            const err = new fetch.FetchError(`redirect location header missing at: ${uri}`, 'no-location', { code: 'EINVALIDREDIRECT' })
            throw err
          }

          if (req.counter >= req.follow) {
            const err = new fetch.FetchError(`maximum redirect reached at: ${uri}`, 'max-redirect', { code: 'EMAXREDIRECT' })
            throw err
          }

          const resolvedUrlParsed = new url.URL(res.headers.get('location'), req.url)
          const resolvedUrl = url.format(resolvedUrlParsed)
          const redirectURL = (isURL.test(res.headers.get('location')))

minipass-fetch

An implementation of window.fetch in Node.js using Minipass streams

MIT
Latest version published 3 months ago

Package Health Score

95 / 100
Full package analysis