function request (url, options) {
  options = Object.assign({}, defaultOptions, options)
  let savedContent
  let savedResponse

  return new Promise((resolve, reject) => {
    fetch(url, options)
      .then(handleResponse)
      .then(handleBody)
      .catch(handleError)

    function handleResponse (response) {
      // Save the response for checking the status later
      savedResponse = response

      // Decode the response body
      switch (options.type) {
        case 'response':
          return response
        case 'json':
          return response.json()
        default:
          return response.text()
      }
    }

    function handleBody (content) {
      // Bubble an error if the response status is not okay
      if (savedResponse && savedResponse.status >= 400) {
        savedContent = content
        throw new Error(`Response status indicates error`)
      }

      // All is well!
      resolve(content)
    }

    function handleError (err) {
      // Overwrite potential decoding errors when the actual problem was the response
      if (savedResponse && savedResponse.status >= 400) {
        err = new Error(`Status ${savedResponse.status}`)
      }

      // Enrich the error message with the response and the content
      let error = new Error(err.message)
      error.response = savedResponse
      error.content = savedContent
      reject(error)
    }
  })
}
// Send a request using the underlying fetch API
Send a request using the underlying fetch API
Send a request using the underlying fetch API
function request (url, options) {
  options = Object.assign({}, defaultOptions, options)
  let savedContent
  let savedResponse
  return new Promise((resolve, reject) => {
    fetch(url, options)
      .then(handleResponse)
      .then(handleBody)
      .catch(handleError)
    function handleResponse (response) {
      savedResponse = response
      switch (options.type) {
        case 'response':
          return response
        case 'json':
          return response.json()
        default:
          return response.text()
      }
    }
    function handleBody (content) {
      if (savedResponse && savedResponse.status >= 400) {
        savedContent = content
        throw new Error(`Response status indicates error`)
      }
      resolve(content)
    }
    function handleError (err) {
      if (savedResponse && savedResponse.status >= 400) {
        err = new Error(`Status ${savedResponse.status}`)
      }
      let error = new Error(err.message)
      error.response = savedResponse
      error.content = savedContent
      reject(error)
    }
  })
}
function request ( url , options ) { options = Object . assign ( { } , defaultOptions , options ) let savedContent let savedResponse return new Promise ( ( resolve , reject ) => { fetch ( url , options ) . then ( handleResponse ) . then ( handleBody ) . catch ( handleError ) function handleResponse ( response ) { savedResponse = response switch ( options . type ) { case 'response' : return response case 'json' : return response . json ( ) default : return response . text ( ) } } function handleBody ( content ) { if ( savedResponse && savedResponse . status >= 400 ) { savedContent = content throw new Error ( ` ` ) } resolve ( content ) } function handleError ( err ) { if ( savedResponse && savedResponse . status >= 400 ) { err = new Error ( ` ${ savedResponse . status } ` ) } let error = new Error ( err . message ) error . response = savedResponse error . content = savedContent reject ( error ) } } ) }
Send a request using the underlying fetch API
Send a request using the underlying fetch API
// Save the response for checking the status later // Decode the response body // Bubble an error if the response status is not okay // All is well! // Overwrite potential decoding errors when the actual problem was the response // Enrich the error message with the response and the content