swaggernaut
v1.0.13
Published
Automated swagger documentation generator
Downloads
4
Readme
Swaggernaut
Automated swagger documentation generator created for micro using microrouter.
More documentation to come, example usage found at ./src/test-spec.js
.
/**
* Test for example use case
*/
import pkg from '../package.json'
import micro from 'micro'
import * as swaggernaut from './index'
// Create a definition, a reference path is returned
const TestResponse = swaggernaut.define('TestResponse', {
type: 'string'
})
// -- or --
// const TestResponse = swaggernaut.define('TestResponse')({
// type: 'string'
// })
// Create a spec (all functions are curried)
const testDoc = swaggernaut.document({
description: 'Get on a simple test routes',
responses: {
200: {
description: 'Congrats you did it',
schema: {
// Use definition reference
$ref: TestResponse
}
}
}
})
// Create a route map
const routes = new swaggernaut.RouteMap([
// Define a route with Iterable or swaggernaut.Route
// Wrap handler function with documentation
['/test', testDoc((req, res) => 'You did it')],
// default method is 'get' but can be on of
// get, post, put, path, delete, head, options
['post', '/test', testDoc((req, res) => 'You posted it')],
// Example use of swagger route (validates input and returns array like above)
swaggernaut.Route('/another', () => 'another put route'),
swaggernaut.Route('put', '/another', () => 'another put route')
])
// Add route to existing map (posible for environment logic)
routes.add(
// Dont document swagger generate doc handler because that
// that would be silly.
swaggernaut.Route(
'/swagger.json',
swaggernaut.generateDoc(swaggernaut.packageToSpec(pkg), routes)
)
)
// Start http.Server
micro(routes.router).listen(3000, () =>
console.log('server is online at http://localhost:3000')
)