Skip to content

Commit

Permalink
feat: 🎸 新增unit模式的共享数据形式
Browse files Browse the repository at this point in the history
  • Loading branch information
倪俊杰 committed Jan 12, 2022
1 parent 4b0fef1 commit ac02e88
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
'plugin:jest/recommended'
],
'parserOptions': {
'ecmaVersion': 8
'ecmaVersion': 'latest'
},
// https://eslint.org/docs/rules/
'rules': {
Expand Down
8 changes: 8 additions & 0 deletions demo/mockServer/mock/$unit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = () => {
return {
car: {
name: 'benz',
price: '$100w'
}
}
}
3 changes: 2 additions & 1 deletion demo/mockServer/mock/tom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 不需要mockSwitch的mock数据
module.exports = ({ params, share }) => {
module.exports = ({ params, share, unit }) => {
let msg = ''
if (share.data.shareData1.sheep === 0) {
msg = 'Tom! Are you crazy?'
Expand All @@ -16,6 +16,7 @@ module.exports = ({ params, share }) => {
name: 'tom',
age: 7,
shareData: share.data,
...unit.data
},
status: '200'
}
Expand Down
54 changes: 37 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ class MockMaster {
this.apiSuffix = config.apiSuffix || '.json'
this.$cache = {} // 设置一个cache,让/mock-switch设置过的数据能够直接给页面
this.$share = {}
this.$unit = {}
}

start() {
// 给cache里挂载方法
this._injectMethodToCache()
// 给share里挂载方法
this._injectMethodToShare()
// 初始化共享数据
// 初始化共享数据-share
this._initMockShareDataByFile()
// 初始化共享数据-unit
this._initMockUnitDataByFile()
// 删除前缀
app.use(this._removeApiPrefix.bind(this))
// 通过cache来控制模拟数据的状态切换
Expand Down Expand Up @@ -172,29 +175,43 @@ class MockMaster {
}

/**
* 初始化共享数据
* 初始化共享数据 - share(涉及积木拼接的共享数据)
* 共享数据是可以被所有接口引用的公共数据;
* 各个接口可以根据判断公共数据的值,根据业务要求返回不同的数据。
*/
_initMockShareDataByFile() {
const shareDataIndexPath = `${this.mockRoot}/$share/index.js`

if (fs.existsSync(shareDataIndexPath)) {
const shareMockDataHandle = require(shareDataIndexPath)
const shareMockDataKeys = Object.keys(shareMockDataHandle())

this.$share.data = {}
this.mockSwitchMap.share.forEach(item => {
shareMockDataKeys.forEach(key => {
if (item.name === key) {
this.$share.updateByRule(
key,
item.selections[0].value
)
}
})
if (!fs.existsSync(shareDataIndexPath)) return

const shareMockDataHandle = require(shareDataIndexPath)
const shareMockDataKeys = Object.keys(shareMockDataHandle())

this.$share.data = {}
this.mockSwitchMap.share.forEach(item => {
shareMockDataKeys.forEach(key => {
if (item.name === key) {
this.$share.updateByRule(
key,
item.selections[0].value
)
}
})
}
})
}

/**
* 初始化共享数据 - unit(不涉及积木拼接的共享数据)
* 共享数据是可以被所有接口引用的公共数据;
* 各个接口可以根据判断公共数据的值,根据业务要求返回不同的数据。
*/
_initMockUnitDataByFile() {
const unitDataIndexPath = `${this.mockRoot}/$unit/index.js`

if (!fs.existsSync(unitDataIndexPath)) return

const unitMockDataHandle = require(unitDataIndexPath)
this.$unit.data = unitMockDataHandle()
}

async _removeApiPrefix(ctx, next) {
Expand Down Expand Up @@ -279,6 +296,7 @@ class MockMaster {
apiMockDataHandle({
params,
share: this.$share,
unit: this.$unit,
cache: this.$cache,
switchMap: this.mockSwitchMap,
}),
Expand All @@ -305,6 +323,7 @@ class MockMaster {
apiMockDataHandle({
params,
share: this.$share,
unit: this.$unit,
cache: this.$cache,
switchMap: this.mockSwitchMap,
}),
Expand All @@ -318,6 +337,7 @@ class MockMaster {
ctx.body = apiMockDataHandle({
params,
share: this.$share,
unit: this.$unit,
cache: this.$cache,
switchMap: this.mockSwitchMap,
})
Expand Down

0 comments on commit ac02e88

Please sign in to comment.