Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagger-ui-express

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-ui-express - npm Package Compare versions

Comparing version 4.0.7 to 4.1.0

110

index.js
'use strict'
var fs = require('fs');
var express = require('express');
var fs = require('fs')
var express = require('express')
var swaggerUi = require('swagger-ui-dist')
var htmlTplString = fs.readFileSync(__dirname + '/indexTemplate.html.tpl')
var jsTplString = fs.readFileSync(__dirname + '/swagger-ui-init.js.tpl')
var favIconHtml = '<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />' +
'<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />'
'<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />'
var swaggerInit
var swaggerInit = ''
var generateHTML = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle) {
var generateHTML = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle, _htmlTplString, _jsTplString) {
var isExplorer

@@ -25,3 +27,3 @@ var customJs

isExplorer = opts.explorer || !!swaggerUrls
customeSiteTitle = opts.customSiteTitle,
customeSiteTitle = opts.customSiteTitle
customCssUrl = opts.customCssUrl

@@ -32,37 +34,40 @@ } else {

}
options = options || {};
var explorerString = isExplorer ? '' : '.swagger-ui .topbar .download-url-wrapper { display: none }';
customCss = explorerString + ' ' + customCss || explorerString;
customfavIcon = customfavIcon || false;
customeSiteTitle = customeSiteTitle || 'Swagger UI';
var html = fs.readFileSync(__dirname + '/indexTemplate.html.tpl');
try {
fs.unlinkSync(__dirname + '/index.html');
} catch (e) {
options = options || {}
var explorerString = isExplorer ? '' : '.swagger-ui .topbar .download-url-wrapper { display: none }'
customCss = explorerString + ' ' + customCss || explorerString
customfavIcon = customfavIcon || false
customeSiteTitle = customeSiteTitle || 'Swagger UI'
_htmlTplString = _htmlTplString || htmlTplString
_jsTplString = _jsTplString || jsTplString
}
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 htmlWithCustomCssUrl = htmlWithCustomJs.replace('<% customCssUrl %>', customCssUrl ? `<link href="${customCssUrl}" rel="stylesheet">` : '')
var favIconString = customfavIcon ? '<link rel="icon" href="' + customfavIcon + '" />' : favIconHtml;
var htmlWithCustomCss = html.toString().replace('<% customCss %>', customCss);
var htmlWithFavIcon = htmlWithCustomCss.replace('<% favIconString %>', favIconString);
var htmlWithCustomJs = htmlWithFavIcon.replace('<% customJs %>', customJs ? `<script src="${customJs}"></script>` : '');
var htmlWithCustomCssUrl = htmlWithCustomJs.replace('<% customCssUrl %>', customCssUrl ? `<link href="${customCssUrl}" rel="stylesheet">` : '');
var initOptions = {
swaggerDoc: swaggerDoc || undefined,
customOptions: options,
swaggerUrl: swaggerUrl || undefined,
swaggerUrls: swaggerUrls || undefined
}
var initOptions = {
swaggerDoc: swaggerDoc || undefined,
customOptions: options,
swaggerUrl: swaggerUrl || undefined,
swaggerUrls: swaggerUrls || undefined
}
var js = fs.readFileSync(__dirname + '/swagger-ui-init.js.tpl');
swaggerInit = js.toString().replace('<% swaggerOptions %>', stringify(initOptions))
return htmlWithCustomCssUrl.replace('<% title %>', customeSiteTitle)
swaggerInit = _jsTplString.toString().replace('<% swaggerOptions %>', stringify(initOptions))
return htmlWithCustomCssUrl.replace('<% title %>', customeSiteTitle)
}
var setup = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle) {
var htmlWithOptions = generateHTML(swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle)
return function (req, res) { res.send(htmlWithOptions) };
};
var html = generateHTML(swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle, htmlTplString, jsTplString)
return function (req, res) {
if (req.swaggerDoc) {
var reqHtml = generateHTML(swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle, htmlTplString, jsTplString)
res.send(reqHtml)
} else {
res.send(html)
}
}
}
function swaggerInitFn (req, res, next) {
var swaggerInitFn = function (req, res, next) {
if (req.url === '/swagger-ui-init.js') {

@@ -77,4 +82,3 @@ res.set('Content-Type', 'application/javascript')

var swaggerInitFunction = function (swaggerDoc, opts) {
var js = fs.readFileSync(__dirname + '/swagger-ui-init.js.tpl');
var swaggerInitFile = js.toString().replace('<% swaggerOptions %>', stringify(opts))
var swaggerInitFile = jsTplString.toString().replace('<% swaggerOptions %>', stringify(opts))
return function (req, res, next) {

@@ -90,6 +94,2 @@ if (req.url === '/swagger-ui-init.js') {

function endsWith(origin, target) {
origin.substr(target.length * -1) === target
}
var swaggerAssetMiddleware = options => {

@@ -113,27 +113,27 @@ var opts = options || {}

var serve = [swaggerInitFn, swaggerAssetMiddleware()];
var serveWithOptions = options => [swaggerInitFn, swaggerAssetMiddleware(options)];
var serve = [swaggerInitFn, swaggerAssetMiddleware()]
var serveWithOptions = options => [swaggerInitFn, swaggerAssetMiddleware(options)]
var stringify = function (obj, prop) {
var placeholder = '____FUNCTIONPLACEHOLDER____';
var fns = [];
var placeholder = '____FUNCTIONPLACEHOLDER____'
var fns = []
var json = JSON.stringify(obj, function (key, value) {
if (typeof value === 'function') {
fns.push(value);
return placeholder;
fns.push(value)
return placeholder
}
return value;
}, 2);
return value
}, 2)
json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function (_) {
return fns.shift();
});
return 'var options = ' + json + ';';
};
return fns.shift()
})
return 'var options = ' + json + ';'
}
module.exports = {
setup: setup,
serve: serve,
setup: setup,
serve: serve,
serveWithOptions: serveWithOptions,
generateHTML: generateHTML,
serveFiles: serveFiles
};
}
{
"name": "swagger-ui-express",
"version": "4.0.7",
"version": "4.1.0",
"description": "Swagger UI Express",

@@ -14,3 +14,4 @@ "main": "./index.js",

"scripts": {
"test": "./node_modules/.bin/mocha test/*"
"test": "./node_modules/.bin/mocha test/*",
"test-app": "node ./test/testapp/run.js "
},

@@ -17,0 +18,0 @@ "keywords": [

@@ -38,4 +38,4 @@ # Swagger UI Express

router.use('/', swaggerUi.serve);
router.get('/', swaggerUi.setup(swaggerDocument));
router.use('/api-docs', swaggerUi.serve);
router.get('/api-docs', swaggerUi.setup(swaggerDocument));
```

@@ -212,2 +212,21 @@

### Modify swagger file on the fly before load
To dynamically set the host, or any other content, in the swagger file based on the incoming request object you may pass the json via the req object; to achieve this just do not pass the the swagger json to the setup function and it will look for `swaggerDoc` in the `req` object.
```javascript
const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/api-docs', function(req, res, next){
swaggerDocument.host = req.get('host');
req.swaggerDoc = swaggerDoc;
next();
}, swaggerUi.serve, swaggerUi.setup());
```
## Requirements

@@ -214,0 +233,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc