Skip to content

Commit

Permalink
Enhance API documentation with Swagger
Browse files Browse the repository at this point in the history
Fixes DEVRhylme-Foundation#5

Add Swagger API documentation to the project.

* **internal/structure/structure.go**
  - Add a new function `AddSwaggerDocs` to set up Swagger documentation.
  - Call `AddSwaggerDocs` in `CreateBaseFileStructure` after copying the directory.

* **.templates/jsbase/src/app.js**
  - Import `swagger-ui-express` and `swagger-jsdoc`.
  - Define Swagger documentation in JSDoc format.
  - Set up Swagger UI to serve at `/api-docs`.

* **.templates/tsbase/src/app.ts**
  - Import `swagger-ui-express` and `swagger-jsdoc`.
  - Define Swagger documentation in JSDoc format.
  - Set up Swagger UI to serve at `/api-docs`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/DEVRhylme-Foundation/expressify/issues/5?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
yashksaini-coder committed Feb 16, 2025
1 parent 7e57b5d commit 2fe4a63
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .templates/jsbase/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require('swagger-jsdoc');

const app = express();

const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Express API with Swagger',
version: '1.0.0',
},
},
apis: ['./src/routes/*.js'], // files containing annotations as above
};

const specs = swaggerJsdoc(options);

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

module.exports = app;
22 changes: 22 additions & 0 deletions .templates/tsbase/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';

const app = express();

const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Express API with Swagger',
version: '1.0.0',
},
},
apis: ['./src/routes/*.ts'], // files containing annotations as above
};

const specs = swaggerJsdoc(options);

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));

export default app;
44 changes: 44 additions & 0 deletions internal/structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func CreateBaseFileStructure(projectName string, language string) error {
} else {
fmt.Println("Directory copied successfully.")
}

AddSwaggerDocs(dstPath, language)

return nil
}

Expand Down Expand Up @@ -102,3 +105,44 @@ func CopyDir(src, dst string) error {
}
return nil
}

func AddSwaggerDocs(projectPath string, language string) error {
var appFilePath string
if language == string(languages.JavaScript) {
appFilePath = filepath.Join(projectPath, "src", "app.js")
} else {
appFilePath = filepath.Join(projectPath, "src", "app.ts")
}

file, err := os.OpenFile(appFilePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open app file: %w", err)
}
defer file.Close()

swaggerSetup := `
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require('swagger-jsdoc');
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Express API with Swagger',
version: '1.0.0',
},
},
apis: ['./src/routes/*.js'], // files containing annotations as above
};
const specs = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
`

if _, err := file.WriteString(swaggerSetup); err != nil {
return fmt.Errorf("unable to write swagger setup to app file: %w", err)
}

return nil
}

0 comments on commit 2fe4a63

Please sign in to comment.