swagger-middlewares
Provides generic HTTP middlewares exposing swagger defination.
Install
npm install swagger-middlewares
Methods
init(swaggerRootDocument, [otherOptions])
- Returns an instance
- Options
swaggerRootDocument
: Overrides standard swagger root document object containing (apiVersion
,host
,info
,basePath
)otherOptions.swaggerSpecPath
: A path or an array of path to load Swagger specification in yaml format, only acceptsyml
oryaml
extension).
<instance>.validateSchema([options], [onError])
- Prints out error message about Swagger Schema validation error
<instance>.koa.spec([uri])
- Returns a Swagger UI Koa middleware
- Options
uri
: optional URI path, default to/api-docs
<instance>.koa.ui([uri])
- Returns a Swagger UI Koa middleware. You may need to include
swagger-ui
npm module in your project bynpm install --save swagger-ui
- Options
uri
: optional URI path, default to/swagger/ui
<instance>.koa.validator()
- Returns an validator middleware. Validator usage can be found at swagger-model-validator
Usage Example
// initializationlet pkg = ;let swagger = ; let app = ; // This will prints out errors if swagger schema is not validswagger; // expose swagger JSON specification// curl http://localhost:3000/api-docsapp; // expose swagger user interface// curl http://localhost:3000/swagger/uiapp; // creates a validator in the koa generator context// do anything with the validation results// exports.get = function * () { // this.validator.swagger.validateModel('ModelName', this.body);// }app;
Limitation
- Only support Swagger Specification 2.x
- Only Provides Koa middleware at the moment
- Only Support one instance per node process
- Limited Validation features, middleware validator is available but it does not force validation