Skip to content

Commit

Permalink
Fix/afterFn data (#15)
Browse files Browse the repository at this point in the history
* fix: close #14, user should receive data through afterFn

* chore: add TuaApi prefix to dist's files, add TuaApi.umd.min.js

* perf: print req data only when it has data
  • Loading branch information
BuptStEve authored Dec 3, 2018
1 parent 9e4a036 commit 2ede19d
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 128 deletions.
6 changes: 4 additions & 2 deletions docs/config/detail.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ export default {
* 第一个参数是接口返回数据对象 `{ code, data, msg }`
* 第二个参数是请求相关参数的对象,例如有请求的 host、type、params、fullPath、reqTime、startTime、endTime 等等

默认值是
默认值如下,即返回接口数据。

```js
const afterFn = ([x]) => x
```

::: warning
注意记得将 `res` 返回,不然业务侧就收不到请求接口的数据了!
注意若是返回了数据,则业务侧将收到这个数据。所以在这里可以添加一些通用逻辑,处理返回的数据。

反之,若是返回 `undefined` 则业务侧将收到 `res.data`
:::

## isShowLoading (小程序 only)
Expand Down
9 changes: 8 additions & 1 deletion examples/apis-mp/fake-wx.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ export default {
* array-data
*/
{
path: 'array-data',
name: 'arrayData',
path: 'array-data',
type: 'get',
params: ['param1', 'param2'],
},
/**
* object-data
*/
{
name: 'objectData',
path: 'object-data',
params: {
param1: 1217,
Expand All @@ -56,12 +57,15 @@ export default {
* no-beforeFn
*/
{
name: 'noBeforeFn',
path: 'no-beforeFn',
commonParams: null,
},
/**
* hide-loading
*/
{
name: 'hideLoading',
path: 'hide-loading',
// 这个接口不需要展示 loading
isShowLoading: false,
Expand All @@ -70,6 +74,7 @@ export default {
* type-get
*/
{
name: 'typeGet',
path: 'type-get',
// 这个接口单独配置类型
type: 'get',
Expand All @@ -78,6 +83,7 @@ export default {
* unknown-type
*/
{
name: 'unknownType',
path: 'unknown-type',
// 这个接口单独配置类型
type: 'foo',
Expand All @@ -86,6 +92,7 @@ export default {
* nav-loading
*/
{
name: 'navLoading',
path: 'nav-loading',
showLoadingFn: wx.showNavigationBarLoading,
hideLoadingFn: wx.hideNavigationBarLoading,
Expand Down
21 changes: 21 additions & 0 deletions examples/apis-web/fake-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default {
* array-params
*/
{
name: 'ap',
path: 'array-params',
params: ['param1', 'param2'],
// 在这里定义将覆盖公共中间件
Expand All @@ -51,6 +52,7 @@ export default {
* object-params
*/
{
name: 'op',
path: 'object-params',
reqType: '',
params: {
Expand All @@ -64,6 +66,7 @@ export default {
* async-common-params
*/
{
name: 'acp',
path: 'async-common-params',
params: [],
// 在这里返回的 params 会和请求的 params 合并
Expand All @@ -75,6 +78,7 @@ export default {
* req-type-axios
*/
{
name: 'rta',
path: 'req-type-axios',
// 用哪个包发起请求目前支持:jsonp、axios
// 如果不指定默认对于 get 请求使用 fetch-jsonp,post 请求使用 axios
Expand All @@ -88,8 +92,25 @@ export default {
* invalid-req-type
*/
{
name: 'irt',
path: 'invalid-req-type',
reqType: 'foobar',
},
/**
* afterFn-data
*/
{
name: 'afterData',
path: 'afterFn-data',
afterFn: ([data]) => ({ ...data, afterData: 'afterData' }),
},
/**
* no-afterFn-data
*/
{
name: 'noAfterData',
path: 'no-afterFn-data',
afterFn: () => {},
},
],
}
4 changes: 4 additions & 0 deletions examples/apis-web/fake-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export default {
* empty-array-params
*/
{
name: 'eap',
path: 'empty-array-params',
type: 'post',
},
/**
* array-params
*/
{
name: 'ap',
path: 'array-params',
type: 'post',
reqType: 'axios',
Expand All @@ -33,6 +35,7 @@ export default {
* object-params
*/
{
name: 'op',
path: 'object-params',
type: 'post',
params: {
Expand All @@ -45,6 +48,7 @@ export default {
* own-host
*/
{
name: 'oh',
path: 'own-host',
type: 'post',
host: 'http://example-test.com/',
Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "tua-api",
"version": "0.3.2",
"main": "dist/umd.js",
"module": "dist/es.js",
"jsnext:main": "dist/es.js",
"version": "0.3.3",
"main": "dist/TuaApi.umd.js",
"module": "dist/TuaApi.es.js",
"jsnext:main": "dist/TuaApi.es.js",
"description": "A common tool helps converting configs to api functions",
"scripts": {
"docs": "vuepress dev docs -h localhost",
Expand Down Expand Up @@ -67,18 +67,19 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"gh-pages": "^2.0.1",
"husky": "^1.1.4",
"husky": "^1.2.0",
"jest": "^23.6.0",
"lint-staged": "^8.0.4",
"lint-staged": "^8.1.0",
"rimraf": "^2.6.2",
"rollup": "^0.67.1",
"rollup": "^0.67.4",
"rollup-plugin-babel": "^3.0.7",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-eslint": "^5.0.0",
"rollup-plugin-json": "^3.1.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-replace": "^2.1.0",
"vuepress": "^0.14.4"
"rollup-plugin-uglify": "^6.0.0",
"vuepress": "^0.14.8"
},
"dependencies": {
"axios": "^0.18.0",
Expand Down
54 changes: 35 additions & 19 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,48 @@ import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import commonjs from 'rollup-plugin-commonjs'
import { eslint } from 'rollup-plugin-eslint'
import { uglify } from 'rollup-plugin-uglify'
import nodeResolve from 'rollup-plugin-node-resolve'

export default {
input: 'src/TuaApi.js',
output: [{
file: 'dist/umd.js',
const output = {
es: {
file: 'dist/TuaApi.es.js',
format: 'es',
},
umd: {
file: 'dist/TuaApi.umd.js',
name: 'TuaApi',
format: 'umd',
exports: 'named',
globals: {
axios: 'axios',
'fetch-jsonp': 'fetchJsonp',
},
}, {
file: 'dist/es.js',
format: 'es',
}],
plugins: [
eslint(),
json(),
nodeResolve(),
babel(),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify('prod'),
}),
],
external: ['axios', 'fetch-jsonp'],
},
}
const plugins = [
eslint(),
json(),
nodeResolve(),
babel(),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify('prod'),
}),
]
const external = ['axios', 'fetch-jsonp']

export default [{
input: 'src/TuaApi.js',
output: [ output.es, output.umd ],
plugins,
external,
}, {
input: 'src/TuaApi.js',
output: {
...output.umd,
file: 'dist/TuaApi.umd.min.js',
},
plugins: [ ...plugins, uglify() ],
external,
}]
8 changes: 4 additions & 4 deletions src/TuaApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class TuaApi {
if (reqType === 'axios') {
const params = {
url: method === 'GET' ? fullUrl : url,
data,
data: method === 'GET' ? {} : data,
method,
...axiosOptions,
...rest,
Expand All @@ -132,7 +132,7 @@ class TuaApi {

// 对于 post 请求使用 axios
return method === 'POST'
? getAxiosPromise({ url, data, ...axiosOptions })
? getAxiosPromise({ url, data, ...axiosOptions, ...rest })
: getFetchJsonpPromise({
url: fullUrl,
jsonpOptions: { ...jsonpOptions, callbackName },
Expand Down Expand Up @@ -274,9 +274,9 @@ class TuaApi {
// 请求执行完成后的钩子
.then(() => afterFn([ctx.res.data, ctx]))
// 抛出错误或响应数据
.then(() => ctx.res.error
.then((data) => ctx.res.error
? Promise.reject(ctx.res.error)
: ctx.res.data
: data || ctx.res.data
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/adapters/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const getAxiosPromise = ({
...rest
}) => {
logger.log(`Req Url: ${url}`)
logger.log(`Req Data:`, data)
if (data && Object.keys(data).length) {
logger.log(`Req Data:`, data)
}

return require('axios')({
url,
Expand Down
11 changes: 8 additions & 3 deletions src/adapters/wx.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ export const getWxPromise = ({
}) => {
method = method.toUpperCase()

method === 'GET'
? logger.log(`Req Url: ${fullUrl}`)
: logger.log(`Req Url: ${url} \nReq Data:`, data)
if (method === 'GET') {
logger.log(`Req Url: ${fullUrl}`)
} else {
logger.log(`Req Url: ${url}`)
if (data && Object.keys(data).length) {
logger.log(`Req Data:`, data)
}
}

// 展示 loading
isShowLoading && showLoadingFn()
Expand Down
Loading

0 comments on commit 2ede19d

Please sign in to comment.