1.0.7 • Published 2 years ago

vp-axios v1.0.7

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

vp-axios

中文 README

A convinient and uniform way to code with axios.

Features

  • provide instace methods GET/POST/PUT/DELETE/FORMDATA with uniform params
  • custom error handler function when request is failed(switchable)
  • error tips of 400/401/403/404/405/413/414/500/502/504 in both English and Chinese
  • hooks
    • beforeHook(config) for custom operations before request, such as adding a loading div
    • afterHook(responce/error, isError) for custom operations after response:such as canceling loading, process response
  • custom error handlers by status: such as redirecting to specific page when 403
  • error info in 200 response data

    • specify getResStatus(resData) to get status
    • specify getResErrMsg(resData) to get error message
    • specify getResData(resData) to get true response data
  • configure to cancel duplicate requests

    • in the new VpAxios instance, configure cancelDuplicated: true to enable cancellation of duplicate requests
    • in the new VpAxios instance, configure duplicatedKeyFn function to generate a duplicate key
    • customize the duplicate key duplicatedKey of a single request when requesting

基础用法

可以通过以下方法new一个VpAxios的实例,第一个参数vpAxiosConfigvp-axios的配置,第二个参数axiosConfig为axios所支持的配置。

// request.js
const request = new VpAxios(vpAxiosConfig, axiosConfig)

vpAxiosConfig 支持以下配置:

{
  // whether or not show tips when error ocurrs
  tip: true, // default
  // how to show tips
  tipFn: () => {},
  errorHandlers: {
    // support 400/401/403/404/405/413/414/500/502/504/any other cutom errorno
  },
  // error msg language: 'zh-cn'/'en'
  lang: 'zh-cn', // default
  // some operation before request send
  beforeHook: (config) => {},
  // some operation after response is recieved
  afterHook: (responce/error, isError) => {},
  // function to get errno in response
  getResStatus: (res) => res.errno, // default
  // function to get err message in response
  getResErrMsg: (res) => res.errmsg, // default
  // function to get data in response
  getResData: (res) => res.data, // default
  // whether to enable cancellation of duplicate requests
  cancelDuplicated: false, // default
  // function of how to generate a duplicate key
  duplicatedKeyFn: (config) => `${config.method}${config.url}` // default
}
1.0.2

2 years ago

1.0.1

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.0

2 years ago