pull/393/head
Bosn 7 years ago
parent 9ed9395e2e
commit 0b455d7b6c

@ -96,4 +96,8 @@ export default class Property extends Model<Property> {
@BelongsTo(() => Repository, 'repositoryId') @BelongsTo(() => Repository, 'repositoryId')
repository: Repository repository: Repository
@Column
/** 是否为必填选项 */
required: boolean
} }

@ -126,10 +126,10 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
let urlWithoutPrefixSlash = /(\/)?(.*)/.exec(url)[2] let urlWithoutPrefixSlash = /(\/)?(.*)/.exec(url)[2]
// let urlWithoutSearch // let urlWithoutSearch
// try { // try {
// let urlParts = new URL(url) // let urlParts = new URL(url)
// urlWithoutSearch = `${urlParts.origin}${urlParts.pathname}` // urlWithoutSearch = `${urlParts.origin}${urlParts.pathname}`
// } catch (e) { // } catch (e) {
// urlWithoutSearch = url // urlWithoutSearch = url
// } // }
// DONE 2.3 腐烂的 KISSY // DONE 2.3 腐烂的 KISSY
// KISSY 1.3.2 会把路径中的 // 替换为 /。在浏览器端拦截跨域请求时,需要 encodeURIComponent(url) 以防止 http:// 被替换为 http:/。但是同时也会把参数一起编码,导致 route 的 url 部分包含了参数。 // KISSY 1.3.2 会把路径中的 // 替换为 /。在浏览器端拦截跨域请求时,需要 encodeURIComponent(url) 以防止 http:// 被替换为 http:/。但是同时也会把参数一起编码,导致 route 的 url 部分包含了参数。
@ -170,7 +170,8 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
attributes: ['id', 'url', 'method'], attributes: ['id', 'url', 'method'],
where: { where: {
repositoryId: [repositoryId, ...collaborators.map(item => item.id)], repositoryId: [repositoryId, ...collaborators.map(item => item.id)],
}, method,
}
}) })
let listMatched = [] let listMatched = []
@ -181,7 +182,7 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
} }
if (listMatched.length > 1) { if (listMatched.length > 1) {
ctx.body = { isOk: false, errMsg: '匹配到多个接口,请修改规则确保接口规则唯一性。 Matched duplicate interfaces, please ensure pattern to be unique.' } ctx.body = { isOk: false, errMsg: '匹配到多个接口,请修改规则确保接口规则唯一性。 Matched multiple interfaces, please ensure pattern to be unique.' }
return return
} else if (listMatched.length === 0) { } else if (listMatched.length === 0) {
ctx.body = { isOk: false, errMsg: '未匹配到任何接口 No matched interface' } ctx.body = { isOk: false, errMsg: '未匹配到任何接口 No matched interface' }
@ -196,6 +197,32 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
attributes, attributes,
where: { interfaceId, scope: 'response' }, where: { interfaceId, scope: 'response' },
}) })
// check required
if (~['GET', 'POST'].indexOf(method)) {
let requiredProperties = await Property.findAll({
attributes,
where: { interfaceId, scope: 'request', required: true },
})
let passed = true
let pFailed: Property | undefined
let params = method === 'GET' ? ctx.query : ctx.body
for (const p of requiredProperties) {
if (typeof params[p.name] === 'undefined') {
passed = false
pFailed = p
break
}
}
if (!passed) {
ctx.body = {
isOk: false,
errMsg: `必选参数${pFailed.name}未传值。 Required parameter ${pFailed.name} has no value.`,
}
ctx.status = 500
return
}
}
properties = properties.map(item => item.toJSON()) properties = properties.map(item => item.toJSON())
// DONE 2.2 支持引用请求参数 // DONE 2.2 支持引用请求参数

@ -200,7 +200,10 @@ router.get('/repository/get', async (ctx) => {
QueryInclude.RepositoryHierarchy, QueryInclude.RepositoryHierarchy,
QueryInclude.Collaborators QueryInclude.Collaborators
], ],
order: [[{ model: Module, as: 'modules' }, 'priority', 'asc']] order: [
[{ model: Module, as: 'modules' }, 'priority', 'asc'],
[{ model: Module, as: 'modules' }, { model: Interface, as: 'interfaces' }, 'priority', 'asc']
]
}) })
await RedisService.setCache(CACHE_KEY.REPOSITORY_GET, JSON.stringify(repository), ctx.query.id) await RedisService.setCache(CACHE_KEY.REPOSITORY_GET, JSON.stringify(repository), ctx.query.id)
} }
@ -412,7 +415,7 @@ router.post('/module/create', async (ctx, next) => {
}) })
router.post('/module/update', async (ctx, next) => { router.post('/module/update', async (ctx, next) => {
const { id, name, description } = ctx.request.body const { id, name, description } = ctx.request.body
await Module.update({ name, description }, { await Module.update({ name, description, id }, {
where: { id } where: { id }
}) })
ctx.body = { ctx.body = {
@ -579,6 +582,13 @@ router.post('/interface/move', async (ctx) => {
const itf = await Interface.findById(itfId) const itf = await Interface.findById(itfId)
if (op === OP_MOVE) { if (op === OP_MOVE) {
itf.moduleId = modId itf.moduleId = modId
await Property.update({
moduleId: modId,
}, {
where: {
interfaceId: itf.id,
}
})
await itf.save() await itf.save()
} else if (op === OP_COPY) { } else if (op === OP_COPY) {
const { id, name, ...otherProps } = itf.dataValues const { id, name, ...otherProps } = itf.dataValues
@ -775,16 +785,16 @@ router.post('/properties/update', async (ctx, next) => {
let itf = await Interface.findById(itfId) let itf = await Interface.findById(itfId)
if (typeof summary.name !== 'undefined') { if (summary.name) {
itf.name = summary.name itf.name = summary.name
} }
if (typeof summary.url !== 'undefined') { if (summary.url) {
itf.url = summary.url itf.url = summary.url
} }
if (typeof summary.method !== 'undefined') { if (summary.method) {
itf.method = summary.method itf.method = summary.method
} }
if (typeof summary.description !== 'undefined') { if (summary.description) {
itf.description = summary.description itf.description = summary.description
} }

Loading…
Cancel
Save