-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SwaggerUIViewer middleware plugin
Redirect root requests to SwaggerUI
- Loading branch information
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Middleware to generate Swagger UI viewer. | ||
* | ||
* @requires AppConfigPlugin | ||
*/ | ||
module.exports = (swaggerJson) => { | ||
return (req, res, next) => { | ||
const {development} = req.plugin('config'); | ||
|
||
const swaggerUi = req.param('swagger-ui'); | ||
|
||
if (development && swaggerJson && swaggerUi) { | ||
let contentType, contentBody; | ||
|
||
if (swaggerUi === 'json') { | ||
contentType = 'application/json'; | ||
contentBody = swaggerJson; | ||
} else { | ||
contentType = 'text/html'; | ||
contentBody = htmlMarkup(req.cfReqObj.uri); | ||
} | ||
|
||
res.setHeader('Content-Type', contentType); | ||
res.status(200).send(contentBody); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
}; | ||
|
||
/** | ||
* Return Swagger UI HTML markup (dependency free). | ||
* | ||
* @param {String} path | ||
* URI path. | ||
* | ||
* @return {String} | ||
*/ | ||
function htmlMarkup(path) { | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta | ||
name="description" | ||
content="SwaggerUI" | ||
/> | ||
<title>SwaggerUI</title> | ||
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css" /> | ||
</head> | ||
<body> | ||
<div id="swagger-ui"></div> | ||
<script src="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js" crossorigin></script> | ||
<script> | ||
window.onload = () => { | ||
window.ui = SwaggerUIBundle({ | ||
url: '${path}?swagger-ui=json', | ||
dom_id: '#swagger-ui', | ||
}); | ||
}; | ||
</script> | ||
</body> | ||
</html> | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters