Skip to content

Commit

Permalink
feat: 更新请求新配置项。新增‘是否尝试将响应数据json化’配置项。
Browse files Browse the repository at this point in the history
fix #113
  • Loading branch information
lei-mu committed May 25, 2023
1 parent 329fe95 commit 68344a1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
48 changes: 40 additions & 8 deletions src/lib/adapters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ export default (config) => {
response.config = config
response.rawData = response.data
try {
let jsonParseHandle = false
const forcedJSONParsingType = typeof config.forcedJSONParsing
if (forcedJSONParsingType === 'boolean') {
jsonParseHandle = config.forcedJSONParsing
} else if (forcedJSONParsingType === 'object') {
const includesMethod = config.forcedJSONParsing.include || []
jsonParseHandle = includesMethod.includes(config.method)
}

// 对可能字符串不是json 的情况容错
if (typeof response.data === 'string') {
if (jsonParseHandle && typeof response.data === 'string') {
response.data = JSON.parse(response.data)
}
// eslint-disable-next-line no-empty
Expand All @@ -57,19 +66,22 @@ export default (config) => {
// #ifdef H5
'file',
// #endif
// #ifdef H5 || APP-PLUS
// #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
'timeout',
// #endif
'formData'
]
requestTask = uni.uploadFile({..._config, ...otherConfig, ...mergeKeys(optionalKeys, config)})
} else if (config.method === 'DOWNLOAD') {
// #ifdef H5 || APP-PLUS
if (!isUndefined(config['timeout'])) {
_config['timeout'] = config['timeout']
}
// #endif
requestTask = uni.downloadFile(_config)
const optionalKeys = [
// #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
'timeout',
// #endif
// #ifdef MP
'filePath',
// #endif
]
requestTask = uni.downloadFile({..._config, ...mergeKeys(optionalKeys, config)})
} else {
const optionalKeys = [
'data',
Expand All @@ -90,6 +102,26 @@ export default (config) => {
// #ifdef APP-PLUS
'firstIpv4',
// #endif
// #ifdef MP-WEIXIN
'enableHttp2',
'enableQuic',
// #endif
// #ifdef MP-TOUTIAO || MP-WEIXIN
'enableCache',
// #endif
// #ifdef MP-WEIXIN
'enableHttpDNS',
'httpDNSServiceId',
'enableChunked',
'forceCellularNetwork',
// #endif
// #ifdef MP-ALIPAY
'enableCookie',
// #endif
// #ifdef MP-BAIDU
'cloudCache',
'defer'
// #endif
]
requestTask = uni.request({..._config, ...mergeKeys(optionalKeys, config)})
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/core/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default {
responseType: 'text',
// #endif
custom: {},
// #ifdef H5 || APP-PLUS || MP-ALIPAY || MP-WEIXIN
// #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
timeout: 60000,
// #endif
// #ifdef APP-PLUS
Expand All @@ -27,5 +27,7 @@ export default {
// #endif
validateStatus: function validateStatus(status) {
return status >= 200 && status < 300
}
},
// 是否尝试将响应数据json化
forcedJSONParsing: true
}
43 changes: 33 additions & 10 deletions src/lib/core/mergeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ export default (globalsConfig, config2 = {}) => {
custom: {...(globalsConfig.custom || {}), ...(config2.custom || {})},
header: deepMerge(globalsConfig.header || {}, config2.header || {})
}
const defaultToConfig2Keys = ['getTask', 'validateStatus', 'paramsSerializer']
const defaultToConfig2Keys = ['getTask', 'validateStatus', 'paramsSerializer', 'forcedJSONParsing']
config = {...config, ...mergeKeys(defaultToConfig2Keys, globalsConfig, config2)}

// eslint-disable-next-line no-empty
if (method === 'DOWNLOAD') {
// #ifdef H5 || APP-PLUS
if (!isUndefined(config2.timeout)) {
config['timeout'] = config2['timeout']
} else if (!isUndefined(globalsConfig.timeout)) {
config['timeout'] = globalsConfig['timeout']
}
// #endif
const downloadKeys = [
// #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
'timeout',
// #endif
// #ifdef MP
'filePath',
// #endif
]
config = {...config, ...mergeKeys(downloadKeys, globalsConfig, config2)}
} else if (method === 'UPLOAD') {
delete config.header['content-type']
delete config.header['Content-Type']
Expand All @@ -61,7 +63,7 @@ export default (globalsConfig, config2 = {}) => {
// #endif
'filePath',
'name',
// #ifdef H5 || APP-PLUS
// #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
'timeout',
// #endif
'formData',
Expand All @@ -71,7 +73,7 @@ export default (globalsConfig, config2 = {}) => {
config[prop] = config2[prop]
}
})
// #ifdef H5 || APP-PLUS
// #ifdef H5 || APP-PLUS || MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO || MP-KUAISHOU
if (isUndefined(config.timeout) && !isUndefined(globalsConfig.timeout)) {
config['timeout'] = globalsConfig['timeout']
}
Expand All @@ -95,6 +97,27 @@ export default (globalsConfig, config2 = {}) => {
// #ifdef APP-PLUS
'firstIpv4',
// #endif
// #ifdef MP-WEIXIN
'enableHttp2',
'enableQuic',
// #endif
// #ifdef MP-TOUTIAO || MP-WEIXIN
'enableCache',
// #endif
// #ifdef MP-WEIXIN
'enableHttpDNS',
'httpDNSServiceId',
'enableChunked',
'forceCellularNetwork',
// #endif
// #ifdef MP-ALIPAY
'enableCookie',
// #endif
// #ifdef MP-BAIDU
'cloudCache',
'defer'
// #endif

]
config = {...config, ...mergeKeys(defaultsKeys, globalsConfig, config2)}
}
Expand Down

0 comments on commit 68344a1

Please sign in to comment.