Skip to content

Commit

Permalink
支持addInterceptor和clearInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
58liuyang committed May 6, 2024
1 parent 2338538 commit 48fe07a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
"WriteResult": false,
"addCard": false,
"addFileToFavorites": false,
"addInterceptor": false,
"addInterceptor": true,
"addPhoneCalendar": {
"object": {
"title": true,
Expand Down Expand Up @@ -608,7 +608,7 @@
"width": true
}
},
"cleanInterceptors": false,
"cleanInterceptors": true,
"clearStorage": true,
"clearStorageSync": true,
"closeBLEConnection": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
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) {
return useNativeRequest ? nativeReuqest(options) : h5Request(options)
}


/**
* 网络请求任务对象
*
* @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)
}


Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 48fe07a

Please sign in to comment.