給你的Egg來一打SwaggerUi(可測試的接口文檔,問你心不心動)

<img width="100" src="https://user-gold-cdn.xitu.io...;h=128&f=png&s=7630"/>javascript

Egg.js 路由裝飾器,讓你的開發更敏捷~
Lady and gentleman,讓咱們慶祝 蛋殼 v1.0.4 版本的發佈~

具體的使用方式請查看文檔java

該新版本主要集成了swagger-ui,讓咱們來秀上一波:git

import { Controller } from 'egg';
import { Post, IgnoreJwtAll, Description, TagsAll,
         Parameters, Responses } from 'egg-shell-decorators';

const USER_SCHEMA = {
  type: 'object',
  properties: {
    name: { type: 'string', description: '姓名' },
    phone: { type: 'string', description: '手機號碼' },
    age: { type: 'integer', format: 'int32', description: '年齡' }
  }
};

@TagsAll('用戶')
@IgnoreJwtAll
export default class SubOrderController extends Controller {

  @Post('/')
  @Description('建立用戶')
  @Parameters([
    { name: 'body', in: 'body', required: true, schema: USER_SCHEMA }
  ])
  @Responses(USER_SCHEMA)
  public listUser({ body: { name, phone, age } }) {
    return { name, phone, age };
  }

}

上面代碼會生成如下的文檔:
github

可測試的噢!阿~這是心動的感受~typescript

以爲上面的代碼侵入性太強?沒問題,路由映射幫你搞定:shell

// app/controller/user.ts
import { Controller } from 'egg';
import { Post, IgnoreJwtAll, TagsAll } from 'egg-shell-decorators';

@TagsAll('用戶')
@IgnoreJwtAll
export default class UserController extends Controller {

  @Post('/')
  public listUser({ body: { name, phone, age } }) {
    return { name, phone, age };
  }

}
// swagger/user.json
{
  "/": {
    "post": {
      "description": "建立用戶",
      "parameters": [
        { "name": "body", "in": "body", "required": true, "schema": { "$ref": "User" } }
      ],
      "responses": { "type": "object", "schema": { "$ref": "User" } }
    }
  }
}
具體的使用方式請查看 文檔

以爲不錯就請做者喝杯咖啡(速溶)️☕️


給個小星星也是能夠的~😘json

加入小組來面基~

因爲本人不多上QQ,因此建的是微信羣,而微信羣碼很快就失效,因此想進交流羣的小夥伴加我微信噢~我拉你進羣,歡迎大佬們加入😁

相關文章
相關標籤/搜索