Skip to content

Commit

Permalink
Add utility to set up Swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
wcalderipe committed Mar 27, 2024
1 parent a372ec8 commit 56561e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/nestjs-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './lib/decorator'
export * from './lib/dto'
export * from './lib/util'
1 change: 1 addition & 0 deletions packages/nestjs-shared/src/lib/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './with-swagger.util'
29 changes: 29 additions & 0 deletions packages/nestjs-shared/src/lib/util/with-swagger.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { INestApplication } from '@nestjs/common'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'

/**
* Adds Swagger documentation to the application.
*
* @param app - The INestApplication instance.
* @returns The modified INestApplication instance.
*/
export const withSwagger =
(params: { title: string; description: string; version: string }) =>
(app: INestApplication): INestApplication => {
// IMPORTANT: This modifies the Nest Swagger module to be compatible with
// DTOs created by Zod schemas. The patch MUST be done before the
// configuration process.
patchNestJsSwagger()

const document = SwaggerModule.createDocument(
app,
new DocumentBuilder().setTitle(params.title).setDescription(params.description).setVersion(params.version).build()
)

SwaggerModule.setup('docs', app, document, {
customSiteTitle: `${params.title} API`
})

return app
}

0 comments on commit 56561e8

Please sign in to comment.