Skip to content

Commit

Permalink
feat: Enable use of CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
Megapixel99 committed Oct 18, 2024
1 parent e3b4d2f commit 2a04d87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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

0 comments on commit 2a04d87

Please sign in to comment.