Skip to content

Commit

Permalink
fix: close #6, console log version
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed Oct 22, 2018
1 parent 1e51d4a commit 70eba9e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/quick-start/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ storage

* host: `https://example-base.com/foo/bar/`
* prefix: `something`
* pathList: `[ 'create', 'modify', 'delete ]`
* pathList: `[ 'create', 'modify', 'delete' ]`

### 文件结构
`api/` 一般是这样的文件结构:
Expand Down
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": "0.2.0",
"version": "0.2.1",
"main": "dist/umd.js",
"module": "dist/es.js",
"jsnext:main": "dist/es.js",
Expand Down
8 changes: 4 additions & 4 deletions rollup.web.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default {
name: 'TuaApi',
format: 'umd',
exports: 'named',
globals: {
axios: 'axios',
'fetch-jsonp': 'fetchJsonp',
},
}, {
file: 'dist/es.js',
format: 'es',
Expand All @@ -28,9 +32,5 @@ export default {
'process.env.NODE_ENV': JSON.stringify('prod'),
}),
],
globals: {
axios: 'axios',
'fetch-jsonp': 'fetchJsonp',
},
external: ['axios', 'fetch-jsonp'],
}
4 changes: 4 additions & 0 deletions src/TuaApiCore.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import koaCompose from 'koa-compose'

import { version } from '../package.json'
import {
map,
log,
pipe,
error,
mergeAll,
Expand All @@ -17,6 +19,8 @@ import {
formatReqParamsMiddleware,
} from './middlewareFns'

log(`Version: ${version}`)

class TuaApiCore {
/**
* @param {String} host 服务器基础地址,例如 https://example.com/
Expand Down
17 changes: 4 additions & 13 deletions src/TuaApiMp.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import TuaApiCore from './TuaApiCore'
import { log, promisifyWxApi } from './utils'
import {
MIN_LOADING_TIME,
WX_VALID_METHODS,
} from './constants'
import { WX_VALID_METHODS } from './constants'

class TuaApi extends TuaApiCore {
constructor (options) {
Expand All @@ -21,8 +18,7 @@ class TuaApi extends TuaApiCore {
reqParams: data,
isShowLoading = true,
showLoadingFn = () => wx.showLoading({ title: '加载中' }),
hideLoadingFn = wx.hideLoading,
minLoadingTime = MIN_LOADING_TIME,
hideLoadingFn = wx.hideLoading.bind(wx),
...rest
}) {
method = method.toUpperCase()
Expand All @@ -31,8 +27,6 @@ class TuaApi extends TuaApiCore {
? log(`Req Url: ${fullUrl}`)
: log(`Req Url: ${url} \nReq Data:`, data)

const lastLoadingTime = Date.now()

// 展示 loading
isShowLoading && showLoadingFn()

Expand All @@ -47,11 +41,8 @@ class TuaApi extends TuaApiCore {
header,
method,
complete: () => {
if (!isShowLoading) return

// 设置最小 loading 时间优化体验
const loadingTime = minLoadingTime - (Date.now() - lastLoadingTime)
setTimeout(hideLoadingFn, loadingTime)
// 同步隐藏 loading
isShowLoading && hideLoadingFn()
},
})
}
Expand Down
4 changes: 0 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// 支持的请求类型
const VALID_REQ_TYPES = ['axios', 'jsonp']

// 最小 loading 显示时间
const MIN_LOADING_TIME = 500

// 小程序中合法的请求方法
const WX_VALID_METHODS = ['OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE', 'CONNECT']

Expand All @@ -12,6 +9,5 @@ const DEFAULT_HEADER = { 'Content-Type': 'application/x-www-form-urlencoded' }
export {
DEFAULT_HEADER,
VALID_REQ_TYPES,
MIN_LOADING_TIME,
WX_VALID_METHODS,
}

0 comments on commit 70eba9e

Please sign in to comment.