-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
executable file
·273 lines (261 loc) · 6.05 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const yapi = require('./yapi.js');
const plugin_path = yapi.path.join(yapi.WEBROOT, 'node_modules');
const plugin_system_path = yapi.path.join(yapi.WEBROOT, 'exts');
const initPlugins = require('../common/plugin.js').initPlugins;
var extConfig = require('../common/config.js').exts;
/**
* 钩子配置
*/
var hooks = {
/**
* 第三方sso登录钩子,暂只支持设置一个
* @param ctx
* @return 必需返回一个 promise 对象,resolve({username: '', email: ''})
*/
third_login: {
type: 'single',
listener: null
},
/**
* 客户端增加接口成功后触发
* @param data 接口的详细信息
*/
interface_add: {
type: 'multi',
listener: []
},
/**
* 客户端删除接口成功后触发
* @param data 接口id
*/
interface_del: {
type: 'multi',
listener: []
},
/**
* 客户端更新接口成功后触发
* @param id 接口id
*/
interface_update: {
type: 'multi',
listener: []
},
/**
* 客户端获取接口数据列表
* @param list 返回接口的数据列表
*/
interface_list: {
type: 'multi',
listener: []
},
/**
* 客户端获取一条接口信息触发
* @param data 接口的详细信息
*/
interface_get: {
type: 'multi',
listener: []
},
/**
* 客户端增加一个新项目
* @param id 项目id
*/
project_add: {
type: 'multi',
listener: []
},
/**
* 客户端更新一个新项目
* @param id 项目id
*/
project_up: {
type: 'multi',
listener: []
},
/**
* 客户端获取一个项目
* @param id 项目id
*/
project_get: {
type: 'multi',
listener: []
},
/**
* 客户端删除删除一个项目
* @param id 项目id
*/
project_del: {
type: 'multi',
listener: []
},
/**
* 导出 markdown 数据
* @param context Object
* {
* projectData: project,
interfaceData: interfaceData,
ctx: ctx,
mockJson: res
* }
*
*/
export_markdown: {
type: 'multi',
listener: []
},
/**
* MockServer生成mock数据后触发
* @param context Object
* {
* projectData: project,
interfaceData: interfaceData,
ctx: ctx,
mockJson: res
* }
*
*/
mock_after: {
type: 'multi',
listener: []
},
/**
* 增加路由的钩子
* type Sync
* @param addPluginRouter Function
* @info
* addPLuginPLugin(config)
*
* config = {
* path, // String 路由名称
* method, // String 请求方法 get post ...
* controller // Class 继承baseController的class
* action // String controller的Action
* }
*
* 示例:
* config = {
* path: "export/pdf",
* method: "get",
* controller: controller,
* action: "exportPdf"
* }
*/
add_router: {
type: 'multi',
listener: []
},
/**
* 增加websocket路由的钩子
* type Sync
* @param addPluginRouter Function
* @info
* addPLuginPLugin(config)
*
* config = {
* path, // String 路由名称
* method, // String 请求方法 get post ...
* controller // Class 继承baseController的class
* action // String controller的Action
* }
*
* 示例:
* config = {
* path: "export/pdf",
* method: "get",
* controller: controller,
* action: "exportPdf"
* }
*/
add_ws_router: {
type: 'multi',
listener: []
},
import_data: {
type: 'multi',
listener: []
},
/**
* addNoticePlugin(config)
*
* config.weixin = {
* title: 'wechat',
* hander: (emails, title, content)=> {...}
* }
*/
addNotice:{
type: 'multi',
listener: []
}
};
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;
}
}
/**
*
* @param {*} hookname
* @return promise
*/
function emitHook(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 Promise.resolve(hooks[name].listener.apply(yapi, args));
}
let promiseAll = [];
if (Array.isArray(hooks[name].listener)) {
let listenerList = hooks[name].listener;
for (let i = 0, l = listenerList.length; i < l; i++) {
promiseAll.push(Promise.resolve(listenerList[i].apply(yapi, args)));
}
}
return Promise.all(promiseAll);
}
}
yapi.bindHook = bindHook;
yapi.emitHook = emitHook;
yapi.emitHookSync = emitHook;
let pluginsConfig = initPlugins(yapi.WEBCONFIG.plugins, 'plugin');
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')
)
) {
throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
}
let pluginModule = require(yapi.path.join(
plugin_path,
'yapi-plugin-' + plugin.name + '/server.js'
));
pluginModule.call(yapi, plugin.options);
});
extConfig = initPlugins(extConfig, 'ext');
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')
)
) {
throw new Error(`config.json配置了插件${plugin},但plugins目录没有找到此插件,请安装此插件`);
}
let pluginModule = require(yapi.path.join(
plugin_system_path,
'yapi-plugin-' + plugin.name + '/server.js'
));
pluginModule.call(yapi, plugin.options);
});
//delete bindHook方法,避免误操作
delete yapi.bindHook;