Skip to content

Commit

Permalink
refactor: type -> method, upper case to lower case (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve authored Sep 11, 2019
1 parent 18d243d commit 21db34b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
7 changes: 5 additions & 2 deletions docs/config/common.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# 公共配置
详细地址指的是填写在 `src/apis/foobar.js` 中的一级配置。这部分的配置优先级比默认配置高,但低于各个接口的自身配置。

## type 请求类型
## type 请求类型 <badge text="2.0.0-" />
重命名为 `method``type` 属性将在 `2.0.0+` 后废弃。

## method 请求类型 <badge text="1.3.5+" />
所有请求类型(可忽略大小写,可选值 OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT)

```js
export default {
// 忽略大小写
type: 'post',
method: 'post',
}
```

Expand Down
3 changes: 2 additions & 1 deletion docs/guide/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ async function (ctx, next) {
| req | 请求 |
| req.host | 服务器地址 |
| req.mock | 模拟的响应数据或是生成数据的函数 |
| req.type | 接口请求类型 get/post... |
| req.type <badge text="2.0.0-" /> | 接口请求类型 get/post... |
| req.method <badge text="1.3.5+" /> | 接口请求类型 get/post... |
| req.path | 接口结尾路径 |
| req.prefix | 接口前缀 |
| req.reqType | 使用什么工具发(axios/jsonp/wx) |
Expand Down
2 changes: 1 addition & 1 deletion examples/apis-web/fake-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
prefix: 'fake-post',

/** @type { import('../../src/').Method } */
type: ('post'),
method: ('post'),

// 所有请求都需要携带的参数
commonParams: { common: 'params' },
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": "1.3.4",
"version": "1.3.5",
"description": "🏗 A common tool helps converting configs to api functions",
"main": "dist/TuaApi.cjs.js",
"module": "dist/TuaApi.esm.js",
Expand Down
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export interface ParamsObject {
export interface CtxReq {
host: string
mock: Mock
// deprecated
type: Method
method: Method
path: string
prefix: string
reqType: ReqType
Expand Down Expand Up @@ -77,7 +79,9 @@ export interface Ctx {
export interface BaseApiConfig {
mock?: Mock
host?: string
// deprecated
type?: Method
method?: Method
prefix?: string
reqType?: ReqType
afterFn?: <T = any, U = any>(args: [U?, Ctx?]) => Promise<T>
Expand Down
38 changes: 25 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class TuaApi {
* @param {object} options
* @param {Object|Function} options.mock 模拟的响应数据或是生成数据的函数
* @param {string} options.url 接口地址
* @param {string} options.type 接口请求类型 get/post...
* @param {string} options.method 接口请求类型 get/post...
* @param {string} options.fullUrl 完整接口地址
* @param {string} options.reqType 使用什么工具发(axios/jsonp/wx)
* @param {object} options.reqParams 请求参数
Expand All @@ -107,7 +107,7 @@ class TuaApi {
_reqFn ({
url,
mock,
type,
method,
fullUrl,
reqType,
reqParams: data,
Expand All @@ -128,16 +128,16 @@ class TuaApi {
return Promise.resolve({ data: resData })
}

const method = type.toUpperCase()
method = method.toLowerCase()

if (reqType === 'wx') {
return getWxPromise({ url, fullUrl, data, method, ...rest })
}

if (reqType === 'axios') {
const params = {
url: method === 'GET' ? fullUrl : url,
data: method === 'GET' ? {} : data,
url: method === 'get' ? fullUrl : url,
data: method === 'get' ? {} : data,
method,
...axiosOptions,
}
Expand All @@ -146,7 +146,7 @@ class TuaApi {
}

// 对于 post 请求使用 axios
return method === 'POST'
return method === 'post'
? getAxiosPromise({ url, data, ...axiosOptions })
: getFetchJsonpPromise({
url: fullUrl,
Expand Down Expand Up @@ -203,6 +203,7 @@ class TuaApi {
* 接受 api 对象,返回待接收参数的单个 api 函数的对象
* @param {object} options
* @param {string} options.type 接口请求类型 get/post...
* @param {string} options.method 接口请求类型 get/post...
* @param {Object|Function} options.mock 模拟的响应数据或是生成数据的函数
* @param {string} options.name 自定义的接口名称
* @param {string} options.path 接口结尾路径
Expand All @@ -219,7 +220,8 @@ class TuaApi {
* @return {object} 以 apiName 为 key,请求函数为值的对象
*/
_getOneReqMap ({
type = 'get',
type,
method = type,
mock,
name,
path,
Expand All @@ -231,8 +233,19 @@ class TuaApi {
useGlobalMiddleware = true,
...rest
}) {
if (type) {
logger.warn(
`[type] will be deprecated, please use [method] instead!\n` +
`[type] 属性将被废弃, 请用 [method] 替代!`
)
}

// 优先使用 name
const apiName = name || path
// 默认值
method = method || 'get'
// 向前兼容
type = method

// 合并全局默认值
rest.host = rest.host || this.host
Expand All @@ -250,16 +263,15 @@ class TuaApi {
* @param {object} runtimeOptions 运行时配置
* @return {Promise}
*/
const apiFn = (
args = {},
runtimeOptions = {}
) => {
// args 可能为 null
args = args === null ? {} : args
const apiFn = (args, runtimeOptions = {}) => {
args = args || {}

// 最终的运行时配置,runtimeOptions 有最高优先级
const runtimeParams = { type, path, params, prefix, apiName, fullPath: `${prefix}/${path}`, ...rest, ...runtimeOptions }

// 向前兼容
runtimeParams.method = runtimeParams.type

// 自定义回调函数名称(用于 jsonp)
runtimeParams.callbackName = runtimeParams.callbackName || `${runtimeParams.path}Callback`

Expand Down

0 comments on commit 21db34b

Please sign in to comment.