I new to Fastify and I am trying to setup swagger documentation with it. I am using TypeScript and all the examples that I found were using JavaScript and require
syntax.
I tried to follow the examples where was possible and now my documentation is not showing anything for the /
route I created.
Here is my current code:
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui';
import Fastify from 'fastify';
import { errorBoundary } from './plugins/errorBoundary';
const fastify = Fastify({
logger: true
});
const port = process.env.PORT || 3003;
// Set custom error handler.
fastify.setErrorHandler(errorBoundary);
// Register @fastify/swagger plugin.
fastify.register(fastifySwagger, {
openapi: {
info: {
title: 'Forest Fire API',
description: 'Forest Fire API Documentation',
version: '1.0.0'
},
servers: [
{
url: 'http://localhost'
}
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer'
}
}
},
tags: [
{
name: 'Root',
description: 'Root endpoints'
}
]
}
});
// Register @fastify/swagger-ui plugin.
fastify.register(fastifySwaggerUi, {
routePrefix: '/docs',
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
uiHooks: {
onRequest: function (_request, _reply, next) {
next();
},
preHandler: function (_request, _reply, next) {
next();
}
},
staticCSP: true,
transformStaticCSP: (header) => header,
transformSpecification: (swaggerObject) => {
return swaggerObject;
},
transformSpecificationClone: true
});
// Root route.
fastify.get(
'/',
{
schema: {
description: 'Root endpoint',
tags: ['Root'],
response: {
200: {
description: 'Succesful response',
type: 'object',
properties: {
message: { type: 'string' },
result: { type: 'object', nullable: true }
}
}
}
}
},
async function (request, reply) {
return reply.send({
message: 'Hello World',
result: null
});
}
);
async function start() {
await fastify.listen({
port: port as number
});
fastify.swagger();
}
start().catch((err) => {
fastify.log.error(err);
process.exit(1);
});
When I access the /docs
route, the following is presented:
I was expecting the /
route to be documented in the page. What are the possible errors?