node.js - Fastify Swagger, thêm nhãn và mô tả cho tuyến đường fastify
Tôi đang sử dụng fastify 3.28.0 (cộng với plugin fastify-swagger) với typescript 4.6.2, và tôi muốn thêm tags, description và summary bên trong mỗi route.
Từ tài liệu: fastify swagger Có thể thêm mô tả, thẻ và tóm tắt cho mỗi tuyến.
Nhưng tôi không thể làm được điều đó vì định nghĩa của sơ đồ không cung cấp chúng:
export interface FastifySchema {
body?: unknown;
querystring?: unknown;
params?: unknown;
headers?: unknown;
response?: unknown;
}
Vì vậy trong trường hợp này tôi không biết cách định nghĩa chúng bên trong các route của fastify:
const userRoutes: FastifyPluginCallback = (fastify, _opts, done) => {
const findRoute: RouteOptions = {
method: 'GET',
url: '/:username',
schema: {
params: schemaParams,
response: {
200: schema
}
},
handler: (request: FastifyRequest<{
Params: UserRequestFindParams,
}>, reply) => {
const user = find(request.params.username)
reply.send(user)
}
}
fastify.route(findRoute)
done()
}
*** CẬP NHẬT
Tôi vừa phát hiện ra rằng một phiên bản mới của fastify-swagger đã được phát hành (fastify-swagger đã được thay thế bằng @fastify/swagger). Với phiên bản mới, tất cả các thuộc tính được đề cập ở trên đều có sẵn.
Câu trả lời #1
- Với phiên bản fastify bằng "4.21.x"
- Chạy lệnh
fastify generate project;cd project; npm i - Tạo một tệp
plugins/swagger.js
const fp = require('fastify-plugin')
module.exports = fp(async function (fastify, opts) {
await fastify.register(require('@fastify/swagger'), {
swagger: {
info: {
title: 'Admin Api',
description: 'admin api',
version: '0.1.0',
},
basePath: '',
schemes: ['http', 'https'],
consumes: ['application/json'],
produces: ['application/json'],
securityDefinitions: {
Authorization: {
description: 'Authorization header token, sample: "Bearer #TOKEN#"',
type: 'apiKey',
name: 'Authorization',
in: 'header'
}
}
},
})
await fastify.register(require('@fastify/swagger-ui'), {
routePrefix: '/docs',
uiConfig: {
docExpansion: 'none', // expand/not all the documentations none|list|full
deepLinking: true
},
uiHooks: {
onRequest: function (request, reply, next) {
next()
},
preHandler: function (request, reply, next) {
next()
}
},
staticCSP: false,
transformStaticCSP: (header) => header,
exposeRoute: true,
})
fastify.addHook('onRoute', (routeOptions) => {
if (routeOptions.schema && routeOptions.url == '/graphql') {
routeOptions.schema.hide = true
}
});
});
- Tạo tệp mới routes/test.js
'use strict'
const fastify = require('fastify')();
const schemaPostList = {
description: 'Admin Post List',
tags: ['Admin'],
summary: 'Admin Post List',
security: [{ "Authorization": []}],
querystring: {
page: { type: 'number' },
user: { type: 'string' }
},
};
const schemaPostDetail = {
description: 'Admin Post Detail',
tags: ['Admin'],
summary: 'Admin Post Detail',
security: [{ "Authorization": []}]
};
module.exports = async function (fastify, opts) {
fastify.get('/list', {schema:schemaPostList},async function (request, reply) {
return 'this is an example'
});
}
- Chạy lệnh
npm run dev
Nó trông giống như:
