mirror of
https://github.com/YMFE/yapi.git
synced 2025-01-18 13:04:46 +08:00
opti: restful api
This commit is contained in:
parent
991dfa95b9
commit
8c928a1e19
@ -2,6 +2,7 @@ import interfaceModel from '../models/interface.js';
|
|||||||
import interfaceCatModel from '../models/interfaceCat.js';
|
import interfaceCatModel from '../models/interfaceCat.js';
|
||||||
import interfaceCaseModel from '../models/interfaceCase.js'
|
import interfaceCaseModel from '../models/interfaceCase.js'
|
||||||
|
|
||||||
|
import _ from 'underscore';
|
||||||
import baseController from './base.js';
|
import baseController from './base.js';
|
||||||
import yapi from '../yapi.js';
|
import yapi from '../yapi.js';
|
||||||
import userModel from '../models/user.js';
|
import userModel from '../models/user.js';
|
||||||
@ -61,6 +62,7 @@ class interfaceController extends baseController {
|
|||||||
}
|
}
|
||||||
params.method = params.method || 'GET';
|
params.method = params.method || 'GET';
|
||||||
params.method = params.method.toUpperCase();
|
params.method = params.method.toUpperCase();
|
||||||
|
params.req_params = params.req_params || [];
|
||||||
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
|
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
|
||||||
|
|
||||||
if (!params.project_id) {
|
if (!params.project_id) {
|
||||||
@ -105,7 +107,23 @@ class interfaceController extends baseController {
|
|||||||
if (params.req_body_form) {
|
if (params.req_body_form) {
|
||||||
data.req_body_form = params.req_body_form;
|
data.req_body_form = params.req_body_form;
|
||||||
}
|
}
|
||||||
if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) {
|
|
||||||
|
if (params.path.indexOf(":") > 0) {
|
||||||
|
let paths = params.path.split("/"), name, i;
|
||||||
|
for (i = 1; i < paths.length; i++) {
|
||||||
|
if (paths[i][0] === ':') {
|
||||||
|
name = paths[i].substr(1);
|
||||||
|
if (!_.find(params.req_params, { name: name })) {
|
||||||
|
params.req_params.push({
|
||||||
|
name: name,
|
||||||
|
desc: ''
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( params.req_params.length > 0) {
|
||||||
data.type = 'var'
|
data.type = 'var'
|
||||||
data.req_params = params.req_params;
|
data.req_params = params.req_params;
|
||||||
} else {
|
} else {
|
||||||
@ -116,9 +134,9 @@ class interfaceController extends baseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let result = await this.Model.save(data);
|
let result = await this.Model.save(data);
|
||||||
|
|
||||||
// let project = await this.projectModel.get(params.project_id);
|
// let project = await this.projectModel.get(params.project_id);
|
||||||
this.catModel.get(params.catid).then((cate)=>{
|
this.catModel.get(params.catid).then((cate) => {
|
||||||
let username = this.getUsername();
|
let username = this.getUsername();
|
||||||
yapi.commons.saveLog({
|
yapi.commons.saveLog({
|
||||||
content: `用户 "${username}" 为分类 "${cate.name}" 添加了接口 "${data.title}"`,
|
content: `用户 "${username}" 为分类 "${cate.name}" 添加了接口 "${data.title}"`,
|
||||||
@ -128,7 +146,7 @@ class interfaceController extends baseController {
|
|||||||
typeid: params.project_id
|
typeid: params.project_id
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = yapi.commons.resReturn(result);
|
ctx.body = yapi.commons.resReturn(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
||||||
@ -278,7 +296,7 @@ class interfaceController extends baseController {
|
|||||||
return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/');
|
return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.path && (params.path !== interfaceData.path || params.method !== interfaceData.method) ) {
|
if (params.path && (params.path !== interfaceData.path || params.method !== interfaceData.method)) {
|
||||||
let checkRepeat = await this.Model.checkRepeat(interfaceData.project_id, params.path, params.method);
|
let checkRepeat = await this.Model.checkRepeat(interfaceData.project_id, params.path, params.method);
|
||||||
if (checkRepeat > 0) {
|
if (checkRepeat > 0) {
|
||||||
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
|
return ctx.body = yapi.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']');
|
||||||
@ -346,7 +364,7 @@ class interfaceController extends baseController {
|
|||||||
let result = await this.Model.up(id, data);
|
let result = await this.Model.up(id, data);
|
||||||
let username = this.getUsername();
|
let username = this.getUsername();
|
||||||
if (params.catid) {
|
if (params.catid) {
|
||||||
this.catModel.get(+params.catid).then((cate)=>{
|
this.catModel.get(+params.catid).then((cate) => {
|
||||||
yapi.commons.saveLog({
|
yapi.commons.saveLog({
|
||||||
content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`,
|
content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`,
|
||||||
type: 'project',
|
type: 'project',
|
||||||
@ -357,7 +375,7 @@ class interfaceController extends baseController {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let cateid = interfaceData.catid;
|
let cateid = interfaceData.catid;
|
||||||
this.catModel.get(cateid).then((cate)=>{
|
this.catModel.get(cateid).then((cate) => {
|
||||||
yapi.commons.saveLog({
|
yapi.commons.saveLog({
|
||||||
content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`,
|
content: `用户 "${username}" 更新了分类 "${cate.name}" 下的接口 "${data.title}"`,
|
||||||
type: 'project',
|
type: 'project',
|
||||||
@ -368,7 +386,7 @@ class interfaceController extends baseController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ctx.body = yapi.commons.resReturn(result);
|
ctx.body = yapi.commons.resReturn(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
||||||
@ -408,7 +426,7 @@ class interfaceController extends baseController {
|
|||||||
let result = await this.Model.del(id);
|
let result = await this.Model.del(id);
|
||||||
await this.caseModel.delByInterfaceId(id);
|
await this.caseModel.delByInterfaceId(id);
|
||||||
let username = this.getUsername();
|
let username = this.getUsername();
|
||||||
this.catModel.get(inter.catid).then((cate)=>{
|
this.catModel.get(inter.catid).then((cate) => {
|
||||||
yapi.commons.saveLog({
|
yapi.commons.saveLog({
|
||||||
content: `用户 "${username}" 删除了分类 "${cate.name}" 下的接口 "${inter.title}"`,
|
content: `用户 "${username}" 删除了分类 "${cate.name}" 下的接口 "${inter.title}"`,
|
||||||
type: 'project',
|
type: 'project',
|
||||||
@ -417,7 +435,7 @@ class interfaceController extends baseController {
|
|||||||
typeid: cate.project_id
|
typeid: cate.project_id
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
ctx.body = yapi.commons.resReturn(result);
|
ctx.body = yapi.commons.resReturn(result);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -507,7 +525,7 @@ class interfaceController extends baseController {
|
|||||||
name: params.name,
|
name: params.name,
|
||||||
desc: params.desc,
|
desc: params.desc,
|
||||||
up_time: yapi.commons.time()
|
up_time: yapi.commons.time()
|
||||||
});
|
});
|
||||||
|
|
||||||
let username = this.getUsername();
|
let username = this.getUsername();
|
||||||
let cate = await this.catModel.get(params.catid);
|
let cate = await this.catModel.get(params.catid);
|
||||||
|
@ -44,6 +44,10 @@ var _interfaceCase = require('../models/interfaceCase.js');
|
|||||||
|
|
||||||
var _interfaceCase2 = _interopRequireDefault(_interfaceCase);
|
var _interfaceCase2 = _interopRequireDefault(_interfaceCase);
|
||||||
|
|
||||||
|
var _underscore = require('underscore');
|
||||||
|
|
||||||
|
var _underscore2 = _interopRequireDefault(_underscore);
|
||||||
|
|
||||||
var _base = require('./base.js');
|
var _base = require('./base.js');
|
||||||
|
|
||||||
var _base2 = _interopRequireDefault(_base);
|
var _base2 = _interopRequireDefault(_base);
|
||||||
@ -113,7 +117,7 @@ var interfaceController = function (_baseController) {
|
|||||||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(ctx) {
|
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(ctx) {
|
||||||
var _this2 = this;
|
var _this2 = this;
|
||||||
|
|
||||||
var params, auth, checkRepeat, data, result;
|
var params, auth, checkRepeat, data, paths, name, i, result;
|
||||||
return _regenerator2.default.wrap(function _callee$(_context) {
|
return _regenerator2.default.wrap(function _callee$(_context) {
|
||||||
while (1) {
|
while (1) {
|
||||||
switch (_context.prev = _context.next) {
|
switch (_context.prev = _context.next) {
|
||||||
@ -145,47 +149,48 @@ var interfaceController = function (_baseController) {
|
|||||||
case 7:
|
case 7:
|
||||||
params.method = params.method || 'GET';
|
params.method = params.method || 'GET';
|
||||||
params.method = params.method.toUpperCase();
|
params.method = params.method.toUpperCase();
|
||||||
|
params.req_params = params.req_params || [];
|
||||||
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
|
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
|
||||||
|
|
||||||
if (params.project_id) {
|
if (params.project_id) {
|
||||||
_context.next = 12;
|
_context.next = 13;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '项目id不能为空'));
|
||||||
|
|
||||||
case 12:
|
case 13:
|
||||||
if (params.path) {
|
if (params.path) {
|
||||||
_context.next = 14;
|
_context.next = 15;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空'));
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口请求路径不能为空'));
|
||||||
|
|
||||||
case 14:
|
case 15:
|
||||||
if (_yapi2.default.commons.verifyPath(params.path)) {
|
if (_yapi2.default.commons.verifyPath(params.path)) {
|
||||||
_context.next = 16;
|
_context.next = 17;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/'));
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/'));
|
||||||
|
|
||||||
case 16:
|
case 17:
|
||||||
_context.next = 18;
|
_context.next = 19;
|
||||||
return this.Model.checkRepeat(params.project_id, params.path, params.method);
|
return this.Model.checkRepeat(params.project_id, params.path, params.method);
|
||||||
|
|
||||||
case 18:
|
case 19:
|
||||||
checkRepeat = _context.sent;
|
checkRepeat = _context.sent;
|
||||||
|
|
||||||
if (!(checkRepeat > 0)) {
|
if (!(checkRepeat > 0)) {
|
||||||
_context.next = 21;
|
_context.next = 22;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '已存在的接口:' + params.path + '[' + params.method + ']'));
|
||||||
|
|
||||||
case 21:
|
case 22:
|
||||||
_context.prev = 21;
|
_context.prev = 22;
|
||||||
data = {
|
data = {
|
||||||
project_id: params.project_id,
|
project_id: params.project_id,
|
||||||
catid: params.catid,
|
catid: params.catid,
|
||||||
@ -210,7 +215,24 @@ var interfaceController = function (_baseController) {
|
|||||||
if (params.req_body_form) {
|
if (params.req_body_form) {
|
||||||
data.req_body_form = params.req_body_form;
|
data.req_body_form = params.req_body_form;
|
||||||
}
|
}
|
||||||
if (params.req_params && Array.isArray(params.req_params) && params.req_params.length > 0) {
|
|
||||||
|
if (params.path.indexOf(":") > 0) {
|
||||||
|
paths = params.path.split("/"), name = void 0, i = void 0;
|
||||||
|
|
||||||
|
for (i = 1; i < paths.length; i++) {
|
||||||
|
if (paths[i][0] === ':') {
|
||||||
|
name = paths[i].substr(1);
|
||||||
|
if (!_underscore2.default.find(params.req_params, { name: name })) {
|
||||||
|
params.req_params.push({
|
||||||
|
name: name,
|
||||||
|
desc: ''
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.req_params.length > 0) {
|
||||||
data.type = 'var';
|
data.type = 'var';
|
||||||
data.req_params = params.req_params;
|
data.req_params = params.req_params;
|
||||||
} else {
|
} else {
|
||||||
@ -220,10 +242,10 @@ var interfaceController = function (_baseController) {
|
|||||||
data.req_body_other = params.req_body_other;
|
data.req_body_other = params.req_body_other;
|
||||||
}
|
}
|
||||||
|
|
||||||
_context.next = 29;
|
_context.next = 31;
|
||||||
return this.Model.save(data);
|
return this.Model.save(data);
|
||||||
|
|
||||||
case 29:
|
case 31:
|
||||||
result = _context.sent;
|
result = _context.sent;
|
||||||
|
|
||||||
|
|
||||||
@ -240,21 +262,21 @@ var interfaceController = function (_baseController) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = _yapi2.default.commons.resReturn(result);
|
ctx.body = _yapi2.default.commons.resReturn(result);
|
||||||
_context.next = 37;
|
_context.next = 39;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 34:
|
case 36:
|
||||||
_context.prev = 34;
|
_context.prev = 36;
|
||||||
_context.t0 = _context['catch'](21);
|
_context.t0 = _context['catch'](22);
|
||||||
|
|
||||||
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
|
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context.t0.message);
|
||||||
|
|
||||||
case 37:
|
case 39:
|
||||||
case 'end':
|
case 'end':
|
||||||
return _context.stop();
|
return _context.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, _callee, this, [[21, 34]]);
|
}, _callee, this, [[22, 36]]);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
function add(_x) {
|
function add(_x) {
|
||||||
|
Loading…
Reference in New Issue
Block a user