从NextJS API路线产生Swagger JSON API
如果您喜欢与Next-Swagger-Doc合作,您会喜欢下一个验证:NextJS API验证,支持ZOD,YUP,最快录音机,JOI等等
该软件包在NextJS API路由上读取了您的JSDOC注销源代码,并生成OpenAPI(Swagger)规范。
nextJS + Swagger-jsdoc = next-swagger-doc
yarn add next-swagger-doc要将next-swagger-doc纳入您的下一个项目。JS13项目,请按照以下步骤操作。此设置将根据您的代码为您的API生成Swagger文档,并提供内置的Swagger UI来查看文档。
接下来,创建一个新的文件lib/swagger.ts 。该文件使用next-swagger-doc库来创建基于您的下一个项目中的API路由的Swagger规范。
import { createSwaggerSpec } from "next-swagger-doc" ;
export const getApiDocs = async ( ) => {
const spec = createSwaggerSpec ( {
apiFolder : "app/api" , // define api folder under app folder
definition : {
openapi : "3.0.0" ,
info : {
title : "Next Swagger API Example" ,
version : "1.0" ,
} ,
components : {
securitySchemes : {
BearerAuth : {
type : "http" ,
scheme : "bearer" ,
bearerFormat : "JWT" ,
} ,
} ,
} ,
security : [ ] ,
} ,
} ) ;
return spec ;
} ;生成一个名为app/api-doc/react-swagger.tsx的新文件。在此文件中,创建和导出一个React组件,该组件利用swagger-ui-react库根据提供的规范渲染Swagger UI。
出于演示目的,这是使用Swagger-UI-React的示例
可以随意使用任何替代Swagger UI库,例如Sotlightio/Elements。我在example文件夹中使用此库添加了一个示例。
'use client' ;
import SwaggerUI from 'swagger-ui-react' ;
import 'swagger-ui-react/swagger-ui.css' ;
type Props = {
spec : Record < string , any > ,
} ;
function ReactSwagger ( { spec } : Props ) {
return < SwaggerUI spec = { spec } /> ;
}
export default ReactSwagger ;创建一个新的文件app/api-doc/page.tsx 。此页面将导入Swagger Spec和Swagger UI组件以显示Swagger文档。
import { getApiDocs } from "@/lib/swagger" ;
import ReactSwagger from "./react-swagger" ;
export default async function IndexPage ( ) {
const spec = await getApiDocs ( ) ;
return (
< section className = "container" >
< ReactSwagger spec = { spec } />
</ section >
) ;
}最后,在app/api/hello/route.ts中向您的API路线添加招牌评论。此评论包括有关API端点的元数据,该端点将由next-swagger-doc读取,并包含在Swagger Spec中。
/**
* @swagger
* /api/hello:
* get:
* description: Returns the hello world
* responses:
* 200:
* description: Hello World!
*/
export async function GET ( _request : Request ) {
// Do whatever you want
return new Response ( 'Hello World!' , {
status : 200 ,
} ) ;
}现在,导航到localhost:3000/api-doc (或任何托管下一个js应用程序),您应该看到Swagger UI。
yarn add next-swagger-doc swagger-ui-reactpages/api-doc.tsx import { GetStaticProps , InferGetStaticPropsType } from 'next' ;
import { createSwaggerSpec } from 'next-swagger-doc' ;
import dynamic from 'next/dynamic' ;
import 'swagger-ui-react/swagger-ui.css' ;
const SwaggerUI = dynamic < {
spec : any ;
} > ( import ( 'swagger-ui-react' ) , { ssr : false } ) ;
function ApiDoc ( { spec } : InferGetStaticPropsType < typeof getStaticProps > ) {
return < SwaggerUI spec = { spec } / > ;
}
export const getStaticProps : GetStaticProps = async ( ) => {
const spec : Record < string , any > = createSwaggerSpec ( {
apiFolder : 'pages/api' // or 'src/pages/api',
definition : {
openapi : '3.0.0' ,
info : {
title : 'Next Swagger API Example' ,
version : '1.0' ,
} ,
} ,
} ) ;
return {
props : {
spec ,
} ,
} ;
} ;
export default ApiDoc ; pages/api/doc.ts import { withSwagger } from "next-swagger-doc" ;
const swaggerHandler = withSwagger ( {
definition : {
openapi : "3.0.0" ,
info : {
title : "NextJS Swagger" ,
version : "0.1.0" ,
} ,
} ,
apiFolder : "pages/api" ,
} ) ;
export default swaggerHandler ( ) ;pages/api/hello.ts import { NextApiRequest , NextApiResponse } from "next" ;
/**
* @swagger
* /api/hello:
* get:
* description: Returns the hello world
* responses:
* 200:
* description: hello world
*/
const handler = ( _req : NextApiRequest , res : NextApiResponse ) => {
res . status ( 200 ) . json ( {
result : "hello world" ,
} ) ;
} ;
export default handler ;next-swagger-doc.json {
"apiFolder" : " pages/api " ,
"schemaFolders" : [ " models " ],
"definition" : {
"openapi" : " 3.0.0 " ,
"info" : {
"title" : " Next Swagger API Example " ,
"version" : " 1.0 "
}
}
}yarn next-swagger-doc-cli next-swagger-doc.jsongh repo clone jellydn/next-swagger-doc
cd examples/next14-app
pnpm install
pnm run dev然后打开http:// localhost:3000/api-doc或http:// localhost:3000/在浏览器上
为了设置ESLINT规则,该规则检查所有API是否实际上都有Swagger JSDOC说明,我们可以使用以下设置:
安装JSDOC ESLINT插件:
yarn add -D eslint-plugin-jsdoc在ESLINT配置文件中创建自定义规则:
{
//...your configuration
"overrides" : [
//...your overrides
{
// Force the setting of a swagger description on each api endpoint
"files" : [ " pages/api/**/*.ts " ],
"plugins" : [ " jsdoc " ],
"rules" : {
"jsdoc/no-missing-syntax" : [
" error " ,
{
"contexts" : [
{
"comment" : " JsdocBlock:has(JsdocTag[tag=swagger]) " ,
"context" : " any " ,
"message" : " @swagger documentation is required on each API. Check this out for syntax info: https://github.com/jellydn/next-swagger-doc "
}
]
}
]
}
]
}该项目使用预先承诺来执行代码质量。要安装预加压钩,请运行:
pre-commit install? Huynh Duc Dung
如果这个项目对您有所帮助,请给!
谢谢这些好人(表情符号钥匙):
Dung duc huynh(kaka) | tmirkovic | 马修·霍洛威(Matthew Holloway) | Leventemihaly | Pahrizal Ma'rup | Aris | Valerio Ageno |
cachho |
该项目遵循全企业规范。欢迎任何形式的贡献!