-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
60 lines (52 loc) · 1.67 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
const swaggerUi = require('swagger-ui-express');
const merge = require('merge');
const defaultOptions = require('./config/default');
const swaggerEventsOptions = require('./config/swaggerEvents');
const processSwagger = require('./processSwagger');
const swaggerEvents = require('./swaggerEvents');
const expressJSDocSwagger = app => (userOptions = {}, userSwagger = {}) => {
const events = swaggerEvents(swaggerEventsOptions(userOptions));
const { instance } = events;
let swaggerObject = {};
const options = {
...defaultOptions,
...userOptions,
};
processSwagger(options, events.processFile)
.then(result => {
swaggerObject = {
...swaggerObject,
...result.swaggerObject,
};
swaggerObject = merge.recursive(true, swaggerObject, userSwagger);
events.finish(swaggerObject, {
jsdocInfo: result.jsdocInfo,
getPaths: result.getPaths,
getComponents: result.getComponents,
getTags: result.getTags,
});
})
.catch(events.error);
if (options.exposeSwaggerUI) {
app.use(options.swaggerUIPath, (req, res, next) => {
swaggerObject = {
...swaggerObject,
host: req.get('host'),
};
req.swaggerDoc = swaggerObject;
next();
}, swaggerUi.serve, swaggerUi.setup(undefined, options.swaggerUiOptions));
}
if (options.exposeApiDocs) {
app.get(options.apiDocsPath, (req, res) => {
res.json({
...swaggerObject,
// we skipped this as is not a valid prop in OpenAPI
// This is only being used in the SwaggerUI Library
host: undefined,
});
});
}
return instance;
};
module.exports = expressJSDocSwagger;