0.0.3 • Published 2 years ago

@hotsuitor/axios-wx v0.0.3

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

TOC

说明

对微信小程序请求方法 wx.request 封装,支持 axios 的方式调用

安装

yarn add @hotsuitor/axios-wx

使用

import axios from '@hotsuitor/axios-wx'

使用方式和axios相同 支持:

  • baseURL公共配置
  • 函数方式调用
  • 创建实例
  • 快捷方法
  • 请求拦截
  • 取消请求

配置说明

// 配置示例
axios({
  url: '', // 请求url
  method: '', // 请求方法
  headers: {}, // 请求头
  data: '', // 请求参数
})

函数方式调用

axios({
  url: 'https://httpbin.org/get',
  method: 'get'
}).then(res => {

}).catch(err => {

})

实例方式

let instance = axios.create({
  headers: {
    token: 'token'
  }
})
instance({
  url: 'https://httpbin.org/get',
  method: 'get'
})

快捷方法

支持微信小程序 [wx.request](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html) 支持的所有方法

axios.get(url, data)
axios.post(url, data)

拦截器

请求拦截

axios.interceptor.request.use((config) => {
  return config
})

响应拦截

axios.interceptor.response.use((res) => {
  return res
})

取消请求

const CancelToken = axios.CancelToken;
let cancel;

axios.get('/user/12345', {
  cancelToken: new CancelToken(function executor(c) {
    // An executor function receives a cancel function as a parameter
    cancel = c;
  })
});

// cancel the request
cancel();

参与开发

  1. git clone 代码
  2. 安装依赖
  3. 配置 Git hook 3.1 npx husky install 3.2 npx husky set pre-commit "npx lint-staged" 3.3 提示:因为没有将钩子 '.husky/pre-commit' 设置为可执行,钩子被忽略。 设置执行权限 chmod +x .husky/pre-commit