swagger-yaml-webpack-plugin
v1.0.2
Published
A Webpack plugin to inject Swagger yaml definitions into the Node.js bundle as global JSON object at compile time in order to serve as desired like through REST API
Downloads
14
Maintainers
Readme
Swagger Yaml Webpack Plugin
Swagger Yaml Webpack Plugin provides you to bundle your swagger yaml documentation into your Node.js output code as a globally accessible variable in JSON format during build time so that you can serve that content anyway you wish such as HTTP response.
Install
npm i --save-dev swagger-yaml-webpack-plugin
Usage
Webpack Config:
const path = require("path");
const SwaggerYamlWebpackPlugin = require("swagger-yaml-webpack-plugin");
module.exports = () => {
return {
// ...
plugins: [
new SwaggerYamlWebpackPlugin({
varName: "SWAGGER_DOC",
directory: path.resolve(__dirname, "./src/apidocs/swagger")
})
]
};
};
It will convert yaml documents in the given directory to JSON string and assign it to SWAGGER_DOC
.
const http = require("http");
http.createServer((req, res) => {
let output;
if (req.url === '/apidoc') {
if (typeof SWAGGER_DOC !== "undefined") {
output = SWAGGER_DOC; // swagger-ui-express can be used together with Express.js for graceful UI.
} else {
output = "No docs found!"
}
} else {
output = "Hello world!";
}
res.write(output);
res.end();
})
.listen(8080);
It will serve Swagger data as JSON through /apidoc
endpoint. If you use Expess.js and would like to serve the Swagger doc with a graceful UI, you can use swagger-ui-express.