Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable use of CDN #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const serve = require('serve-static')

module.exports.serveSwaggerUI = function serveSwaggerUI (documentUrl, opts = {}) {
const { plugins, ...options } = opts
const { plugins, cdnBaseUrl, ...options } = opts

return [serve(path.resolve(require.resolve('swagger-ui-dist'), '..'), { index: false }),
function returnUiInit (req, res, next) {
Expand All @@ -24,12 +24,12 @@ module.exports.serveSwaggerUI = function serveSwaggerUI (documentUrl, opts = {})
},
function renderSwaggerHtml (req, res) {
res.type('html').send(renderHtmlPage('Swagger UI', `
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
<link rel="stylesheet" type="text/css" href="${cdnBaseUrl ?? '.'}/swagger-ui.css" >
`, `
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js"></script>
<script src="./swagger-ui-standalone-preset.js"></script>
<script src="./swagger-ui-init.js"></script>
<script src="${cdnBaseUrl ?? '.'}/swagger-ui-bundle.js"></script>
<script src="${cdnBaseUrl ?? '.'}/swagger-ui-standalone-preset.js"></script>
<script src="${cdnBaseUrl ?? '.'}/swagger-ui-init.js"></script>
`))
}
]
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,26 @@ suite(name, function () {
})
})

test('use a CDN', (done) => {
const app = express()
const oapi = openapi(undefined, { basePath: '/base-path' })

app.use(oapi)

app.get(oapi.routePrefix, oapi.swaggerui({ cdnBaseUrl: 'https://notARealURL' }))

supertest(app)
.get(oapi.routePrefix)
.expect(200, (err, res) => {
assert(!err, err)
assert(res.text.includes('<link rel="stylesheet" type="text/css" href="https://notARealURL/swagger-ui.css" >'))
assert(res.text.includes('<script src="https://notARealURL/swagger-ui-bundle.js"></script>'))
assert(res.text.includes('<script src="https://notARealURL/swagger-ui-standalone-preset.js"></script>'))
assert(res.text.includes('<script src="https://notARealURL/swagger-ui-init.js"></script>'))
done()
})
})

// Other tests
require('./_validate')()
require('./_routes')()
Expand Down
Loading