-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·119 lines (102 loc) · 4.18 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
'use strict';
//rootRequire for the library
global.libRequire = function (name) {
return require(__dirname + '/' + name);
};
module.exports = (app, options) => {
const fs = require('fs');
const _ = require('lodash');
const Model = require('./lib/models/models.js');
const generateEndpoints = require('./lib/routes/index.js').generateEndpoints;
const swaggerTools = require('swagger-tools');
const generateSwaggerfile = require('./lib/swagger/index.js').generateSwaggerFile;
const endpoints = rootRequire(options.endpoints);
//generate swagger documentation
let swaggerDoc = generateSwaggerfile(app, endpoints, options.models, options);
fs.writeFile(`${options.swagger.swaggerDoc}`, JSON.stringify(swaggerDoc));
//initialize swaggerUI and generate documentation
swaggerTools.initializeMiddleware(swaggerDoc, (middleware) => {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter({
controllers: options.controllers
}));
// serve documentation on /docs
app.use(middleware.swaggerUi());
});
//initialize our own middlewares for specific routes
endpoints.forEach(endpoint => {
if (!_.isEmpty(endpoint.middleWares)) {
endpoint.middleWares.forEach(mw => {
app.use(endpoint.path, rootRequire(options.middleware + '/' + mw));
})
}
});
//apply request validation to specific routes
endpoints.forEach(endpoint => {
if (!endpoint.autovalidate && endpoint.requestEntity) {
app.use(endpoint.path, (req, res, next) => {
req.requestEntity = new Model(endpoint.requestEntity);
try {
req.requestEntity.populateRequestEntity(req.body);
req.requestEntity.populatePathParams(req.params);
req.requestEntity.populateQueryParams(req.query);
req.requestEntity.throwIfErrors();
} catch (e) {
res.status(400).json(e.message);
return;
}
next();
});
}
});
const exposeToApigateway = (endpoints) => {
app.get('/v0.1/public-endpoints', (req, res, next) => {
let result = {};
endpoints.forEach((endpoint) => {
if (endpoint.via) {
result[endpoint.via] = result[endpoint.via] || [];
let entry = {
uri: endpoint.path,
method: endpoint.method,
protected_params: [],
params: []
};
if (endpoint.requestEntity) {
let model = new Model(endpoint.requestEntity);
_.mapKeys(model._scheme.query || {}, function(param, key) {
let paramEntry = {
name: key,
type: 'query'
};
if (param.source) {
paramEntry.source = param.source;
}
entry.params.push(paramEntry);
if (param.protected) {
entry.protected_params.push(paramEntry);
}
});
_.mapKeys(model._scheme.path || {}, function(param, key) {
let paramEntry = {
name: key,
type: 'path'
};
entry.params.push(paramEntry);
if (param.protected) {
entry.protected_params.push(paramEntry);
}
});
}
result[endpoint.via].push(entry);
}
});
res.json(result);
} ) } ;
//serve endpoints for apigateway
exposeToApigateway(endpoints);
return {
exposeToApigateway: exposeToApigateway
}
};