Skip to content

Commit

Permalink
Merge pull request #50 from Megapixel99/add-custom-plugins
Browse files Browse the repository at this point in the history
Configure options to allow custom plugins
  • Loading branch information
Megapixel99 authored Nov 2, 2023
2 parents c3a0074 + 02f131a commit 2045f04
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ Options:
- `options.coerce`: Enable data type [`coercion`](https://www.npmjs.com/package/ajv#coercing-data-types)
- `options.htmlui`: Turn on serving `redoc` or `swagger-ui` html ui
- `options.basePath`: When set, will strip the value of `basePath` from the start of every path.
The options object can also accept configuration parameters for Swagger and Redoc. The full list of Swagger and Redoc configuration options can be found here: https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ and here: https://redocly.com/docs/redoc/config/ respectively.

##### Coerce

Expand Down Expand Up @@ -283,11 +282,11 @@ Serve an interactive UI for exploring the OpenAPI document.
[Redoc](https://github.com/Rebilly/ReDoc/) and [SwaggerUI](https://www.npmjs.com/package/swagger-ui) are
two of the most popular tools for viewing OpenAPI documents and are bundled with the middleware.
They are not turned on by default but can be with the option mentioned above or by using one
of these middleware.
of these middleware. Both interactive UIs also accept an optional object as a function argument which accepts configuration parameters for Swagger and Redoc. The full list of Swagger and Redoc configuration options can be found here: https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ and here: https://redocly.com/docs/redoc/config/ respectively.

**Example:**

```javascript
app.use('/redoc', oapi.redoc)
app.use('/swaggerui', oapi.swaggerui)
app.use('/redoc', oapi.redoc())
app.use('/swaggerui', oapi.swaggerui())
```
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ module.exports = function ExpressOpenApi (_routePrefix, _doc, _opts) {
middleware.callbacks = middleware.component.bind(null, 'callbacks')

// Expose ui middleware
middleware.redoc = ui.serveRedoc(`${routePrefix}.json`, opts)
middleware.swaggerui = ui.serveSwaggerUI(`${routePrefix}.json`, opts)
middleware.redoc = (options) => ui.serveRedoc(`${routePrefix}.json`, options)
middleware.swaggerui = (options) => ui.serveSwaggerUI(`${routePrefix}.json`, options)

// OpenAPI document as json
router.get(`${routePrefix}.json`, (req, res) => {
Expand Down
25 changes: 11 additions & 14 deletions lib/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const path = require('path')
const serve = require('serve-static')

module.exports.serveRedoc = function serveRedoc (documentUrl, opts) {
module.exports.serveRedoc = function serveRedoc (documentUrl, opts = {}) {
const toKebabCase = (string) =>
string
.replace(/([a-z])([A-Z])/g, '$1-$2')
Expand All @@ -25,25 +25,22 @@ module.exports.serveRedoc = function serveRedoc (documentUrl, opts) {
}]
}

module.exports.serveSwaggerUI = function serveSwaggerUI (documentUrl, opts) {
const options = {
url: documentUrl,
dom_id: '#swagger-ui'
}
Object.keys(opts).forEach((key) => {
if (!['coerce', 'htmlui', 'basePath'].includes(key)) {
options[key] = opts[key]
}
})
module.exports.serveSwaggerUI = function serveSwaggerUI (documentUrl, opts = {}) {
const { plugins, ...options } = opts

return [serve(path.resolve(require.resolve('swagger-ui-dist'), '..'), { index: false }),
function returnUiInit (req, res, next) {
if (req.path.endsWith('/swagger-ui-init.js')) {
res.type('.js')
res.send(`window.onload = function () {
window.ui = SwaggerUIBundle(${JSON.stringify(options, null, 2)})
}
`)
window.ui = SwaggerUIBundle({
url: '${documentUrl}',
dom_id: '#swagger-ui',
${plugins?.length ? `plugins: [${plugins}],` : ''}
...${JSON.stringify(options)}
})
}`
)
} else {
next()
}
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ suite(name, function () {

test('create a basic valid Swagger UI document and check the HTML title', function (done) {
const app = express()
app.use(openapi().swaggerui)
app.use(openapi().swaggerui())
supertest(app)
.get(`${openapi.defaultRoutePrefix}.json`)
.end((err, res) => {
Expand All @@ -118,7 +118,7 @@ suite(name, function () {

test('serves onload function in swagger-ui-init.js file', function (done) {
const app = express()
app.use(openapi().swaggerui)
app.use(openapi().swaggerui())
supertest(app)
.get(`${openapi.defaultRoutePrefix}/swagger-ui-init.js`)
.end((err, res) => {
Expand All @@ -130,7 +130,7 @@ suite(name, function () {

test('create a basic valid ReDoc document and check the HTML title', function (done) {
const app = express()
app.use(openapi().redoc)
app.use(openapi().redoc())
supertest(app)
.get(`${openapi.defaultRoutePrefix}.json`)
.end((err, res) => {
Expand Down

0 comments on commit 2045f04

Please sign in to comment.