import { getPersistenceData } from "@/utils/persistence" ; import { TOKEN } from "@/constants" ; import { request as umiRequest } from "umi" ; import { message , notification } from "antd" ; interface RequestOptions { url : string ; method ? : 'GET' | 'POST' | 'DELETE' | 'PUT' ; headers ? : any ; data ? : any ; body ? : any ; msg ? : string ; [ rest ] : any ; } function request ( params ) { let { url , method = 'GET' , headers , data , body , msg } = params ; if ( ! headers ) { headers = defaultHeaders ( ) ; } if ( method === 'GET' && data ) { const urlQueryParams = new URLSearchParams ( data ) ; url = url + `?${ urlQueryParams . toString ( ) }` ; } return new Promise ( ( resolve , reject ) => { umiRequest ( url , { method , headers , data , body } ) . then ( res => { if ( msg ) { message . success ( msg ) ; } resolve ( res ) ; } ) . catch ( err => { console . log ( new Error ( err ) ) ; notification . error ( { message : err . toString ( ) } ) ; reject ( err ) ; } ) ; } ) ; } function defaultHeaders ( ) { const token = getPersistenceData ( TOKEN ) ; return { 'Authorization' : token } ; } export default request ;