-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·366 lines (323 loc) · 12.5 KB
/
index.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
'use strict';
require('reflect-metadata');
const _ = require('lodash');
const nodePath = require('path');
const uuidV1 = require('uuid/v1');
const fs = require('fs');
const { Container, ContainerInstance } = require('typedi');
const { toSwaggerParams, mixedValidate } = require('koa-swagger-joi')
const { registerRouter } = require('./src/router')
const { Joi } = require('koa-swagger-joi').default
const CUSTOM_MIDDLEWARE_NAME = 'customMiddlewares';
const StatusError = require('./src/exception/status-error');
const ControllerHandler = require('./src/handler/controller-handler');
const MethodHandler = require('./src/handler/method-handler');
const { deepGet, urlFormat, deepClone, pathCLowercase } = require('./src/util');
const { generateResponse, getDefinitions } = require('./src/loader');
const contextIdProp = Symbol('controllerContextId');
const ctMap = new Map();
const ctHandler = new ControllerHandler();
const methodHandler = new MethodHandler(ctMap);
const swaggerHttpMethod = [ 'get', 'post', 'put', 'delete', 'patch' ];
Container.of = function (instanceId) {
if (instanceId === undefined)
return this.globalInstance;
var container = this.instances.find(function (instance) { return instance.id === instanceId; });
if (!container) {
container = new ContainerInstance(instanceId);
container.services.push(...this.globalInstance.services.map(s => ({
...s,
value: s.global ? s.value : undefined
})));
this.instances.push(container);
}
return container;
};
const EggShell = (app, options = {}) => {
const routerInfoList = [];
const { router } = app;
const ctrlFnList = [];
const urlNamingStrategy = options.urlNamingStrategy;
// 设置全局路由前缀
if (options.prefix) router.prefix(options.prefix);
let swaggerJson = null;
let validateSwaggerJson = null;
// 开启swagger
let swaggerOpt = null;
if (options.swaggerOpt && options.swaggerOpt.open) {
registerRouter(app);
swaggerOpt = options.swaggerOpt;
swaggerOpt.definitionPath = swaggerOpt.definitionPath || './app/definition';
swaggerJson = {
swagger: '2.0',
info: {
title: swaggerOpt.title || '',
version: swaggerOpt.version || '',
},
basePath: swaggerOpt.basePath || (options.prefix === '/' ? '' : options.prefix),
schemes: swaggerOpt.schemes || [ 'http' ],
tags: [],
paths: {},
definitions: {}
};
validateSwaggerJson = deepClone(swaggerJson);
// definition
if (swaggerOpt.definitionPath) {
const definition = getDefinitions(app, swaggerOpt.definitionPath);
swaggerJson.definitions = definition;
validateSwaggerJson.definitions = definition;
}
}
for (const c of ctMap.values()) {
// 获取控制器元数据
let { ignoreJwtAll, prefix, tagsAll, beforeAll, afterAll, hiddenAll, renderController } = ctHandler.getMetada(c.constructor);
if (renderController) {
ignoreJwtAll = true;
}
// 获取类自定义的属性名和方法名
const propertyNames = _.filter(Object.getOwnPropertyNames(c), pName => {
return pName !== 'constructor' && pName !== 'pathName' && pName !== 'fullPath';
});
// 解析前缀
const fullPath = c.fullPath.
split('\\').join('/').
replace(/[\/]{2,9}/g, '/').
replace(/(\.ts)|(\.js)/g, '');
const rootPath = 'controller/';
const ctrlRootPath = fullPath.substring(fullPath.indexOf(rootPath) + rootPath.length);
// 如果这个控制器没有通过装饰器设置prefix,就按照文件路径作为前缀
prefix = urlFormat(urlNamingStrategy, prefix || ctrlRootPath);
prefix = prefix.startsWith('/') ? prefix : '/' + prefix;
// 获取swagger映射
// let loadParameters = null;
if (swaggerOpt && swaggerOpt.open) {
if (tagsAll && !hiddenAll) {
if (!tagsAll.name) tagsAll.name = prefix;
swaggerJson.tags.push(tagsAll);
validateSwaggerJson.tags.push(tagsAll);
}
}
for (const pName of propertyNames) {
// 解析函数元数据
let {reqMethod, path = "", before, after, responseMessage, responseErrorMessage, responseCode, responseErrorCode, ignoreJwt, tags, summary, description, parameters, responses, produces, consumes, hidden, render} = methodHandler.getMetada(c[pName]);
if (render) {
hidden = true;
ignoreJwt = true;
}
if (!reqMethod) {
continue
}
if (path === '/') {
path = '';
}
reqMethod.split(',').forEach(requestMethod => {
requestMethod = requestMethod.toLowerCase();
if (swaggerOpt && swaggerOpt.open && !hiddenAll && !hidden) {
let finallyPath = prefix + path;
if (!path && pName !== 'index') {
finallyPath += ("/" + pName)
}
finallyPath = urlFormat(urlNamingStrategy, replaceColon(finallyPath));
let params = typeof parameters === 'function' ? parameters(finallyPath) : parameters;
let validateParameters = params instanceof Array ? null : params;
let swaggerParameters = params instanceof Array ? params : toSwaggerParams(params);
let swaggerResponses = generateResponse(fullPath, ctrlRootPath, responses, swaggerJson.definitions);
if (validateParameters && validateParameters.headers) {
// 不对header参数做效验
delete validateParameters.headers;
}
if (options.jwtValidationName && !ignoreJwtAll && !ignoreJwt) {
swaggerParameters.unshift({
name: 'Authorization', in: 'header', description: 'Token', type: 'string'
});
}
if (swaggerHttpMethod.indexOf(requestMethod) >= 0) {
if (!swaggerJson.paths[finallyPath]) {
swaggerJson.paths[finallyPath] = {};
validateSwaggerJson.paths[finallyPath] = {};
}
const swaggerPaths = {
tags: ((tags && !Array.isArray(tags)) ? [tags] : tags) || [prefix],
summary: summary || description,
description,
produces: (produces && !Array.isArray(produces)) ? [produces] : produces,
consumes: (consumes && !Array.isArray(consumes)) ? [consumes] : consumes,
responses: swaggerResponses
};
swaggerJson.paths[finallyPath][requestMethod] = { ...swaggerPaths, parameters: swaggerParameters }
validateSwaggerJson.paths[finallyPath][requestMethod] = { ...swaggerPaths, parameters: validateParameters }
}
}
const routerCb = async(ctx, next) => {
ctx[contextIdProp] = uuidV1();
const initCtx = (target) => {
target.ctx = ctx;
target.app = ctx.app;
target.config = ctx.app.config;
target.service = ctx.service;
target[contextIdProp] = ctx[contextIdProp];
}
const injectContext = (obj) => {
Object.getOwnPropertyNames(obj).map(prop => {
if (!!obj[prop] && typeof obj[prop] === 'object') {
const type = obj[prop].constructor;
if (obj[prop][contextIdProp] !== ctx[contextIdProp] && (Container.has(type) || Container.has(type.name))) {
initCtx(obj[prop]);
injectContext(obj[prop]);
}
}
});
}
// 实例化控制器类
let instance = new c.constructor(ctx);
if (!instance.ctx && !instance.app) {
instance = Container.of(ctx[contextIdProp]).get(c.constructor);
initCtx(instance);
injectContext(instance);
}
try {
ctx.body = ctx.request ? ctx.request.body : null;
ctx.state.ctrlInfo = {
responseCode,
responseErrorCode,
responseMessage,
responseErrorMessage,
result: null
};
// 执行控制器中的action方法, 并把返回结果绑定到ctx上
const result = await instance[pName](ctx, next);
if (render) {
ctx.set('Content-Type', 'text/html; charset=utf-8');
return await ctx.render(render, result);
}
// 只有开启自动返回内容选项, 才会自动调用后面的中间件。不然得在控制器里自己调用next方法
if (options.autoResponse) {
ctx.state.ctrlInfo.result = result;
// 注意! 如果控制器有return结果, 那么我们这里强制调用一次next方法, 确保中间件继续往下走
if (result !== undefined) {
await next()
}
}
Container.reset(ctx[contextIdProp]);
} catch (error) {
throw error
}
};
const { routerAfterMiddleware: afterList = [], routerBeforeMiddleware: beforeList = [] } = app.config;
const getMiddlewareList = arr => arr.map(item => {
let middlewarePath = item;
if (typeof item === 'function') {
Object.getOwnPropertySymbols(item).forEach(key => {
if (typeof item[key] !== 'string') {
return;
}
let temp = item[key].replace(/\\/g, '/');
temp = temp.split('/middleware/');
if (temp.length === 2) {
middlewarePath = temp[1].substr(0, temp[1].lastIndexOf('.')).replace('/', '.');
}
})
}
const pathList = pathCLowercase(middlewarePath.replace('.', '/')).split('/');
const fn = deepGet(app.middlewares, pathList);
const config = deepGet(app.config, middlewarePath.split('.')) || deepGet(app.config, pathList)
if (fn) {
return fn(config, app)
}
}).filter(item => !!item);
var ctrlBefores = getMiddlewareList(beforeAll);
var ctrlAfters = getMiddlewareList(afterAll);
var actBefores = getMiddlewareList(before);
var actAfters = getMiddlewareList(after);
const routerPath = urlFormat(urlNamingStrategy, nodePath.join(prefix, path || pName));
const afterWares = getMiddlewareList(afterList);
const beforeWares = getMiddlewareList(beforeList.filter(name => {
// 判断该路由是否忽略jwt效验, 如果忽略就跳过
const ret = !(((options.jwtValidationName === name || options.jwtValidationName === name.replace(name[0], name[0].toUpperCase()))
&& (ignoreJwt || ignoreJwtAll)));
routerInfoList.push({
method: requestMethod,
path: routerPath,
verifyJwt: ret,
});
return ret;
}));
const finalBefores = beforeWares.concat(ctrlBefores).concat(actBefores);
const finalAfters = actAfters.concat(ctrlAfters).concat(afterWares);
const rtFn = ((routerPath, _prefix, _path, _pName, _reqMethod, _finalBefores, _finalAfters, _routerCb) => () => {
let routerIndexPath;
router[_reqMethod](routerPath, ..._finalBefores, _routerCb, ..._finalAfters);
if (_pName === "index") {
routerIndexPath = urlFormat(urlNamingStrategy, nodePath.join(_prefix, _path || ""))
router[_reqMethod](routerIndexPath, ..._finalBefores, _routerCb, ..._finalAfters);
}
if (options.defaultIndex === routerPath || options.defaultIndex === routerIndexPath) {
router.get('/', ..._finalBefores, _routerCb, ..._finalAfters);
}
})(routerPath, prefix, path, pName, requestMethod, finalBefores, finalAfters, routerCb)
ctrlFnList.push(rtFn)
});
}
}
app.use(mixedValidate(validateSwaggerJson));
ctrlFnList.map(fn => fn());
if (swaggerOpt && swaggerOpt.open) {
const outPath = nodePath.join(nodePath.join(__dirname, './public/swagger-ui/json/api-docs.json'));
try {
fs.statSync(outPath);
fs.writeFileSync(outPath, JSON.stringify(swaggerJson), { encoding: 'utf8' });
} catch(e) {
throw e
}
}
return {
routerInfoList,
};
};
const paramsRegex = /:[\w-]*/g;
function replaceColon (path) {
const matchs = paramsRegex.exec(path);
if (!matchs) return path;
const pathItem = matchs[0].replace(':', '{') + '}';
path = path.replace(matchs[0], pathItem);
return replaceColon(path);
}
module.exports = {
CUSTOM_MIDDLEWARE_NAME,
Joi,
EggShell,
StatusError,
Get: methodHandler.get(),
Post: methodHandler.post(),
Put: methodHandler.put(),
Delete: methodHandler.delete(),
Patch: methodHandler.patch(),
Options: methodHandler.options(),
Head: methodHandler.head(),
Before: methodHandler.before(),
After: methodHandler.after(),
ResponseMessage: methodHandler.message(),
ResponseErrorMessage: methodHandler.errorMessage(),
ResponseCode: methodHandler.responseCode(),
ResponseErrorCode: methodHandler.responseErrorCode(),
IgnoreJwt: methodHandler.ignoreJwt(),
// Tags: methodHandler.tags(),
Summary: methodHandler.summary(),
Description: methodHandler.description(),
Parameters: methodHandler.parameters(),
Responses: methodHandler.responses(),
Produces: methodHandler.produces(),
Consumes: methodHandler.consumes(),
Hidden: methodHandler.hidden(),
// TokenType: methodHandler.tokenType(),
Render: methodHandler.render(),
IgnoreJwtAll: ctHandler.ignoreJwtAll(),
BeforeAll: ctHandler.beforeAll(),
AfterAll: ctHandler.afterAll(),
Prefix: ctHandler.prefix(),
TagsAll: ctHandler.tagsAll(),
Controller: ctHandler.tagsAll(),
HiddenAll: ctHandler.hiddenAll(),
TokenTypeAll: ctHandler.tokenTypeAll(),
View: ctHandler.renderController()
};