Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 同步ReactNative与微信的网络请求参数 #5752

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/taro-rn/src/__tests__/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ describe('request', () => {
expect(res.data).toMatch(expectData)
})

it('RN请求入参同于微信小程序', () => {
const url = 'https://test.taro.com/v1'
const expectData = JSON.stringify({ data: 'calorie' })
const options = {
url,
responseType: 'json',
complete: (res) => {
},
success: (res) => {
expect(JSON.stringify(res.data)).toMatch(expectData)
},
fail: (res) => {
}
}
Taro.request(options)
})

test('数据被序列化', async () => {
const fetch = jest.fn((url, params) => {
return new Promise((resolve, reject) => {
Expand Down
60 changes: 39 additions & 21 deletions packages/taro-rn/src/api/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,45 @@ function request (options) {
params.credentials = options.credentials
params.cache = options.cache
params.method = method
return fetch(url, params)
.then(response => {
res.statusCode = response.status
res.header = response.headers
if (options.dataType === 'json') {
return response.json()
}
if (options.responseType === 'arraybuffer') {
return response.arrayBuffer()
}
if (options.responseType === 'text') {
return response.text()
}
if (typeof options.dataType === 'undefined') {
return response.json()
}
return Promise.resolve(null)
}).then(data => {
res.data = data
return res
})
const originSuccess = options.success
const originFail = options.fail
const originComplete = options.complete
let completeRes
const p = new Promise((resolve, reject) => {
fetch(url, params)
.then(response => {
res.statusCode = response.status
res.header = response.headers
if (options.dataType === 'json') {
return response.json()
}
if (options.responseType === 'arraybuffer') {
return response.arrayBuffer()
}
if (options.responseType === 'text') {
return response.text()
}
if (typeof options.dataType === 'undefined') {
return response.json()
}
return Promise.resolve(null)
})
.then(resData => {
res.data = resData
completeRes = Object.assign({}, res)
originSuccess && originSuccess(res)
resolve(res)
})
.catch(error => {
completeRes = Object.assign({}, error)
originFail && originFail(error)
reject(error)
})
.finally(() => {
originComplete && originComplete(completeRes)
})
})
return p
}

export default {
Expand Down