From 0b455d7b6c7bafcc5e4ded6178d5a73ee6cc69ae Mon Sep 17 00:00:00 2001 From: Bosn Date: Tue, 18 Sep 2018 16:13:31 +0800 Subject: [PATCH] fix bugs --- src/models/bo/property.ts | 4 ++++ src/routes/mock.ts | 37 ++++++++++++++++++++++++++++++++----- src/routes/repository.ts | 22 ++++++++++++++++------ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/models/bo/property.ts b/src/models/bo/property.ts index c11f7c8..0207873 100644 --- a/src/models/bo/property.ts +++ b/src/models/bo/property.ts @@ -96,4 +96,8 @@ export default class Property extends Model { @BelongsTo(() => Repository, 'repositoryId') repository: Repository + @Column + /** 是否为必填选项 */ + required: boolean + } \ No newline at end of file diff --git a/src/routes/mock.ts b/src/routes/mock.ts index 6a13267..3c626b7 100644 --- a/src/routes/mock.ts +++ b/src/routes/mock.ts @@ -126,10 +126,10 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => { let urlWithoutPrefixSlash = /(\/)?(.*)/.exec(url)[2] // let urlWithoutSearch // try { - // let urlParts = new URL(url) - // urlWithoutSearch = `${urlParts.origin}${urlParts.pathname}` + // let urlParts = new URL(url) + // urlWithoutSearch = `${urlParts.origin}${urlParts.pathname}` // } catch (e) { - // urlWithoutSearch = url + // urlWithoutSearch = url // } // DONE 2.3 腐烂的 KISSY // 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'], where: { repositoryId: [repositoryId, ...collaborators.map(item => item.id)], - }, + method, + } }) let listMatched = [] @@ -181,7 +182,7 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => { } 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 } else if (listMatched.length === 0) { ctx.body = { isOk: false, errMsg: '未匹配到任何接口 No matched interface' } @@ -196,6 +197,32 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => { attributes, 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()) // DONE 2.2 支持引用请求参数 diff --git a/src/routes/repository.ts b/src/routes/repository.ts index bb4a980..c5d9c3a 100644 --- a/src/routes/repository.ts +++ b/src/routes/repository.ts @@ -200,7 +200,10 @@ router.get('/repository/get', async (ctx) => { QueryInclude.RepositoryHierarchy, 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) } @@ -412,7 +415,7 @@ router.post('/module/create', async (ctx, next) => { }) router.post('/module/update', async (ctx, next) => { const { id, name, description } = ctx.request.body - await Module.update({ name, description }, { + await Module.update({ name, description, id }, { where: { id } }) ctx.body = { @@ -579,6 +582,13 @@ router.post('/interface/move', async (ctx) => { const itf = await Interface.findById(itfId) if (op === OP_MOVE) { itf.moduleId = modId + await Property.update({ + moduleId: modId, + }, { + where: { + interfaceId: itf.id, + } + }) await itf.save() } else if (op === OP_COPY) { const { id, name, ...otherProps } = itf.dataValues @@ -775,16 +785,16 @@ router.post('/properties/update', async (ctx, next) => { let itf = await Interface.findById(itfId) - if (typeof summary.name !== 'undefined') { + if (summary.name) { itf.name = summary.name } - if (typeof summary.url !== 'undefined') { + if (summary.url) { itf.url = summary.url } - if (typeof summary.method !== 'undefined') { + if (summary.method) { itf.method = summary.method } - if (typeof summary.description !== 'undefined') { + if (summary.description) { itf.description = summary.description }