AdonisJS 5Swagger插件
AdonisJS 5 的 Swagger 插件 adonis5-swagger,用起来还挺顺手的,尤其你想快速整一份 API 文档的时候,省事不少。只要装个包、连个依赖、跑几个命令,SwaggerUI 页面就有了,基本不用你多费脑子。
Swagger 的接入流程蛮清晰:先 npm i --save adonis5-swagger
装好包, node ace invoke adonis5-swagger
一键连依赖。文档自动挂在 SwaggerUI 上,像 /docs
或你自定义的路由上。
你写个路由比如:
Route.get('/api/hello', 'TestController.hello')
再建个控制器:
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
export default class TestController {
public async hello({ response }: HttpContextContract) {
return response.ok({ greeting: 'Hello world!' })
}
}
搞定后 Swagger 页面就能看到接口结构和返回示例,调试接口也方便。
记得去改下 config/swagger.ts
,像标题、版本号、基本 info 都能自定义,还有接口分组也能配,文档一多就挺有用的。
如果你是 Adonis 老用户或者在用 TypeScript 搭接口项目,这插件还蛮推荐的,跟框架贴合度不错,代码也不会乱。
你也可以看看类似的资源,比如 基于 TypeScript 类的 Swagger API 自动生成 或 vue_swaggerui_webpack2,配合前后端联调更顺手。
嗯,如果你想在 Adonis 项目里把接口文档自动化,那这个 adonis5-swagger 值得一试。
262.25KB
文件大小:
评论区