From b007a49841eff37eae1b44d87e75375ae081124e Mon Sep 17 00:00:00 2001 From: Garfield Lee Date: Mon, 8 Jul 2019 19:46:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(taro):=20=E4=B8=BA=20Taro.request=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=AE=9A=E4=B9=89=E6=B7=BB=E5=8A=A0=20abort?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95=EF=BC=8C=E5=AE=8C=E5=96=84=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=20#3654=20(#3715)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(taro): 为 Taro.request 类型定义添加 abort 方法,完善注释 #3654 * docs(request): 更新 Taro.request abort 相关文档 --- docs/apis/network/request/request.md | 22 ++++++- packages/taro/types/index.d.ts | 95 +++++++++++++++++----------- 2 files changed, 77 insertions(+), 40 deletions(-) diff --git a/docs/apis/network/request/request.md b/docs/apis/network/request/request.md index 34f6ae20f2a9..e0dc36122199 100644 --- a/docs/apis/network/request/request.md +++ b/docs/apis/network/request/request.md @@ -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 参数说明:** | 参数 | 类型 | 必填 | 默认值 | 说明 | @@ -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 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | - diff --git a/packages/taro/types/index.d.ts b/packages/taro/types/index.d.ts index b3db2dbf0bcb..7ecff383413b 100644 --- a/packages/taro/types/index.d.ts +++ b/packages/taro/types/index.d.ts @@ -898,6 +898,17 @@ declare namespace Taro { */ header: any } + /** + * 网络请求任务对象 + * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.html + */ + interface requestTask extends Promise> { + /** + * 中断请求任务 + * @see https://developers.weixin.qq.com/miniprogram/dev/api/network/request/RequestTask.abort.html + */ + abort(): void + } type Param < P extends any | string | ArrayBuffer = any > = { /** * 开发者服务器接口地址 @@ -1028,11 +1039,11 @@ 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'; @@ -1040,44 +1051,54 @@ declare namespace Taro { * 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(OBJECT: request.Param): Promise> + function request(OBJECT: request.Param): request.requestTask type arrayBuffer = Uint8Array | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | ArrayBuffer