pull/8/head
parent
58f0b7ac49
commit
59b89e2ae1
|
@ -108,3 +108,8 @@ dist
|
||||||
|
|
||||||
coverage
|
coverage
|
||||||
yarn-error.log
|
yarn-error.log
|
||||||
|
|
||||||
|
|
||||||
|
es
|
||||||
|
lib
|
||||||
|
types
|
384
README.md
384
README.md
|
@ -22,16 +22,16 @@ $ npm i axios-miniprogram
|
||||||
|
|
||||||
小程序平台专用请求库,实现了 [axios](https://github.com/axios/axios) 大部分功能,用法只存在少许差异,如果您是 [axios](https://github.com/axios/axios) 的老用户,那么不需要学习就可以直接上手使用。
|
小程序平台专用请求库,实现了 [axios](https://github.com/axios/axios) 大部分功能,用法只存在少许差异,如果您是 [axios](https://github.com/axios/axios) 的老用户,那么不需要学习就可以直接上手使用。
|
||||||
|
|
||||||
* 支持 微信小程序、支付宝小程序、百度小程序、字节跳动小程序、QQ 小程序、uniapp。
|
- 支持 微信小程序、支付宝小程序、百度小程序、字节跳动小程序、QQ 小程序、uniapp。
|
||||||
* 支持 `Typescript`,健全的类型系统,智能的 `IDE` 提示。
|
- 支持 `Typescript`,健全的类型系统,智能的 `IDE` 提示。
|
||||||
* 支持 `Promise`。
|
- 支持 `Promise`。
|
||||||
* 支持 拦截器。
|
- 支持 拦截器。
|
||||||
* 支持 取消请求。
|
- 支持 取消请求。
|
||||||
* 支持 自定义合法状态码。
|
- 支持 自定义合法状态码。
|
||||||
* 支持 自定义参数序列化。
|
- 支持 自定义参数序列化。
|
||||||
* 支持 自定义转换数据。
|
- 支持 自定义转换数据。
|
||||||
* 支持 自定义错误处理。
|
- 支持 自定义错误处理。
|
||||||
* 支持 自定义平台适配器
|
- 支持 自定义平台适配器
|
||||||
|
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
|
@ -42,25 +42,33 @@ $ npm i axios-miniprogram
|
||||||
```typescript
|
```typescript
|
||||||
// 发送 GET 请求
|
// 发送 GET 请求
|
||||||
axios({
|
axios({
|
||||||
method: 'get',
|
url: "/user",
|
||||||
url: '/test',
|
method: "get",
|
||||||
params: { test: 1 }
|
params: {
|
||||||
}).then((response) => {
|
id: 1,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
// 请求成功后做些什么
|
// 请求成功后做些什么
|
||||||
}).catch((error) => {
|
})
|
||||||
|
.catch((error) => {
|
||||||
// 请求失败后做些什么
|
// 请求失败后做些什么
|
||||||
});
|
});
|
||||||
|
|
||||||
// 发送 POST 请求
|
// 发送 POST 请求
|
||||||
axios({
|
axios({
|
||||||
method: 'post',
|
url: "/user",
|
||||||
url: '/test',
|
method: "post",
|
||||||
data: { test: 1 }
|
data: {
|
||||||
}).then((response) => {
|
id: 1,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
// 请求成功后做些什么
|
// 请求成功后做些什么
|
||||||
}).catch((error) => {
|
})
|
||||||
|
.catch((error) => {
|
||||||
// 请求失败后做些什么
|
// 请求失败后做些什么
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### `axios(url, config?)`
|
### `axios(url, config?)`
|
||||||
|
@ -69,129 +77,150 @@ axios({
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 默认发送 GET 请求
|
// 默认发送 GET 请求
|
||||||
axios('/test/xxx').then((response) => {
|
axios("/user")
|
||||||
|
.then((response) => {
|
||||||
// 请求成功后做些什么
|
// 请求成功后做些什么
|
||||||
}).catch((error) => {
|
})
|
||||||
|
.catch((error) => {
|
||||||
// 请求失败后做些什么
|
// 请求失败后做些什么
|
||||||
});
|
});
|
||||||
|
|
||||||
// 发送 POST 请求
|
// 发送 POST 请求
|
||||||
axios('/test/xxx', { method: 'post' }).then((response) => {
|
axios("/user", {
|
||||||
|
method: "post",
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
// 请求成功后做些什么
|
// 请求成功后做些什么
|
||||||
}).catch((error) => {
|
})
|
||||||
|
.catch((error) => {
|
||||||
// 请求失败后做些什么
|
// 请求失败后做些什么
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
还可以使用请求方法的别名来简化请求。
|
还可以使用请求方法的别名来简化请求。
|
||||||
|
|
||||||
* ##### axios.request(config)
|
- ##### axios.request(config)
|
||||||
* ##### axios.options(url, config?)
|
- ##### axios.options(url, config?)
|
||||||
* ##### axios.get(url, params?, config?)
|
- ##### axios.get(url, params?, config?)
|
||||||
* ##### axios.head(url, params?, config?)
|
- ##### axios.head(url, params?, config?)
|
||||||
* ##### axios.post(url, data?, config?)
|
- ##### axios.post(url, data?, config?)
|
||||||
* ##### axios.put(url, data?, config?)
|
- ##### axios.put(url, data?, config?)
|
||||||
* ##### axios.delete(url, params?, config?)
|
- ##### axios.delete(url, params?, config?)
|
||||||
* ##### axios.trace(url, config?)
|
- ##### axios.trace(url, config?)
|
||||||
* ##### axios.connect(url, config?)
|
- ##### axios.connect(url, config?)
|
||||||
|
|
||||||
|
|
||||||
常用例子,其他同理:
|
常用例子,其他同理:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 发送 GET 请求
|
// 发送 GET 请求
|
||||||
axios.get('/test');
|
axios.get("/user");
|
||||||
|
|
||||||
// 携带参数
|
// 携带参数
|
||||||
axios.get('/test', { test: 1 });
|
axios.get("/user", {
|
||||||
|
test: 1,
|
||||||
|
});
|
||||||
|
|
||||||
// 携带额外配置
|
// 携带额外配置
|
||||||
axios.get('/test', { test: 1 }, {
|
axios.get(
|
||||||
|
"/user",
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json; charset=utf-8'
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
// 发送 POST 请求
|
// 发送 POST 请求
|
||||||
axios.post('/test');
|
axios.post("/user");
|
||||||
|
|
||||||
// 携带数据
|
// 携带数据
|
||||||
axios.post('/test', { test: 1 });
|
axios.post("/user", {
|
||||||
|
id: 1,
|
||||||
|
});
|
||||||
|
|
||||||
// 携带额外配置
|
// 携带额外配置
|
||||||
axios.post('/test', { test: 1 }, {
|
axios.post(
|
||||||
|
"/user",
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json; charset=utf-8'
|
"Content-Type": "application/json; charset=utf-8",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
## 配置`config`
|
## 配置`config`
|
||||||
|
|
||||||
非全平台兼容的属性只会在平台支持的情况下生效。
|
非全平台兼容的属性只会在平台支持的情况下生效。
|
||||||
|
|
||||||
|参数|类型|默认值|说明|全平台兼容|
|
| 参数 | 类型 | 默认值 | 说明 | 全平台兼容 |
|
||||||
|:-|:-|:-|:-|:-|
|
| :---------------- | :------------------------ | :------------------------------------------------------------------------------------ | :----------------- | :--------- |
|
||||||
|adapter|Function|[查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts)|自定义适配器|是|
|
| adapter | Function | [查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts) | 自定义适配器 | 是 |
|
||||||
|baseURL|String| |基础地址|是|
|
| baseURL | String | | 基础地址 | 是 |
|
||||||
|url|String| |请求地址|是|
|
| url | String | | 请求地址 | 是 |
|
||||||
|method|String|get|请求方法| |
|
| method | String | get | 请求方法 | |
|
||||||
|params|Object| |请求参数|是|
|
| params | Object | | 请求参数 | 是 |
|
||||||
|data|String/Object/ArrayBuffer| |请求数据|是|
|
| data | String/Object/ArrayBuffer | | 请求数据 | 是 |
|
||||||
|headers|Object|[查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts)|请求头|是|
|
| headers | Object | [查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts) | 请求头 | 是 |
|
||||||
|validateStatus|Function|[查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts)|自定义合法状态码|是|
|
| validateStatus | Function | [查看](https://github.com/early-autumn/axios-miniprogram/blob/master/src/defaults.ts) | 自定义合法状态码 | 是 |
|
||||||
|paramsSerializer|Function| |自定义参数序列化|是|
|
| paramsSerializer | Function | | 自定义参数序列化 | 是 |
|
||||||
|transformRequest|Function/Array<.Function>| |自定义转换请求数据|是|
|
| transformRequest | Function/Array<.Function> | | 自定义转换请求数据 | 是 |
|
||||||
|transformResponse|Function/Array<.Function>| |自定义转换响应数据|是|
|
| transformResponse | Function/Array<.Function> | | 自定义转换响应数据 | 是 |
|
||||||
|errorHandler|Function| |自定义错误处理|是|
|
| errorHandler | Function | | 自定义错误处理 | 是 |
|
||||||
|cancelToken|Object| |取消令牌|是|
|
| cancelToken | Object | | 取消令牌 | 是 |
|
||||||
|timeout|Number|10000|超时时间| |
|
| timeout | Number | 10000 | 超时时间 | |
|
||||||
|dataType|String|json|响应数据格式|是|
|
| dataType | String | json | 响应数据格式 | 是 |
|
||||||
|responseType|String|text|响应数据类型|是|
|
| responseType | String | text | 响应数据类型 | 是 |
|
||||||
|enableHttp2|Boolean|false|开启 http2| |
|
| enableHttp2 | Boolean | false | 开启 http2 | |
|
||||||
|enableQuic|Boolean|false|开启 quic| |
|
| enableQuic | Boolean | false | 开启 quic | |
|
||||||
|enableCache|Boolean|false|开启 cache| |
|
| enableCache | Boolean | false | 开启 cache | |
|
||||||
|sslVerify|Boolean|true|验证 ssl 证书| |
|
| sslVerify | Boolean | true | 验证 ssl 证书 | |
|
||||||
|
|
||||||
#### `config.method`的合法值
|
#### `config.method`的合法值
|
||||||
|
|
||||||
可以使用大写,也可以使用小写。
|
可以使用大写,也可以使用小写。
|
||||||
|
|
||||||
|值|说明|全平台兼容|
|
| 值 | 说明 | 全平台兼容 |
|
||||||
|:-|:-|:-|
|
| :------ | :--- | :--------- |
|
||||||
|OPTIONS| | |
|
| OPTIONS | | |
|
||||||
|GET| |是|
|
| GET | | 是 |
|
||||||
|HEAD| | |
|
| HEAD | | |
|
||||||
|POST| |是|
|
| POST | | 是 |
|
||||||
|PUT| |是|
|
| PUT | | 是 |
|
||||||
|DELETE| |是|
|
| DELETE | | 是 |
|
||||||
|TRACE| | |
|
| TRACE | | |
|
||||||
|CONNECT| | |
|
| CONNECT | | |
|
||||||
|
|
||||||
#### `config.dataType`的合法值
|
#### `config.dataType`的合法值
|
||||||
|
|
||||||
|值|说明|全平台兼容|
|
| 值 | 说明 | 全平台兼容 |
|
||||||
|:-|:-|:-|
|
| :--- | :--------------------------------------------------------- | :--------- |
|
||||||
|json|返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse|是|
|
| json | 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse | 是 |
|
||||||
|其他|不对返回的内容进行 JSON.parse|是|
|
| 其他 | 不对返回的内容进行 JSON.parse | 是 |
|
||||||
|
|
||||||
#### `config.responseType`的合法值
|
#### `config.responseType`的合法值
|
||||||
|
|
||||||
|值|说明|全平台兼容|
|
| 值 | 说明 | 全平台兼容 |
|
||||||
|:-|:-|:-|
|
| :---------- | :----------------------- | :--------- |
|
||||||
|text|响应的数据为文本|是|
|
| text | 响应的数据为文本 | 是 |
|
||||||
|arraybuffer|响应的数据为 ArrayBuffer|是|
|
| arraybuffer | 响应的数据为 ArrayBuffer | 是 |
|
||||||
|
|
||||||
#### 自定义合法状态码`config.validateStatus`
|
#### 自定义合法状态码`config.validateStatus`
|
||||||
|
|
||||||
可以让请求按照您的要求成功或者失败。
|
可以让请求按照您的要求成功或者失败。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios('/test', {
|
axios("/user", {
|
||||||
validateStatus: function validateStatus(status) {
|
validateStatus: function validateStatus(status) {
|
||||||
// 这样,状态码在 200 到 400 之间都是请求成功
|
// 这样,状态码在 200 到 400 之间都是请求成功
|
||||||
return status >= 200 && status < 400;
|
return status >= 200 && status < 400;
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -200,10 +229,12 @@ axios('/test', {
|
||||||
可以使用自己的规则去序列化参数。
|
可以使用自己的规则去序列化参数。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios('/test', {
|
axios("/user", {
|
||||||
paramsSerializer: function paramsSerializer(params) {
|
paramsSerializer: function paramsSerializer(params) {
|
||||||
return qs.stringify(params, {arrayFormat: 'brackets'});
|
return qs.stringify(params, {
|
||||||
}
|
arrayFormat: "brackets",
|
||||||
|
});
|
||||||
|
},
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -212,15 +243,19 @@ axios('/test', {
|
||||||
可以在请求发出之前转换请求数据,在请求成功之后转换响应数据。
|
可以在请求发出之前转换请求数据,在请求成功之后转换响应数据。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios('/test', {
|
axios("/user", {
|
||||||
transformRequest: [function transformRequest(data, headers) {
|
transformRequest: [
|
||||||
|
function transformRequest(data, headers) {
|
||||||
// 转换请求数据
|
// 转换请求数据
|
||||||
return data;
|
return data;
|
||||||
}],
|
},
|
||||||
transformResponse: [function transformResponse(data) {
|
],
|
||||||
|
transformResponse: [
|
||||||
|
function transformResponse(data) {
|
||||||
// 转换响应数据
|
// 转换响应数据
|
||||||
return data;
|
return data;
|
||||||
}],
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -232,24 +267,24 @@ axios('/test', {
|
||||||
axios.defaults.errorHandler = function errorHandler(error) {
|
axios.defaults.errorHandler = function errorHandler(error) {
|
||||||
// 做一些想做的事情
|
// 做一些想做的事情
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
};
|
||||||
|
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
errorHandler: function errorHandler(error) {
|
errorHandler: function errorHandler(error) {
|
||||||
// 做一些想做的事情
|
// 做一些想做的事情
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
也可以发送请求时通过自定义配置传入。
|
也可以发送请求时通过自定义配置传入。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios('/test', {
|
axios("/user", {
|
||||||
errorHandler: function errorHandler(error) {
|
errorHandler: function errorHandler(error) {
|
||||||
// 做一些想做的事情
|
// 做一些想做的事情
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -287,7 +322,7 @@ axios.defaults.adapter = function adapter(adapterConfig) {
|
||||||
// 成功的回调函数
|
// 成功的回调函数
|
||||||
success,
|
success,
|
||||||
// 失败的回调函数
|
// 失败的回调函数
|
||||||
fail
|
fail,
|
||||||
} = adapterConfig;
|
} = adapterConfig;
|
||||||
|
|
||||||
// 在 adapterConfig 中选择您需要的参数发送请求
|
// 在 adapterConfig 中选择您需要的参数发送请求
|
||||||
|
@ -297,9 +332,9 @@ axios.defaults.adapter = function adapter(adapterConfig) {
|
||||||
data,
|
data,
|
||||||
header,
|
header,
|
||||||
success,
|
success,
|
||||||
fail
|
fail,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// 如果 adapterConfig 的数据结构适用于当前平台,则可以。
|
// 如果 adapterConfig 的数据结构适用于当前平台,则可以。
|
||||||
axios.defaults.adapter = wx.request;
|
axios.defaults.adapter = wx.request;
|
||||||
|
@ -310,9 +345,10 @@ axios.defaults.adapter = wx.request;
|
||||||
##### 全局默认配置`axios.defaults`
|
##### 全局默认配置`axios.defaults`
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios.defaults.baseURL = 'https://www.xxx.com';
|
axios.defaults.baseURL = "https://www.api.com";
|
||||||
axios.defaults.headers.common['Accept'] = 'application/json, test/plain, */*';
|
axios.defaults.headers.common["Accept"] = "application/json, test/plain, */*";
|
||||||
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
axios.defaults.headers.post["Content-Type"] =
|
||||||
|
"application/x-www-form-urlencoded; charset=utf-8";
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 自定义实例默认配置
|
##### 自定义实例默认配置
|
||||||
|
@ -321,24 +357,26 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: 'https://www.xxx.com',
|
baseURL: "https://www.api.com",
|
||||||
headers: {
|
headers: {
|
||||||
common: {
|
common: {
|
||||||
'Accept': 'application/json, test/plain, */*'
|
Accept: "application/json, test/plain, */*",
|
||||||
},
|
},
|
||||||
post: {
|
post: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
|
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
也可以创建后修改。
|
也可以创建后修改。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
instance.defaults.baseURL = 'https://www.xxx.com';
|
instance.defaults.baseURL = "https://www.api.com";
|
||||||
instance.defaults.headers.common['Accept'] = 'application/json, test/plain, */*';
|
instance.defaults.headers.common["Accept"] =
|
||||||
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
|
"application/json, test/plain, */*";
|
||||||
|
instance.defaults.headers.post["Content-Type"] =
|
||||||
|
"application/x-www-form-urlencoded; charset=utf-8";
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 配置优先顺序
|
##### 配置优先顺序
|
||||||
|
@ -349,63 +387,64 @@ instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlenco
|
||||||
|
|
||||||
非全平台兼容的属性只会在平台支持的情况下生效。
|
非全平台兼容的属性只会在平台支持的情况下生效。
|
||||||
|
|
||||||
|属性|类型|说明|全平台兼容|
|
| 属性 | 类型 | 说明 | 全平台兼容 |
|
||||||
|:-|:-|:-|:-|
|
| :--------- | :------------------------ | :------------------------------------------- | :--------- |
|
||||||
|status|Number|状态码|是|
|
| status | Number | 状态码 | 是 |
|
||||||
|statusText|String|状态文本|是|
|
| statusText | String | 状态文本 | 是 |
|
||||||
|data|String/Object/ArrayBuffer|开发者服务器返回的数据|是|
|
| data | String/Object/ArrayBuffer | 开发者服务器返回的数据 | 是 |
|
||||||
|headers|Object|响应头|是|
|
| headers | Object | 响应头 | 是 |
|
||||||
|config|Object|Axios 请求配置|是|
|
| config | Object | Axios 请求配置 | 是 |
|
||||||
|cookies|Array<.String>|开发者服务器返回的 cookies,格式为字符串数组| |
|
| cookies | Array<.String> | 开发者服务器返回的 cookies,格式为字符串数组 | |
|
||||||
|profile|Object|网络请求过程中一些关键时间点的耗时信息| |
|
| profile | Object | 网络请求过程中一些关键时间点的耗时信息 | |
|
||||||
|
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### `axios.interceptors`
|
### `axios.interceptors`
|
||||||
|
|
||||||
可以先拦截请求或响应,然后再由then或catch处理。
|
可以先拦截请求或响应,然后再由 then 或 catch 处理。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 添加请求拦截器
|
// 添加请求拦截器
|
||||||
axios.interceptors.request.use(function (config) {
|
axios.interceptors.request.use(
|
||||||
|
function(config) {
|
||||||
// 在发送请求之前做些什么
|
// 在发送请求之前做些什么
|
||||||
console.log('request');
|
|
||||||
return config;
|
return config;
|
||||||
}, function (error) {
|
},
|
||||||
|
function(error) {
|
||||||
//处理请求错误
|
//处理请求错误
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// 添加响应拦截器
|
// 添加响应拦截器
|
||||||
axios.interceptors.response.use(function (response) {
|
axios.interceptors.response.use(
|
||||||
|
function(response) {
|
||||||
// 请求成功后做些什么
|
// 请求成功后做些什么
|
||||||
console.log('response');
|
|
||||||
return response;
|
return response;
|
||||||
}, function (error) {
|
},
|
||||||
|
function(error) {
|
||||||
// 处理响应错误
|
// 处理响应错误
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
});
|
}
|
||||||
|
);
|
||||||
axios('/test').then(function (response){
|
|
||||||
console.log('ok');
|
|
||||||
});
|
|
||||||
|
|
||||||
// log 'request' 'response' 'ok'
|
|
||||||
```
|
```
|
||||||
|
|
||||||
如果以后需要删除拦截器,则可以。
|
如果以后需要删除拦截器,则可以。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
const myInterceptor = axios.interceptors.request.use(function() {
|
||||||
|
// 在发送请求之前做些什么
|
||||||
|
});
|
||||||
axios.interceptors.request.eject(myInterceptor);
|
axios.interceptors.request.eject(myInterceptor);
|
||||||
```
|
```
|
||||||
|
|
||||||
还可以将拦截器添加到`axios`的`自定义实例`中。
|
还可以将拦截器添加到`axios`的`自定义实例`中。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
const myInterceptor = instance.interceptors.request.use(function() {
|
||||||
axios.interceptors.request.eject(myInterceptor);
|
// 在发送请求之前做些什么
|
||||||
|
});
|
||||||
|
instance.interceptors.request.eject(myInterceptor);
|
||||||
```
|
```
|
||||||
|
|
||||||
### `axios.CancelToken`
|
### `axios.CancelToken`
|
||||||
|
@ -415,13 +454,13 @@ axios.interceptors.request.eject(myInterceptor);
|
||||||
```typescript
|
```typescript
|
||||||
let cancel;
|
let cancel;
|
||||||
|
|
||||||
axios('/test', {
|
axios("/api", {
|
||||||
cancelToken: new axios.CancelToken(function (c){
|
cancelToken: new axios.CancelToken(function(c) {
|
||||||
cancel = c;
|
cancel = c;
|
||||||
})
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
cancel('取消请求');
|
cancel("取消请求");
|
||||||
```
|
```
|
||||||
|
|
||||||
还可以使用`CancelToken.source`工厂方法创建`CancelToken`。
|
还可以使用`CancelToken.source`工厂方法创建`CancelToken`。
|
||||||
|
@ -429,11 +468,11 @@ cancel('取消请求');
|
||||||
```typescript
|
```typescript
|
||||||
const source = axios.CancelToken.source();
|
const source = axios.CancelToken.source();
|
||||||
|
|
||||||
axios('/test', {
|
axios("/api", {
|
||||||
cancelToken: source.token
|
cancelToken: source.token,
|
||||||
});
|
});
|
||||||
|
|
||||||
source.cancel('取消请求');
|
source.cancel("取消请求");
|
||||||
```
|
```
|
||||||
|
|
||||||
### `axios.isCancel`
|
### `axios.isCancel`
|
||||||
|
@ -441,8 +480,8 @@ source.cancel('取消请求');
|
||||||
可以判断当前错误是否来自取消请求
|
可以判断当前错误是否来自取消请求
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios('/test').catch((error) => {
|
axios("/user").catch((error) => {
|
||||||
if(axios.isCancel(error)){
|
if (axios.isCancel(error)) {
|
||||||
// 请求被取消了
|
// 请求被取消了
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -453,11 +492,13 @@ axios('/test').catch((error) => {
|
||||||
根据配置中的`url`和`params`生成一个`URI`。
|
根据配置中的`url`和`params`生成一个`URI`。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// uri === '/test?id=1'
|
|
||||||
const uri = axios.getUri({
|
const uri = axios.getUri({
|
||||||
url: '/test',
|
url: "/user",
|
||||||
params: { id: 1 }
|
params: {
|
||||||
|
id: 1,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
// '/user?id=1'
|
||||||
```
|
```
|
||||||
|
|
||||||
### `axios.create(defaults)`
|
### `axios.create(defaults)`
|
||||||
|
@ -467,20 +508,16 @@ const uri = axios.getUri({
|
||||||
`自定义实例`拥有和`axios`相同的调用方式和请求方法的别名。
|
`自定义实例`拥有和`axios`相同的调用方式和请求方法的别名。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios.defaults.baseURL = 'https://www.xxx.com';
|
axios.defaults.baseURL = "https://www.api.com";
|
||||||
|
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
params: {
|
params: {
|
||||||
id: 1
|
id: 1,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// 最终请求的 URL 是这样的 => https://www.xxx.com/test?id=1
|
instance("/user");
|
||||||
// https://www.xxx.com 来自 axios.defaults.baseURL
|
// 'https://www.api.com/user?id=1'
|
||||||
// /test 来自传入的 '/test'
|
|
||||||
// id=1 来自 instance.defaults.params
|
|
||||||
instance('/test');
|
|
||||||
instance.get('/test');
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### `axios.Axios`
|
### `axios.Axios`
|
||||||
|
@ -490,22 +527,23 @@ instance.get('/test');
|
||||||
直接实例化`axios.Axios`可以得到一个`原始实例`,不能当函数调用,传入的自定义配置就是`原始实例`的默认配置,而不会像`axios.create(defaults)`一样去合并`axios`中的默认配置。
|
直接实例化`axios.Axios`可以得到一个`原始实例`,不能当函数调用,传入的自定义配置就是`原始实例`的默认配置,而不会像`axios.create(defaults)`一样去合并`axios`中的默认配置。
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios.defaults.baseURL = 'https://www.xxx.com';
|
|
||||||
|
|
||||||
const instance = new axios.Axios({
|
const instance = new axios.Axios({
|
||||||
params: { value: 123 }
|
beseURL: "https://www.api.com",
|
||||||
|
params: {
|
||||||
|
id: 1,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// 最终请求的 URL 是这样的 => /test?value=123
|
instance.get("/user");
|
||||||
// /test 来自传入的 '/test'
|
// 'https://www.api.com/user?id=1'
|
||||||
// value=123 来自 instance.defaults.params
|
|
||||||
instance.get('/test');
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## 执行流程
|
## 执行流程
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
axios('/test').then().catch();
|
axios("/user")
|
||||||
|
.then()
|
||||||
|
.catch();
|
||||||
|
|
||||||
// 请求成功
|
// 请求成功
|
||||||
// axios => axios.interceptors.request => config.transformRequest => config.paramsSerializer => config.adapter => config.validateStatus => config.transformResponse => axios.interceptors.response => then
|
// axios => axios.interceptors.request => config.transformRequest => config.paramsSerializer => config.adapter => config.validateStatus => config.transformResponse => axios.interceptors.response => then
|
||||||
|
|
1242
es/index.js
1242
es/index.js
File diff suppressed because it is too large
Load Diff
1244
lib/index.js
1244
lib/index.js
File diff suppressed because it is too large
Load Diff
|
@ -1,4 +1,5 @@
|
||||||
import { Adapter, Platform } from './types';
|
import { Adapter, Platform } from './types';
|
||||||
|
import { isUndefined } from './helpers/utils';
|
||||||
|
|
||||||
// uniapp
|
// uniapp
|
||||||
declare let uni: Platform;
|
declare let uni: Platform;
|
||||||
|
@ -28,7 +29,7 @@ export default function adaptive(): Adapter | undefined {
|
||||||
|
|
||||||
let adapter: Adapter | undefined;
|
let adapter: Adapter | undefined;
|
||||||
|
|
||||||
while (stack.length !== 0 && adapter === void 0) {
|
while (stack.length !== 0 && isUndefined(adapter)) {
|
||||||
try {
|
try {
|
||||||
adapter = (stack.shift() as () => Adapter)();
|
adapter = (stack.shift() as () => Adapter)();
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Headers, AxiosRequestConfig } from '../types';
|
import { Headers, AxiosRequestConfig } from '../types';
|
||||||
import { omit } from '../helpers/utils';
|
import { isUndefined, omit } from '../helpers/utils';
|
||||||
import { methodToLowercase } from './transformMethod';
|
import { methodToLowercase } from './transformMethod';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,7 +10,7 @@ import { methodToLowercase } from './transformMethod';
|
||||||
export default function flattenHeaders(config: AxiosRequestConfig): Headers {
|
export default function flattenHeaders(config: AxiosRequestConfig): Headers {
|
||||||
const { headers } = config;
|
const { headers } = config;
|
||||||
|
|
||||||
if (headers === void 0) {
|
if (isUndefined(headers)) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -79,9 +79,9 @@ function deepMergeConfig(
|
||||||
) {
|
) {
|
||||||
keys.forEach((key) => {
|
keys.forEach((key) => {
|
||||||
if (isPlainObject(config2[key])) {
|
if (isPlainObject(config2[key])) {
|
||||||
config[key] = deepMerge(config1[key] ?? {}, config2[key] as AnyObject);
|
config[key] = deepMerge<AnyObject>(config1[key] ?? {}, config2[key] as AnyObject);
|
||||||
} else if (isPlainObject(config1[key])) {
|
} else if (isPlainObject(config1[key])) {
|
||||||
config[key] = deepMerge(config1[key] as AnyObject);
|
config[key] = deepMerge<AnyObject>(config1[key] as AnyObject);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
||||||
|
import { isString, isUndefined } from '../helpers/utils';
|
||||||
import transformRequest from './transformRequest';
|
import transformRequest from './transformRequest';
|
||||||
import transformResponse from './transformResponse';
|
import transformResponse from './transformResponse';
|
||||||
import createError from './createError';
|
import createError from './createError';
|
||||||
|
@ -20,14 +21,14 @@ export default function request(config: AxiosRequestConfig): Promise<AxiosRespon
|
||||||
* @param response Axios 响应体
|
* @param response Axios 响应体
|
||||||
*/
|
*/
|
||||||
function catchError(message: any, response?: AxiosResponse): void {
|
function catchError(message: any, response?: AxiosResponse): void {
|
||||||
if (typeof message !== 'string') {
|
if (!isString(message)) {
|
||||||
message = '配置不正确或者网络异常';
|
message = message.fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
reject(createError(message, config, requestConfig, response));
|
reject(createError(message, config, requestConfig, response));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (adapter === void 0) {
|
if (isUndefined(adapter)) {
|
||||||
catchError('平台适配失败,您需要参阅文档使用自定义适配器手动适配当前平台');
|
catchError('平台适配失败,您需要参阅文档使用自定义适配器手动适配当前平台');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -41,7 +42,7 @@ export default function request(config: AxiosRequestConfig): Promise<AxiosRespon
|
||||||
function handleResponse(res: Response): void {
|
function handleResponse(res: Response): void {
|
||||||
const response = transformResponse(res, config);
|
const response = transformResponse(res, config);
|
||||||
|
|
||||||
if (config.validateStatus === void 0 || config.validateStatus(response.status)) {
|
if (isUndefined(config.validateStatus) || config.validateStatus(response.status)) {
|
||||||
resolve(response);
|
resolve(response);
|
||||||
} else {
|
} else {
|
||||||
catchError(`请求失败,状态码为 ${response.status}`, response);
|
catchError(`请求失败,状态码为 ${response.status}`, response);
|
||||||
|
@ -57,9 +58,9 @@ export default function request(config: AxiosRequestConfig): Promise<AxiosRespon
|
||||||
|
|
||||||
// 如果存在取消令牌
|
// 如果存在取消令牌
|
||||||
// 则调用取消令牌里的 listener 监听用户的取消操作
|
// 则调用取消令牌里的 listener 监听用户的取消操作
|
||||||
if (cancelToken !== void 0) {
|
if (!isUndefined(cancelToken)) {
|
||||||
cancelToken.listener.then(function onCanceled(reason): void {
|
cancelToken.listener.then(function onCanceled(reason): void {
|
||||||
if (task !== void 0) {
|
if (!isUndefined(task)) {
|
||||||
task.abort();
|
task.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { Data, Headers, TransformData } from '../types';
|
import { Data, Headers, TransformData } from '../types';
|
||||||
|
import { isUndefined } from '../helpers/utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换数据
|
* 转换数据
|
||||||
|
@ -12,7 +13,7 @@ export default function transformData(
|
||||||
headers: Headers,
|
headers: Headers,
|
||||||
transforms?: TransformData | TransformData[]
|
transforms?: TransformData | TransformData[]
|
||||||
): Data {
|
): Data {
|
||||||
if (transforms === void 0) {
|
if (isUndefined(transforms)) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { AnyObject, Params } from '../types';
|
import { AnyObject, Params } from '../types';
|
||||||
import { encode, isPlainObject, isDate } from './utils';
|
import { encode, isPlainObject, isDate, isUndefined, isNull } from './utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过请求地址和序列化参数生成新的请求地址
|
* 通过请求地址和序列化参数生成新的请求地址
|
||||||
|
@ -36,7 +36,7 @@ function paramsSerialization(params: AnyObject): string {
|
||||||
const parts: string[] = [];
|
const parts: string[] = [];
|
||||||
|
|
||||||
Object.entries(params).forEach(function encodeKeyValue([key, value]): void {
|
Object.entries(params).forEach(function encodeKeyValue([key, value]): void {
|
||||||
if (value === null || value === void 0 || value !== value) {
|
if (isNull(value) || isUndefined(value) || value !== value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { AnyObject } from '../types';
|
import { AnyObject, ObjectTree } from '../types';
|
||||||
|
|
||||||
const _toString = Object.prototype.toString;
|
const _toString = Object.prototype.toString;
|
||||||
|
|
||||||
|
@ -19,21 +19,40 @@ export function encode(str: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是不是一个日期对象
|
* 是否是 Date
|
||||||
*
|
|
||||||
* @param date 判断目标
|
|
||||||
*/
|
*/
|
||||||
export function isDate(date: unknown): date is Date {
|
export function isDate(date: unknown): date is Date {
|
||||||
return _toString.call(date) === '[object Date]';
|
return _toString.call(date) === '[object Date]';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是不是一个普通对象
|
* 是否是普通对象
|
||||||
*
|
|
||||||
* @param obj 判断目标
|
|
||||||
*/
|
*/
|
||||||
export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
|
export function isPlainObject<T = never>(
|
||||||
return _toString.call(obj) === '[object Object]';
|
value: unknown
|
||||||
|
): value is [T] extends never[] ? AnyObject : T {
|
||||||
|
return _toString.call(value) === '[object Object]';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否是 undefined
|
||||||
|
*/
|
||||||
|
export function isUndefined(value: unknown): value is undefined {
|
||||||
|
return typeof value === 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否是字符型
|
||||||
|
*/
|
||||||
|
export function isString(value: unknown): value is number {
|
||||||
|
return typeof value === 'string';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否是 Null
|
||||||
|
*/
|
||||||
|
export function isNull(value: unknown): value is null {
|
||||||
|
return value === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,19 +60,19 @@ export function isPlainObject(obj: unknown): obj is Record<string, unknown> {
|
||||||
*
|
*
|
||||||
* @param objs n 个对象
|
* @param objs n 个对象
|
||||||
*/
|
*/
|
||||||
export function deepMerge(...objs: Record<string, any>[]): Record<string, any> {
|
export function deepMerge<T = never>(...objs: ObjectTree[]): [T] extends never[] ? ObjectTree : T {
|
||||||
const result: Record<string, any> = {};
|
const result: ObjectTree = {};
|
||||||
|
|
||||||
function assignValue(key: string, val: any) {
|
function assignValue(key: string, val: unknown) {
|
||||||
// 如果当前结果和当前值都为普通对象
|
// 如果当前结果和当前值都为普通对象
|
||||||
// 递归进行深度合并
|
// 递归进行深度合并
|
||||||
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
||||||
result[key] = deepMerge(result[key], val);
|
result[key] = deepMerge(result[key] as ObjectTree, val as ObjectTree);
|
||||||
}
|
}
|
||||||
// 如果只有当前值为普通对象
|
// 如果只有当前值为普通对象
|
||||||
// 直接深拷贝当前值
|
// 直接深拷贝当前值
|
||||||
else if (isPlainObject(val)) {
|
else if (isPlainObject(val)) {
|
||||||
result[key] = deepMerge({}, val);
|
result[key] = deepMerge({}, val as ObjectTree);
|
||||||
}
|
}
|
||||||
// 如果都不是普通对象
|
// 如果都不是普通对象
|
||||||
// 直接赋值
|
// 直接赋值
|
||||||
|
@ -62,13 +81,13 @@ export function deepMerge(...objs: Record<string, any>[]): Record<string, any> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
objs.forEach(function assignObj(obj: Record<string, any>): void {
|
objs.forEach(function assignObj(obj: ObjectTree): void {
|
||||||
Object.entries(obj).forEach(function assignKey([key, value]) {
|
Object.entries(obj).forEach(function ([key, value]) {
|
||||||
assignValue(key, value);
|
assignValue(key, value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result as [T] extends never[] ? ObjectTree : T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -191,7 +191,7 @@ export declare interface AdapterRequestTask {
|
||||||
* 适配器
|
* 适配器
|
||||||
*/
|
*/
|
||||||
export declare interface Adapter {
|
export declare interface Adapter {
|
||||||
(config: AdapterRequestConfig): AdapterRequestTask | void;
|
(config: AdapterRequestConfig): AdapterRequestTask | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -318,6 +318,8 @@ export declare interface AxiosRequestConfig {
|
||||||
* 验证 ssl 证书
|
* 验证 ssl 证书
|
||||||
*/
|
*/
|
||||||
sslVerify?: boolean;
|
sslVerify?: boolean;
|
||||||
|
|
||||||
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -734,3 +736,7 @@ export declare interface AxiosInstance extends AxiosBaseInstance {
|
||||||
*/
|
*/
|
||||||
isCancel: (value: any) => boolean;
|
isCancel: (value: any) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ObjectTree<T = unknown> {
|
||||||
|
[key: string]: T | ObjectTree<T>;
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
import { Adapter } from './types';
|
|
||||||
/**
|
|
||||||
* 自适应当前平台
|
|
||||||
*/
|
|
||||||
export default function adaptive(): Adapter | undefined;
|
|
|
@ -1,6 +0,0 @@
|
||||||
import { AxiosInstance } from './types';
|
|
||||||
/**
|
|
||||||
* Axios 实例拓展
|
|
||||||
*/
|
|
||||||
declare const axios: AxiosInstance;
|
|
||||||
export default axios;
|
|
|
@ -1,9 +0,0 @@
|
||||||
import { Cancel } from '../types';
|
|
||||||
export default class CancelClass implements Cancel {
|
|
||||||
message?: string | undefined;
|
|
||||||
/**
|
|
||||||
* @param message 取消信息
|
|
||||||
*/
|
|
||||||
constructor(message?: string | undefined);
|
|
||||||
toString(): string;
|
|
||||||
}
|
|
|
@ -1,23 +0,0 @@
|
||||||
import { CancelToken, CancelExecutor, CancelTokenSource } from '../types';
|
|
||||||
import Cancel from './Cancel';
|
|
||||||
export default class CancelTokenClass implements CancelToken {
|
|
||||||
/**
|
|
||||||
* 取消请求
|
|
||||||
*/
|
|
||||||
private _reason?;
|
|
||||||
listener: Promise<Cancel>;
|
|
||||||
constructor(executor: CancelExecutor);
|
|
||||||
throwIfRequested(): void;
|
|
||||||
/**
|
|
||||||
* 返回一个 CancelTokenSource
|
|
||||||
*
|
|
||||||
* CancelTokenSource.token 是一个 CancelToken 对象
|
|
||||||
*
|
|
||||||
* CancelTokenSource.cancel 是一个 CancelAction 函数
|
|
||||||
*
|
|
||||||
* 调用 CancelTokenSource.cancel('这里可以填写您的错误信息')
|
|
||||||
*
|
|
||||||
* 取消请求 CancelTokenSource.token
|
|
||||||
*/
|
|
||||||
static source(): CancelTokenSource;
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
import Cancel from './Cancel';
|
|
||||||
/**
|
|
||||||
* 是否是取消请求实例
|
|
||||||
*
|
|
||||||
* @param value 判断的值
|
|
||||||
*/
|
|
||||||
export default function isCancel(value: unknown): value is Cancel;
|
|
|
@ -1,37 +0,0 @@
|
||||||
import { Params, Data, Interceptors, AxiosRequestConfig, AxiosResponse, Axios } from '../types';
|
|
||||||
export default class AxiosClass implements Axios {
|
|
||||||
defaults: AxiosRequestConfig;
|
|
||||||
interceptors: Interceptors;
|
|
||||||
/**
|
|
||||||
* @param defaults 自定义默认配置
|
|
||||||
*/
|
|
||||||
constructor(defaults?: AxiosRequestConfig);
|
|
||||||
getUri(config: AxiosRequestConfig): string;
|
|
||||||
request<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
options<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
get<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
head<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
post<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
put<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
delete<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
trace<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
connect<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 合并配置后发送 HTTP 请求
|
|
||||||
*
|
|
||||||
* @param method 请求方法
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param params 请求参数
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
private _requestMethodWithoutParams;
|
|
||||||
/**
|
|
||||||
* 合并配置后发送 HTTP 请求
|
|
||||||
*
|
|
||||||
* @param method 请求方法
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param data 请求数据
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
private _requestMethodWithoutData;
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
import { InterceptorResolved, InterceptorRejected, InterceptorExecutor, InterceptorManager } from '../types';
|
|
||||||
/**
|
|
||||||
* 拦截器管理器
|
|
||||||
*/
|
|
||||||
export default class InterceptorManagerClass<T> implements InterceptorManager<T> {
|
|
||||||
/**
|
|
||||||
* 生成拦截器 id
|
|
||||||
*/
|
|
||||||
private _id;
|
|
||||||
/**
|
|
||||||
* 拦截器集合
|
|
||||||
*/
|
|
||||||
private _interceptors;
|
|
||||||
use(resolved: InterceptorResolved<T>, rejected?: InterceptorRejected): number;
|
|
||||||
eject(id: number): void;
|
|
||||||
forEach(executor: InterceptorExecutor<T>, reverse?: 'reverse'): void;
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
import { AxiosRequestConfig, RequestConfig, AxiosResponse, AxiosError } from '../types';
|
|
||||||
/**
|
|
||||||
* 创建 AxiosError 的工厂方法
|
|
||||||
*
|
|
||||||
* 返回一个新的 AxiosError 对象
|
|
||||||
*
|
|
||||||
* @param message 错误信息
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
* @param request 通用请求配置
|
|
||||||
* @param response Axios 响应体
|
|
||||||
*/
|
|
||||||
export default function createError(message: string, config: AxiosRequestConfig, request: RequestConfig, response?: AxiosResponse): AxiosError;
|
|
|
@ -1,7 +0,0 @@
|
||||||
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
|
||||||
/**
|
|
||||||
* 发送请求
|
|
||||||
*
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export default function dispatchRequest(config: AxiosRequestConfig): Promise<AxiosResponse>;
|
|
|
@ -1,7 +0,0 @@
|
||||||
import { Headers, AxiosRequestConfig } from '../types';
|
|
||||||
/**
|
|
||||||
* 拉平请求头
|
|
||||||
*
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export default function flattenHeaders(config: AxiosRequestConfig): Headers;
|
|
|
@ -1,8 +0,0 @@
|
||||||
import { AxiosRequestConfig } from '../types';
|
|
||||||
/**
|
|
||||||
* 合并 Axios 请求配置
|
|
||||||
*
|
|
||||||
* @param config1 Axios 请求配置1
|
|
||||||
* @param config2 Axios 请求配置2
|
|
||||||
*/
|
|
||||||
export default function mergeConfig(config1?: AxiosRequestConfig, config2?: AxiosRequestConfig): AxiosRequestConfig;
|
|
|
@ -1,7 +0,0 @@
|
||||||
import { AxiosRequestConfig, AxiosResponse } from '../types';
|
|
||||||
/**
|
|
||||||
* 请求函数
|
|
||||||
*
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export default function request(config: AxiosRequestConfig): Promise<AxiosResponse>;
|
|
|
@ -1,9 +0,0 @@
|
||||||
import { Data, Headers, TransformData } from '../types';
|
|
||||||
/**
|
|
||||||
* 转换数据
|
|
||||||
*
|
|
||||||
* @param data 请求数据/响应数据
|
|
||||||
* @param headers 请求头/响应头
|
|
||||||
* @param transforms 请求数据转换函数/响应数据转换函数
|
|
||||||
*/
|
|
||||||
export default function transformData(data: Data, headers: Headers, transforms?: TransformData | TransformData[]): Data;
|
|
|
@ -1,13 +0,0 @@
|
||||||
import { AliasMethod, AdapterMethod, Method } from '../types';
|
|
||||||
/**
|
|
||||||
* 请求方法转全小写
|
|
||||||
*
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export declare function methodToLowercase(method?: Method): AliasMethod;
|
|
||||||
/**
|
|
||||||
* 请求方法转全大写
|
|
||||||
*
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export declare function methodToUppercase(method?: Method): AdapterMethod;
|
|
|
@ -1,9 +0,0 @@
|
||||||
import { AxiosRequestConfig, RequestConfig } from '../types';
|
|
||||||
/**
|
|
||||||
* Axios 请求配置转换成各大平台通用请求配置
|
|
||||||
*
|
|
||||||
* 抹平差异
|
|
||||||
*
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export default function transformRequest(config: AxiosRequestConfig): RequestConfig;
|
|
|
@ -1,10 +0,0 @@
|
||||||
import { AxiosRequestConfig, AxiosResponse, Response } from '../types';
|
|
||||||
/**
|
|
||||||
* 各大平台通用响应体转成 Axios 响应体
|
|
||||||
*
|
|
||||||
* 抹平差异
|
|
||||||
*
|
|
||||||
* @param response 通用响应体
|
|
||||||
* @param config Axios 请求配置
|
|
||||||
*/
|
|
||||||
export default function transformResponse(response: Response, config: AxiosRequestConfig): AxiosResponse;
|
|
|
@ -1,3 +0,0 @@
|
||||||
import { AxiosRequestConfig } from './types';
|
|
||||||
declare const defaults: AxiosRequestConfig;
|
|
||||||
export default defaults;
|
|
|
@ -1,16 +0,0 @@
|
||||||
import { AnyObject, Params } from '../types';
|
|
||||||
/**
|
|
||||||
* 默认参数序列化
|
|
||||||
*
|
|
||||||
* @param params 请求参数
|
|
||||||
*/
|
|
||||||
declare function paramsSerialization(params: AnyObject): string;
|
|
||||||
/**
|
|
||||||
* 处理 URL 参数
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param params 请求参数
|
|
||||||
* @param paramsSerializer 自定义参数序列化
|
|
||||||
*/
|
|
||||||
export default function buildURL(url: string, params?: Params, paramsSerializer?: typeof paramsSerialization): string;
|
|
||||||
export {};
|
|
|
@ -1,6 +0,0 @@
|
||||||
/**
|
|
||||||
* 拼接 baseURL 和 url 获得完整的 URL
|
|
||||||
*
|
|
||||||
* combineURL('1/2///','////3/4') => '1/2/3/4'
|
|
||||||
*/
|
|
||||||
export default function combineURL(baseURL: string, url: string): string;
|
|
|
@ -1,8 +0,0 @@
|
||||||
/**
|
|
||||||
* 检查是否是一个绝对 URL
|
|
||||||
*
|
|
||||||
* xxx:// 或者 "//" 开头, 视为绝对地址
|
|
||||||
*
|
|
||||||
* @param url 需要检查的 URL
|
|
||||||
*/
|
|
||||||
export default function isAbsoluteURL(url: string): boolean;
|
|
|
@ -1,39 +0,0 @@
|
||||||
import { AnyObject } from '../types';
|
|
||||||
/**
|
|
||||||
* 对字符串进行编码转换
|
|
||||||
*
|
|
||||||
* @param str 字符串
|
|
||||||
*/
|
|
||||||
export declare function encode(str: string): string;
|
|
||||||
/**
|
|
||||||
* 是不是一个日期对象
|
|
||||||
*
|
|
||||||
* @param date 判断目标
|
|
||||||
*/
|
|
||||||
export declare function isDate(date: unknown): date is Date;
|
|
||||||
/**
|
|
||||||
* 是不是一个普通对象
|
|
||||||
*
|
|
||||||
* @param obj 判断目标
|
|
||||||
*/
|
|
||||||
export declare function isPlainObject(obj: unknown): obj is Record<string, unknown>;
|
|
||||||
/**
|
|
||||||
* 深度合并多个对象
|
|
||||||
*
|
|
||||||
* @param objs n 个对象
|
|
||||||
*/
|
|
||||||
export declare function deepMerge(...objs: Record<string, any>[]): Record<string, any>;
|
|
||||||
/**
|
|
||||||
* 从对象中提取一部分属性
|
|
||||||
*
|
|
||||||
* @param obj 源对象
|
|
||||||
* @param keys 需要提取的 key
|
|
||||||
*/
|
|
||||||
export declare function pick<T extends AnyObject, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>;
|
|
||||||
/**
|
|
||||||
* 从对象中剔除一部分属性
|
|
||||||
*
|
|
||||||
* @param obj 源对象
|
|
||||||
* @param keys 需要剔除的 key
|
|
||||||
*/
|
|
||||||
export declare function omit<T extends AnyObject, K extends keyof T>(obj: T, ...keys: K[]): Omit<T, K>;
|
|
|
@ -1,3 +0,0 @@
|
||||||
import axios from './axios';
|
|
||||||
export * from './types';
|
|
||||||
export default axios;
|
|
|
@ -1,596 +0,0 @@
|
||||||
/**
|
|
||||||
* 任意值对象
|
|
||||||
*/
|
|
||||||
export declare interface AnyObject<T extends any = any> {
|
|
||||||
[x: string]: T;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 请求方法
|
|
||||||
*/
|
|
||||||
export declare type AdapterMethod = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT';
|
|
||||||
/**
|
|
||||||
* 请求方法别名
|
|
||||||
*/
|
|
||||||
export declare type AliasMethod = 'options' | 'get' | 'head' | 'post' | 'put' | 'delete' | 'trace' | 'connect';
|
|
||||||
/**
|
|
||||||
* Axios 请求方法
|
|
||||||
*/
|
|
||||||
export declare type Method = AliasMethod | AdapterMethod;
|
|
||||||
/**
|
|
||||||
* Axios 参数
|
|
||||||
*/
|
|
||||||
export declare type Params = AnyObject;
|
|
||||||
/**
|
|
||||||
* Axios 数据
|
|
||||||
*/
|
|
||||||
export declare type Data = string | AnyObject | ArrayBuffer;
|
|
||||||
/**
|
|
||||||
* Axios 头
|
|
||||||
*/
|
|
||||||
export declare interface Headers extends Partial<Record<'common' | AliasMethod, AnyObject<string>>> {
|
|
||||||
/**
|
|
||||||
* 自定义配置
|
|
||||||
*/
|
|
||||||
[x: string]: AnyObject<string> | string | undefined;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 通用请求配置
|
|
||||||
*/
|
|
||||||
export declare interface RequestConfig {
|
|
||||||
/**
|
|
||||||
* 接口地址
|
|
||||||
*/
|
|
||||||
url: string;
|
|
||||||
/**
|
|
||||||
* HTTP 请求方法
|
|
||||||
*/
|
|
||||||
method: AdapterMethod;
|
|
||||||
/**
|
|
||||||
* 请求数据
|
|
||||||
*/
|
|
||||||
data: Data;
|
|
||||||
/**
|
|
||||||
* 请求头 同 headers
|
|
||||||
*/
|
|
||||||
header: AnyObject;
|
|
||||||
/**
|
|
||||||
* 请求头 同 header
|
|
||||||
*/
|
|
||||||
headers: AnyObject;
|
|
||||||
/**
|
|
||||||
* 返回的数据格式
|
|
||||||
*/
|
|
||||||
dataType?: 'json' | '其他';
|
|
||||||
/**
|
|
||||||
* 响应的数据类型
|
|
||||||
*/
|
|
||||||
responseType?: 'text' | 'arraybuffer';
|
|
||||||
/**
|
|
||||||
* 超时时间
|
|
||||||
*/
|
|
||||||
timeout?: number;
|
|
||||||
/**
|
|
||||||
* 开启 http2
|
|
||||||
*/
|
|
||||||
enableHttp2?: boolean;
|
|
||||||
/**
|
|
||||||
* 开启 quic
|
|
||||||
*/
|
|
||||||
enableQuic?: boolean;
|
|
||||||
/**
|
|
||||||
* 开启 cache
|
|
||||||
*/
|
|
||||||
enableCache?: boolean;
|
|
||||||
/**
|
|
||||||
* 验证 ssl 证书
|
|
||||||
*/
|
|
||||||
sslVerify?: boolean;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 通用响应体
|
|
||||||
*/
|
|
||||||
export declare interface Response {
|
|
||||||
/**
|
|
||||||
* 响应状态码
|
|
||||||
*/
|
|
||||||
statusCode?: number;
|
|
||||||
/**
|
|
||||||
* 响应状态码
|
|
||||||
*/
|
|
||||||
status?: number;
|
|
||||||
/**
|
|
||||||
* 响应头 Headers
|
|
||||||
*/
|
|
||||||
header?: AnyObject;
|
|
||||||
/**
|
|
||||||
* 响应头 Headers
|
|
||||||
*/
|
|
||||||
headers?: Headers;
|
|
||||||
/**
|
|
||||||
* 响应数据
|
|
||||||
*/
|
|
||||||
data: Data;
|
|
||||||
/**
|
|
||||||
* 开发者服务器返回的 cookies,格式为字符串数组
|
|
||||||
*/
|
|
||||||
cookies?: string[];
|
|
||||||
/**
|
|
||||||
* 网络请求过程中一些关键时间点的耗时信息
|
|
||||||
*/
|
|
||||||
profile?: AnyObject;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 适配器请求配置
|
|
||||||
*/
|
|
||||||
export declare interface AdapterRequestConfig extends RequestConfig {
|
|
||||||
/**
|
|
||||||
* 成功的响应函数
|
|
||||||
*/
|
|
||||||
success: (res: Response) => void;
|
|
||||||
/**
|
|
||||||
* 失败的响应函数
|
|
||||||
*/
|
|
||||||
fail: (err: unknown) => void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 适配器请求任务
|
|
||||||
*/
|
|
||||||
export declare interface AdapterRequestTask {
|
|
||||||
/**
|
|
||||||
* 取消请求
|
|
||||||
*/
|
|
||||||
abort(): void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 适配器
|
|
||||||
*/
|
|
||||||
export declare interface Adapter {
|
|
||||||
(config: AdapterRequestConfig): AdapterRequestTask | void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 平台
|
|
||||||
*/
|
|
||||||
export declare interface Platform {
|
|
||||||
request: Adapter;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 转换数据函数
|
|
||||||
*/
|
|
||||||
export declare interface TransformData {
|
|
||||||
(data: Data, headers: Headers): Data;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 错误处理程序
|
|
||||||
*/
|
|
||||||
export declare interface ErrorHandler {
|
|
||||||
(error: any): Promise<any> | any;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 请求配置
|
|
||||||
*/
|
|
||||||
export declare interface AxiosRequestConfig {
|
|
||||||
/**
|
|
||||||
* 自定义适配器
|
|
||||||
*/
|
|
||||||
adapter?: Adapter;
|
|
||||||
/**
|
|
||||||
* 基础地址
|
|
||||||
*/
|
|
||||||
baseURL?: string;
|
|
||||||
/**
|
|
||||||
* 请求地址
|
|
||||||
*/
|
|
||||||
url?: string;
|
|
||||||
/**
|
|
||||||
* 请求方法
|
|
||||||
*/
|
|
||||||
method?: Method;
|
|
||||||
/**
|
|
||||||
* 请求参数
|
|
||||||
*/
|
|
||||||
params?: Params;
|
|
||||||
/**
|
|
||||||
* 请求数据
|
|
||||||
*/
|
|
||||||
data?: Data;
|
|
||||||
/**
|
|
||||||
* 请求头
|
|
||||||
*/
|
|
||||||
headers?: Headers;
|
|
||||||
/**
|
|
||||||
* 自定义合法状态码
|
|
||||||
*/
|
|
||||||
validateStatus?: (status: number) => boolean;
|
|
||||||
/**
|
|
||||||
* 自定义参数序列化
|
|
||||||
*/
|
|
||||||
paramsSerializer?: (params?: AnyObject) => string;
|
|
||||||
/**
|
|
||||||
* 转换请求数据
|
|
||||||
*/
|
|
||||||
transformRequest?: TransformData | TransformData[];
|
|
||||||
/**
|
|
||||||
* 转换响应数据
|
|
||||||
*/
|
|
||||||
transformResponse?: TransformData | TransformData[];
|
|
||||||
/**
|
|
||||||
* 自定义错误处理
|
|
||||||
*/
|
|
||||||
errorHandler?: ErrorHandler;
|
|
||||||
/**
|
|
||||||
* 取消令牌
|
|
||||||
*/
|
|
||||||
cancelToken?: CancelToken;
|
|
||||||
/**
|
|
||||||
* 超时时间
|
|
||||||
*/
|
|
||||||
timeout?: number;
|
|
||||||
/**
|
|
||||||
* 响应数据格式
|
|
||||||
*/
|
|
||||||
dataType?: 'json' | '其他';
|
|
||||||
/**
|
|
||||||
* 响应数据类型
|
|
||||||
*/
|
|
||||||
responseType?: 'text' | 'arraybuffer';
|
|
||||||
/**
|
|
||||||
* 开启 http2
|
|
||||||
*/
|
|
||||||
enableHttp2?: boolean;
|
|
||||||
/**
|
|
||||||
* 开启 quic
|
|
||||||
*/
|
|
||||||
enableQuic?: boolean;
|
|
||||||
/**
|
|
||||||
* 开启 cache
|
|
||||||
*/
|
|
||||||
enableCache?: boolean;
|
|
||||||
/**
|
|
||||||
* 验证 ssl 证书
|
|
||||||
*/
|
|
||||||
sslVerify?: boolean;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 响应体
|
|
||||||
*/
|
|
||||||
export declare interface AxiosResponse<T extends Data = Data> {
|
|
||||||
/**
|
|
||||||
* 状态码
|
|
||||||
*/
|
|
||||||
status: number;
|
|
||||||
/**
|
|
||||||
* 状态文本
|
|
||||||
*/
|
|
||||||
statusText: string;
|
|
||||||
/**
|
|
||||||
* 服务端返回的数据
|
|
||||||
*/
|
|
||||||
data: T;
|
|
||||||
/**
|
|
||||||
* 响应头
|
|
||||||
*/
|
|
||||||
headers: Headers;
|
|
||||||
/**
|
|
||||||
* Axios 请求配置
|
|
||||||
*/
|
|
||||||
config: AxiosRequestConfig;
|
|
||||||
/**
|
|
||||||
* 开发者服务器返回的 cookies,格式为字符串数组
|
|
||||||
*/
|
|
||||||
cookies?: string[];
|
|
||||||
/**
|
|
||||||
* 网络请求过程中一些关键时间点的耗时信息
|
|
||||||
*/
|
|
||||||
profile?: AnyObject;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 拦截器成功的回调函数
|
|
||||||
*/
|
|
||||||
export declare interface InterceptorResolved<T = any> {
|
|
||||||
(value: T): Promise<T> | T;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 拦截器失败的回调函数
|
|
||||||
*/
|
|
||||||
export declare interface InterceptorRejected {
|
|
||||||
(error: any): any;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 拦截器
|
|
||||||
*/
|
|
||||||
export declare interface Interceptor<T = any> {
|
|
||||||
/**
|
|
||||||
* 拦截器成功的回调函数
|
|
||||||
*/
|
|
||||||
resolved: InterceptorResolved<T>;
|
|
||||||
/**
|
|
||||||
* 拦截器失败的回调函数
|
|
||||||
*/
|
|
||||||
rejected?: InterceptorRejected;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 拦截器执行器
|
|
||||||
*/
|
|
||||||
export declare interface InterceptorExecutor<T = any> {
|
|
||||||
(interceptor: Interceptor<T>): void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 拦截器管理器
|
|
||||||
*/
|
|
||||||
export declare interface InterceptorManager<T = any> {
|
|
||||||
[x: string]: any;
|
|
||||||
/**
|
|
||||||
* 添加拦截器
|
|
||||||
*
|
|
||||||
* @param resolved 成功的回调函数
|
|
||||||
* @param rejected 失败的回调函数
|
|
||||||
*/
|
|
||||||
use(resolved: InterceptorResolved<T>, rejected?: InterceptorRejected): number;
|
|
||||||
/**
|
|
||||||
* 删除拦截器
|
|
||||||
*
|
|
||||||
* @param id 拦截器 id
|
|
||||||
*/
|
|
||||||
eject(id: number): void;
|
|
||||||
/**
|
|
||||||
* 遍历所有拦截器
|
|
||||||
*
|
|
||||||
* @param executor 拦截器执行器
|
|
||||||
* @param reverse 是否倒序遍历
|
|
||||||
*/
|
|
||||||
forEach(executor: InterceptorExecutor<T>, reverse?: 'reverse'): void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 拦截器
|
|
||||||
*/
|
|
||||||
export declare interface Interceptors {
|
|
||||||
/**
|
|
||||||
* request 请求拦截器
|
|
||||||
*/
|
|
||||||
request: InterceptorManager<AxiosRequestConfig>;
|
|
||||||
/**
|
|
||||||
* response 响应拦截器
|
|
||||||
*/
|
|
||||||
response: InterceptorManager<AxiosResponse>;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 实例
|
|
||||||
*/
|
|
||||||
export declare interface Axios {
|
|
||||||
/**
|
|
||||||
* 默认配置
|
|
||||||
*/
|
|
||||||
defaults: AxiosRequestConfig;
|
|
||||||
/**
|
|
||||||
* Axios 拦截器
|
|
||||||
*/
|
|
||||||
interceptors: Interceptors;
|
|
||||||
/**
|
|
||||||
* 根据配置中的 url 和 params 生成一个 URI
|
|
||||||
*
|
|
||||||
* @param config 请求配置
|
|
||||||
*/
|
|
||||||
getUri(config: AxiosRequestConfig): string;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求
|
|
||||||
*
|
|
||||||
* @param config 请求配置
|
|
||||||
*/
|
|
||||||
request<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 OPTIONS
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
options<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 GET
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param params 请求参数
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
get<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 HEAD
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param params 请求参数
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
head<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 POST
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param data 请求数据
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
post<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 PUT
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param data 请求数据
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
put<T extends Data>(url: string, data?: Data, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 DELETE
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param params 请求参数
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
delete<T extends Data>(url: string, params?: Params, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 TRACE
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
trace<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求 CONNECT
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
connect<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 类接口
|
|
||||||
*/
|
|
||||||
export declare interface AxiosConstructor {
|
|
||||||
new (config?: AxiosRequestConfig): Axios;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* AxiosError 类继承自 Error
|
|
||||||
*/
|
|
||||||
export declare interface AxiosError extends Error {
|
|
||||||
/**
|
|
||||||
* 是 Axios 错误
|
|
||||||
*/
|
|
||||||
isAxiosError: boolean;
|
|
||||||
/**
|
|
||||||
* 请求配置
|
|
||||||
*/
|
|
||||||
config: AxiosRequestConfig;
|
|
||||||
/**
|
|
||||||
* 通用请求配置
|
|
||||||
*/
|
|
||||||
request: RequestConfig;
|
|
||||||
/**
|
|
||||||
* Axios 响应体
|
|
||||||
*/
|
|
||||||
response?: AxiosResponse;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消请求
|
|
||||||
*/
|
|
||||||
export declare interface Cancel {
|
|
||||||
/**
|
|
||||||
* 取消信息
|
|
||||||
*/
|
|
||||||
message?: string;
|
|
||||||
/**
|
|
||||||
* 序列化
|
|
||||||
*/
|
|
||||||
toString(): string;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消请求类接口
|
|
||||||
*/
|
|
||||||
export declare interface CancelConstructor {
|
|
||||||
new (message?: string): Cancel;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消操作
|
|
||||||
*/
|
|
||||||
export declare interface CancelAction {
|
|
||||||
(message?: string): void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消操作执行器
|
|
||||||
*/
|
|
||||||
export declare interface CancelExecutor {
|
|
||||||
(cancel: CancelAction): void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消令牌
|
|
||||||
*/
|
|
||||||
export declare interface CancelToken {
|
|
||||||
/**
|
|
||||||
* 取消时被触发
|
|
||||||
*/
|
|
||||||
listener: Promise<Cancel>;
|
|
||||||
/**
|
|
||||||
* 如果已经取消, 则抛出取消对象
|
|
||||||
*/
|
|
||||||
throwIfRequested(): void;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消令牌 source
|
|
||||||
*/
|
|
||||||
export declare interface CancelTokenSource {
|
|
||||||
/**
|
|
||||||
* 取消令牌
|
|
||||||
*/
|
|
||||||
token: CancelToken;
|
|
||||||
/**
|
|
||||||
* 取消操作
|
|
||||||
*/
|
|
||||||
cancel: CancelAction;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 取消令牌类接口
|
|
||||||
*/
|
|
||||||
export declare interface CancelTokenConstructor {
|
|
||||||
new (executor: CancelExecutor): CancelToken;
|
|
||||||
/**
|
|
||||||
* 返回一个 CancelTokenSource
|
|
||||||
*
|
|
||||||
* CancelTokenSource.token 是一个 CancelToken 对象
|
|
||||||
*
|
|
||||||
* CancelTokenSource.cancel 是一个 CancelAction 函数
|
|
||||||
*
|
|
||||||
* 调用 CancelTokenSource.cancel('这里可以填写您的错误信息')
|
|
||||||
*
|
|
||||||
* 取消请求 CancelTokenSource.token
|
|
||||||
*/
|
|
||||||
source(): CancelTokenSource;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 实例基础拓展
|
|
||||||
*
|
|
||||||
* * 支持两种函数调用方式
|
|
||||||
*/
|
|
||||||
export declare interface AxiosBaseInstance extends Axios {
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求
|
|
||||||
*
|
|
||||||
* 调用方式一
|
|
||||||
*
|
|
||||||
* @param config 请求配置
|
|
||||||
*/
|
|
||||||
<T extends Data>(config: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
/**
|
|
||||||
* 发送 HTTP 请求
|
|
||||||
*
|
|
||||||
* 调用方式二
|
|
||||||
*
|
|
||||||
* @param url 请求地址
|
|
||||||
* @param config 额外配置
|
|
||||||
*/
|
|
||||||
<T extends Data>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Axios 实例拓展
|
|
||||||
*
|
|
||||||
* * 支持两种函数调用方式
|
|
||||||
*
|
|
||||||
* * 同时拓展了一些静态属性和方法
|
|
||||||
*/
|
|
||||||
export declare interface AxiosInstance extends AxiosBaseInstance {
|
|
||||||
/**
|
|
||||||
* 创建 Axios 实例基础拓展
|
|
||||||
*
|
|
||||||
* @param defaults 自定义默认配置
|
|
||||||
*/
|
|
||||||
create(defaults?: AxiosRequestConfig): AxiosBaseInstance;
|
|
||||||
/**
|
|
||||||
* Axios 类
|
|
||||||
*/
|
|
||||||
Axios: AxiosConstructor;
|
|
||||||
/**
|
|
||||||
* 取消令牌 类
|
|
||||||
*/
|
|
||||||
CancelToken: CancelTokenConstructor;
|
|
||||||
/**
|
|
||||||
* 检查错误是否来自取消请求
|
|
||||||
*
|
|
||||||
* @param value 判断的值
|
|
||||||
*/
|
|
||||||
isCancel: (value: any) => boolean;
|
|
||||||
}
|
|
Loading…
Reference in New Issue