dispatchRequest

promis = dispatchRequest(newConfig)
Copy the code

dispatchRequest

function dispatchRequest(config){
  throwIfCancellationRequested(config)
  config.headers = config.headers || {}
  // Output the requested data conversion based on the request type
  config.data = transfromData.call(config,config.data,config.headers,config.transformRequest)
  // Output the header based on the request type
  config.headers = utils.merge(config.headers.common || {},config.headers[config.method] || {},config.headers)
  // Remove unnecessary headers
  utils.forEach(['delete'.'get'.'head'.'post'.'put'.'patch'.'common'].(method) = > {
    delete config.headers[method]
  })
  // Get the HTTP request method
  var adapter = config.adapter || defaults.adapter
  // Make a data request
  return adapter(config).then((response) = > {
    // cancelToken to terminate the request
    throwIfCancellationRequested(config)
    // Convert the response according to transfromResponse
    response.data = transformData.call(config,response.data,response.headers,config.transformResponse)
  },(reason) = > {
    if(! isCancel(reason)){ throwIfCancellationRequested(config)if(reason&& reason.respobse){
        // Determine the reason data to convertreason.response.data = transfromData.call(config,reason.response.data,reason.response.headers,config.transfromResponse) }}return Promise.reject(reason)
  })
}
Copy the code
throwIfCancellationRequested
function throwIfCancellationRequested(config){
  // Throw an exception if the request fails
  if(config.cancelToken){
    config.cancelToken.throwIfRequested()
  }
}
Copy the code
transformData
function transformData(data,headers,fns){
  var context = this || defaults
  // Iterate over fn to print the conversion to data
  utils.forEach(fns,(fn) = > {
    data = fn.call(context,data,headers)
  })
  return data
}
Copy the code

Cancel

axios.Cancel = require('./cancel/Cancel')
axios.CancelToken = require('./cancel/CancelToken')
axios.isCancel = require('./cancel/isCancel')
Copy the code

Cancel

// Declare the Cancel class
function Cancel(message){
  this.message = message
}
Cancel.prototype.toString = function (){
  return 'Cancel' + (this.message ? ':' + this.message : ' ')
}
Cancel.prototype.__CANCEL__ = true
Copy the code

CancelToken

function CancelToken(executor){
  if(typeofexecutor ! = ='function') {throw new Error('executor must be a function')}var resolvePromise
  / / get the resolve
  this.promise = new Promise((resolve) = >{
    resolvePromise = resolve
  })
  var token = this
  // Generate reason
  executor(function(message){
    if(token.reason){
      return
    }
    token.reason = new Cancel(message)
    resolvePromise(token.reason)
  })
}
CancelToken.prototype.throwIfRequested = function(){
  if(this.reason){
    throw this.reason
  }
}
CancelTokem.source = function(){
  var cancel
  var token = new CancelToken(function executor(c){
    cancel = c
  })
  return {token,cancel}
}
Copy the code

isCancel

// Determine whether to cancel
function isCancel(value){
  return!!!!! (value && value.__CANCEL__) }Copy the code