import { isFunction , isPlainObject } from '../helpers/is' ; import { assert , toUpperCase } from '../helpers/utils' ; import { AxiosAdapterRequestConfig , AxiosAdapterRequestMethod , AxiosAdapterTask , } from './adapter' ; import { AxiosRequestConfig , AxiosResponse , AxiosResponseError } from './Axios' ; import { isCancelToken } from './cancel' ; import { AxiosErrorResponse , createError } from './createError' ; import { generateType } from './generateType' ; function tryToggleProgressUpdate ( adapterConfig , progressUpdate ? ) { if ( isFunction ( progressUpdate ) ) { switch ( adapterConfig . type ) { case 'upload' : if ( isFunction ( adapterConfig . onUploadProgress ) ) { progressUpdate ( adapterConfig . onUploadProgress ) ; } break ; case 'download' : if ( isFunction ( adapterConfig . onDownloadProgress ) ) { progressUpdate ( adapterConfig . onDownloadProgress ) ; } break ; default : } } } export function request < TData = any > ( config ) { return new Promise ( ( resolve , reject ) => { assert ( isFunction ( config . adapter ) , 'adapter 需要是一个 function' ) ; const adapterConfig = { ... config , url : config . url ?? '' , type : generateType ( config ) , method : toUpperCase < AxiosAdapterRequestMethod > ( config . method , 'GET' ) , success , fail , } ; const adapterTask = config . adapter ! ( adapterConfig ) as AxiosAdapterTask | undefined ; function success ( response ) { response . config = config ; response . request = adapterTask ; if ( ! isFunction ( config . validateStatus ) || config . validateStatus ( response . status ) ) { resolve ( response ) ; } else { catchError ( '请求失败' , response ) ; } } function fail ( error ) { error . config = config ; error . request = adapterTask ; catchError ( '网络错误' , error ) ; } function catchError ( message , response ? ) { reject ( createError ( message , config , adapterTask , response ) ) ; } if ( isPlainObject ( adapterTask ) ) { tryToggleProgressUpdate ( adapterConfig , adapterTask . onProgressUpdate ) ; } if ( isCancelToken ( config . cancelToken ) ) { config . cancelToken . listener . then ( ( reason ) => { if ( isPlainObject ( adapterTask ) ) { tryToggleProgressUpdate ( adapterConfig , adapterTask . offProgressUpdate ) ; if ( isFunction ( adapterTask . abort ) ) { adapterTask . abort ( ) ; } } reject ( reason ) ; } ) ; } } ) ; }