Skip to content

Commit 9c26670

Browse files
authored
Merge pull request #115 from mateusbs17/master
Use swagger-ui-dist instead of swagger-ui
2 parents 2a449f7 + 22758bd commit 9c26670

File tree

6 files changed

+193
-111
lines changed

6 files changed

+193
-111
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ sudo: false
44

55
node_js:
66
- "6"
7-
- "8"
8-
- "node"
7+
- "10"

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ function initApp(options) {
8080
app.conf.spec = {};
8181
}
8282
}
83-
if (!app.conf.spec.swagger) {
84-
app.conf.spec.swagger = '2.0';
83+
if (!app.conf.spec.openapi) {
84+
app.conf.spec.openapi = '3.0.0';
8585
}
8686
if (!app.conf.spec.info) {
8787
app.conf.spec.info = {

lib/swagger-ui.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const fs = BBPromise.promisifyAll(require('fs'));
55
const path = require('path');
66
const HTTPError = require('../lib/util.js').HTTPError;
77

8-
// Swagger-ui helpfully exporting the absolute path of its dist directory
9-
const docRoot = `${require('swagger-ui').dist}/`;
8+
// Swagger-ui-dist helpfully exporting the absolute path of its dist directory
9+
const docRoot = `${require('swagger-ui-dist').getAbsoluteFSPath()}/`;
1010

1111
function processRequest(app, req, res) {
1212

@@ -27,25 +27,48 @@ function processRequest(app, req, res) {
2727
return fs.readFileAsync(filePath)
2828
.then((body) => {
2929
if (reqPath === '/index.html') {
30+
const css = `
31+
/* Removes Swagger's image from the header bar */
32+
.topbar-wrapper .link img {
33+
display: none;
34+
}
35+
/* Adds the application's name in the header bar */
36+
.topbar-wrapper .link::after {
37+
content: "${app.info.name}";
38+
}
39+
/* Removes input field and explore button from header bar */
40+
.swagger-ui .topbar .download-url-wrapper {
41+
display: none;
42+
}
43+
/* Modifies the font in the information area */
44+
.swagger-ui .info li, .swagger-ui .info p, .swagger-ui .info table, .swagger-ui .info a {
45+
font-size: 16px;
46+
line-height: 1.4em;
47+
}
48+
/* Removes authorize button and section */
49+
.scheme-container {
50+
display: none
51+
}
52+
`;
3053
body = body.toString()
3154
.replace(/((?:src|href)=['"])/g, '$1?doc&path=')
3255
// Some self-promotion
33-
.replace(/<a id="logo".*?<\/a>/,
34-
`<a id="logo" href="${app.info.homepage}">${app.info.name}</a>`)
56+
.replace(/<\/style>/, `${css}\n </style>`)
3557
.replace(/<title>[^<]*<\/title>/, `<title>${app.info.name}</title>`)
3658
// Replace the default url with ours, switch off validation &
3759
// limit the size of documents to apply syntax highlighting to
38-
.replace(/docExpansion: "none"/, 'docExpansion: "list", ' +
39-
'validatorUrl: null, ' +
40-
'highlightSizeThreshold: 10000')
41-
.replace(/ url: url,/, 'url: "/?spec",');
60+
.replace(/dom_id: '#swagger-ui'/, 'dom_id: "#swagger-ui", ' +
61+
'docExpansion: "none", defaultModelsExpandDepth: -1, validatorUrl: null, displayRequestDuration: true')
62+
.replace(/"https:\/\/petstore.swagger.io\/v2\/swagger.json"/,
63+
'"/?spec"');
4264
}
4365

4466
let contentType = 'text/html';
4567
if (/\.js$/.test(reqPath)) {
4668
contentType = 'text/javascript';
4769
body = body.toString()
48-
.replace(/underscore-min\.map/, '?doc&path=lib/underscore-min.map');
70+
.replace(/underscore-min\.map/, '?doc&path=lib/underscore-min.map')
71+
.replace(/sourceMappingURL=/, 'sourceMappingURL=/?doc&path=');
4972
} else if (/\.png$/.test(reqPath)) {
5073
contentType = 'image/png';
5174
} else if (/\.map$/.test(reqPath)) {
@@ -54,11 +77,13 @@ function processRequest(app, req, res) {
5477
contentType = 'application/x-font-ttf';
5578
} else if (/\.css$/.test(reqPath)) {
5679
contentType = 'text/css';
57-
body = body.toString().replace(/\.\.\/(images|fonts)\//g, '?doc&path=$1/');
80+
body = body.toString()
81+
.replace(/\.\.\/(images|fonts)\//g, '?doc&path=$1/')
82+
.replace(/sourceMappingURL=/, 'sourceMappingURL=/?doc&path=');
5883
}
5984

6085
res.setHeader('Content-Type', contentType);
61-
res.setHeader('content-security-policy', "default-src 'none'; " +
86+
res.setHeader('Content-Security-Policy', "default-src 'none'; " +
6287
"script-src 'self' 'unsafe-inline'; connect-src *; " +
6388
"style-src 'self' 'unsafe-inline'; img-src 'self'; font-src 'self';");
6489
res.send(body.toString());

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "service-template-node",
3-
"version": "0.5.9",
3+
"version": "0.6.0",
44
"description": "A blueprint for MediaWiki REST API services",
55
"main": "./app.js",
66
"scripts": {
@@ -42,7 +42,7 @@
4242
"preq": "^0.5.6",
4343
"service-runner": "^2.6.4",
4444
"swagger-router": "^0.7.2",
45-
"swagger-ui": "git+https://github.com/wikimedia/swagger-ui#master"
45+
"swagger-ui-dist": "^3.22.1"
4646
},
4747
"devDependencies": {
4848
"ajv": "^6.5.4",

0 commit comments

Comments
 (0)