diff --git a/packages/taro-platform-harmony-hybrid/build/config/harmony-definition.json b/packages/taro-platform-harmony-hybrid/build/config/harmony-definition.json index 6bc71654ff87..b7be7f4ea5b8 100644 --- a/packages/taro-platform-harmony-hybrid/build/config/harmony-definition.json +++ b/packages/taro-platform-harmony-hybrid/build/config/harmony-definition.json @@ -391,7 +391,7 @@ "WriteResult": false, "addCard": false, "addFileToFavorites": false, - "addInterceptor": false, + "addInterceptor": true, "addPhoneCalendar": { "object": { "title": true, @@ -608,7 +608,7 @@ "width": true } }, - "cleanInterceptors": false, + "cleanInterceptors": true, "clearStorage": true, "clearStorageSync": true, "closeBLEConnection": { diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts index 3507247cfb54..7a1a568b1317 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts @@ -1,11 +1,13 @@ -import { request as h5Request } from '@tarojs/taro-h5' - -import { request as nativeReuqest } from './nativeRequest' +import { request as h5Request, addInterceptor as h5AddInterceptor, cleanInterceptors as h5CleanInterceptors } from '@tarojs/taro-h5' +import { request as nativeReuqest, addInterceptor as nativeAddInterceptor, cleanInterceptors as nativeCleanInterceptors } from './nativeRequest' /** - * 封装请求方法 - * @param options 请求选项 + * 发起 HTTPS 网络请求 + * + * @canUse request + * @__object [url, data, header, timeout, method[OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT], dataType[json, text, base64, arraybuffer], responseType[text, arraybuffer], enableCache] + * @__success [data, header, statusCode, cookies] * @param useNativeRequest 默认使用true */ export function request (options: any, useNativeRequest: boolean = true) { @@ -13,3 +15,29 @@ export function request (options: any, useNativeRequest: boolean = true) { } +/** + * 网络请求任务对象 + * + * @canUse RequestTask + * @__class [abort, onHeadersReceived, offHeadersReceived] + */ + +/** + * 使用拦截器 + * + * @canUse addInterceptor + */ +export function addInterceptor (interceptor, useNativeRequest: boolean = true){ + return useNativeRequest ? nativeAddInterceptor(interceptor) : h5AddInterceptor(interceptor) +} + +/** + * 清除所有拦截器 + * + * @canUse cleanInterceptors + */ +export function cleanInterceptors (interceptor, useNativeRequest: boolean = true) { + return useNativeRequest ? nativeCleanInterceptors(interceptor) : h5CleanInterceptors(interceptor) +} + + diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/nativeRequest.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/nativeRequest.ts index ec627dae335c..8a9a0bd7f896 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/nativeRequest.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/network/request/nativeRequest.ts @@ -63,36 +63,15 @@ function taroInterceptor (chain) { const { Link } = Taro const link = new Link(taroInterceptor) -/** - * 发起 HTTPS 网络请求 - * - * @canUse request - * @__object [url, data, header, timeout, method[OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT], dataType[json, text, base64, arraybuffer], responseType[text, arraybuffer], enableCache] - * @__success [data, header, statusCode, cookies] - */ + export function request (options) { const result = link.request.bind(link)(options) result.catch(() => {}) return result } -/** - * 网络请求任务对象 - * - * @canUse RequestTask - * @__class [abort, onHeadersReceived, offHeadersReceived] - */ -/** - * 使用拦截器 - * - * @canNotUse addInterceptor - */ export const addInterceptor = link.addInterceptor.bind(link) -/** - * 清除所有拦截器 - * - * @canNotUse cleanInterceptors - */ + export const cleanInterceptors = link.cleanInterceptors.bind(link)