Dar*_*rkZ 12 node.js jsdoc swagger visual-studio-code openapi
我在Express 中使用swagger-jsdoc。使用这个库来描述一个 api 端点,我在 YAML 的 JSDock 块中使用以下几行:
/**
* @swagger
* /users:
* post:
* summary: Register a user
* tags: [Users]
* description: Register a new user and return its cookie token (connect.sid)
* parameters:
* - in: body
* name: body
* schema:
* type: object
* required: [login, password, confirm]
* description: user's credential
* properties:
* login:
* type: string
* minLength: 3
* maxLength: 10
* email:
* type: string
* password:
* type: string
* minLength: 6
* confirm:
* type: string
* responses:
* 200:
* description: OK
* schema:
* $ref: '#/components/schemas/AuthState'
* 422:
* $ref: '#/components/responses/UnprocessableEntity'
*/
router.post('/', usersController.register);
Run Code Online (Sandbox Code Playgroud)
但问题是,当我放置新行时,VSCode 完全忽略了缩进,它也没有显示缩进级别,这使得制定规范变得非常困难,因为我必须按 [tab] 才能达到所需的缩进等级。彩虹缩进等扩展也不起作用,因为它们面向 vscode 缩进。
我可以使用任何设置或扩展来处理这个问题吗?或者也许我使用了错误的方法,并且有更好和更多使用的方法可以与 Express 一起使用?也想听听这些
我创建了一个简单的扩展,用于在使用swagger-jsdoc
.
所有内容都记录在自述文件中,但基本上您可以像这样编写规范(允许自动缩进)
/**
* Spec for the route /auth/register.
*
@openapi
/auth/register:
post:
summary: Register as user
tags:
- Auth
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- name
- email
- password
properties:
name:
type: string
email:
type: string
format: email
description: Must be unique
password:
type: string
format: password
minLength: 8
description: At least one number and one letter
*
* More regular comments here
*/
router.post("/auth/register", authMiddleware.validate, authController.register);
Run Code Online (Sandbox Code Playgroud)
选择您的评论块,按cmd + shift + P
(MacOS) 或ctrl + shift + P
(Windows) 并搜索Format swagger-jsdoc comment
。
该扩展将:
/**
* Spec for the route /auth/register.
*
* @openapi
* /auth/register:
* post:
* summary: Register as user
* tags:
* - Auth
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* - email
* - password
* properties:
* name:
* type: string
* email:
* type: string
* format: email
* description: Must be unique
* password:
* type: string
* format: password
* minLength: 8
* description: At least one number and one letter
*
*
* More regular comments here
*/
router.post("/auth/register", authMiddleware.validate, authController.register);
Run Code Online (Sandbox Code Playgroud)
你好,不管怎样,我通过在 jsdoc 中使用 JSON 编写 OpenAPI 位来避免 YAML 缩进兼容问题。swagger-jsdoc 包在 jsdoc 注释中使用 JSON。
在第一个示例中,我编写了普通的 JSON,其中 VSCode 缩进,之后我使用列选择将 放在*
每行前面
/**
* @openapi
* "/abc": {
* "get": {
* "description": "Welcome to swagger-jsdoc!",
* "responses": {
* "200": {
* "description": "Returns a mysterious string.",
* "content": {
* "text/xml": {
* "schema": {
* "$ref": "#/components/schemas/MyResponse"
* }
* }
* }
* }
* }
* }
* }
*/
app.get('/abc', (req, res) => {
res.send('Hello World!');
});
Run Code Online (Sandbox Code Playgroud)
*
在第二个示例中,我只需转到该方法即可获得 JSON 缩进get
。之后编写 JSON 时,VSCode 会给我缩进。
/**
* @openapi
* "/123": {
* "get": {
"description": "My numeric endpoint",
"responses": {
"200": {
"description": "Returns a mysterious number",
"content": {
"application/json": {
"$ref": "#/components/schemas/NumResponse"
}
}
}
}
}
}
*/
app.get('/123', (req, res) => {
res.send(123);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1321 次 |
最近记录: |