From e69fae69ae37e67e3e7dd2409df76b5b43e3033d Mon Sep 17 00:00:00 2001 From: bigfengyu Date: Mon, 21 Oct 2019 17:02:30 +0800 Subject: [PATCH] fix: interface/get --- src/routes/repository.ts | 58 +++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/src/routes/repository.ts b/src/routes/repository.ts index ee56a14..1f67dc1 100644 --- a/src/routes/repository.ts +++ b/src/routes/repository.ts @@ -533,47 +533,43 @@ router.get('/interface/list', async (ctx) => { } }) router.get('/interface/get', async (ctx) => { - let { id, repositoryId, method, url } = ctx.query + let { id } = ctx.query - if (!await AccessUtils.canUserAccess(ACCESS_TYPE.REPOSITORY, ctx.session.id, repositoryId)) { - ctx.body = Consts.COMMON_ERROR_RES.ACCESS_DENY + if (id === undefined || id === '') { + ctx.body = { + isOk: false, + errMsg: '请输入参数id' + } return } - let itf: any - if (id) { - itf = await Interface.findByPk(id, { - attributes: { exclude: [] } - }) - } else if (repositoryId && method && url) { - // 同 /app/mock/:repository/:method/:url - let urlWithoutPrefixSlash = /(\/)?(.*)/.exec(url)[2] - let repository = await Repository.findByPk(repositoryId) - let collaborators = await repository.$get('collaborators') + let itf = await Interface.findByPk(id, { + attributes: { exclude: [] }, + include: [QueryInclude.Properties] + }) - itf = await Interface.findOne({ - attributes: { exclude: [] }, - where: { - repositoryId: [repositoryId, ...(collaborators).map(item => item.id)], - method, - url: [urlWithoutPrefixSlash, '/' + urlWithoutPrefixSlash], - }, - }) + if (!itf) { + ctx.body = { + isOk: false, + errMsg: `没有找到 id 为 ${id} 的接口` + } } - itf = itf.toJSON() - let scopes = ['request', 'response'] - for (let i = 0; i < scopes.length; i++) { - let properties: any = await Property.findAll({ - attributes: { exclude: [] }, - where: { interfaceId: itf.id, scope: scopes[i] }, - }) - properties = properties.map((item: any) => item.toJSON()) - itf[scopes[i] + 'Properties'] = Tree.ArrayToTree(properties).children + if ( + !(await AccessUtils.canUserAccess( + ACCESS_TYPE.REPOSITORY, + ctx.session.id, + itf.repositoryId + )) + ) { + ctx.body = Consts.COMMON_ERROR_RES.ACCESS_DENY + return } + const itfJSON: { [k: string]: any } = itf.toJSON() + ctx.type = 'json' - ctx.body = Tree.stringifyWithFunctonAndRegExp({ data: itf }) + ctx.body = Tree.stringifyWithFunctonAndRegExp({ data: itfJSON }) }) router.post('/interface/create', isLoggedIn, async (ctx, next) => { let creatorId = ctx.session.id