import fp from 'fastify-plugin'; import swagger from '@fastify/swagger'; import swaggerUI from '@fastify/swagger-ui'; import { readFileSync } from 'fs'; import { join } from 'path'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export default fp(async (fastify) => { // Load OpenAPI spec const openAPISpec = JSON.parse( readFileSync(join(__dirname, '../openapi.json'), 'utf-8') ); // Register Swagger plugin await fastify.register(swagger, { mode: 'static', specification: { document: openAPISpec }, }); // Register Swagger UI await fastify.register(swaggerUI, { routePrefix: '/docs', uiConfig: { docExpansion: 'list', deepLinking: true, displayRequestDuration: true, filter: true, showExtensions: true, showCommonExtensions: true, tryItOutEnabled: true, }, staticCSP: true, transformStaticCSP: (header) => header, transformSpecification: (swaggerObject) => { return swaggerObject; }, transformSpecificationClone: true, }); // Serve OpenAPI spec as JSON endpoint fastify.get('/openapi.json', async () => { return openAPISpec; }); fastify.log.info('📚 Swagger UI available at /docs'); fastify.log.info('📄 OpenAPI spec available at /openapi.json'); });