configure-swagger-docs.helper.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import { INestApplication } from '@nestjs/common'
  2. import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
  3. import { ConfigService } from '@nestjs/config'
  4. import * as basicAuth from 'express-basic-auth'
  5. const SWAGGER_ENVS = ['local', 'dev', 'staging']
  6. export function configureSwaggerDocs(app: INestApplication, configService: ConfigService) {
  7. if (SWAGGER_ENVS.includes(configService.get<string>('NODE_ENV'))) {
  8. app.use(
  9. ['/docs', '/docs-json', '/docs-yaml'],
  10. basicAuth({
  11. challenge: true,
  12. users: {
  13. [configService.get<string>('SWAGGER_USER')]: configService.get<string>('SWAGGER_PASSWORD')
  14. }
  15. })
  16. )
  17. const config = new DocumentBuilder()
  18. .setTitle('API')
  19. .setDescription('The API description')
  20. .setVersion('1.0')
  21. .addTag('auth')
  22. .addBearerAuth()
  23. .build()
  24. const document = SwaggerModule.createDocument(app, config)
  25. SwaggerModule.setup('/docs', app, document)
  26. }
  27. }