Skip to content

Commit

Permalink
✨ Added Swagger API Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ICEatm committed Jun 20, 2024
1 parent 0e9b811 commit 1985493
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
66 changes: 63 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.1",
"axios": "^1.7.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down
41 changes: 40 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { VersioningType } from '@nestjs/common';
import { VersioningType, INestApplication } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

Expand Down Expand Up @@ -36,6 +37,44 @@ async function bootstrap() {
*/
app.enableVersioning({ type: VersioningType.URI });

/**
* Sets up Swagger configuration and documentation for the application.
*
* This function initializes the Swagger documentation for the application using the SwaggerModule
* and DocumentBuilder from the NestJS framework. It configures the Swagger document with the
* title, description, and version, which can be provided through environment variables or default
* to specified strings if not provided.
*
* @remarks
* The Swagger documentation is an essential tool for documenting and testing the API. It provides
* a user-friendly interface to explore the endpoints, parameters, and responses. This setup ensures
* that the Swagger documentation is available at the `/docs` endpoint of the application.
*
* @example
* ```typescript
* const app = await NestFactory.create(AppModule);
* setupSwagger(app);
* await app.listen(3000);
* ```
*
* @param app - The NestJS application instance.
*
* @returns void
*/
const setupSwagger = (app: INestApplication): void => {
const swaggerConfig = new DocumentBuilder()
.setTitle((process.env.SWAGGER_TITLE as string) ?? 'Swagger Title')
.setDescription(
(process.env.SWAGGER_DESCRIPTION as string) ?? 'Swagger Description',
)
.setVersion((process.env.SWAGGER_VERSION as string) ?? '1.0')
.build();

const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('docs', app, swaggerDocument);
};

setupSwagger(app);
await app.listen(3000);
}
bootstrap();
8 changes: 8 additions & 0 deletions src/modules/index/index.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Controller, Get } from '@nestjs/common';
import { IndexService } from './index.service';

@ApiTags('Index')
@Controller()
export class IndexController {
constructor(private readonly indexService: IndexService) {}

@Get()
@ApiOperation({
summary: 'Get Index',
description: 'Retrieves the index information.',
})
@ApiResponse({ status: 200, description: 'Successful response.' })
@ApiResponse({ status: 500, description: 'Internal server error.' })
getIndex() {
return this.indexService.getIndex();
}
Expand Down

0 comments on commit 1985493

Please sign in to comment.