koa2-swagger-ui
Host swagger ui at a given directory from your koa v2 app
Inspired by:
install
npm install koa2-swagger-ui --save
config
for more swaggerOptions see swagger-ui
defaults:
title: 'swagger',
oauthOptions: {},
swaggerOptions: {
dom_id: 'swagger-ui-container',
url: 'http://petstore.swagger.io/v2/swagger.json',
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
docExpansion: 'none',
jsonEditor: false,
defaultModelRendering: 'schema',
showRequestHeaders: false,
swaggerVersion: 'x.x.x'
validatorUrl: null,
},
routePrefix: '/docs',
specPrefix: '/docs/spec',
exposeSpec: false,
hideTopbar: false,
favicon: '/favicon.png',
customCSS: `h1 { color: red }`,
example
import Koa from 'koa';
import { koaSwagger } from 'koa2-swagger-ui';
const app = new Koa();
app.use(
koaSwagger({
routePrefix: '/swagger',
swaggerOptions: {
url: 'http://petstore.swagger.io/v2/swagger.json',
},
}),
);
app.listen(3000);
example with koa-router and yaml source
depends on yamljs to turn your Yaml into a JS object
npm install --save yamljs
const Koa = require('koa');
const Router = require('koa-router');
const yamljs = require('yamljs');
const koaSwagger = require('koa2-swagger-ui');
const router = new Router({ prefix: '/' });
const app = new Koa();
const router = new Router();
const spec = yamljs.load('./openapi.yaml');
router.use(koaSwagger({ swaggerOptions: { spec } }));
router.get('/docs', koaSwagger({ routePrefix: false, swaggerOptions: { spec } }));
app.use(router.routes());
app.listen(3000);