javascript - 使用 Fastify 在 Swagger 中添加动态查询参数
问题描述
是否可以在 Swagger 上使用 Fastify 记录动态查询参数,允许客户端使用 Swagger v1.0.0 在 Swagger UI 上的文本字段内传递参数值?在我的情况下是输入conversationId
参数的动态值。
这是我在配置文件夹中的 swagger.js 文件。
exports.options = {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Wrapper API',
description: 'Building a wrapper api',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost:3000',
schemes: [
'http',
'https'
],
consumes: ['application/json'],
produces: ['application/json'],
}
}
+ 这是我的路线
const healthBotController = require('../controllers/healthBotWrapperController')
const routes = [
{
method: 'GET',
url: '/',
handler: healthBotController.getEndpoints
},
]
module.exports = routes;
我试图搜索和阅读文档,但我还没有找到解决问题的方法。
提前谢谢你。
解决方案
要在 swagger 中添加查询参数,需要在路由配置中定义一个 JSON Schema:
const fastify = require('fastify')()
fastify.register(require('fastify-swagger'), {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Wrapper API',
description: 'Building a wrapper api',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost:3000',
schemes: [
'http',
'https'
],
consumes: ['application/json'],
produces: ['application/json'],
securityDefinitions: {
ApiToken: {
description: 'Authorization header token, sample: "Bearer #TOKEN#"',
type: 'apiKey',
name: 'Authorization',
in: 'header'
},
Oauth2Token: {
description: 'OAUTH2',
type: 'oauth2',
flow: 'accessCode',
authorizationUrl: 'https://example.com/oauth/authorize',
tokenUrl: 'https://example.com/oauth/token',
scopes: {
read: 'Grants read access',
foo: 'Grants foao scope'
}
}
}
}
})
fastify.route({
method: 'GET',
url: '/',
schema: {
security: [{ ApiToken: [], Oauth2Token: [] }],
querystring: {
foo: { type: 'number' },
bar: { type: 'string' }
}
},
handler: function foo () {}
})
fastify.listen(8080)
推荐阅读
- javascript - 我如何合并或连接数组数组?
- sql - 是否有可能加快这个加入?
- php - 安装、激活/停用插件时调用日志文件 (WordPress)
- reporting-services - 如何让 sql 报告服务器工作 2019
- html - 大家好。我刚开始独自学习html,我有一个问题
- vbscript - 如何将 VBScript 变量传递给服务器端 JScript?
- python - drf-spectacular 的 extend_schema 不起作用
- node.js - 为什么使用 Firebase Functions 作为 API 端点这么慢?
- javascript - 使用所需键过滤 JSON
- javascript - 我可以只检查第一个元素吗