当前位置:网站首页>Best practices of swagger in egg project
Best practices of swagger in egg project
2022-06-24 13:23:00 【CS Xiaoyao Sword Fairy】
swagger stay egg Best practices in the project
Write By CS Xiaoyao sword immortal
My home page : csxiaoyao.com
GitHub: github.com/csxiaoyaojianxian
Email: [email protected]
QQ: 1724338257
1. background
swagger It's a RESTful The interface is based on YAML、JSON Language documentation and code generation tools online , It allows Deployment Management API Become simpler than ever .swagger stay java Widely used , Other languages can also be easily integrated . This paper is based on node.js Enterprise application framework egg.js For example , Integrate swagger To automatically generate interface documents based on function annotations .
2. Best practices
2.1 Project structures,
Reference link :https://github.com/csxiaoyaojianxian/JavaScriptStudy/tree/master/17-nodejs/20-egg-swagger-doc
egg There are two ways to build : Quick build and normal build . Because this case is relatively simple , To avoid unnecessary configuration of the project , The common construction method is used here , You can refer to the link above , The project directory structure is as follows :
egg-example
├── app
│ ├── contract
│ │ └── format.js
│ ├── controller
│ │ └── home.js
│ └── router.js
├── config
│ └── config.default.js
│ └── plugin.js
└── package.json among , Contains a route /index Called HomeController Controller index Method , This route will be swagger Handle .
2.2 egg-swagger-doc install
Reference resources npm project : https://www.npmjs.com/package/egg-swagger-doc stay egg Install... In the project swagger:
$ npm i egg-swagger-doc --save
2.3 swagger The plug-in configuration
First, in the egg Enable swaggerdoc plug-in unit :
// {app_root}/config/plugin.js
exports.swaggerdoc = {
enable: true, // Is it enabled?
package: 'egg-swagger-doc' // Specify the package name
}And then config Add... To the configuration file swaggerdoc Plug in configuration information :
// {app_root}/config/config.default.js
exports.swaggerdoc = {
dirScanner: './app/controller', // Configure the controller path for auto scan
apiInfo: {
title: ' Interface document ', // The title of the interface document
description: 'swagger Test interface documentation ', // Interface document description
version: '1.0.0', // Interface document version
termsOfService: 'http://swagger.io/terms/', // Conditions of service
contact: {
email: '[email protected]' // Contact information
},
license: {
name: 'Apache 2.0',
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
},
},
basePath: '/', // Configure the base path
schemes: ['http', 'https'], // Configure supported protocols
consumes: ['application/json'], // Specifies the submitted content type to process the request (Content-Type), Such as application/json、text/html
produces: ['application/json'], // Specify the type of content returned , Only when the request In the request header (Accept) The specified type is included in the type to return
securityDefinitions: {}, // Configure interface security authorization mode
enableSecurity: false, // Whether to enable Authorization , Default false
// enableValidate: true, // Whether to enable parameter verification , Default true
routerMap: false, // Whether to enable automatic route generation ( Experimental function ), Default true
enable: true // Default true
}2.4 Annotation parameter writing
The writing of parameters is divided into two parts :controller and contract, After the plug-in is imported , If you do not modify the default configuration , After the application starts , Will automatically scan app/controller and app/contract The files under the .controller by api The controller , and contract The file under is the defined request body and response body .
The comments for the controller are divided into two parts , The first comment block for each controller must contain @controller Can be resolved to a controller , Then it will analyze the... Contained under the controller one by one api notes .
/**
* @controller HomeController
*/
class HomeController extends Controller {
/**
* @router get /index route
* @summary Subtitle information of the interface
* @description Description of the interface
* @request query integer id For parameters id Description of
* @request query string name For parameters name Description of
* @response 200 indexJsonBody
*/
async index () {
...
}
}and contract The following defines the common request body and response body , You can constrain the format .
const JsonBody = {
code: { type: 'number', required: true, example: 0 },
message: { type: 'string', required: true, example: 'success' },
data: { type: 'Enum', required: true, example: [] },
}
module.exports = {
indexJsonBody: {
...JsonBody,
data: { type: 'string', example: 'test' },
},
};2.5 Generate
After the configuration , perform dev Script , You can open /swagger-ui.html See the generated document .
$ npm run dev
3. Parameter description
swagger There are the following commonly used comments :
@Controller {ControllerName}
@Router {Mothod} {Path}
@Request {Position} {Type} {Name} {Description}
@Response {HttpStatus} {Type} {Description}
@Deprecated
@Description {Description}
@Summary {Summary}about swagger Annotation parameter details , You can refer to https://www.npmjs.com/package/egg-swagger-doc, You can also do it in swagger Generated by cross reference in the editor https://editor.swagger.io.
4. Reference documents
- my github:https://github.com/csxiaoyaojianxian/JavaScriptStudy/tree/master/17-nodejs/20-egg-swagger-doc
- npm:https://www.npmjs.com/package/egg-swagger-doc
- editor:https://editor.swagger.io
边栏推荐
- 实现领域驱动设计 - 使用ABP框架 - 创建实体
- [day ui] affix component learning
- #云原生征文#Ingress案例实战
- Sqlserver 2000 has long-lasting vitality
- The difference between apt and apt get
- Comparator sort functional interface
- 我開導一個朋友的一些話以及我個人對《六祖壇經》的一點感悟
- IIS WCF 808 port service access exception
- Istio Troubleshooting: uneven grpc service load
- Opengauss kernel: simple query execution
猜你喜欢

使用 Abp.Zero 搭建第三方登录模块(一):原理篇

CVPR 2022 | 美团技术团队精选论文解读

C语言中常量的定义和使用

openGauss内核:简单查询的执行

Getting started with the lvgl Library - colors and images

Party, Google's autoregressive Wensheng graph model

简述聚类分析

"Interesting" is the competitiveness of the new era

Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer

About the hacked database
随机推荐
Metamask项目方给Solidity程序员的16个安全建议
"I, an idiot, have recruited a bunch of programmers who can only" Google "
Express 100 Express query interface (API) interface specification document - detailed version
openGauss内核:简单查询的执行
Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
Opengauss kernel: simple query execution
How can ffmpeg streaming to the server save video as a file through easydss video platform?
YOLOv6:又快又准的目标检测框架开源啦
DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中
MySQL master-slave replication
Troubleshooting and optimization of files that cannot be globally searched by ordinary users in easydss video platform customization project
Attack popular science: DDoS
Use abp Zero builds a third-party login module (I): Principles
What is SCRM? What is the difference between SCRM and CRM
Cohere、OpenAI、AI21联合发布部署模型的最佳实践准则
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
敏捷之道 | 敏捷开发真的过时了么?
Integrate API interface parameter Dictionary of accounts of multiple local distribution companies - Express 100
“我这个白痴,招到了一堆只会“谷歌”的程序员!”
我从根上解决了微信占用手机内存问题