0.0.1 • Published 6 years ago

@cyingy/axios-instance v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

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)

argsdescriptiondefaulttype
name请求 headers 的 key-string
value请求 headers 的 key 的值,如值为 false('', null 等),删除该 key 项-string
scopes请求 headers 的 scopes'common'string / array

setToken(token, type, scopes )

argsdescriptiondefaulttype
tokenAuthorization的值-string
typeAuthorization的的类型,如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) 同时设置 onRequestErroronResponseError

setupProgress(loading)

const loading = {
  start: () => {},
  set: percent => {},
  fail: () => {},
  finish: () => {},
}

axios.setupProgress(loading)

Others Request helpers ($get, $post, ...)

axios.$get()
axios.$post()
...