From 0a2f9754db6567b724155fa3ca3978eb85075893 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Wed, 24 Oct 2018 19:16:45 +0800 Subject: [PATCH] =?UTF-8?q?feat(taro-h5):=20=E8=AE=BE=E7=BD=AE=20H5=20?= =?UTF-8?q?=E7=AB=AF=20Taro.request=20=E4=BF=9D=E7=95=99=20jsonp-retry=20?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-h5/src/api/request/index.js | 2 + packages/taro/types/index.d.ts | 52 ++++++++++++++++++++--- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/taro-h5/src/api/request/index.js b/packages/taro-h5/src/api/request/index.js index 8af79473e36b..d187dc5705bd 100644 --- a/packages/taro-h5/src/api/request/index.js +++ b/packages/taro-h5/src/api/request/index.js @@ -21,11 +21,13 @@ export default function request (options) { const params = {} const res = {} if (options.jsonp) { + Object.assign(params, options) params.params = options.data params.cache = options.jsonpCache if (typeof options.jsonp === 'string') { params.name = options.jsonp } + delete params.jsonp return jsonpRetry(url, params) .then(data => { res.statusCode = 200 diff --git a/packages/taro/types/index.d.ts b/packages/taro/types/index.d.ts index 27269b7c561c..1b50598a8e7f 100644 --- a/packages/taro/types/index.d.ts +++ b/packages/taro/types/index.d.ts @@ -482,7 +482,7 @@ declare namespace Taro { */ method?: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT' /** - * 如果设为json,会尝试对返回的数据做一次 JSON.parse + * 如果设为 json,会尝试对返回的数据做一次 JSON.parse * * @default json */ @@ -495,35 +495,73 @@ declare namespace Taro { */ responseType?: string, /** - * 设置H5端是否使用jsonp方式获取数据 + * 设置 H5 端是否使用jsonp方式获取数据 * * @default false */ jsonp?: boolean, /** - * 设置H5端 jsonp 请求 url 是否需要被缓存 + * 设置 H5 端 jsonp 请求 url 是否需要被缓存 * * @default false */ jsonpCache?: boolean, /** - * 设置H5端是否允许跨域请求。有效值:no-cors, cors, same-origin + * 设置 H5 端是否允许跨域请求。有效值:no-cors, cors, same-origin * * @default same-origin */ mode?: 'no-cors' | 'cors' | 'same-origin', /** - * 设置H5端是否携带 Cookie。有效值:include, same-origin, omit + * 设置 H5 端是否携带 Cookie。有效值:include, same-origin, omit * * @default omit */ credentials?: 'include' | 'same-origin' | 'omit', /** - * 设置H5端缓存模式。有效值:default, no-cache, reload, force-cache, only-if-cached + * 设置 H5 端缓存模式。有效值:default, no-cache, reload, force-cache, only-if-cached * * @default default */ - cache?: 'default' | 'no-cache' | 'reload' | 'force-cache' | 'only-if-cached' + cache?: 'default' | 'no-cache' | 'reload' | 'force-cache' | 'only-if-cached', + /** + * 设置 H5 端请求响应超时时间 + * + * @default 2000 + */ + timeout?: number, + /** + * 设置 H5 端请求重试次数 + * + * @default 2 + */ + retryTimes?: number, + /** + * 设置 H5 端请求的兜底接口 + */ + backup?: string | string[], + /** + * 设置 H5 端请求响应的数据校验函数,若返回 false,则请求兜底接口,若无兜底接口,则报请求失败 + */ + dataCheck?(): boolean, + /** + * 设置 H5 端请求是否使用缓存 + * + * @default false + */ + useStore: boolean, + /** + * 设置 H5 端请求缓存校验的 key + */ + storeCheckKey?: string, + /** + * 设置 H5 端请求缓存签名 + */ + storeSign?: string, + /** + * 设置 H5 端请求校验函数,一般不需要设置 + */ + storeCheck?(): boolean } } /**