fix #9 add RESTFul API support

pull/19/head
Bosn 7 years ago
parent 900ea1d225
commit 7f9442cd0a

@ -37,9 +37,8 @@ npm run dev
### production ### production
```sh ```sh
# 1. change server config in /config/config.prod.js
# 2. start server in production mode # start server in production mode
npm start npm start
``` ```

@ -11,7 +11,7 @@
"dev": "NODE_ENV=development nodemon --watch scripts --watch src scripts/dev.js", "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", "dev-local": "NODE_ENV=local nodemon --watch scripts --watch src scripts/dev.js",
"start": "NODE_ENV=production node dispatch.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", "test": "NODE_ENV=development TEST_MODE=true mocha --exit --reporter nyan",
"linter": "standard --fix", "linter": "standard --fix",
"watch-test": "NODE_ENV=development nodemon --watch scripts --watch src --watch test ./node_modules/.bin/mocha --timeout 5000", "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", "mysql": "^2.11.1",
"node-fetch": "^1.7.1", "node-fetch": "^1.7.1",
"node-print": "0.0.4", "node-print": "0.0.4",
"path-to-regexp": "^2.1.0",
"sequelize": "^3.30.4", "sequelize": "^3.30.4",
"sequelize-cli": "^3.1.0", "sequelize-cli": "^3.1.0",
"underscore": "^1.8.3", "underscore": "^1.8.3",

@ -5,6 +5,7 @@ const attributes = { exclude: [] }
const Tree = require('./utils/tree') const Tree = require('./utils/tree')
const pt = require('node-print').pt const pt = require('node-print').pt
const beautify = require('js-beautify').js_beautify const beautify = require('js-beautify').js_beautify
const urlUtils = require('./utils/url')
// 检测是否存在重复接口,会在返回的插件 JS 中提示。同时也会在编辑器中提示。 // 检测是否存在重复接口,会在返回的插件 JS 中提示。同时也会在编辑器中提示。
const parseDuplicatedInterfaces = (repository) => { const parseDuplicatedInterfaces = (repository) => {
@ -125,7 +126,13 @@ router.all('/app/mock/(\\d+)/(\\w+)/(.+)', async (ctx, next) => {
let repository = await Repository.findById(repositoryId) let repository = await Repository.findById(repositoryId)
let collaborators = await repository.getCollaborators() 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, attributes,
where: { where: {
repositoryId: [repositoryId, ...collaborators.map(item => item.id)], repositoryId: [repositoryId, ...collaborators.map(item => item.id)],
@ -135,8 +142,30 @@ router.all('/app/mock/(\\d+)/(\\w+)/(.+)', async (ctx, next) => {
}) })
if (!itf) { if (!itf) {
ctx.body = {} // try RESTFul API search...
return 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 let interfaceId = itf.id

@ -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
Loading…
Cancel
Save