yapi/client/plugin.js

45 lines
885 B
JavaScript
Raw Normal View History

2017-09-05 10:24:13 +08:00
const config = process.env.config;
2017-09-05 17:04:59 +08:00
/**
* type single 只绑定一个监听函数,会返回处理结果
* mulit 绑定多个监听函数
*
*/
2017-09-05 10:24:13 +08:00
var hooks = {
'third_login': {
type: 'single',
2017-09-05 17:04:59 +08:00
listener: null
2017-09-05 10:24:13 +08:00
},
'add_interface': {
type: 'mulit',
2017-09-05 17:04:59 +08:00
listener: []
2017-09-05 10:24:13 +08:00
}
};
2017-09-05 17:04:59 +08:00
function bindHook(name, listener) {
2017-09-05 10:24:13 +08:00
if (!name) throw new Error('缺少hookname');
if (name in hooks === false) {
throw new Error('不存在的hookname');
}
if (hooks[name].type === 'multi') {
2017-09-05 17:04:59 +08:00
hooks[name].listener.push(listener);
2017-09-05 10:24:13 +08:00
} else {
2017-09-05 17:04:59 +08:00
hooks[name].listener = listener;
2017-09-05 10:24:13 +08:00
}
}
2017-09-05 17:04:59 +08:00
var yapi = {
hooks: hooks,
bindHook: bindHook
}
2017-09-05 10:24:13 +08:00
if (config.plugins && Array.isArray(config.plugins)) {
config.plugins.forEach(plugin => {
2017-09-05 17:04:59 +08:00
let pluginModule = require(`plugins/yapi-plugin-${plugin}/client.js`);
pluginModule.call(yapi) ;
2017-09-05 10:24:13 +08:00
})
}
module.exports = hooks;