0.0.1 • Published 6 years ago
@cyingy/axios-instance v0.0.1
axios-instance
instance of axios
Installing
Using npm:
npm install axios @cyingy/axios-instance
Using yarn:
yarn add axios @cyingy/axios-instance
API
setHeader(name, value, scopes)
args | description | default | type |
---|---|---|---|
name | 请求 headers 的 key | - | string |
value | 请求 headers 的 key 的值,如值为 false('', null 等),删除该 key 项 | - | string |
scopes | 请求 headers 的 scopes | 'common' | string / array |
setToken(token, type, scopes )
args | description | default | type |
---|---|---|---|
token | Authorization 的值 | - | string |
type | Authorization 的的类型,如Bearer | - | string |
scopes | 请求 headers 的 scopes | 'common' | string |
onRequest(fn) axios 请求前拦截器
axios.onRequest(config => {
// Do something before request is sent
// eg. set token
axios.setToken()
return config
})
onResponse(fn) axios 返回前拦截器
axios.onResponse(response => {
// Do something with response data
// eg. checkStatus()
if (response.status >= 200 && response.status < 300) {
return response.data
}
return Promise.reject(response)
})
onRequestError(fn) axios 请求错误拦截器
axios.onRequestError(error => {
// Do something with request error
return Promise.reject(error)
})
onResponseError(fn)
axios 返回错误拦截器
axios.onResponseError(response => {
// Do something with response error
return Promise.reject(response)
})
onError(fn)
同时设置 onRequestError
和 onResponseError
setupProgress(loading)
const loading = {
start: () => {},
set: percent => {},
fail: () => {},
finish: () => {},
}
axios.setupProgress(loading)
Others Request helpers ($get, $post, ...)
axios.$get()
axios.$post()
...
0.0.1
6 years ago