Skip to content

Commit

Permalink
fix: close #13, add axios/fetch-jsonp as dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed Nov 14, 2018
1 parent f2689c9 commit c909273
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 92 deletions.
24 changes: 6 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,14 @@ $ yarn add tua-api
import TuaApi from 'tua-api'
```

#### 安装武器
安装“武器”分为两种情况:
#### 配置武器
配置“武器”分为两种情况:

* [已配置 CORS 跨域请求头](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS),或是没有跨域需求时,那么再装一个 `axios` 即可
* [已配置 CORS 跨域请求头](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS),或是没有跨域需求时,无需任何操作(默认采用的就是 `axios`)。

```bash
$ npm i -S axios
# OR
$ yarn add axios
```

* 若是用不了 CORS,那么就需要装 `fetch-jsonp` 借助 jsonp 实现跨域

```bash
$ npm i -S fetch-jsonp
# OR
$ yarn add fetch-jsonp
```
* 若是用不了 CORS,那么就需要设置 `reqType: 'jsonp'` 借助 jsonp 实现跨域

但是 jsonp 只支持使用 get 的方式请求,所以如果需要发送 post 或其他方式的请求,还是需要装 `axios`
但是 jsonp 只支持使用 get 的方式请求,所以如果需要发送 post 或其他方式的请求,还是需要使用 `axios`(服务端还是需要配置 CORS)

### 小程序端
#### 安装本体即可
Expand All @@ -73,7 +61,7 @@ $ yarn add tua-api
```js
import TuaApi from 'tua-api'

const tuaApi = new TuaApi({ reqType: 'wx' })
const tuaApi = new TuaApi()
```

> 小程序还用不了 npm?[@tua-mp/service](https://tuateam.github.io/tua-mp/tua-mp-service/) 了解一下?
Expand Down
22 changes: 5 additions & 17 deletions docs/quick-start/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,14 @@ $ yarn add tua-api
import TuaApi from 'tua-api'
```

### 安装武器
安装“武器”分为两种情况:
#### 配置武器
配置“武器”分为两种情况:

* [已配置 CORS 跨域请求头](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS),或是没有跨域需求时,那么再装一个 `axios` 即可
* [已配置 CORS 跨域请求头](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS),或是没有跨域需求时,无需任何操作(默认采用的就是 `axios`)。

```bash
$ npm i -S axios
# OR
$ yarn add axios
```

* 若是用不了 CORS,那么就需要装 `fetch-jsonp` 借助 jsonp 实现跨域

```bash
$ npm i -S fetch-jsonp
# OR
$ yarn add fetch-jsonp
```
* 若是用不了 CORS,那么就需要设置 `reqType: 'jsonp'` 借助 jsonp 实现跨域

但是 jsonp 只支持使用 get 的方式请求,所以如果需要发送 post 或其他方式的请求,还是需要装 `axios`
但是 jsonp 只支持使用 get 的方式请求,所以如果需要发送 post 或其他方式的请求,还是需要使用 `axios`(服务端还是需要配置 CORS)

::: tip
不推荐使用 jsonp 的方式,有以下几个原因:
Expand Down
4 changes: 2 additions & 2 deletions examples/apis-mp/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TuaApi from '../../src/TuaApi'

const tuaApi = new TuaApi({ reqType: 'wx' })
const tuaApi = new TuaApi()

