# wx-axios-promise

> 小程序的request库，封装了wx的所有api为Promise，用法和axios类似

Latest version **2.4.1** (published 2020-07-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install wx-axios-promise
pnpm add wx-axios-promise
yarn add wx-axios-promise
bun add wx-axios-promise
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2020-07-18 |
| First published | 2019-02-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 45 |
| Author | abmao |
| Maintainers | abmao |
| Keywords | 小程序, wx, request, axios, promise, ajax |

## Links

- npm: https://www.npmjs.com/package/wx-axios-promise
- Repository: https://github.com/hengshanMWC/wx-axios-promise
- Homepage: https://github.com/hengshanMWC/wx-axios-promise#readme
- Issues: https://github.com/hengshanMWC/wx-axios-promise/issues
- npm.io page: https://npm.io/package/wx-axios-promise

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 2.4.1 (latest) — 2020-07-18
- 2.4.2-beta.1 (beta) — 2022-04-18
- 2.4.0 — 2020-07-16
- 2.3.3 — 2019-03-19
- 2.3.2 — 2019-03-08
- 2.3.1 — 2019-03-08
- 2.2.1 — 2019-03-06
- 2.2.0 — 2019-03-06
- 2.1.2 — 2019-03-05
- 2.1.1 — 2019-03-05
- 2.1.0 — 2019-03-05
- 1.0.0 — 2019-02-26

## README

## wx-axios-promise
### 特征
* 提供和axios相似体验，支持默认参数配置，拦截功能，和create创建新的对象
* 默认将小程序的api封装成Promise，通过降级，兼容低版本手机系统
### 使用方法
npm i wx-axios-promise -S

[小程序 npm 支持](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html?search-key=npm)

[小程序代码片段测试](https://developers.weixin.qq.com/s/GSiAP0mj787V)


```
import Abi from 'wx-axios-promise'
let api = Abi()
```
传递相关配置来创建请求(以下参数为默认)
```
//详情可参考wx.request
let api = Abi({
    url: '',//默认的接口后缀
    method: 'get',//默认的HTTP 请求方法
    dataType: 'json',//默认的返回类型
    responseType: 'text',
    header: {
      'content-type': "application/json"
    }
  })
```
除上面的创造方法外，我们还可以用实例上的create的方法创建新实例。
```
let api = Abi()
let newApi = api.create()
```
请求操作
```
/**
*默认是get
*如果你设置了默认的url。会自动配置 默认url + url
*如果你的url是http://或者https://开头，那么不会添加默认url
*/
//多种请求方式
api(url, data)
api(SERVER[api], apiData)
api.get(SERVER[api], apiData)
api(SERVER.URL + SERVER[api], apiData)
api(`${SERVER[api]}?page=${apiData.page}&count=${apiData.count}`)
api({
   url: SERVER[api],
   data: apiData,
   <!--method: 'get',-->
   <!--dataType: 'json',-->
   <!--responseType: 'text',-->
   <!--header: {-->
   <!--    content-type': "application/json"-->
   <!--}-->
})

api.post(url, data)
支持
'get',
'post',
'put',
'delete',
'options',
'head',
'trace',
'connect'
```
可以架起请求、响应、成功、失败的拦截
```
api.interceptors.response.use(function (config){
  //接口||wx.接口
  return config.data || config
}, function(error){
    return error
})
api.interceptors.request.use(function (config){
  //返回的是和wx.request相关的参数
  console.log(config)
  wx.showLoading({
    title: '加载内容'
  })
}, function(error){
    return error
})
```
wx全Promise
```
api.wx.chooseImage()
.then( res => api.wx.uploadFile())
.then()
```
当然，如果你并不需要这个功能，你也可以在创建的时候设置第二个参数为false

---
_Source: https://npm.io/package/wx-axios-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
