项目作者: cjfff

项目描述 :
egg decorator make router, swaager and validator user input
高级语言: TypeScript
项目地址: git://github.com/cjfff/egg-swagger-decorator.git
创建时间: 2021-07-06T05:05:19Z
项目社区:https://github.com/cjfff/egg-swagger-decorator

开源协议:

下载


egg-swagger-decorator npm-url

using decorator to auto generate swagger json docs

Installation

  1. yarn add @chenxxx/egg-swagger-decorator

Introduction

egg Swagger Decorator

using decorator to auto generate swagger json docs

based on Swagger OpenAPI Specification 2.0

Example

  1. // using commonds below to start and test the example server
  2. git clone https://github.com/cjfff/egg-swagger-decorator.git
  3. cd egg-swagger-decorator
  4. npm install
  5. npm run dev
  6. finally open:
  7. http://localhost:7001/swagger-html

Requirements

  • egg
  • typescript > 2.8

Introduction

  1. // router.js
  2. import { Application } from 'egg';
  3. import { wrapper } from '../lib';
  4. export default (app: Application) => {
  5. wrapper(app, {
  6. // // [optional] default is /swagger-html
  7. // swaggerHtmlEndpoint: '/sw',
  8. // // [optional] default is /swagger-json
  9. // swaggerJsonEndpoint: '/sj',
  10. // // [optional] default is false. if true, will call makeSwaggerRouter(app) automatically
  11. // makeswaggerRouter: false,
  12. title: 'foo',
  13. version: 'v1.0.0',
  14. description: 'bar',
  15. // autoMount 是否开启
  16. // defaultRequestMounte: true
  17. });
  18. };

using decorator to make api definition

  1. // controller/test.ts
  2. import { Controller, Context } from "egg";
  3. import {
  4. request,
  5. summary,
  6. path,
  7. tags,
  8. responses,
  9. middlewares,
  10. description,
  11. } from "@chenxxx/egg-swagger-decorator";
  12. import { PathParamsVO, IGetUserResultVO, IUserListVO } from "../dto";
  13. const testTag = tags(["test"])
  14. export default class Test extends Controller {
  15. @request("get", "/users")
  16. @description("get user list")
  17. @testTag
  18. @middlewares(async (ctx: Context, next) => {
  19. ctx.logger.info("mid");
  20. await next();
  21. })
  22. @responses(IUserListVO)
  23. public async getUsers() {
  24. const { ctx } = this;
  25. const users = [{ name: "cjfff" }];
  26. ctx.body = { users };
  27. }
  28. @request("get", "/users/{id}")
  29. @summary("get user info by id")
  30. @testTag
  31. @path(PathParamsVO)
  32. @responses(IGetUserResultVO)
  33. public async getUser() {
  34. const { id } = this.ctx.params;
  35. this.ctx.body = { id };
  36. }
  37. }

dto define

  1. // ../dto.ts
  2. export class PathParamsVO {
  3. @ApiProperty({
  4. type: "number",
  5. description: "用户id",
  6. example: 10086,
  7. })
  8. @IsNumber({ maxDecimalPlaces: 1000 }, { message: "id 必须是数字2323232" })
  9. id: number;
  10. }
  11. export class IUserVO {
  12. @ApiProperty({
  13. type: 'string'
  14. })
  15. name: string;
  16. }
  17. export class IUserListVO {
  18. @ApiProperty({
  19. type: IUserVO,
  20. isArray: true
  21. })
  22. users: IUserVO[]
  23. }
  24. export class IGetUserResultVO {
  25. @ApiProperty({
  26. type: 'number'
  27. })
  28. id: number;
  29. }

avaliable annotations:

  • tags
  • query
  • path
  • body
  • formData
  • middlewares
  • summary
  • description
  • responses
  1. request // @request('POST', '/users')
  2. tags // @tags(['example'])
  3. query // @query({limit: {type: 'number', required: true, default: 10, description: 'desc'}})
  4. path // @path({limit: {type: 'number', required: true, default: 10, description: 'desc'}})
  5. body // @body({groups: {type: 'array', required: true, items: { type: 'string', example: 'group1' }}})
  6. formData // @formData({file: {type: 'file', required: true, description: 'file content'}})
  7. middlewares
  8. // support koa middlewares.
  9. // eg. @middlewares([func1,func2])
  10. summary // @summary('api summary')
  11. description // @description('api description')
  12. responses
  13. // @responses({ 200: { description: 'success'}, 400: { description: 'error'}})
  14. // responses is optional
runing the project and it will generate docs through swagger ui

image.png

Other

if your ide is the vscode. you can use the vscode to use a json schema generation the class schema for the swagger schema. marketplace

thank for

https://github.com/Cody2333/egg-swagger-decorator

License

© MIT