// 使用中间件
tuaApi.use(async (ctx, next) => {
Expand All @@ -13,4 +13,4 @@ tuaApi.use(async (ctx, next) => {
// console.log('after: ', ctx)
})

export const fakeWx = tuaApi.getApi(require('./fake-wx').default)
export const fakeWxApi = tuaApi.getApi(require('./fake-wx').default)
4 changes: 2 additions & 2 deletions examples/apis-web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ tuaApi.use(async (ctx, next) => {
// console.log('after: ', ctx)
})

export const fakeGet = tuaApi.getApi(require('./fake-get').default)
export const fakePost = tuaApi.getApi(require('./fake-post').default)
export const fakeGetApi = tuaApi.getApi(require('./fake-get').default)
export const fakePostApi = tuaApi.getApi(require('./fake-post').default)
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
"collectCoverage": true,
"collectCoverageFrom": [
"src/**"
],
"setupFiles": [
"./test/__mocks__/wxMock.js"
]
},
"eslintIgnore": [
Expand All @@ -53,7 +50,6 @@
"devDependencies": {
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"axios": "^0.18.0",
"axios-mock-adapter": "^1.15.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.1",
Expand All @@ -70,7 +66,6 @@
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"fetch-jsonp": "^1.1.3",
"gh-pages": "^2.0.1",
"husky": "^1.1.4",
"jest": "^23.6.0",
Expand All @@ -86,6 +81,8 @@
"vuepress": "^0.14.4"
},
"dependencies": {
"axios": "^0.18.0",
"fetch-jsonp": "^1.1.3",
"koa-compose": "^4.1.0"
}
}
4 changes: 2 additions & 2 deletions src/TuaApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { version } from '../package.json'
import {
map,
pipe,
isWx,
logger,
mergeAll,
apiConfigToReqFnParams,
Expand All @@ -25,7 +26,6 @@ import {
} from './middlewareFns'

logger.log(`Version: ${version}`)

class TuaApi {
/**
* @param {Object} options
Expand All @@ -38,7 +38,7 @@ class TuaApi {
*/
constructor ({
host,
reqType = 'axios',
reqType = isWx() ? 'wx' : 'axios',
middleware = [],
axiosOptions = {},
jsonpOptions = {},
Expand Down
3 changes: 3 additions & 0 deletions src/utils/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isWx = () =>
typeof wx !== 'undefined' &&
typeof wx.request === 'function'
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './fp'
export * from './mp'
export * from './env'
export * from './logger'
export * from './params'
12 changes: 6 additions & 6 deletions test/__tests__/axios.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

import { fakeGet, fakePost } from '../../examples/apis-web/'
import { fakeGetApi, fakePostApi } from '../../examples/apis-web/'

const mock = new MockAdapter(axios)

const params = {
param1: 'steve',
param2: 'young',
}
const reqOH = fakePost['own-host']
const reqAP = fakePost['array-params']
const reqOP = fakePost['object-params']
const reqTA = fakeGet['req-type-axios']
const reqEAP = fakePost['empty-array-params']
const reqOH = fakePostApi['own-host']
const reqAP = fakePostApi['array-params']
const reqOP = fakePostApi['object-params']
const reqTA = fakeGetApi['req-type-axios']
const reqEAP = fakePostApi['empty-array-params']

const reqAPUrl = `http://example-base.com/fake-post/array-params`
const reqOPUrl = `http://example-base.com/fake-post/object-params`
Expand Down
25 changes: 0 additions & 25 deletions test/__tests__/core.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fakeGet from '../../examples/apis-mp/fake-wx'
import TuaApi from '../../src/TuaApi'

describe('error handling', () => {
Expand All @@ -12,27 +11,3 @@ describe('error handling', () => {
expect(() => new TuaApi({ reqType: '' })).toThrow(TypeError(`invalid reqType`))
})
})

describe('middleware', () => {
const tuaApi = new TuaApi({ reqType: 'wx' })
const globalMiddlewareFn = jest.fn(async (ctx, next) => {
await next()

expect(ctx.endTime).toBeDefined()
expect(ctx.reqTime).toBeDefined()
})
tuaApi.use(globalMiddlewareFn)

const fakeGetApi = tuaApi.getApi(fakeGet)

beforeEach(() => {
wx.__TEST_DATA__ = { testData: {} }
})

test('useGlobalMiddleware', async () => {
await fakeGetApi.arrayData()
expect(globalMiddlewareFn).toBeCalledTimes(0)
await fakeGetApi.fail()
expect(globalMiddlewareFn).toBeCalledTimes(1)
})
})
10 changes: 5 additions & 5 deletions test/__tests__/jsonp.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fetchJsonp from 'fetch-jsonp'

import { fakeGet } from '../../examples/apis-web/'
import { fakeGetApi } from '../../examples/apis-web/'

jest.mock('fetch-jsonp')

const reqAP = fakeGet['array-params']
const reqOP = fakeGet['object-params']
const reqACP = fakeGet['async-common-params']
const reqIRT = fakeGet['invalid-req-type']
const reqAP = fakeGetApi['array-params']
const reqOP = fakeGetApi['object-params']
const reqACP = fakeGetApi['async-common-params']
const reqIRT = fakeGetApi['invalid-req-type']

const returnVal = { code: 0, data: 'array data' }

Expand Down
47 changes: 37 additions & 10 deletions test/__tests__/wx.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { fakeWx } from '../../examples/apis-mp/'

const reqF = fakeWx['fail']
const reqAD = fakeWx.arrayData
const reqOD = fakeWx['object-data']
const reqNB = fakeWx['no-beforeFn']
const reqHL = fakeWx['hide-loading']
const reqTG = fakeWx['type-get']
const reqUT = fakeWx['unknown-type']
const reqNL = fakeWx['nav-loading']
import '../__mocks__/wxMock'
import TuaApi from '../../src/TuaApi'
import fakeWx from '../../examples/apis-mp/fake-wx'
import { fakeWxApi } from '../../examples/apis-mp/'

const reqF = fakeWxApi['fail']
const reqAD = fakeWxApi.arrayData
const reqOD = fakeWxApi['object-data']
const reqNB = fakeWxApi['no-beforeFn']
const reqHL = fakeWxApi['hide-loading']
const reqTG = fakeWxApi['type-get']
const reqUT = fakeWxApi['unknown-type']
const reqNL = fakeWxApi['nav-loading']

const testObjData = { code: 0, data: 'object data' }
const testArrData = [ 0, 'array data' ]

describe('middleware', () => {
const tuaApi = new TuaApi()
const fakeWxApi = tuaApi.getApi(fakeWx)
const globalMiddlewareFn = jest.fn(async (ctx, next) => {
await next()

expect(ctx.endTime).toBeDefined()
expect(ctx.reqTime).toBeDefined()
})

tuaApi.use(globalMiddlewareFn)

beforeEach(() => {
wx.__TEST_DATA__ = { testData: {} }
})

test('useGlobalMiddleware', async () => {
await fakeWxApi.arrayData()
expect(globalMiddlewareFn).toBeCalledTimes(0)
await fakeWxApi.fail()
expect(globalMiddlewareFn).toBeCalledTimes(1)
})
})

describe('fake wx requests', () => {
beforeEach(() => {
wx.__TEST_DATA__ = {}
Expand Down

0 comments on commit c909273

Please sign in to comment.