Skip to content

Commit

Permalink
fix(taro): 为 Taro.request 类型定义添加 abort 方法,完善注释 #3654 (#3715)
Browse files Browse the repository at this point in the history
* fix(taro): 为 Taro.request 类型定义添加 abort 方法,完善注释 #3654

* docs(request): 更新 Taro.request abort 相关文档
  • Loading branch information
Garfield550 authored and luckyadam committed Jul 8, 2019
1 parent e6bf829 commit b007a49
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 40 deletions.
22 changes: 19 additions & 3 deletions docs/apis/network/request/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ title: Taro.request(OBJECT)
sidebar_label: request
---


发起网络请求,支持 `Promise` 化使用。

> 暂不支持使用 [RequestTask.onHeadersReceived(function callback)](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.onHeadersReceived.html)[RequestTask.offHeadersReceived(function callback)](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.offHeadersReceived.html) 方法。
**OBJECT 参数说明:**

| 参数 | 类型 | 必填 | 默认值 | 说明 |
Expand Down Expand Up @@ -56,12 +57,27 @@ Taro.request({
.then(res => console.log(res.data))
```

## 小程序端使用 RequestTask.abort()

```jsx
const requestTask = Taro.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json'
},
success (res) {
console.log(res.data)
}
})
requestTask.abort() // 取消请求任务
```

## API支持度


| API | 微信小程序 | H5 | React Native | 支付宝小程序 | 百度小程序 | 头条小程序 | QQ 轻应用 |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| Taro.request | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

95 changes: 58 additions & 37 deletions packages/taro/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,17 @@ declare namespace Taro {
*/
header: any
}
/**
* 网络请求任务对象
* @see https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.html
*/
interface requestTask<T> extends Promise<request.Promised<T>> {
/**
* 中断请求任务
* @see https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.abort.html
*/
abort(): void
}
type Param < P extends any | string | ArrayBuffer = any > = {
/**
* 开发者服务器接口地址
Expand Down Expand Up @@ -1028,56 +1039,66 @@ declare namespace Taro {
* 发起网络请求。**使用前请先阅读[说明](https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html)**。
*
* **返回值:**
*
* @returns {request.requestTask} 返回一个 `requestTask` 对象,通过 `requestTask`,可中断请求任务。
*
* @since 1.4.0
*
* 返回一个 `requestTask` 对象,通过 `requestTask`,可中断请求任务。
*
* **Bug & Tip:**
*
* 1. `tip`: content-type 默认为 'application/json';
* 2. `tip`: url 中不能有端口;
* 3. `bug`: 开发者工具 `0.10.102800` 版本,`header` 的 `content-type` 设置异常;
*
* **示例代码:**
*
* @example
* // 回调函数(Callback)用法:
* const requestTask = Taro.request({
* url: 'test.php', //仅为示例,并非真实的接口地址
* data: {
* x: '' ,
* y: ''
* },
* header: {
* 'content-type': 'application/json' // 默认值
* },
* success: function(res) {
* console.log(res.data)
* }
* })
* requestTask.abort()
*
* // Promise 用法:
* const requestTask = Taro.request({
* url: 'test.php', //仅为示例,并非真实的接口地址
* data: {
* x: '' ,
* y: ''
* },
* header: {
* 'content-type': 'application/json' // 默认值
* },
* success: function(res) {
* console.log(res.data)
* }
* })
* requestTask.then(res => {
* console.log(res.data)
* })
* requestTask.abort()
*
* // async/await 用法:
* const requestTask = Taro.request(params)
* const res = await requestTask
* requestTask.abort()
*
* // 不需要 abort 的 async/await 用法:
* const res = await Taro.request(params)
*
```javascript
Taro.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success: function(res) {
console.log(res.data)
}
})
```
*
* **示例代码:**
*
```javascript
const requestTask = Taro.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '' ,
y: ''
},
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log(res.data)
}
})
requestTask.abort() // 取消请求任务
```
* @see https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
*/
function request<T = any, U = any>(OBJECT: request.Param<U>): Promise<request.Promised<T>>
function request<T = any, U = any>(OBJECT: request.Param<U>): request.requestTask<T>

type arrayBuffer = Uint8Array | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | ArrayBuffer

Expand Down

0 comments on commit b007a49

Please sign in to comment.