所以,我正在尝试使用 Swagger 创建 api 文档,但是,我正在尝试避免为我们的所有调用使用巨大的 yaml 文件。这是我想要做的:
server.js
const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
title: "API",
version: "1.0.0",
description: "The API",
contact: {
name: "name"
},
},
servers: [
{
url: "http://localhost:5000/api/"
},
],
},
basePath: "/",
apis: [
"./swagger/main.yaml",
]
};
const swaggerDocs = swaggerJSDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
main.yaml
components:
securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: apikey
paths:
/user:
$ref: "user.yaml" #<-- I've tried many different variations of this path but none of them worked
user.yaml
get:
summary: "Get user by ID"
parameters:
- in: query
name: id
required: true
schema:
type: integer
responses:
"200":
description: "Successful response"
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
security:
- ApiKeyAuth: []
我已经尝试了很多不同的文件路径变体,但都没有用。我一直收到错误消息:
Could not resolve reference: Tried to resolve a relative URL, without having a basePath. path: 'user.yaml' basePath: 'undefined'
根据 their documentation for using
$ref
,这似乎是可能的。
我只是想把一切都组织起来,避免为我所有的 api 调用使用一个巨大的 yaml 文件。