mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-03 04:50:32 +08:00
opti: 代码格式优化
This commit is contained in:
parent
7a20bb4e8c
commit
a7692a6b4f
@ -37,6 +37,7 @@ module.exports = {
|
||||
"error",
|
||||
"always"
|
||||
],
|
||||
"strict": 0
|
||||
"strict": 0,
|
||||
"comma-dangle": ["error", "never"]
|
||||
}
|
||||
};
|
@ -1,10 +1,10 @@
|
||||
import interfaceModel from '../models/interface.js'
|
||||
import baseController from './base.js'
|
||||
import yapi from '../yapi.js'
|
||||
import interfaceModel from '../models/interface.js';
|
||||
import baseController from './base.js';
|
||||
import yapi from '../yapi.js';
|
||||
|
||||
class interfaceController extends baseController {
|
||||
constructor(ctx) {
|
||||
super(ctx)
|
||||
super(ctx);
|
||||
this.Model = yapi.getInst(interfaceModel);
|
||||
}
|
||||
|
||||
@ -37,16 +37,18 @@ class interfaceController extends baseController{
|
||||
*/
|
||||
async add(ctx) {
|
||||
let params = ctx.request.body;
|
||||
|
||||
params = yapi.commons.handleParams(params, {
|
||||
project_id: 'number',
|
||||
title: 'string',
|
||||
path: 'string',
|
||||
method: 'string',
|
||||
desc: 'string'
|
||||
})
|
||||
});
|
||||
params.method = params.method || 'GET';
|
||||
params.method = params.method.toUpperCase()
|
||||
params.method = params.method.toUpperCase();
|
||||
params.res_body_type = params.res_body_type ? params.res_body_type.toLowerCase() : 'json';
|
||||
|
||||
if (!params.project_id) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
|
||||
}
|
||||
@ -56,7 +58,7 @@ class interfaceController extends baseController{
|
||||
}
|
||||
|
||||
if (!yapi.commons.verifyPath(params.path)) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/')
|
||||
return ctx.body = yapi.commons.resReturn(null, 400, '接口path第一位必须是/,最后一位不能为/');
|
||||
}
|
||||
|
||||
let checkRepeat = await this.Model.checkRepeat(params.project_id, params.path, params.method);
|
||||
@ -79,15 +81,19 @@ class interfaceController extends baseController{
|
||||
uid: this.getUid(),
|
||||
add_time: yapi.commons.time(),
|
||||
up_time: yapi.commons.time()
|
||||
}
|
||||
};
|
||||
|
||||
if(params.req_params_form) data.req_params_form = params.req_params_form;
|
||||
if(params.req_params_other) data.req_params_other = params.req_params_other;
|
||||
if (params.req_params_form) {
|
||||
data.req_params_form = params.req_params_form;
|
||||
}
|
||||
if (params.req_params_other) {
|
||||
data.req_params_other = params.req_params_other;
|
||||
}
|
||||
|
||||
let result = await this.Model.save(data);
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
} catch (e) {
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message)
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,14 +109,16 @@ class interfaceController extends baseController{
|
||||
*/
|
||||
async get(ctx) {
|
||||
let params = ctx.request.query;
|
||||
|
||||
if (!params.id) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
|
||||
}
|
||||
|
||||
try {
|
||||
let result = await this.Model.get(params.id);
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
} catch (e) {
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message)
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,17 +132,18 @@ class interfaceController extends baseController{
|
||||
* @returns {Object}
|
||||
* @example ./api/interface/list.json
|
||||
*/
|
||||
|
||||
async list(ctx) {
|
||||
let project_id = ctx.request.query.project_id;
|
||||
|
||||
if (!project_id) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
|
||||
}
|
||||
|
||||
try {
|
||||
let result = await this.Model.list(project_id);
|
||||
ctx.body = yapi.commons.resReturn(result)
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
} catch (err) {
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message)
|
||||
ctx.body = yapi.commons.resReturn(null, 402, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,22 +176,25 @@ class interfaceController extends baseController{
|
||||
|
||||
async up(ctx) {
|
||||
let params = ctx.request.body;
|
||||
|
||||
params = yapi.commons.handleParams(params, {
|
||||
title: 'string',
|
||||
path: 'string',
|
||||
method: 'string',
|
||||
desc: 'string'
|
||||
})
|
||||
});
|
||||
params.method = params.method || 'GET';
|
||||
params.method = params.method.toUpperCase()
|
||||
params.method = params.method.toUpperCase();
|
||||
|
||||
let id = ctx.request.body.id;
|
||||
|
||||
if (!id) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 400, '接口id不能为空');
|
||||
}
|
||||
let interfaceData = await this.Model.get(id);
|
||||
|
||||
if (params.path && !yapi.commons.verifyPath(params.path)) {
|
||||
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) {
|
||||
@ -194,26 +206,44 @@ class interfaceController extends baseController{
|
||||
|
||||
let data = {
|
||||
up_time: yapi.commons.time()
|
||||
};
|
||||
|
||||
if (params.path) {
|
||||
data.path = params.path;
|
||||
}
|
||||
if (params.title) {
|
||||
data.title = params.title;
|
||||
}
|
||||
if (params.desc) {
|
||||
data.desc = params.desc;
|
||||
}
|
||||
if (params.method) {
|
||||
data.method = params.method;
|
||||
}
|
||||
|
||||
if(params.path) data.path = params.path;
|
||||
if(params.title) data.title = params.title;
|
||||
if(params.desc) data.desc = params.desc;
|
||||
if(params.method) data.method = params.method;
|
||||
if (params.req_headers) {
|
||||
data.req_headers = params.req_headers;
|
||||
}
|
||||
|
||||
if(params.req_headers) data.req_headers = params.req_headers;
|
||||
if (params.req_params_form) {
|
||||
data.req_params_form = params.req_params_form;
|
||||
}
|
||||
if (params.req_params_other) {
|
||||
data.req_params_other = params.req_params_other;
|
||||
}
|
||||
|
||||
if(params.req_params_form) data.req_params_form = params.req_params_form;
|
||||
if(params.req_params_other) data.req_params_other = params.req_params_other;
|
||||
|
||||
if(params.res_body_type) data.res_body_type = params.res_body_type;
|
||||
if(params.res_body) data.res_body = params.res_body;
|
||||
if (params.res_body_type) {
|
||||
data.res_body_type = params.res_body_type;
|
||||
}
|
||||
if (params.res_body) {
|
||||
data.res_body = params.res_body;
|
||||
}
|
||||
|
||||
try {
|
||||
let result = await this.Model.up(id, data);
|
||||
ctx.body = yapi.commons.resReturn(result)
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
} catch (e) {
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message)
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
||||
}
|
||||
|
||||
}
|
||||
@ -247,9 +277,9 @@ class interfaceController extends baseController{
|
||||
|
||||
|
||||
let result = await this.Model.del(id);
|
||||
ctx.body = yapi.commons.resReturn(result)
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
} catch (err) {
|
||||
ctx.body = yapi.commons.resReturn(null, 402, err.message)
|
||||
ctx.body = yapi.commons.resReturn(null, 402, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -82,10 +82,11 @@ class interfaceModel extends baseModel {
|
||||
_id: id
|
||||
});
|
||||
}
|
||||
|
||||
up(id, data) {
|
||||
data.up_time = yapi.commons.time();
|
||||
return this.model.update({
|
||||
_id: id,
|
||||
_id: id
|
||||
}, data, { runValidators: true });
|
||||
}
|
||||
}
|
||||
|
@ -23,10 +23,18 @@ exports.log = (msg, type) => {
|
||||
let f;
|
||||
|
||||
switch (type) {
|
||||
case 'log': f = console.log; break;
|
||||
case 'warn': f = console.warn; break;
|
||||
case 'error': f = console.error; break;
|
||||
default: f = console.log; break;
|
||||
case 'log':
|
||||
f = console.log;
|
||||
break;
|
||||
case 'warn':
|
||||
f = console.warn;
|
||||
break;
|
||||
case 'error':
|
||||
f = console.error;
|
||||
break;
|
||||
default:
|
||||
f = console.log;
|
||||
break;
|
||||
}
|
||||
|
||||
f(type + ':', msg);
|
||||
@ -204,8 +212,10 @@ exports.handleParams = (params, keys) => {
|
||||
var filter = keys[key];
|
||||
if (params[key]) {
|
||||
switch (filter) {
|
||||
case 'string': params[key] = trim(params[key] + ''); break;
|
||||
case 'number': params[key] = parseInt(params[key], 10); break;
|
||||
case 'string': params[key] = trim(params[key] + '');
|
||||
break;
|
||||
case 'number': params[key] = parseInt(params[key], 10);
|
||||
break;
|
||||
default: params[key] = trim(params + '');
|
||||
}
|
||||
}
|
||||
|
@ -19,13 +19,13 @@ function connect(){
|
||||
let options = {};
|
||||
|
||||
if (config.user) {
|
||||
options.user = config.db.user,
|
||||
options.user = config.db.user;
|
||||
options.pass = config.db.pass;
|
||||
}
|
||||
|
||||
let db = mongoose.connect(`mongodb://${config.db.servername}:${config.db.port}/${config.db.DATABASE}`, options);
|
||||
|
||||
db.then(function (res) {
|
||||
db.then(function () {
|
||||
yapi.commons.log('mongodb load success...');
|
||||
}, function (err) {
|
||||
yapi.commons.log(err, 'Mongo connect error');
|
||||
|
Loading…
Reference in New Issue
Block a user