I am building an Express app with TypeScript, but now I am struggling to add swagger to my project. Here is my index.ts
import UserRoutes from "./routers/UserRoutes";
import swaggerJsdoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
const options: swaggerJsdoc.Options = // in the comment
const swaggerSpec = swaggerJsdoc(options);
this.app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
this.app.use("/api/v1/users", UserRoutes);
here is the UserRoutes.ts
class UserRoutes extends BaseRoutes {
public routes(): void {
this.router.get("/", UserController.index);
}
}
export default new UserRoutes().router;
and here is my UserController
class UserController implements IUserController {
index = async (req: Request, res: Response): Promise<Response> => {
const service = new UserService(req);
const users = await service.getAll();
return res.send({ data: users, message: "success" });
};
export default new UserController();
and finally here is my UserService.ts
class UserService {
getAll = async () => {
const users = await db.user.findAll({
attributes: ["id", "username"],
});
return users;
};
}
I don't really understand where do I need to put swagger docs like ```ts
/**
@swagger
blablabla
**/
I tried adding it in the routes and the service, and all I got is this in the swagger ui:
No operations defined in spec!
any idea/example online? I could not find example onlie which has the same architecture as what I made, and I am new to Node js as well. Thank you in advance,