forked from scottie1984/swagger-ui-express
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (62 loc) · 2.45 KB
/
index.js
File metadata and controls
73 lines (62 loc) · 2.45 KB
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
'use strict'
var fs = require('fs');
var express = require('express');
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" />'
var setup = function (swaggerDoc, opts, options, customCss, customfavIcon, swaggerUrl, customeSiteTitle) {
var isExplorer
var customJs
if (opts && typeof opts === 'object') {
isExplorer = opts.explorer
options = opts.swaggerOptions
customCss = opts.customCss
customJs = opts.customJs
customfavIcon = opts.customfavIcon
swaggerUrl = opts.swaggerUrl
customeSiteTitle = opts.customSiteTitle
} else {
//support legacy params based function
isExplorer = opts
}
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');
try {
fs.unlinkSync(__dirname + '/index.html');
} catch (e) {
}
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 initOptions = {
swaggerDoc: swaggerDoc || undefined,
customOptions: options,
swaggerUrl: swaggerUrl || undefined
}
var htmlWithOptions = htmlWithCustomJs.replace('<% swaggerOptions %>', JSON.stringify(initOptions)).replace('<% title %>', customeSiteTitle)
return function (req, res) { res.send(htmlWithOptions) };
};
var serve = express.static(__dirname + '/static');
var stringify = function (obj, prop) {
var placeholder = '____FUNCTIONPLACEHOLDER____';
var fns = [];
var json = JSON.stringify(obj, function (key, value) {
if (typeof value === 'function') {
fns.push(value);
return placeholder;
}
return value;
}, 2);
json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function (_) {
return fns.shift();
});
return json + ';';
};
module.exports = {
setup: setup,
serve: serve
};