- 利用
柯里化
的优雅axios
二次封装 - 完整的
TS
类型支持
pnpm add axios currying-axios
默认已将axios
响应的data
解构出
import { curryingAxios } from 'currying-axios'
import axios from 'axios'
const Basic = axios.create({
// your config
})
Basic.interceptors.request.use((config) => {
// your config
return config
})
Basic.interceptors.response.use((response) => {
// your config
return response
})
const basic = curryingAxios(Basic) // 传入axios实例或者直接传入axios
// 返回一个带有post和get方法的对象,共用传入axios的错误处理逻辑和请求/响应拦截规则
const qr = basic('localhost:3000/qrcode') // 传入BaseURL
const om = basic('localhost:3000/org-manager') // 传入BaseURL
// 已将 axios data 解构出
const data = await qr.get('/info', { id: 'ctrap' }, {
onDownloadProgress() {},
})
const data = await om.post('/create', { name: 'cxk' }, {
onUploadProgress() {},
})