1.0.9 • Published 2 years ago

@duying/axios-plus v1.0.9

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

axios-plus

安装

npm install @duying/axios-plus

示例

Http.js文件

import AxiosPlus from '@duying/axios-plus'

export default class Http extends AxiosPlus {
  baseURL = 'https://some-domain.com/api'

  urlPrefix = '/a'

  headers = {
    'custom-name': 'hello',
    'custom-age': 18
  }

  constructor () {
    super()
    this.addCustomInterceptors()
  }

  addCustomInterceptors () {
    // 添加请求拦截器
    this.addInterceptor('request', 'key1', [
      function (config) {
        // 在发送请求之前做些什么
        // this 关键字为 AxiosEngine 实例
        return config
      },
      function (error) {
        // 对请求错误做些什么
        // this 关键字为 AxiosEngine 实例
        return Promise.reject(error)
      }
    ])
    // 添加响应拦截器
    this.addInterceptor('response', 'key2', [
      function (response) {
        // 2xx 范围内的状态码都会触发该函数
        // 对响应数据做点什么
        // this 关键字为 AxiosEngine 实例
        return response
      },
      function (error) {
        // 超出 2xx 范围的状态码都会触发该函数
        // 对响应错误做点什么
        // this 关键字为 AxiosEngine 实例
        return Promise.reject(error)
      }
    ])
  }
}

CustomApi.js文件

import Http from './Http.js'

export default class CustomApi {
  // 发起 get 请求
  // https://some-domain.com/api/a/getList?keyword=abc
  static getAction () {
    const http = new Http()
    const params = { keyword: 'abc' }
    return http.get('/getList', { params })
  }

  // 发起 post 请求
  // https://some-domain.com/api/a/submitOrder
  static submitAction () {
    const http = new Http()
    const data = { title: '我是标题', content: '我是正文' }
    return http.post('/submitOrder', { data })
  }
}

AxiosPlus 类属性

属性名类型说明默认值
baseURLString相对于 url 的路径,完整API地址由 baseURL+urlPrefix+url组成-
urlPrefixString相对于 url 的路径-
headersObject自定义请求头{}
timeoutNumber如果请求时间超过 timeout 的值,则请求会被中断,0 永不超时,单位:毫秒60000
responseTypeString浏览器将要响应的数据类型,可选值包括: arraybuffer document json text stream blobjson

AxiosPlus 类方法

方法名说明
get(url: String, { params?: Object, headers?: Object, responseType?: String }): PromiseGET请求
delete(url: String, { params?: Object, headers?: Object, responseType?: String }): PromiseDELETE请求
post(url: String, { params?: Object, headers?: Object, responseType?: String, data?: any }): PromisePOST请求
put(url: String, { params?: Object, headers?: Object, responseType?: String, data?: any }): PromisePUT请求
patch(url: String, { params?: Object, headers?: Object, responseType?: String, data?: any }): PromisePATCH请求
addInterceptor(type: String, key: String, onFulfilled?: (value: any) => value, onRejected?: (error: any) => error): void添加拦截器。参数type可选值为 request response,不区分大小写
removeInterceptor(type: String, key: String): void移除拦截器。参数type可选值为 request response,不区分大小写
validateStatus(status: Number): Boolean定义了对于给定的 HTTP状态码是 resolve 还是 reject promise。默认状态码为 2xx 时 resolve, 其它为 reject promise
paramsSerializer(params: Object): String序列化params
1.0.9

2 years ago

1.0.8

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.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago