Skip to content

Commit b92a06a

Browse files
committed
feat: support function in params and commonParms
1 parent 20bc1f4 commit b92a06a

File tree

13 files changed

+122
-21
lines changed

13 files changed

+122
-21
lines changed

docs/config/common.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ export default {
9191
}
9292
```
9393

94+
::: tip
95+
<badge text="1.7.0+" /> 后,支持函数模式
96+
97+
```js
98+
export default {
99+
commonParams: (params) => ({ t: Date.now(), foo: params.foo }),
100+
}
101+
```
102+
:::
103+
94104
## axiosOptions 透传参数配置
95105
由于 tua-api 是依赖于 `axios` 或是 `fetch-jsop` 来发送请求的。所以势必要提供参数透传的功能。
96106

docs/config/self.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ export default {
6363
}
6464
```
6565

66+
::: tip
67+
<badge text="1.7.0+" /> 后,支持函数模式
68+
69+
```js
70+
export default {
71+
pathList: [
72+
{
73+
...
74+
params: (params) => ({
75+
t: Date.now(),
76+
foo: params.foo,
77+
}),
78+
},
79+
],
80+
}
81+
```
82+
:::
83+
6684
## commonParams 覆盖公共参数
6785
有时某个接口正好不需要上一级中 `commonParams` 的参数。那么可以传递 `null` 覆盖上一级中的 `commonParams`
6886

examples/apis-web/fake-fn.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default {
2+
// 该参数表示请求的公用服务器地址。
3+
baseUrl: 'http://example-base.com/',
4+
5+
// 请求的中间路径,建议与文件同名,以便后期维护。
6+
prefix: 'fake-fn',
7+
8+
// 所有请求都需要携带的参数
9+
commonParams: (args) => ({
10+
...args,
11+
c: Math.random(),
12+
}),
13+
14+
reqType: 'axios',
15+
16+
// 接口地址数组
17+
pathList: [
18+
/**
19+
* fn-params
20+
*/
21+
{
22+
name: 'fp',
23+
path: 'fn-params',
24+
method: 'post',
25+
params: ({ param1, param2 }) => ({
26+
t: Math.random(),
27+
p1: param1,
28+
p2: param2,
29+
}),
30+
},
31+
],
32+
}

examples/apis-web/fake-post.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export default {
1919
/**
2020
* empty-array-params
2121
*/
22-
{
23-
name: 'eap',
24-
path: 'empty-array-params',
25-
},
22+
{ name: 'eap', path: 'empty-array-params' },
2623
/**
2724
* array-params
2825
*/
@@ -84,10 +81,7 @@ export default {
8481
/**
8582
* application/json
8683
*/
87-
{
88-
name: 'pj',
89-
path: 'post-json',
90-
},
84+
{ name: 'pj', path: 'post-json' },
9185
/**
9286
* raw-data
9387
*/

examples/apis-web/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ export const fakePostApi: {
5858
): Promise<T>
5959
}
6060
}
61+
62+
export const fakeFnApi: {
63+
'fp': ReqFn & {
64+
<T = Result>(
65+
params: { t?: any },
66+
options?: RuntimeOptions
67+
): Promise<T>
68+
}
69+
}

examples/apis-web/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ tuaApi.use(async (ctx, next) => {
1717
// console.log('after: ', ctx)
1818
})
1919

20+
export const fakeFnApi = tuaApi.getApi(require('./fake-fn').default)
2021
export const fakeGetApi = tuaApi.getApi(require('./fake-get').default)
2122
export const fakePostApi = tuaApi.getApi(require('./fake-post').default)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tua-api",
3-
"version": "1.6.1",
3+
"version": "1.7.0",
44
"description": "🏗 A common tool helps converting configs to api functions",
55
"main": "dist/TuaApi.cjs.js",
66
"module": "dist/TuaApi.esm.js",

src/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type Mock = AnyFunction | any
1212

1313
export type ApiConfig = WxApiConfig | WebApiConfig
1414

15-
export type ParamsConfig = string[] | ParamsObject
15+
export type ParamsConfig = string[] | ParamsObject | ((args?: object) => object)
1616

1717
export type Middleware<T> = (ctx: T, next: () => Promise<any>) => Promise<any>
1818

@@ -96,7 +96,7 @@ export interface BaseApiConfig {
9696
beforeFn?: <T = any>() => Promise<T>
9797
middleware?: Middleware<Ctx>[]
9898
customFetch?: AnyPromiseFunction
99-
commonParams?: object
99+
commonParams?: object | ((args?: object) => object),
100100
axiosOptions?: AxiosOptions
101101
jsonpOptions?: JsonpOptions
102102
useGlobalMiddleware?: boolean

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
map,
77
pipe,
88
isWx,
9+
runFn,
910
logger,
1011
mergeAll,
1112
apiConfigToReqFnParams,
@@ -145,9 +146,7 @@ class TuaApi {
145146

146147
// mock data
147148
if (mock) {
148-
const resData = typeof mock === 'function'
149-
? mock(data)
150-
: { ...mock }
149+
const resData = { ...runFn(mock, data) }
151150

152151
return Promise.resolve({ data: resData })
153152
}
@@ -255,7 +254,7 @@ class TuaApi {
255254
mock,
256255
name,
257256
path,
258-
params = {},
257+
params: rawParams = {},
259258
prefix,
260259
afterFn = ([x]) => x,
261260
beforeFn = Promise.resolve.bind(Promise),
@@ -308,6 +307,8 @@ class TuaApi {
308307
const apiFn = (args, runtimeOptions = {}) => {
309308
args = args || {}
310309

310+
const params = runFn(rawParams, args)
311+
311312
// 最终的运行时配置,runtimeOptions 有最高优先级
312313
const runtimeParams = {
313314
type,
@@ -361,7 +362,7 @@ class TuaApi {
361362

362363
apiFn.key = `${prefix}/${apiName}`
363364
apiFn.mock = mock
364-
apiFn.params = params
365+
apiFn.params = rawParams
365366

366367
return { [apiName]: apiFn }
367368
}

src/middlewareFns.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ERROR_STRINGS } from './constants'
22
import {
3+
runFn,
34
isFormData,
45
combineUrls,
56
checkArrayParams,
@@ -59,7 +60,7 @@ const formatReqParamsMiddleware = (ctx, next) => {
5960
const {
6061
args,
6162
params,
62-
commonParams,
63+
commonParams: rawCommonParams,
6364
} = ctx.req
6465

6566
if (typeof args !== 'object') {
@@ -74,10 +75,11 @@ const formatReqParamsMiddleware = (ctx, next) => {
7475

7576
checkArrayParams(ctx.req)
7677

78+
const commonParams = runFn(rawCommonParams, args)
7779
// 根据配置生成请求的参数
7880
ctx.req.reqParams = Array.isArray(params)
7981
? { ...commonParams, ...args }
80-
: { ...getDefaultParamObj(ctx.req), ...args }
82+
: { ...getDefaultParamObj({ ...ctx.req, commonParams }), ...args }
8183

8284
return next()
8385
}

0 commit comments

Comments
 (0)