From e69773fdd6889d14808dd094bc61d047ae8d09d5 Mon Sep 17 00:00:00 2001 From: Clement Date: Mon, 6 May 2024 20:37:03 +0200 Subject: [PATCH] add swagger example --- Express/barAndCafe/src/app.ts | 23 ++++++++++++++++------- Express/barAndCafe/src/server.ts | 6 ++++++ Express/barAndCafe/src/swaggerDef.ts | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 Express/barAndCafe/src/swaggerDef.ts diff --git a/Express/barAndCafe/src/app.ts b/Express/barAndCafe/src/app.ts index ea7282d..f9c7f25 100644 --- a/Express/barAndCafe/src/app.ts +++ b/Express/barAndCafe/src/app.ts @@ -1,23 +1,32 @@ -import express from "express"; +import express from "express" /** * Initialize Express application instance. * @returns An initialized Express application object. */ -const app = express(); +const app = express() /** - * Handle GET request for homepage route ('/'). + * Handle GET request for homepage route ('/welcome'). * Send back a simple json response. * @param {express.Request} req - HTTP Request object. * @param {express.Response} res - HTTP Response object. */ function getWelcome(req: express.Request, res: express.Response) { - const out = {hello:"world"}; - res.send(out); + const out = {hello:"world"} + res.send(out) } -app.get("/welcome", getWelcome); +/** + * @openapi + * /welcome: + * get: + * description: Welcome to swagger-jsdoc! + * responses: + * 200: + * description: Returns a welcome json. + */ +app.get("/welcome", getWelcome) -export default app; \ No newline at end of file +export default app \ No newline at end of file diff --git a/Express/barAndCafe/src/server.ts b/Express/barAndCafe/src/server.ts index 625a94c..1ce2e91 100644 --- a/Express/barAndCafe/src/server.ts +++ b/Express/barAndCafe/src/server.ts @@ -1,10 +1,16 @@ import * as dotenv from "dotenv"; import app from "./app"; +import options from "./swaggerDef"; +const swaggerJsdoc = require("swagger-jsdoc"), + swaggerUi = require("swagger-ui-express"); dotenv.config({path: '../../.env'}) const port = parseInt(process.env.BAR_PORT || '3000') +const specs = swaggerJsdoc(options) +app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs)) + app.listen(port, () =>{ console.log(`serveur running in ${port}`) }) \ No newline at end of file diff --git a/Express/barAndCafe/src/swaggerDef.ts b/Express/barAndCafe/src/swaggerDef.ts new file mode 100644 index 0000000..07aac1d --- /dev/null +++ b/Express/barAndCafe/src/swaggerDef.ts @@ -0,0 +1,25 @@ +import * as dotenv from "dotenv"; +dotenv.config({path: '../../.env'}) +const port = parseInt(process.env.BAR_PORT || '3000') + + +const options = { + definition: { + openapi: "3.1.0", + info: { + title: "LogRocket Express API with Swagger", + version: "0.1.0", + description: + "This is a simple CRUD API application made with Express and documented with Swagger", + }, + servers: [ + { + url: "http://localhost:"+port.toString(), + }, + ], + }, + apis: ['./src/*.ts'], + } + + +export default options \ No newline at end of file