Skip to content

Commit

Permalink
fix(core): close #33, do not pass api's params to axios (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve authored Apr 22, 2019
1 parent a70c67d commit 17825c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tua-api",
"version": "1.2.0",
"version": "1.2.1",
"description": "🏗 A common tool helps converting configs to api functions",
"main": "dist/TuaApi.cjs.js",
"module": "dist/TuaApi.esm.js",
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,14 @@ class TuaApi {
data: method === 'GET' ? {} : data,
method,
...axiosOptions,
...rest,
}

return getAxiosPromise(params)
}

// 对于 post 请求使用 axios
return method === 'POST'
? getAxiosPromise({ url, data, ...axiosOptions, ...rest })
? getAxiosPromise({ url, data, ...axiosOptions })
: getFetchJsonpPromise({
url: fullUrl,
jsonpOptions: { ...jsonpOptions, callbackName },
Expand Down
14 changes: 14 additions & 0 deletions test/__tests__/axios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const params = {

const reqAPUrl = `http://example-base.com/fake-post/array-params`
const reqOPUrl = `http://example-base.com/fake-post/object-params`
const reqGOPUrl = `http://example-base.com/fake-get/object-params`
const reqOHUrl = `http://example-test.com/fake-post/own-host`
const reqTAUrl = `http://example-base.com/fake-get/req-type-axios?asyncCp=asyncCp`
const reqEAPUrl = `http://example-base.com/fake-post/empty-array-params`
Expand Down Expand Up @@ -56,6 +57,10 @@ describe('error handling', () => {
})

describe('fake get requests', () => {
beforeEach(() => {
mock.resetHistory()
})

test('req-type-axios', async () => {
const data = { code: 0, data: 'req-type-axios' }
mock.onGet(reqTAUrl).reply(200, data)
Expand All @@ -75,6 +80,15 @@ describe('fake get requests', () => {

expect(resData).toEqual(data)
})

test('required param', async () => {
const data = [ 0, 'array data' ]
mock.onGet(reqGOPUrl + `?param1=1217&param2=steve&param3=young`).reply(200, data)
const resData = await fakeGetApi.op({ param3: 'young' }, { reqType: 'axios' })

expect(mock.history.get[0].params).toBe(undefined)
expect(resData).toEqual({ code: 0, data: 'array data' })
})
})

describe('fake post requests', () => {
Expand Down

0 comments on commit 17825c7

Please sign in to comment.