Skip to content

Commit

Permalink
fix dynamic loading for serveFiles and added customJsStr
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Scott committed May 13, 2022
1 parent 8cc6416 commit ec32130
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

var options = {}

app.use('/api-docs', function(req, res, next){
swaggerDocument.host = req.get('host');
req.swaggerDoc = swaggerDocument;
next();
}, swaggerUi.serve, swaggerUi.setup());
}, swaggerUi.serveFiles(swaggerDocument, options), swaggerUi.setup());
```

### Two swagger documents
Expand Down
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var htmlTplString = `
<script src="./swagger-ui-standalone-preset.js"> </script>
<script src="./swagger-ui-init.js"> </script>
<% customJs %>
<% customJsStr %>
<% customCssUrl %>
<style>
<% customCss %>
Expand Down Expand Up @@ -140,12 +141,14 @@ window.onload = function() {
var generateHTML = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customSiteTitle, _htmlTplString, _jsTplString) {
var isExplorer
var customJs
var customJsStr
var swaggerUrls
var customCssUrl
if (opts && typeof opts === 'object') {
options = opts.swaggerOptions
customCss = opts.customCss
customJs = opts.customJs
customJsStr = opts.customJsStr
customfavIcon = opts.customfavIcon
swaggerUrl = opts.swaggerUrl
swaggerUrls = opts.swaggerUrls
Expand All @@ -167,7 +170,8 @@ var generateHTML = function (swaggerDoc, opts, options, customCss, customfavIcon
var favIconString = customfavIcon ? '<link rel="icon" href="' + customfavIcon + '" />' : favIconHtml
var htmlWithCustomCss = _htmlTplString.toString().replace('<% customCss %>', customCss)
var htmlWithFavIcon = htmlWithCustomCss.replace('<% favIconString %>', favIconString)
var htmlWithCustomJs = htmlWithFavIcon.replace('<% customJs %>', customJs ? `<script src="${customJs}"></script>` : '')
var htmlWithCustomJsUrl = htmlWithFavIcon.replace('<% customJs %>', customJs ? `<script src="${customJs}"></script>` : '')
var htmlWithCustomJs = htmlWithCustomJsUrl.replace('<% customJsStr %>', customJsStr ? `<script>${customJsStr}</script>` : '')
var htmlWithCustomCssUrl = htmlWithCustomJs.replace('<% customCssUrl %>', customCssUrl ? `<link href="${customCssUrl}" rel="stylesheet">` : '')

var initOptions = {
Expand Down Expand Up @@ -210,6 +214,9 @@ var swaggerInitFunction = function (swaggerDoc, opts) {
if (trimQuery(req.url) === '/package.json') {
res.sendStatus(404)
} else if (trimQuery(req.url) === '/swagger-ui-init.js') {
if (req.swaggerDoc) {
swaggerInitFile = jsTplString.toString().replace('<% swaggerOptions %>', stringify(opts))
}
res.set('Content-Type', 'application/javascript')
res.send(swaggerInitFile)
} else {
Expand Down
20 changes: 20 additions & 0 deletions test/testapp/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ var swaggerHtml = swaggerUi.generateHTML(swaggerDocument, swaggerUiOpts)
app.use('/api-docs-html1', swaggerUi.serveFiles(swaggerDocument, swaggerUiOpts))
app.get('/api-docs-html1', (req, res) => { res.send(swaggerHtml) });

var swaggerUiOpts3 = {
explorer: false,
swaggerOptions: options,
customCss: '.swagger-ui .topbar { background-color: pink }',
swaggerUrl: '/swagger.json',
customJsStr: 'window.alert("123")',
operationsSorter: 'alpha',
}

app.use('/api-docs-jsstr', swaggerUi.serve)
app.get('/api-docs-jsstr', swaggerUi.setup(null, swaggerUiOpts3));

// let count = 0
// app.use('/api-docs-dynamic', function(req, res, next){
// count = count + 1
// swaggerDocument.info.description = `Hello ${count}!`;
// req.swaggerDoc = swaggerDocument;
// next();
// }, swaggerUi.serveFiles(swaggerDocument, options), swaggerUi.setup());

app.use(function(req, res) {
res.send(404, 'Page not found');
});
Expand Down

0 comments on commit ec32130

Please sign in to comment.