importaxios,{AxiosRequestConfig}from"axios";import{ResultType}from"./requestType";constbaseURL="http://localhost:8080/"constinstance=axios.create({baseURL:baseURL,timeout:1000,});// Add a request interceptorinstance.interceptors.request.use(function (config){// Do something before request is sentreturnconfig;},function (error){// Do something with request errorreturnPromise.reject(error);});// Add a response interceptorinstance.interceptors.response.use(function (response){// Any status code that lie within the range of 2xx cause this function to trigger// Do something with response datareturnresponse;},function (error){// Any status codes that falls outside the range of 2xx cause this function to trigger// Do something with response errorreturnPromise.reject(error);});constaxiosGet=<T>({url,params,headers={}}:AxiosRequestConfig):Promise<ResultType<T>>=>{headers["Content-Type"]='application/json;charset=utf-8'returnnewPromise(async (resolve,reject)=>{try{constresult=awaitinstance({url,method:"get",params:params,headers})constresultData=result.dataasResultType<T>resolve(resultData)}catch (error){reject(error)}})}export{axiosGet}
Top comments (0)