Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/real-jacket/texios

基于 typescript 实现的 axios
https://github.com/real-jacket/texios

Last synced: about 15 hours ago
JSON representation

基于 typescript 实现的 axios

Awesome Lists containing this project

README

        

# Texios [![codecov](https://codecov.io/gh/real-jacket/texios/branch/master/graph/badge.svg)](https://codecov.io/gh/real-jacket/texios) npm version GitHub

基于 typescript 实现的 axios

## 基本使用

### 安装

```npm
$ npm install texios

// or

$ yarn add texios
```

### Example

建议使用 ES 模块

```javascript
import texios from 'texios'

texios({
method: 'get',
url: '/simple/get',
params: {
a: 1,
b: 2
}
})
```

支持使用 api 的方式

```javascript
texios.request({
method: 'get',
url: '/extend/get'
})

texios.get('/more/get')

texios.post('/extend/post', { msg: 'hello post' })
```

## 相关 API

### 支持拦截器

```javascript
// 请求拦截
texios.interceptors.request.use(config => {
config.headers.test += '1'
return config
})

texios.interceptors.request.use(config => {
config.headers.test += '2'
return config
})
// 响应拦截
texios.interceptors.request.use(config => {
config.headers.test += '3'
return config
})

texios.interceptors.response.use(res => {
res.data += '1'
return res
})
```

### 支持配置化

```javascript
import texios, { TexiosTransformer } from 'texios'
import qs from 'qs'

// 对请求、响应做一个处理
const request = texios.create({
transformRequest: [
function(data) {
return qs.stringify(data)
},
...(texios.defaults.transformRequest as TexiosTransformer[])
],
transformResponse: [
...(texios.defaults.transformResponse as TexiosTransformer[]),
function(data) {
if (typeof data === 'object') {
data.b = 2
}
return data
}
],
headers: {
demo: 'test-xxx'
}
})

request({
url: '/config/post',
method: 'post',
data: {
a: 1
}
})
.then(res => {
console.log(res.data)
})
.catch(err => {
console.log(err)
})

```