Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优先精确匹配path完全相同的api #326

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
56 changes: 48 additions & 8 deletions controllers/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const { MockProxy, ProjectProxy, UserGroupProxy } = require('../proxy')
const redis = util.getRedis()
const defPageSize = config.get('pageSize')

// /api/{user}/{id} => /api/:user/:id
const replacePathParams = (url) => url.replace(/{/g, ':').replace(/}/g, '')

async function checkByMockId (mockId, uid) {
const api = await MockProxy.getById(mockId)

Expand Down Expand Up @@ -224,15 +227,26 @@ module.exports = class MockController {
mockURL = mockURL.replace(apis[0].project.url, '') || '/'
}

api = apis.filter((item) => {
const url = item.url.replace(/{/g, ':').replace(/}/g, '') // /api/{user}/{id} => /api/:user/:id
return item.method === method && pathToRegexp(url).test(mockURL)
})[0]
api = apis
.filter(item => {
const url = replacePathParams(item.url)
return item.method === method && pathToRegexp(url).test(mockURL)
})
// 按匹配度排序(包含路径参数越少匹配度越高)
.sort(function (a, b) {
const ap = pathToRegexp.parse(
replacePathParams(a.url)
)
const bp = pathToRegexp.parse(
replacePathParams(b.url)
)
return ap.length - bp.length
})[0]

if (!api) ctx.throw(404)

Mock.Handler.function = function (options) {
const mockUrl = api.url.replace(/{/g, ':').replace(/}/g, '') // /api/{user}/{id} => /api/:user/:id
const mockUrl = replacePathParams(api.url)
options.Mock = Mock
options._req = ctx.request
options._req.params = util.params(mockUrl, mockURL)
Expand All @@ -241,8 +255,8 @@ module.exports = class MockController {
}

if (/^http(s)?/.test(api.mode)) { // 代理模式
const url = nodeURL.parse(api.mode.replace(/{/g, ':').replace(/}/g, ''), true)
const params = util.params(api.url.replace(/{/g, ':').replace(/}/g, ''), mockURL)
const url = nodeURL.parse(replacePathParams(api.mode), true)
const params = util.params(replacePathParams(api.url), mockURL)
const pathname = pathToRegexp.compile(url.pathname)(params)
try {
apiData = await axios({
Expand All @@ -258,6 +272,31 @@ module.exports = class MockController {
return
}
} else {
/**
* 支持直接返回数组
*/
// eslint-disable-next-line no-empty-character-class
const arrayModeRegx = /^{\s*"?(\s*__arr__\s*)\|([0-9]+)\s*"?\s*:\s*(.*)}/s

if (arrayModeRegx.test(api.mode)) {
const [, key, count] = arrayModeRegx.exec(api.mode)
api.mode = `[
function({
Mock
}) {
this.push(...Mock.mock({
${api.mode.slice(1, -1)}
})['${key}'])
this._res = {
headers: {
'X-Total-Count': ${count}
}
};
return this.pop()
}
]`
}

const vm = new VM({
timeout: 1000,
sandbox: {
Expand Down Expand Up @@ -289,7 +328,8 @@ module.exports = class MockController {
}
}
/* istanbul ignore next */
if (_res.status && parseInt(_res.status, 10) !== 200 && _res.data) apiData = _res.data
if (_res.status && _res.data) apiData = _res.data
// if (_res.status && parseInt(_res.status, 10) !== 200 && _res.data) apiData = _res.data
delete apiData['_res']
}
}
Expand Down