二次封装Axios减小对单个库的依赖
本文采用Vue3+Ts以类的形式二次封装Axios
创建Axios继承类,定义AxRequestConfig接口继承原Axios的CreateAxiosDefaults实例类型,在此基础上添加interceptors配置,使new Axios时可以传入每个实例自己的拦截器函数↓
1 import axios from 'axios' 2 import type { AxiosInstance, CreateAxiosDefaults, AxiosRequestConfig, AxiosResponse } from 'axios' 3 4 // 封装拦截器接口,用于拦截不同基本地址请求 5 interface AxInterceptors<T = AxiosResponse> { 6 requestSucessFn?: (config: AxiosRequestConfig) => AxiosRequestConfig 7 requestFailureFn?: (err: any) => any 8 responseSucessFn?: (res: T) => T 9 responseFailureFn?: (err: any) => any 10 } 11 // 封装拦截器接口,用于拦截不同基本地址请求 12 interface AxRequestConfig<T = AxiosResponse> extends CreateAxiosDefaults { 13 interceptors?: AxInterceptors<T> 14 } 15 16 class AxRequtst { 17 instance: AxiosInstance 18 19 constructor(config: AxRequestConfig) { 20 this.instance = axios.create(config) //外部导入配置对象baseurl,timeout等 21 //请求全局拦截 22 // this.instance.interceptors.request.use( 23 // (config) => { 24 // return config 25 // }, 26 // (err) => { 27 // return err 28 // }, 29 // ) 30 //响应全局拦截 31 // this.instance.interceptors.response.use( 32 // (config) => { 33 // return config 34 // }, 35 // (err) => { 36 // return err 37 // }, 38 // ) 39 // 拦截设置的不同请求 40 this.instance.interceptors.request.use( 41 config.interceptors?.requestSucessFn, 42 config.interceptors?.requestFailureFn, 43 ) 44 // 拦截设置的不同响应 45 this.instance.interceptors.response.use( 46 config.interceptors?.responseSucessFn, 47 config.interceptors?.responseFailureFn, 48 ) 49 } 50 51 request(config: AxiosRequestConfig) { 52 // 判断是否发送请求 53 // isLoading.value = true 54 return new Promise((resolve, reject) => { 55 this.instance 56 .request(config) 57 .then((res) => { 58 resolve(res) 59 // isLoading.value = false 60 }) 61 .catch((err) => { 62 reject(err) 63 // isLoading.value = false 64 }) 65 }) 66 } 67 68 get(config: AxiosRequestConfig) { 69 return this.request({ ...config, method: 'GET' }) 70 } 71 72 post(config: AxiosRequestConfig) { 73 return this.request({ ...config, method: 'POST' }) 74 } 75 76 delete(config: AxiosRequestConfig) { 77 return this.request({ ...config, method: 'DELETE' }) 78 } 79 80 patch(config: AxiosRequestConfig) { 81 return this.request({ ...config, method: 'PATCH' }) 82 } 83 } 84 85 export default AxRequtst
然后就可以如下所示,自定义实例传入baseUrl、time、interceptors等初始配置↓
1 import usePublicStore from '@/stores/public' 2 import { BASE_URL, TIME_OUT } from './config' 3 import AxRequtst from './request' 4 5 const publicStore = usePublicStore() 6 7 const axRequest = new AxRequtst({ 8 baseURL: BASE_URL, 9 timeout: TIME_OUT, 10 interceptors: { 11 requestSucessFn: (config: any) => { 12 // console.log('axRequest的请求成功拦截') 13 // 携带token 14 const Htoken = localStorage.getItem('token') 15 if (Htoken) config.headers.token = Htoken 16 // loading 17 publicStore.isLoading = true 18 19 return config 20 }, 21 requestFailureFn: (err) => { 22 // console.log('axRequest的请求失败拦截') 23 return err 24 }, 25 responseSucessFn: (res) => { 26 // console.log('axRequest的响应成功拦截') 27 // loading 28 publicStore.isLoading = false 29 return res.data 30 }, 31 responseFailureFn: (err) => { 32 // console.log('axRequest的响应失败拦截') 33 publicStore.isLoading = false 34 return err 35 }, 36 }, 37 }) 38 // 测试拦截不同关系请求 39 const mockRequest = new AxRequtst({ 40 timeout: 8000, 41 // baseURL: '/hd', 42 interceptors: { 43 requestSucessFn: (config: any) => { 44 // console.log("请求成功拦截") 45 return config 46 }, 47 requestFailureFn: (err) => { 48 // console.log("请求失败拦截"); 49 return err 50 }, 51 responseSucessFn: (res) => { 52 // console.log("响应成功拦截"); 53 return res 54 }, 55 responseFailureFn: (err) => { 56 // console.log("响应失败拦截"); 57 return err 58 }, 59 }, 60 // headers: { 61 // 'content-type': 'application/json', 62 // }, 63 }) 64 65 // 数藏 66 const hdRequest = new AxRequtst({ 67 timeout: 8000, 68 baseURL: '/hd', 69 interceptors: { 70 requestSucessFn: (config: any) => { 71 // console.log("请求成功拦截") 72 return config 73 }, 74 requestFailureFn: (err) => { 75 // console.log("请求失败拦截"); 76 return err 77 }, 78 responseSucessFn: (res) => { 79 // console.log("响应成功拦截"); 80 return res 81 }, 82 responseFailureFn: (err) => { 83 // console.log("响应失败拦截"); 84 return err 85 }, 86 }, 87 // headers: { 88 // 'content-type': 'application/json', 89 // }, 90 }) 91 92 export { axRequest, mockRequest, hdRequest }

浙公网安备 33010602011771号