Gathering detailed insights and metrics for egg-swagger-doc
Gathering detailed insights and metrics for egg-swagger-doc
Gathering detailed insights and metrics for egg-swagger-doc
Gathering detailed insights and metrics for egg-swagger-doc
npm install egg-swagger-doc
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (88.19%)
HTML (11.81%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
194 Stars
120 Commits
63 Forks
6 Watchers
16 Branches
9 Contributors
Updated on May 15, 2025
Latest Version
2.3.2
Package Id
egg-swagger-doc@2.3.2
Unpacked Size
8.79 MB
Size
2.23 MB
File Count
26
NPM Version
6.4.1
Node Version
8.11.1
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
应用于eggjs的plugin,可自动生成SwaggerUI。应用启动后访问/swaagger-ui.html可以浏览页面,访问/swagger-doc,获取swaggerjson. 这是一个简单例子,详见here
1$ npm i egg-swagger-doc --save
1// {app_root}/config/plugin.js 2exports.swaggerdoc = { 3 enable: true, 4 package: 'egg-swagger-doc', 5};
1// {app_root}/config/config.default.js 2exports.swaggerdoc = { 3 dirScanner: './app/controller', 4 apiInfo: { 5 title: 'egg-swagger', 6 description: 'swagger-ui for egg', 7 version: '1.0.0', 8 }, 9 schemes: ['http', 'https'], 10 consumes: ['application/json'], 11 produces: ['application/json'], 12 securityDefinitions: { 13 // apikey: { 14 // type: 'apiKey', 15 // name: 'clientkey', 16 // in: 'header', 17 // }, 18 // oauth2: { 19 // type: 'oauth2', 20 // tokenUrl: 'http://petstore.swagger.io/oauth/dialog', 21 // flow: 'password', 22 // scopes: { 23 // 'write:access_token': 'write access_token', 24 // 'read:access_token': 'read access_token', 25 // }, 26 // }, 27 }, 28 enableSecurity: false, 29 // enableValidate: true, 30 routerMap: false, 31 enable: true, 32};
see config/config.default.js for more detail.
完成插件引入之后,如果不修改默认配置,应用启动后,会自动扫描app/controller和app/contract下的文件。controller下的文件先不做描述。contract下的文件为定义好的请求体和响应体。
实验性功能:如果routerMap为true,允许自动生成API路由
格式:@Controller {ControllerName}
a.如果文件第一个注释块中存在标签@Controller,应用会扫描当前文件下的所有注释块,否则扫描将会跳过该文件。
b.如果不标示ControllerName,程序会将当前文件的文件名作为ControllerName。
例:
1/** 2 * @Controller user 3 */ 4class UserController extends Controller { 5 //some method 6}
格式:@Router {Mothod} {Path}
a.Mothod,请求的方法(post/get/put/delete等),不区分大小写。
b.Path,请求的路由。
格式:@Request {Position} {Type} {Name} {Description}
a.position.参数的位置,该值可以是body/path/query/header/formData.
b.Type.参数类型,body之外位置目前只支持基础类型,integer/string/boolean/number,及基础类型构成的数组,body中则支持contract中定义的类型。如果position是formData还将支持 file 类型
c.Name.参数名称.如果参数名称以*开头则表示必要,否则非必要。
d.Description.参数描述
c.如果你想给query或者path的参数设置example,你可以在Description前添加以'eg:'开头的参数,实例如下
@Request query string contactId eg:200032234567 顾问ID
格式:@Response {HttpStatus} {Type} {Description}
a.HttpStatus.Http状态码。
b.Type.同Request中body位置的参数类型。
d.Description.响应描述。
如果注释块中包含此标识,则表示该注释块注明的接口,未完成或不启用。
格式:@Description {Description}
接口具体描述
格式:@Summary {Summary}
接口信息小标题
例:
1/** 2 * @Controller user 3 */ 4class HomeController extends Controller { 5 /** 6 * @Router POST /user 7 * @Request body createUser name description-createUser 8 * @Request header string access_token 9 * @Response 200 baseResponse ok 10 */ 11 async index() { 12 this.ctx.body = 'hi, ' + this.app.plugins.swagger.name; 13 }
如果在config中开启并定义了securityDefinitions,默认enableSecurity为false.则可在注释块中加入@apikey,加入安全验证。也可定义成其他名字,只需@定义好的字段名就好。关于securityDefinitions的定义可以自行搜索。
1exports.swaggerdoc = { 2 securityDefinitions: { 3 apikey: { 4 type: 'apiKey', 5 name: 'clientkey', 6 in: 'header', 7 }, 8 // oauth2: { 9 // type: 'oauth2', 10 // tokenUrl: 'http://petstore.swagger.io/oauth/dialog', 11 // flow: 'password', 12 // scopes: { 13 // 'write:access_token': 'write access_token', 14 // 'read:access_token': 'read access_token', 15 // }, 16 // }, 17 }, 18 enableSecurity: true, 19};
关于Contract的定义其实在测试代码里面,已经把支持的所有情况都定义出来了。详见here,这里我简单说明一下,以下是测试代码中的部分contract。
1module.exports = { 2 createResource: { 3 resourceId: { type: 'string', required: true, example: '1' }, 4 resourceNametrue: { type: 'string', required: true }, 5 resourceType: { type: 'string', required: true, enum: ['video', 'game', 'image'] }, 6 resourceTag: { type: 'array', itemType: 'string' }, 7 owner: { type: 'User', required: true }, 8 owners: { type: 'array', itemType: 'User' } 9 }, 10};
@基础类型
1module.exports = { 2 Model名称:{ 3 字段名称: { type: 字段类型,required: 字段必要性, example: 示例} 4 } 5}
注:type可以是array之外的类型,包括自定义的类型,目前自定义类型不支持example
@ENUM
1module.exports = { 2 Model名称:{ 3 字段名称: { type: 字段类型,required: 字段必要性, enum:[]} 4 } 5}
注: type只能是string或number,enum为具体的数值组成的集合
@ARRAY
1module.exports = { 2 Model名称:{ 3 字段名称: { type: "array",required: 字段必要性, itemType:数组元素类型} 4 } 5}
type为array,itemType为具体的数组元素类型,支持自定义类型。
@自定义类型
关于自定义类型,必须定义在contract目录下,在contract下的其他类型中使用时,直接使用Model名称引入。
因为contract的定义和validate-rule的定义具有极大的相似性,所以目前的版本中定义contract的同时会简单的生成相应的validate-rule.具体的使用'ctx.rule.'加Model名称直接引入。
上面的model,在做验证的时候就可以使用如下的方式(需使用egg-validate)
1 2ctx.validate(ctx.rule.createResource, ctx.request.body); 3
Please open an issue here.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 8/18 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
82 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More