发布一个egg插件,egg-swagger2,通过配置生成swagger文档访问
发布于 6 年前 作者 CaanDoll 6035 次浏览 来自 分享

github地址:https://github.com/CaanDoll/egg-swagger2

安装

$ npm i egg-swagger2 -S

依赖的插件

  • static

开启插件

// config/plugin.js
exports.swagger2 = {
  enable: true,
  package: 'egg-swagger2',
};

使用场景

  • 启动程序后自动生成swagger文档。与注解不同,是以配置的方式呈现。相比较注解而言稍微麻烦些但是不需要引入TS或者编译器。完全支持swagger语法
  • 推荐在app/router.js中与每个路由对应使用

插件配置

// {app_root}/config/config.default.js
exports.swagger2 = {
  enable:false, // 禁用swagger , 默认为true
  base: {
    /* default config,support cover
    schemes: [
        'http',
    ],
    host: '127.0.0.1:7001',
    basePath: '/',
    consumes: [
    'application/json',
    ],
    produces: [
    'application/json',
    ],
    */
    info: {
      description: 'This is a test swagger-ui html',
      version: '1.0.0',
      title: 'TEST',
      contact: {
        email: 'caandoll@aliyun.com',
      },
      license: {
        name: 'Apache 2.0',
        url: 'http://www.apache.org/licenses/LICENSE-2.0.html',
      },
    },
    tags: [
      {
        name: 'admin',
        description: 'Admin desc',
      },
      {
        name: 'role',
        description: 'Role desc',
      },
    ],
    definitions:{
    // model definitions
    },
    securityDefinitions:{
    // security definitions
    }
  },
};

请到 config/config.default.js 查看详细配置项说明。

示例

// {app_root}/app/router.js
 module.exports = app => {
   const { router, controller, swagger } = app;
   router.post('/login', controller.test.postLogin);
   swagger.post('/login', {
       tags: [
         'admin',
       ],
       summary: 'Login a admin',
       description: '',
       parameters: [
         {
           in: 'body',
           name: 'body',
           description: 'admin\'s username & password',
           required: true,
           schema: {
             type: 'object',
             required: [ 'username', 'password' ],
             properties: {
               username: {
                 type: 'string',
                 description: 'admin\'s username',
               },
               password: {
                 type: 'string',
                 description: 'admin\'s password',
               },
             },
           },
         },
       ],
       responses: {
         200: {
           description: 'SUCCEED',
           schema: {
             type: 'object',
             properties: {
               status: {
                 type: 'string',
                 description: 'status',
               },
               data: {
                 type: 'object',
                 description: 'data',
                 properties: {
                   token: {
                     type: 'string',
                     description: 'token',
                   },
                 },
               },
             },
           },
         },
       },
     });
   router.get('/roles', controller.test.getRoles);
   swagger.get('/roles', {
     tags: [
       'role',
     ],
     summary: 'search role by page',
     description: '',
     parameters: [
       {
         in: 'query',
         name: 'name',
         description: 'role\'s name',
       },
       {
         in: 'query',
         name: 'pageIndex',
         description: 'pageIndex',
       },
       {
         in: 'query',
         name: 'pageSize',
         description: 'pageSize',
       },
     ],
     responses: {
       200: {
          description: 'SUCCEED',
          schema: {
            type: 'object',
            properties: {
              status: {
                type: 'string',
                description: 'status',
              },
              datas: {
                type: 'array',
                description: 'result datas',
                properties: {
                  token: {
                    type: 'string',
                    description: 'token',
                  },
                },
              },
              pageIndex:{
                type: 'number',
                description: 'pageIndex',
              },
              pageSize:{
                type: 'number',
                description: 'pageSize',
              },
              totalCount:{
                type: 'number',
                description: 'totalCount',
              },
            },
          },
        },
     },
   });
 };

启动

image.png 通过static目录去访问

第一次做插件,不足请各位大神多多指教

回到顶部