Axios请求的封装
在王红元老师vue3课程中学习到的axios封装,基于每个baseurl的拦截器,单个的拦截器 以及单次请求的拦截器! 太强咯!
import { AxiosRequestConfig, AxiosResponse } from 'axios'
export interface LzsRequestInterceptors<T = AxiosResponse> {
requestInterceptor?: (config: AxiosRequestConfig) => AxiosRequestConfig
requestInterceptorCatch?: (error: any) => any
responseInterceptor?: (config: T) => T
responseInterceptorCathch?: (error: any) => any
}
export interface LzsRequestConfig<T = AxiosResponse>
extends AxiosRequestConfig {
interceptors?: LzsRequestInterceptors<T>
showLoading?: boolean
}
import axios from 'axios'
import type { AxiosInstance } from 'axios'
import type { LzsRequestConfig, LzsRequestInterceptors } from './type'
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading'
import { ElLoading } from 'element-plus/es'
class LzsRequest {
instance: AxiosInstance
interceptors?: LzsRequestInterceptors
showLoading: boolean
loading?: LoadingInstance
constructor(config: LzsRequestConfig) {
this.instance = axios.create(config)
this.interceptors = config.interceptors
this.showLoading = config.showLoading ?? true
this.instance.interceptors.request.use(
this.interceptors?.requestInterceptor,
this.interceptors?.requestInterceptorCatch
)
this.instance.interceptors.response.use(
this.interceptors?.responseInterceptor,
this.interceptors?.responseInterceptorCathch
)
this.instance.interceptors.request.use(
(config) => {
if (this.showLoading) {
this.loading = ElLoading.service({
lock: true,
text: '正在请求数据....',
background: 'rgba(0, 0, 0, 0.5)'
})
}
return config
},
(err) => {
return err
}
)
this.instance.interceptors.response.use(
(config) => {
this.loading?.close()
const data = config.data
if (data.returnCode === '-1001') {
console.log('请求失败~, 错误信息')
} else {
return data
}
},
(err) => {
console.log('所有的实例都有的拦截器: 响应失败拦截')
this.loading?.close()
if (err.response.status === 404) {
console.log('404的错误~')
}
return err
}
)
}
request<T>(config: LzsRequestConfig<T>): Promise<T> {
return new Promise((resovle, reject) => {
if (config.interceptors?.requestInterceptor) {
config = config.interceptors.requestInterceptor(config)
}
if (config.showLoading === false) {
this.showLoading = config.showLoading
}
this.instance
.request<any, T>(config)
.then((res) => {
if (config.interceptors?.responseInterceptor) {
res = config.interceptors.responseInterceptor(res)
}
this.showLoading = true
resovle(res)
})
.catch((err) => {
this.showLoading = true
reject(err)
})
})
}
get<T>(config: LzsRequestConfig<T>): Promise<T> {
return this.request<T>({
...config,
method: 'Get'
})
}
post<T>(config: LzsRequestConfig<T>): Promise<T> {
return this.request<T>({
...config,
method: 'POST'
})
}
delete<T>(config: LzsRequestConfig<T>): Promise<T> {
return this.request<T>({
...config,
method: 'DELETE'
})
}
patch<T>(config: LzsRequestConfig<T>): Promise<T> {
return this.request<T>({
...config,
method: 'PATCH'
})
}
}
export default LzsRequest