# simple-axios-wechat

> 小于1kb的简易的axios微信小程序版使用typescript实现

Latest version **0.2.1** (published 2023-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-axios-wechat
pnpm add simple-axios-wechat
yarn add simple-axios-wechat
bun add simple-axios-wechat
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2023-04-27 |
| First published | 2023-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jaye |
| Maintainers | jaye_l |
| Keywords | axios, wechat, http, https, request, weixin, ajax |

## Links

- npm: https://www.npmjs.com/package/simple-axios-wechat
- Repository: https://github.com/lei1248276/simple-axios-wechat
- Homepage: https://github.com/lei1248276/simple-axios-wechat#readme
- Issues: https://github.com/lei1248276/simple-axios-wechat/issues
- npm.io page: https://npm.io/package/simple-axios-wechat

## Dependencies (1)

- [miniprogram-api-typings](https://npm.io/package/miniprogram-api-typings.md) ^3.9.1

## 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

- 0.2.1 (latest) — 2023-04-27
- 0.2.0 — 2023-04-27
- 0.1.6 — 2023-03-18
- 0.1.5 — 2023-03-15
- 0.1.4 — 2023-03-15
- 0.1.3 — 2023-03-11
- 0.1.2 — 2023-03-07
- 0.1.1 — 2023-03-07
- 0.1.0 — 2023-03-06
- 0.0.5 — 2023-03-05
- 0.0.3 — 2023-03-04
- 0.0.2 — 2023-03-04
- 0.0.1 — 2023-03-04

## README

# simple-axios-wechat
-  小于1kb的简易的axios微信小程序版使用typescript实现

## 目录
- [安装](#Installing)
- [调用](#直接调用)
- [method 别名](#method别名)
- [Example](#Example)
  - [get请求并定义返回数据类型](#get请求并定义返回数据类型)
  - [post请求并定义返回数据类型](#post请求并定义返回数据类型)
- [全局默认值](#全局默认值)
- [创建实例](#创建实例)
- [拦截器](#拦截器)
- [获取RequestTask（中断请求、监听 HTTP）](#获取RequestTask)
- [自定义请求方法](#自定义请求方法)

## Installing

### Package manager

Using npm:

```bash
$ npm install simple-axios-wechat
```

Using yarn:

```bash
$ yarn add simple-axios-wechat
```

### 直接调用
- （SimpleAxios([config](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html))）
```ts
import SimpleAxios from 'simple-axios-wechat'

SimpleAxios<{result: any}>(config)
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })
```

### method别名
- 与SimpleAxios.create()实例一致
##### axios.request<{result: any}>(config)
##### axios.get<{result: any}>(url[, config])
##### axios.post<{result: any}>(url[, data[, config]])
##### axios.put<{result: any}>(url[, data[, config]])
##### axios.delete<{result: any}>(url[, config])
##### axios.head<{result: any}>(url[, config])
##### axios.options<{result: any}>(url[, config])
##### axios.trace<{result: any}>(url[, data[, config]])
##### axios.connect<{result: any}>(url[, data[, config]])

## Example
### get请求并定义返回数据类型
```ts
import SimpleAxios from 'simple-axios-wechat'
// 默认 get 请求
SimpleAxios<{result: string}>({ url: 'http://example.org/get' })
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })

SimpleAxios.get<{result: string}>('http://example.org/get')
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })
```

### post请求并定义返回数据类型
```js
SimpleAxios<{result: string}>({ url: 'http://example.org/get', method: 'POST' })
  .then(res => { console.log(res) })
  .catch(err => { console.error(err) })

SimpleAxios.post<{result: string}>('http://example.org/post', { data: {} })
  .then((res) => { console.log(res) })
  .catch((err) => { console.error(err) });
```

### 全局默认值
- （默认值优先级与axios一致，后设置大于先设置）
```ts
import SimpleAxios from 'simple-axios-wechat'

SimpleAxios.defaults.baseURL = 'http://example.org/get'
SimpleAxios.defaults.timeout = 10 * 1000
```

### 创建实例
```ts
import SimpleAxios from 'simple-axios-wechat'

const simpleAxios = SimpleAxios.create({
  baseURL: 'http://example.org/get',
  timeout: 10 * 1000
})
```

### 拦截器
```ts
import SimpleAxios from 'simple-axios-wechat'

const simpleAxios = SimpleAxios.create({
  baseURL: 'http://example.org/get'
})

simpleAxios.interceptors.request.use((config) => {
  // 干点啥?
  return config
}, (err) => {
  return Promise.reject(err)
})

simpleAxios.interceptors.response.use((response) => {
  // 总得干点啥吧?
  return response
}, (err) => {
  return Promise.reject(err)
})
```

### 获取RequestTask
- 在请求config配置项中通过回调获取[RequestTask](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.html)（比如用来 abort()）
```ts
import SimpleAxios from 'simple-axios-wechat'

SimpleAxios<{data: any, state: number}>({
  url: 'http://example.org/get',
  getRequestTask: (task) => { task.abort() }
})
  .catch(err => { console.error(err) })
```

### 自定义请求方法
- （要求返回一个Promise）
```ts
import SimpleAxios from 'simple-axios-wechat'

const simpleAxios = SimpleAxios.create({
  baseURL: 'http://example.org/get',
  adapter: function adapter(config) {
    return new Promise((resolve, reject) => {
      const RequestTask = wx.request({
        ...config,
        success: (res) => { resolve(res) },
        fail: (err) => { reject(err) }
      })
      // ! ** 自定义adapter需要通过回调暴露RequestTask **
      config.getRequestTask && config.getRequestTask(RequestTask)
    })
  }
})
```

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