Giter VIP home page Giter VIP logo

midway-swagger's Introduction

midway-swagger

OpenAPI(Swagger)规范是一种用于描述 RESTful API 的强大定义格式。 midway-swagger 提供了声明式的 api 及参数验证。

快速开始

npm i midway-init -g
midway-init --package=midway-swagger

进入项目目录

yarn
yarn dev

查看 swagger ui

访问: http://127.0.0.1:7001/swagger-ui

配置 swagger (open api)

进入 src/app.ts

/**
 * open api 文档配置
 */
document
    .setTitle('midway openApi')
    .setVersion(process.env.npm_package_version)
    .setDescription('支持基于 interface 的验证')
    // api 认证方式
    .setSecuritySchemes({
        ApiKeyAuth: {
            type: 'apiKey',
            in: 'header',
            name: 'authorization',
        },
    })

配置 swagger ui

进入 src/app.ts

await openApi(app, {
    /**
     * 是否开启 swagger ui
     */
    enable: true,
    /**
     * swagger ui 路由前缀
     */
    routerPrefix: '/swagger-ui',

    /**
     * swagger ui version
     */
    swaggerUiVersion: '3.30.2',
})

声明 API

import { provide, inject, Context } from 'midway'
import { get, Param, controller } from '../../decorator/openApi'

@provide()
@controller('/user', {
    description: '用户模块'
})
export class UserController {
    @inject()
    ctx: Context

    @get('/:id', {
        description: '用户信息',
        responses: 'API.UserRes',
    })
    async getUser(
        @Param('number', {
            minimum: 1,
            description: '用户id',
        })
        id: number
    ) {
        return {
            id,
            username: 'string',
            phone: '13800138000',
        }
    }
}

路由参数

在路由处理程序中查找所有使用的 @Body()@Query()@Param() 等装饰器来生成 API 文档。该模块利用反射创建相应的模型定义。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.