This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@xxl-father/baxios

1.0.2-test-6 • Public • Published

axios工具

介绍

这个库是一个对axios的二次封装,让请求能够预先定义好,需要发送的时候再使用对应的方法。更好的支持RESTful风格的请求,另外还支持根据自定义响应状态码映射表调用对应的方法

  //响应体必须为这种格式
  type Data = {
    data:any
    code:number
    msg:string
  } 
  

使用

import { createAxios } form 'baxios'
const config = {
  baseURL: location.origin + '/api/',
  withCredentials: true,
  timeout: 5000
}
// 响应状态码映射
const codeMap = {
  '200':"成功",
  '404':"没有查询到数据"
}
const request = axios.create(config)
const baxios = createBAxios({
  ...config
},codeMap)
// 创建请求 未发送 在这里设置完uri 路径过后
let test = baxios("uri")

/**
 * 另外baxios 上还可以绑定几个事件
 *  onErrMsg: (callback: (msg: string) => void) => void;
 *  onSuccMsg: (callback: (msg: string) => void) => void;
 *  onReqError: (callback: (err: import('axios').AxiosError) => void) => void;
 *  onResError: (callback: (err: import('axios').AxiosError) => void) => void;
 * **/
// 真实发送 这里的方法不能设置uri 路径
// 返回promise 且若是不报错的话 直接返回response 里的data
test.get()
test.post()
test.postForm()

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i @xxl-father/baxios

      Weekly Downloads

      6

      Version

      1.0.2-test-6

      License

      ISC

      Unpacked Size

      13.6 kB

      Total Files

      7

      Last publish

      Collaborators

      • b-hole