Skip to content

Commit

Permalink
Refactor NestJS application bootstrap to use shared withSwagger
Browse files Browse the repository at this point in the history
  • Loading branch information
wcalderipe committed Mar 27, 2024
1 parent 56561e8 commit 154f5e7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 89 deletions.
39 changes: 8 additions & 31 deletions apps/armory/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
import { withSwagger } from '@narval/nestjs-shared'
import { ClassSerializerInterceptor, INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory, Reflector } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { Config } from './armory.config'
import { ArmoryModule } from './armory.module'
import { ApplicationExceptionFilter } from './shared/filter/application-exception.filter'
import { HttpExceptionFilter } from './shared/filter/http-exception.filter'
import { ZodExceptionFilter } from './shared/filter/zod-exception.filter'

/**
* Adds Swagger documentation to the application.
*
* @param app - The INestApplication instance.
* @returns The modified INestApplication instance.
*/
const withSwagger = (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('Armory')
.setDescription('Armory is the most secure access management for web3')
.setVersion('1.0')
.build()
)

SwaggerModule.setup('docs', app, document, {
customSiteTitle: 'Armory API'
})

return app
}

/**
* Adds global pipes to the application.
*
Expand Down Expand Up @@ -95,7 +66,13 @@ async function bootstrap(): Promise<void> {

await lastValueFrom(
of(application).pipe(
map(withSwagger),
map(
withSwagger({
title: 'Armory',
description: 'Armory is the most secure access management for web3',
version: '1.0'
})
),
map(withGlobalPipes),
map(withGlobalInterceptors),
map(withGlobalFilters(configService)),
Expand Down
37 changes: 8 additions & 29 deletions apps/policy-engine/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
import { ConfigService } from '@narval/config-module'
import { withSwagger } from '@narval/nestjs-shared'
import { INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { Config } from './policy-engine.config'
import { PolicyEngineModule } from './policy-engine.module'
import { ApplicationExceptionFilter } from './shared/filter/application-exception.filter'
import { HttpExceptionFilter } from './shared/filter/http-exception.filter'

/**
* Adds Swagger documentation to the application.
*
* @param app - The INestApplication instance.
* @returns The modified INestApplication instance.
*/
const withSwagger = (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('Policy Engine')
.setDescription('The next generation of authorization for web3')
.setVersion('1.0')
.build()
)

SwaggerModule.setup('docs', app, document)

return app
}

/**
* Adds global pipes to the application.
*
Expand Down Expand Up @@ -74,7 +47,13 @@ async function bootstrap() {

await lastValueFrom(
of(application).pipe(
map(withSwagger),
map(
withSwagger({
title: 'Policy Engine',
description: 'The next generation of authorization for web3',
version: '1.0'
})
),
map(withGlobalPipes),
map(withGlobalFilters(configService)),
switchMap((app) => app.listen(port))
Expand Down
37 changes: 8 additions & 29 deletions apps/vault/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import { withSwagger } from '@narval/nestjs-shared'
import { INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory } from '@nestjs/core'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { patchNestJsSwagger } from 'nestjs-zod'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { MainModule } from './main.module'

/**
* Adds Swagger documentation to the application.
*
* @param app - The INestApplication instance.
* @returns The modified INestApplication instance.
*/
const withSwagger = (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('Vault')
.setDescription('The next generation of authorization for web3')
.setVersion('1.0')
.build()
)

SwaggerModule.setup('docs', app, document)

return app
}

/**
* Adds global pipes to the application.
*
Expand All @@ -56,7 +29,13 @@ async function bootstrap() {

await lastValueFrom(
of(application).pipe(
map(withSwagger),
map(
withSwagger({
title: 'Vault',
description: 'The next generation of authorization for web3',
version: '1.0'
})
),
map(withGlobalPipes),
switchMap((app) => app.listen(port))
)
Expand Down

0 comments on commit 154f5e7

Please sign in to comment.