1.1.0 • Published 4 years ago
axios-apitool v1.1.0
axios-apitool
安装
Using npm:
$ npm install axios-apitool
使用
main.js // 设置 api 参数
import axiosApiTool from "axios-apitool";
axiosApiTool.setting({
baseURL:string
api: {
login: "./login",
},
timeout:15000,
InterceptorsRequest: (config:AxiosRequestConfig,otherConfig:RequestOtherConfig) => {
//-- config => AxiosRequestConfig
},
InterceptorsResponse: (response:AxiosResponse,otherConfig:RequestOtherConfig) => {
//-- response => AxiosResponse
},
});
OtherConfig
interface OtherConfig {
[key: string]: any;
headers: { [key: string]: string };
}
key | type | description |
---|---|---|
baseURL | string | 域名 |
api | {key:string:string} | 所有接口 API |
timeout | number / 默认 30000ms | 超时时间 |
InterceptorsRequest | (config:AxiosRequestConfig,otherConfig)=>{} | 拦截响应前 |
InterceptorsResponse | (response:AxiosResponse,otherConfig) => {}, | 拦截响应后 |
InterceptorsRequest
与 InterceptorsResponse
方法中第二参数 OtherConfig
拦截单独请求中配置的自定义 config 方便在特殊接口中做出特殊处理 (下面有示例)
Page.vue|js // 页面中使用
配置好 axiosApiTool.Setting 可以在页面或 js 文件中 直接 import 使用。 比如 VUE2 可以挂载到 Vue.prototype.$axiosApiTool = axiosApiTool 一般在仓库做异步处理直接 import 就可以
axiosApiTool.api
.login(data, "GET", { option })
.then((res) => {})
.catch((err) => {});
axiosApiTool 将会根据 axiosApiTool.setting
中设置的 api
对象中的 key
生成请求方法,返回 Promise
对象。 方法中第三个参数可以设置本次请求的单独 headers
请求头配置, 也可以设置自定义其他的对象, 在拦截器中 OtherConfig
参数获取,做出处理
方法参数描述 —— 对应示例中的 login
key | type | description |
---|---|---|
参数一 | {} | 参数 |
参数二 | 可选: "GET"/"OPTIONS"/"HEAD"/"POST"/"PUT"/"DELETE"/"TRACE"/"CONNECT" | 请求方式 |
参数三 | {key:string:string,headers:{}} | 自定义选项 |
axiosApiTool.queue 队列
axiosApiTool.queue 可以获取当前请求的队列
强制中断某个请求
// axiosApiTool.queue[apiKey]();
axiosApiTool.queue.login();
执行 queue
中对应 apikey
的函数 强行中断某个请求 catch 可以捕捉中断信息
1.0.2
4 years ago
1.1.0
4 years ago
1.0.1
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.0
4 years ago
0.0.9
4 years ago
0.0.8
4 years ago
0.0.7
4 years ago
0.0.6
4 years ago
0.0.5
4 years ago
0.0.4
4 years ago
0.0.3
4 years ago
0.0.2
4 years ago
0.0.1
4 years ago