Getting No operations defined in spec! or Typescript error depending on my apis[] settings
Nikola-Milovic opened this issue · 1 comments
Trying to get swaggerjsdocs work with my express typescript setup. I either get No operations defined in spec! and no error or I get TypeError: Cannot convert undefined or null to object
depending on what path I try and use for my apis[] setting
TypeError: Cannot convert undefined or null to object
When trying to use swaggerjsdocs. My setup is Typescript in the src folder and dist for the compiled js.
App.ts
...
// Express configuration
app.set("port", process.env.PORT || 3000);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
route(app);
//Swagger
const swaggerOptions = {
swaggerDefinition: {
openapi: "3.0.0",
info: {
title: "REST API",
description:"",
version: "1.0.0",
},
},
apis: [ "dist/controllers/*.controller.js"],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
export default app;
My server.ts
const server = app.listen(app.get("port"),
routes/routes.ts
import controllers...
export default function (app: express.Express): void {
const auth = authMiddleware();
app.use("/api/test/account", TestController);
app.use("/api/example/", ExampleController);
}
And my controllers are
const router = express.Router();
router.post("/something", async (req, res) => {
try {
const iid: string = req.body["id"];
res.sendStatus(200);
} catch (e) {
res.sendStatus(500);
}
});
export const TestController = router;
Preferably I'd point the swaggerDocs towards my routes/routes.ts and it would know what to do, if that's not possible I'll remove the routes/routes.ts all together. I tried pointing to routes/routes.ts, routes.js, *controllers.ts, *.controller.js,
And for the filepath I tried ${__dirname}/dist and src, (both the ts and js files), ./, full path and so on. Nothing has worked so far, I either get the above error or I don't get any output
Can you provide a repo reproducing your problem to us to try to understand it better?