diff --git a/README.md b/README.md index 4bb0e18..b39b140 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,8 @@ npm run dev ### production ```sh -# 1. change server config in /config/config.prod.js -# 2. start server in production mode +# start server in production mode npm start ``` diff --git a/package.json b/package.json index 31306ab..a567fb2 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dev": "NODE_ENV=development nodemon --watch scripts --watch src scripts/dev.js", "dev-local": "NODE_ENV=local nodemon --watch scripts --watch src scripts/dev.js", "start": "NODE_ENV=production node dispatch.js", - "check": "npm run linter && test", + "check": "npm run linter;npm run test;", "test": "NODE_ENV=development TEST_MODE=true mocha --exit --reporter nyan", "linter": "standard --fix", "watch-test": "NODE_ENV=development nodemon --watch scripts --watch src --watch test ./node_modules/.bin/mocha --timeout 5000", @@ -36,6 +36,7 @@ "mysql": "^2.11.1", "node-fetch": "^1.7.1", "node-print": "0.0.4", + "path-to-regexp": "^2.1.0", "sequelize": "^3.30.4", "sequelize-cli": "^3.1.0", "underscore": "^1.8.3", diff --git a/src/routes/mock.js b/src/routes/mock.js index fbe34e4..c8d367d 100644 --- a/src/routes/mock.js +++ b/src/routes/mock.js @@ -5,6 +5,7 @@ const attributes = { exclude: [] } const Tree = require('./utils/tree') const pt = require('node-print').pt const beautify = require('js-beautify').js_beautify +const urlUtils = require('./utils/url') // 检测是否存在重复接口,会在返回的插件 JS 中提示。同时也会在编辑器中提示。 const parseDuplicatedInterfaces = (repository) => { @@ -125,7 +126,13 @@ router.all('/app/mock/(\\d+)/(\\w+)/(.+)', async (ctx, next) => { let repository = await Repository.findById(repositoryId) let collaborators = await repository.getCollaborators() - let itf = await Interface.findOne({ + console.log(repositoryId) + console.log(method) + console.log(url) + + let itf + + itf = await Interface.findOne({ attributes, where: { repositoryId: [repositoryId, ...collaborators.map(item => item.id)], @@ -135,8 +142,30 @@ router.all('/app/mock/(\\d+)/(\\w+)/(.+)', async (ctx, next) => { }) if (!itf) { - ctx.body = {} - return + // try RESTFul API search... + let list = await Interface.findAll({ + attributes: ['id', 'url', 'method'], + where: { + repositoryId: [repositoryId, ...collaborators.map(item => item.id)] + } + }) + + let listMatched = [] + for (let item of list) { + if (urlUtils.urlMatchesPattern(url, item.url)) { + listMatched.push(item) + } + } + + if (listMatched.length > 1) { + ctx.body = { isOk: false, errMsg: '匹配到多个接口,请修改规则确保接口规则唯一性。 Matched duplicate interfaces, please ensure pattern to be unique.' } + return + } else if (listMatched.length === 0) { + ctx.body = { isOk: false, errMsg: '未匹配到任何接口 No matched interface' } + return + } else { + itf = await Interface.findById(listMatched[0].id) + } } let interfaceId = itf.id diff --git a/src/routes/utils/url.js b/src/routes/utils/url.js new file mode 100644 index 0000000..19c6871 --- /dev/null +++ b/src/routes/utils/url.js @@ -0,0 +1,36 @@ +let pathToRegexp = require('path-to-regexp') + +const pkg = {} + +pkg.getRelative = url => { + if (!url || typeof url !== 'string') return null + url = url.toLowerCase() + const prefixes = ['https://', 'http://'] + for (let item of prefixes) { + if (url.indexOf(item) > -1) { + url = url.substring(item.length) + if (url.indexOf('/') > -1) { + url = url.substring(url.indexOf('/')) + } else { + url = '/' + } + break + } + } + if (url.indexOf('?') > -1) { + url = url.substring(0, url.indexOf('?')) + } + return url +} + +pkg.urlMatchesPattern = (url, pattern) => { + console.log('matching') + console.log(url) + console.log(pattern) + url = pkg.getRelative(url) + pattern = pkg.getRelative(pattern) + let re = pathToRegexp(pattern) + return re.test(url) +} + +module.exports = pkg