2017-09-05 10:24:13 +08:00
|
|
|
|
const yapi = require('./yapi.js');
|
2017-09-28 20:26:54 +08:00
|
|
|
|
const plugin_path = yapi.path.join(yapi.WEBROOT, 'node_modules');
|
2017-09-17 13:36:51 +08:00
|
|
|
|
const plugin_system_path = yapi.path.join(yapi.WEBROOT, 'exts');
|
|
|
|
|
const initPlugins = require('../common/lib.js').initPlugins;
|
|
|
|
|
var extConfig = require('../common/config.js').exts;
|
2017-09-25 00:07:23 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 钩子配置
|
|
|
|
|
*/
|
2017-09-05 17:04:59 +08:00
|
|
|
|
var hooks = {
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 第三方sso登录钩子,暂只支持设置一个
|
|
|
|
|
* @param ctx
|
2017-10-12 20:14:34 +08:00
|
|
|
|
* @return 必需返回一个 promise 对象,resolve({username: '', email: ''})
|
2017-09-25 00:07:23 +08:00
|
|
|
|
*/
|
2017-09-17 13:36:51 +08:00
|
|
|
|
'third_login': {
|
|
|
|
|
type: 'single',
|
|
|
|
|
listener: null
|
|
|
|
|
},
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 客户端增加接口成功后触发
|
|
|
|
|
* @param id 接口id
|
|
|
|
|
*/
|
2017-09-17 13:36:51 +08:00
|
|
|
|
'interface_add': {
|
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 客户端删除接口成功后触发
|
|
|
|
|
* @param id 接口id
|
|
|
|
|
*/
|
2017-09-17 13:36:51 +08:00
|
|
|
|
'interface_del': {
|
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 客户端更新接口成功后触发
|
|
|
|
|
* @param id 接口id
|
|
|
|
|
*/
|
2017-10-27 12:13:59 +08:00
|
|
|
|
'interface_update': {
|
2017-09-25 00:07:23 +08:00
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 客户端获取接口数据列表
|
|
|
|
|
* @param id project_id
|
|
|
|
|
*/
|
2017-10-27 12:13:59 +08:00
|
|
|
|
'interface_list': {
|
2017-09-25 00:07:23 +08:00
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 客户端获取一条接口信息触发
|
|
|
|
|
* @param id 接口id
|
|
|
|
|
*/
|
2017-10-27 12:13:59 +08:00
|
|
|
|
'interface_get': {
|
2017-09-25 00:07:23 +08:00
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 客户端增加一个新项目
|
|
|
|
|
* @param id 项目id
|
|
|
|
|
*/
|
2017-10-27 12:13:59 +08:00
|
|
|
|
'project_add': {
|
2017-09-17 13:36:51 +08:00
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 客户端删除删除一个项目
|
|
|
|
|
* @param id 项目id
|
|
|
|
|
*/
|
2017-10-27 12:13:59 +08:00
|
|
|
|
'project_del': {
|
2017-09-17 13:36:51 +08:00
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
2017-11-01 14:06:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 导出 markdown 数据
|
|
|
|
|
* @param context Object
|
|
|
|
|
* {
|
|
|
|
|
* projectData: project,
|
|
|
|
|
interfaceData: interfaceData,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
mockJson: res
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
'export_markdown': {
|
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* MockServer生成mock数据后触发
|
|
|
|
|
* @param context Object
|
|
|
|
|
* {
|
|
|
|
|
* projectData: project,
|
|
|
|
|
interfaceData: interfaceData,
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
mockJson: res
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-09-17 13:36:51 +08:00
|
|
|
|
mock_after: {
|
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
},
|
2017-09-25 00:07:23 +08:00
|
|
|
|
/**
|
|
|
|
|
* 增加路由的钩子
|
|
|
|
|
* type Sync
|
|
|
|
|
* @param addPluginRouter Function
|
2017-11-01 14:06:49 +08:00
|
|
|
|
* @info
|
2017-09-25 00:07:23 +08:00
|
|
|
|
* addPLuginPLugin(config)
|
2017-11-01 14:06:49 +08:00
|
|
|
|
*
|
2017-09-25 00:07:23 +08:00
|
|
|
|
* config = {
|
2017-11-01 14:06:49 +08:00
|
|
|
|
* path, // String 路由名称
|
|
|
|
|
* method, // String 请求方法 get post ...
|
2017-09-25 00:07:23 +08:00
|
|
|
|
* controller // Class 继承baseController的class
|
|
|
|
|
* action // String controller的Action
|
|
|
|
|
* }
|
2017-11-01 14:06:49 +08:00
|
|
|
|
*
|
|
|
|
|
* 示例:
|
|
|
|
|
* config = {
|
|
|
|
|
* path: "export/pdf",
|
|
|
|
|
* method: "get",
|
|
|
|
|
* controller: controller,
|
|
|
|
|
* action: "exportPdf"
|
|
|
|
|
* }
|
2017-09-25 00:07:23 +08:00
|
|
|
|
*/
|
2017-09-17 13:36:51 +08:00
|
|
|
|
add_router: {
|
|
|
|
|
type: 'multi',
|
|
|
|
|
listener: []
|
|
|
|
|
}
|
2017-09-05 17:04:59 +08:00
|
|
|
|
};
|
|
|
|
|
|
2017-09-17 13:36:51 +08:00
|
|
|
|
function bindHook(name, listener) {
|
|
|
|
|
if (!name) throw new Error('缺少hookname');
|
|
|
|
|
if (name in hooks === false) {
|
|
|
|
|
throw new Error('不存在的hookname');
|
|
|
|
|
}
|
|
|
|
|
if (hooks[name].type === 'multi') {
|
|
|
|
|
hooks[name].listener.push(listener);
|
|
|
|
|
} else {
|
|
|
|
|
if (typeof hooks[name].listener === 'function') {
|
|
|
|
|
throw new Error('重复绑定singleHook(' + name + '), 请检查');
|
|
|
|
|
}
|
|
|
|
|
hooks[name].listener = listener;
|
|
|
|
|
}
|
2017-09-08 11:18:30 +08:00
|
|
|
|
|
2017-09-05 17:04:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 18:03:09 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {*} hookname
|
|
|
|
|
* @return promise
|
|
|
|
|
*/
|
|
|
|
|
function emitHook(name) {
|
2017-09-17 13:36:51 +08:00
|
|
|
|
if (hooks[name] && typeof hooks[name] === 'object') {
|
|
|
|
|
let args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
|
if (hooks[name].type === 'single' && typeof hooks[name].listener === 'function') {
|
2017-10-27 12:13:59 +08:00
|
|
|
|
return Promise.resolve(hooks[name].listener.apply(yapi, args));
|
2017-09-17 13:36:51 +08:00
|
|
|
|
}
|
2017-10-10 18:03:09 +08:00
|
|
|
|
let promiseAll = [];
|
2017-09-17 13:36:51 +08:00
|
|
|
|
if (Array.isArray(hooks[name].listener)) {
|
|
|
|
|
let listenerList = hooks[name].listener;
|
2017-10-27 12:13:59 +08:00
|
|
|
|
for (let i = 0, l = listenerList.length; i < l; i++) {
|
2017-10-10 18:03:09 +08:00
|
|
|
|
promiseAll.push(Promise.resolve(listenerList[i].apply(yapi, args)));
|
2017-09-17 13:36:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-10 18:03:09 +08:00
|
|
|
|
return Promise.all(promiseAll);
|
2017-09-17 13:36:51 +08:00
|
|
|
|
}
|
2017-09-05 17:04:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 13:36:51 +08:00
|
|
|
|
function emitHookSync(name) {
|
|
|
|
|
if (hooks[name] && typeof hooks[name] === 'object') {
|
|
|
|
|
let args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
|
if (hooks[name].type === 'single' && typeof hooks[name].listener === 'function') {
|
|
|
|
|
return hooks[name].listener.apply(yapi, args);
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(hooks[name].listener)) {
|
|
|
|
|
hooks[name].listener.forEach((listener) => {
|
|
|
|
|
listener.apply(yapi, args)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-05 17:04:59 +08:00
|
|
|
|
yapi.bindHook = bindHook;
|
|
|
|
|
yapi.emitHook = emitHook;
|
2017-10-10 18:03:09 +08:00
|
|
|
|
yapi.emitHookSync = emitHook;
|
2017-09-17 13:36:51 +08:00
|
|
|
|
|
2017-09-05 17:04:59 +08:00
|
|
|
|
|
2017-09-05 10:24:13 +08:00
|
|
|
|
|
2017-10-05 09:12:44 +08:00
|
|
|
|
let pluginsConfig = initPlugins(yapi.WEBCONFIG.plugins, 'plugin');
|
2017-09-17 13:36:51 +08:00
|
|
|
|
pluginsConfig.forEach(plugin => {
|
|
|
|
|
if (!plugin || plugin.enable === false || plugin.server === false) return null;
|
|
|
|
|
|
|
|
|
|
if (!yapi.commons.fileExist(yapi.path.join(plugin_path, 'yapi-plugin-' + plugin.name + '/server.js'))) {
|
2017-10-05 09:12:44 +08:00
|
|
|
|
throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
|
2017-09-17 13:36:51 +08:00
|
|
|
|
}
|
|
|
|
|
let pluginModule = require(yapi.path.join(plugin_path, 'yapi-plugin-' + plugin.name + '/server.js'));
|
2017-10-05 09:12:44 +08:00
|
|
|
|
pluginModule.call(yapi, plugin.options)
|
2017-09-17 13:36:51 +08:00
|
|
|
|
})
|
|
|
|
|
|
2017-10-05 09:12:44 +08:00
|
|
|
|
extConfig = initPlugins(extConfig, 'ext');
|
2017-09-17 13:36:51 +08:00
|
|
|
|
|
|
|
|
|
extConfig.forEach(plugin => {
|
|
|
|
|
if (!plugin || plugin.enable === false || plugin.server === false) return null;
|
|
|
|
|
|
|
|
|
|
if (!yapi.commons.fileExist(yapi.path.join(plugin_system_path, 'yapi-plugin-' + plugin.name + '/server.js'))) {
|
2017-10-05 09:12:44 +08:00
|
|
|
|
throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
|
2017-09-17 13:36:51 +08:00
|
|
|
|
}
|
|
|
|
|
let pluginModule = require(yapi.path.join(plugin_system_path, 'yapi-plugin-' + plugin.name + '/server.js'));
|
2017-10-05 09:12:44 +08:00
|
|
|
|
pluginModule.call(yapi, plugin.options)
|
2017-09-17 13:36:51 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//delete bindHook方法,避免误操作
|
|
|
|
|
delete yapi.bindHook
|
|
|
|
|
|
|
|
|
|
|