* 添加项目分组 * @interface /interface/add * @method POST * @category interface * @foldnumber 10 * @param {Number} project_id 项目id,不能为空 * @param {String} title 接口标题,不能为空 * @param {String} path 接口请求路径,不能为空 * @param {String} method 请求方式 * @param {Array} [req_headers] 请求的h
(ctx)
| 198 | * @example ./api/interface/add.json |
| 199 | */ |
| 200 | async add(ctx) { |
| 201 | let params = ctx.params; |
| 202 | |
| 203 | if (!this.$tokenAuth) { |
| 204 | let auth = await this.checkAuth(params.project_id, 'project', 'edit'); |
| 205 | |
| 206 | if (!auth) { |
| 207 | return (ctx.body = yapi.commons.resReturn(null, 40033, '没有权限')); |
| 208 | } |
| 209 | } |
| 210 | params.method = params.method || 'GET'; |
| 211 | params.res_body_is_json_schema = _.isUndefined(params.res_body_is_json_schema) |
| 212 | ? false |
| 213 | : params.res_body_is_json_schema; |
| 214 | params.req_body_is_json_schema = _.isUndefined(params.req_body_is_json_schema) |
| 215 | ? false |
| 216 | : params.req_body_is_json_schema; |
| 217 | params.method = params.method.toUpperCase(); |
| 218 | params.req_params = params.req_params || []; |
| 219 | params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json'; |
| 220 | let http_path = url.parse(params.path, true); |
| 221 | |
| 222 | if (!yapi.commons.verifyPath(http_path.pathname)) { |
| 223 | return (ctx.body = yapi.commons.resReturn( |
| 224 | null, |
| 225 | 400, |
| 226 | 'path第一位必需为 /, 只允许由 字母数字-/_:.! 组成' |
| 227 | )); |
| 228 | } |
| 229 | |
| 230 | handleHeaders(params) |
| 231 | |
| 232 | params.query_path = {}; |
| 233 | params.query_path.path = http_path.pathname; |
| 234 | params.query_path.params = []; |
| 235 | Object.keys(http_path.query).forEach(item => { |
| 236 | params.query_path.params.push({ |
| 237 | name: item, |
| 238 | value: http_path.query[item] |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | let checkRepeat = await this.Model.checkRepeat(params.project_id, params.path, params.method); |
| 243 | |
| 244 | if (checkRepeat > 0) { |
| 245 | return (ctx.body = yapi.commons.resReturn( |
| 246 | null, |
| 247 | 40022, |
| 248 | '已存在的接口:' + params.path + '[' + params.method + ']' |
| 249 | )); |
| 250 | } |
| 251 | |
| 252 | let data = Object.assign(params, { |
| 253 | uid: this.getUid(), |
| 254 | add_time: yapi.commons.time(), |
| 255 | up_time: yapi.commons.time() |
| 256 | }); |
| 257 |
no test coverage detected