Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/panio123/axios-nest
https://github.com/panio123/axios-nest
Last synced: about 5 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/panio123/axios-nest
- Owner: panio123
- License: mit
- Created: 2024-09-07T15:32:14.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-25T10:28:02.000Z (about 2 months ago)
- Last Synced: 2024-11-03T03:18:46.577Z (17 days ago)
- Language: TypeScript
- Size: 432 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# axios-nest
## 为了更方便的管理接口,自己弄了这么个插件
### 示例:
```js
/**
* /api/api-list.js
*/
export default {
//直接配置`url`,默认为`get`请求
login: "/user/login",
//可以嵌套
order: {
list: "/order/list",
info: "/order/info",
},
/**
* 也可以直接配置 axios `RequestConfig`
*
* 当出现`url`属性时,插件判定这是一个`RequestConfig`
*/
upload: {
url: "/upload/img",
method: 'post',
onUploadProgress: function () {
// Do whatever you want with the Axios progress event
},
}
}```
```js
/**
* /api/index.js
*/
import apiList from './api-list.js'let axios = axios.create({
baseURL:'./api'
});let axiosNest = new AxiosNest(axios);
let $api = axiosNest.buildApiList(apiList);
export default $api;
```
```js
import $api from '/api/index.js';$api.login({
data:{}
}).then();//发起默认的`get`请求
await $api.order.list({});//可以发起指定的请求
//插件由TS编写,使用时自带属性提示功能,嵌套再多也不怕
await $api.order.list.post(data,{});
await $api.order.list.get(params,{});await $api.upload({
data:formData
})```
### 原理简述:
#### 假如这是你提供的 api 配置对象
```js
export default {
login: "/user/login",
order: {
list: "/order/list",
info: "/order/info",
},
upload: {
url: "/upload/img",
method: 'post',
}
}```
#### 通过`axiosNest`处理后,你得到的
```js
export default {
login: axios,
order: {
list: axios,
info: axios,
},
upload: axios
}```
### 希望能帮到大家,让api管理更加整洁