• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

carlansley/swagger2-koa: Koa 2 middleware for swagger2

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

carlansley/swagger2-koa

开源软件地址:

https://github.com/carlansley/swagger2-koa

开源编程语言:

TypeScript 100.0%

开源软件介绍:

Build Status Coverage Status Dependencies Known Vulnerabilities

swagger2-koa

Koa 2 async-style middleware for loading, parsing and validating requests via swagger2, and serving UI via swagger-ui.

  • router(document) => koa2-style Router
  • validate(document) => koa2 middleware
  • ui(document) => koa2 middleware

Installation

$ npm install swagger2-koa --save

Usage

router(document) => koa2-style Router

This is the easiest way to use swagger2-koa; it creates a standalone koa server, adds the validate middleware, and returns a Router object that allows you to add your route implementations.

import * as swagger from 'swagger2';
import {ui, router as swaggerRouter, Router} from 'swagger2-koa';

...
const document = swagger.loadDocumentSync('./swagger.yml');
const router: Router = swaggerRouter(document);

router.get('/ping', async (context) => {
  context.status = 200;
  context.body = {
    serverTime: new Date().toISOString()
  };
});

...

router.app()         // get the koa 2 server
  .use(ui(document)) // only needed if you want to publish a swagger-ui for the API
  .listen(3000);     // start handling requests on port 3000

Note: in addition to validate (described below), router adds the following middleware to its koa server:

  • @koa/cors
  • @koa/router
  • koa-bodyparser

validate(document) => koa2 middleware

If you already have a Koa server, this middleware adds basic loading and validation of HTTP requests and responses against swagger 2.0 document:

import * as swagger from 'swagger2';
import { validate } from 'swagger2-koa';

const app = new Koa();

// load YAML swagger file
const document = swagger.loadDocumentSync('./swagger.yml');

// validate document
if (!swagger.validateDocument(document)) {
  throw Error(`./swagger.yml does not conform to the Swagger 2.0 schema`);
}

app.use(body());
app.use(validate(document));

The validate middleware behaves as follows:

  • expects context.body to contain request body in object form (e.g. via use of koa-body)
  • if the request is for a path not defined in swagger document, an HTTP 404 is returned to the client (subsequent middleware is never processed).
  • if the request body does not validate, an HTTP 400 is returned to the client (subsequent middleware is never processed)
  • if the response body does not validate, an HTTP 500 is returned to the client

For either request (HTTP 400) or response (HTTP 500) errors, details of the schema validation error are passed back in the body. e.g.:

{
  "code": "SWAGGER_RESPONSE_VALIDATION_FAILED",
  "errors": [{
     "actual": {"badTime": "mock"},
     "expected": {
        "schema": {"type": "object", "required": ["time"], "properties": {"time": {"type": "string", "format": "date-time"}}}
     },
     "where": "response"
  }]
}

ui(document, pathRoot = "/", skipPaths = []) => koa2 middleware

You can also serve a swagger-ui for your API from a given path root (pathRoot defaults to "/"):

import * as swagger from 'swagger2';
import { ui } from 'swagger2-koa';

const app = new Koa();

const document = swagger.loadDocumentSync('./swagger.yml');
app.use(ui(document));

app.use(ui(document, "/swagger")) adds routes /swagger/api-docs and /swagger.

By using the skipPaths parameter, it is possible to create routes such as:

/api          : Swagger UI
/api/api-docs : Swagger API Docs
/api/v1       : Actual API

with:

app.use(ui(document, "/api", ['/api/v1']));

Debugging

This library uses debug, which can be activated using the DEBUG environment variable:

export DEBUG=swagger2-koa:*

Limitations

  • only supports Koa 2-style async/await middleware interface
  • requires node version 14 and above

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap