forked from BRIKEV/express-jsdoc-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (40 loc) · 1.34 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
const swaggerUi = require('swagger-ui-express');
const merge = require('merge');
const processSwagger = require('./processSwagger');
const swaggerEvents = require('./swaggerEvents');
const DEFAULT_SWAGGERUI_URL = '/api-docs';
const DEFAULT_EXPOSE_SWAGGERUI = true;
const DEFAULT_APIDOCS_URL = '/v3/api-docs';
const DEFAULT_EXPOSE_APIDOCS = false;
const expressJSDocSwagger = app => (options = {}, userSwagger = {}) => {
const events = swaggerEvents();
const { instance } = events;
let swaggerObject = {};
processSwagger(options, events.processFile)
.then(result => {
swaggerObject = {
...swaggerObject,
...result,
};
swaggerObject = merge.recursive(true, swaggerObject, userSwagger);
events.finish(swaggerObject);
})
.catch(events.error);
if (options.exposeSwaggerUI || DEFAULT_EXPOSE_SWAGGERUI) {
app.use(options.swaggerUIPath || DEFAULT_SWAGGERUI_URL, (req, res, next) => {
swaggerObject = {
...swaggerObject,
host: req.get('host'),
};
req.swaggerDoc = swaggerObject;
next();
}, swaggerUi.serve, swaggerUi.setup());
}
if (options.exposeApiDocs || DEFAULT_EXPOSE_APIDOCS) {
app.get(options.apiDocsPath || DEFAULT_APIDOCS_URL, (req, res) => {
res.json(swaggerObject);
});
}
return instance;
};
module.exports = expressJSDocSwagger;