# tiny-axios-wrap

> This is an axios wrap

Latest version **0.1.6** (published 2017-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install tiny-axios-wrap
pnpm add tiny-axios-wrap
yarn add tiny-axios-wrap
bun add tiny-axios-wrap
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2017-04-27 |
| First published | 2017-03-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Author | DengChao |
| Maintainers | dengchao2056 |
| Keywords | tiny-axios-wrap, tiny-axios |

## Links

- npm: https://www.npmjs.com/package/tiny-axios-wrap
- Repository: https://github.com/harrydengchao/tiny-axios-wrap
- Homepage: https://github.com/harrydengchao/tiny-axios-wrap#readme
- Issues: https://github.com/harrydengchao/tiny-axios-wrap/issues
- npm.io page: https://npm.io/package/tiny-axios-wrap

## Dependencies (2)

- [qs](https://npm.io/package/qs.md) ^6.4.0
- [axios](https://npm.io/package/axios.md) ^0.15.3

## Recent versions

- 0.1.6 (latest) — 2017-04-27
- 0.1.5 — 2017-04-27
- 0.1.4 — 2017-04-26
- 0.1.3 — 2017-04-21
- 0.1.2 — 2017-04-20
- 0.1.1 — 2017-03-29
- 0.1.0 — 2017-03-29

## README

```javascript
npm install --save tiny-axios-wrap
```

### Use
```javascript
// import tiny-axios
import ajax from 'tiny-axios-wrap'

ajax(config, options)
.then(res => {
  // 若 code == 0， 为有效返回，则 res 返回的都是有效数据
  // res => {
  //  config: {},  请求的配置
  //  data: { code: 0, data: '' },  返回的数据
  //  ...
  // }
})
.catch(err => {
  // 请求错误、code != 0，都在这里返回
})

/**-------------config--------------------------**/
config = {
  method: 'post',
  url: '',
  data: {}
}
config = {
  method: 'get',
  url: '',
  params: {}
}

// 设置超时，10秒
config.timeout = 10000;

// 设置 json 格式请求
config = {
  headers: {
    'Content-Type': 'application/json'
  },
  transformRequest: [function (data) {
    return JSON.stringify(data);
  }]
}

/**------------options---------------------------**/
options = {
  // loading组件，加载状态，不需要自动关闭
  loading: {
    open() {},
    close() {}
  },
  // toast组件，错误弹框，应能自动关闭
  toast: {
    open() {},
    success() {},
    error() {},
    warn() {}
  },
  // 允许正常输出的，非成功返回码, number
  outputCodes: [],
  // 根据返回码拦截处理
  resCodeIntercept: [
    { code: '', handler() {} }
  ],
  utils: {
    auth: {
    	// 特征码字段
    	code: 'code',
    	// 成功代号
    	num: 0,
    	// 失败提示字段
    	message: 'error',
      defaultReqErrorBefore: 'rq',
      defaultResErrorBefore: 'rs',
      defaultReqErrorCode: '000',
      defaultResErrorCode: '000',
    },
    alert: {
       /* timeout 连接超时*/
    	timeoutError: '连接超时，请重试',
      // 没有数据返回
      notFoundData: '没有数据',
    	otherError: '网络错误'
    }
  }
}
```
---
```javascript
// 简单的自定义封装
import axios from 'tiny-axios-wrap'
import Loading from 'tiny-loading'
import Tost from 'tiny-tost'
let ajax = (config) => {
  let options = {
    loading: Loading,
    toast: Tost,
    resCodeIntercept: {
      code: 202,
      handler() {
        console.log(202)
      }
    },
    utils: {
      auth: {
      	code: 'code',
      	num: 0,
      	message: 'msg'
      }
    }
  }
  return new Promise((resolve, reject) => {
    axios(config, options)
    .then(res => {
      res.data.code === 0 && resolve(res.data)
    })
    .catch(err => reject(err))
  })
}
ajax.prototype.then = res => {}
ajax.prototype.catch = err => {}

export default ajax

// 使用
ajax({
  method: 'post',
  url: 'http://xxxxxx',
  data: {}
})
.then(res => {
  console.warn(res);
})
.catch(err => {
  console.error(err);
})
```

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