Skip to content

Commit

Permalink
Merge pull request #1 from tuateam/fix/header
Browse files Browse the repository at this point in the history
fix: support miniprogram passing header by beforeFn
  • Loading branch information
BuptStEve authored Oct 1, 2018
2 parents e0ecead + 9b6a393 commit 68acf66
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ src
test
*.log
coverage
.circleci
3 changes: 3 additions & 0 deletions examples/apis-mp/fake-wx.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default {
*/
{
path: 'fail',
beforeFn: () => Promise.resolve({
header: { cookie: '123' },
}),
},
/**
* array-data
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.1.0",
"version": "0.1.1",
"main": "dist/umd.js",
"module": "dist/es.js",
"jsnext:main": "dist/es.js",
Expand Down
5 changes: 5 additions & 0 deletions src/TuaApiCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ class TuaApiCore {

// 执行完 beforeFn 后执行的函数
const beforeFnCallback = (rArgs = {}) => {
// 兼容小程序传递请求头(建议还是放在中间件中)
if (rArgs.header) {
ctx.req.reqFnParams.header = rArgs.header
}

if (!rArgs.params) return

// 合并 beforeFn 中传入的 params
Expand Down
2 changes: 2 additions & 0 deletions src/TuaApiWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TuaApi extends TuaApiCore {
callbackName,
jsonpOptions,
axiosOptions,
...rest
}) {
if (VALID_REQ_TYPES.indexOf(reqType) === -1) {
error(`reqType 的有效值为: ${VALID_REQ_TYPES.join(', ')}!`)
Expand All @@ -36,6 +37,7 @@ class TuaApi extends TuaApiCore {
data,
method: type.toLowerCase(),
...axiosOptions,
...rest,
})
}

Expand Down

0 comments on commit 68acf66

Please sign in to comment.