-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
request的实现优化,request的h5版本复用taro-h5的实现,同时通过接口参数,业务可以灵活选择实现机制
- Loading branch information
Showing
4 changed files
with
302 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 7 additions & 91 deletions
98
packages/taro-platform-harmony-hybrid/src/api/apis/network/request/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,14 @@ | ||
import Taro from '@tarojs/api' | ||
import { isFunction } from '@tarojs/shared' | ||
import { request as h5Request } from '@tarojs/taro-h5' | ||
import { request as nativeReuqest } from './nativeRequest' | ||
|
||
import { NativeRequest } from '../../interface/NativeRequest' | ||
import native, { judgeUseAxios } from '../../NativeApi' | ||
import { getParameterError, shouldBeObject } from '../../utils' | ||
|
||
export const _request = (options) => { | ||
const name = 'request' | ||
|
||
const isObject = shouldBeObject(options) | ||
if (!isObject.flag) { | ||
const res = { errMsg: `${name}:fail ${isObject.msg}` } | ||
return Promise.reject(res) | ||
} | ||
|
||
const { url, success, fail, complete, method, ...otherOptions } = options as Exclude<typeof options, undefined> | ||
if (typeof url !== 'string') { | ||
const res = { | ||
errMsg: getParameterError({ | ||
para: 'url', | ||
correct: 'string', | ||
wrong: url, | ||
}), | ||
} | ||
isFunction(fail) && fail(res) | ||
isFunction(complete) && complete(res) | ||
return Promise.reject(res) | ||
} | ||
|
||
let task!: Taro.RequestTask<any> | ||
const result: ReturnType<typeof Taro.request> = new Promise((resolve, reject) => { | ||
const upperMethod = method ? method.toUpperCase() : method | ||
const taskID = native.request({ | ||
url, | ||
method: upperMethod, | ||
...otherOptions, | ||
success: (res: any) => { | ||
isFunction(success) && success(res) | ||
isFunction(complete) && complete(res) | ||
resolve(res) | ||
}, | ||
fail: (res: any) => { | ||
isFunction(fail) && fail(res) | ||
isFunction(complete) && complete(res) | ||
reject(res) | ||
}, | ||
}) | ||
task = judgeUseAxios ? taskID : NativeRequest.getRequestTask(taskID) | ||
}) as any | ||
|
||
result.onHeadersReceived = task.onHeadersReceived.bind(task) | ||
result.offHeadersReceived = task.offHeadersReceived.bind(task) | ||
result.abort = task.abort.bind(task) | ||
return result | ||
} | ||
|
||
function taroInterceptor (chain) { | ||
return _request(chain.requestParams) | ||
} | ||
|
||
// @ts-ignore | ||
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] | ||
* 封装请求方法 | ||
* @param options 请求选项 | ||
* @param useNativeRequest 默认使用true | ||
*/ | ||
export function request (options) { | ||
const result = link.request.bind(link)(options) | ||
result.catch(() => {}) | ||
return result | ||
export function request(options: any, useNativeRequest: boolean = true){ | ||
return useNativeRequest ? nativeReuqest(options) : h5Request(options); | ||
} | ||
|
||
/** | ||
* 网络请求任务对象 | ||
* | ||
* @canUse RequestTask | ||
* @__class [abort, onHeadersReceived, offHeadersReceived] | ||
*/ | ||
|
||
/** | ||
* 使用拦截器 | ||
* | ||
* @canNotUse addInterceptor | ||
*/ | ||
export const addInterceptor = link.addInterceptor.bind(link) | ||
|
||
/** | ||
* 清除所有拦截器 | ||
* | ||
* @canNotUse cleanInterceptors | ||
*/ | ||
export const cleanInterceptors = link.cleanInterceptors.bind(link) |
98 changes: 98 additions & 0 deletions
98
packages/taro-platform-harmony-hybrid/src/api/apis/network/request/nativeRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import Taro from '@tarojs/api' | ||
import { isFunction } from '@tarojs/shared' | ||
|
||
import { NativeRequest } from '../../interface/NativeRequest' | ||
import native, { judgeUseAxios } from '../../NativeApi' | ||
import { getParameterError, shouldBeObject } from '../../utils' | ||
|
||
export const _request = (options) => { | ||
const name = 'request' | ||
|
||
const isObject = shouldBeObject(options) | ||
if (!isObject.flag) { | ||
const res = { errMsg: `${name}:fail ${isObject.msg}` } | ||
return Promise.reject(res) | ||
} | ||
|
||
const { url, success, fail, complete, method, ...otherOptions } = options as Exclude<typeof options, undefined> | ||
if (typeof url !== 'string') { | ||
const res = { | ||
errMsg: getParameterError({ | ||
para: 'url', | ||
correct: 'string', | ||
wrong: url, | ||
}), | ||
} | ||
isFunction(fail) && fail(res) | ||
isFunction(complete) && complete(res) | ||
return Promise.reject(res) | ||
} | ||
|
||
let task!: Taro.RequestTask<any> | ||
const result: ReturnType<typeof Taro.request> = new Promise((resolve, reject) => { | ||
const upperMethod = method ? method.toUpperCase() : method | ||
const taskID = native.request({ | ||
url, | ||
method: upperMethod, | ||
...otherOptions, | ||
success: (res: any) => { | ||
isFunction(success) && success(res) | ||
isFunction(complete) && complete(res) | ||
resolve(res) | ||
}, | ||
fail: (res: any) => { | ||
isFunction(fail) && fail(res) | ||
isFunction(complete) && complete(res) | ||
reject(res) | ||
}, | ||
}) | ||
task = judgeUseAxios ? taskID : NativeRequest.getRequestTask(taskID) | ||
}) as any | ||
|
||
result.onHeadersReceived = task.onHeadersReceived.bind(task) | ||
result.offHeadersReceived = task.offHeadersReceived.bind(task) | ||
result.abort = task.abort.bind(task) | ||
return result | ||
} | ||
|
||
function taroInterceptor (chain) { | ||
return _request(chain.requestParams) | ||
} | ||
|
||
// @ts-ignore | ||
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) |
Oops, something went wrong.