npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@bvhoach2393/nest-swagger

v1.0.11

Published

A NestJS library for easy Swagger/OpenAPI documentation setup

Readme

@bvhoach2393/nest-swagger

A NestJS library for easy Swagger/OpenAPI documentation setup with environment variable configuration support.

Description

This library provides a simple and flexible way to configure Swagger documentation for NestJS applications. It supports environment variable configuration and includes both basic and authentication-enabled Swagger setups.

Features

  • 🚀 Easy Swagger setup for NestJS applications
  • 🔧 Environment variable configuration
  • 🔐 Support for authentication in Swagger documentation
  • ⚠️ Automatic warnings for missing environment variables
  • 📝 TypeScript support with full type definitions
  • 🧪 Comprehensive unit tests

Installation

Install the library

npm install @bvhoach2393/nest-swagger

Install peer dependencies

npm install @nestjs/swagger swagger-ui-express @nestjs/config

Environment Variables

The library reads the following environment variables:

  • SWAGGER_TITLE - Title for your API documentation
  • SWAGGER_DESCRIPTION - Description for your API documentation
  • SWAGGER_VERSION - Version of your API
  • SWAGGER_PATH - Path where Swagger UI will be available (e.g., 'api-docs')
  • SWAGGER_API_DOC_PATH - Path for JSON documentation endpoint
  • SWAGGER_TAG - Default tag for your API endpoints

Example .env file

SWAGGER_TITLE=My Awesome API
SWAGGER_DESCRIPTION=This is an awesome API for managing resources
SWAGGER_VERSION=1.0.0
SWAGGER_PATH=api-docs
SWAGGER_API_DOC_PATH=api-json
SWAGGER_TAG=api

Usage

1. Import the module in your app module

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { NestSwaggerModule } from '@bvhoach2393/nest-swagger';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    NestSwaggerModule,
    // ... other modules
  ],
})
export class AppModule {}

2. Setup Swagger in your main.ts

import { NestFactory } from '@nestjs/core';
import { NestSwaggerService } from '@bvhoach2393/nest-swagger';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  // Get the swagger service
  const swaggerService = app.get(NestSwaggerService);
  
  // Setup basic Swagger
  swaggerService.setupSwagger(app);
  
  // Or setup Swagger with authentication
  // swaggerService.setupSwaggerWithAuth(app);
  
  await app.listen(3000);
}
bootstrap();

3. Override configuration programmatically (optional)

import { SwaggerConfig } from '@bvhoach2393/nest-swagger';

const customConfig: SwaggerConfig = {
  title: 'Custom API Title',
  description: 'Custom API Description',
  version: '2.0.0',
  path: 'docs',
  apiDocPath: 'api-json',
  tag: 'custom'
};

swaggerService.setupSwagger(app, customConfig);

Important Notes

⚠️ ConfigService is Optional: The library can work without ConfigService. If ConfigService is not available, it will use default values or the configuration you provide manually.

  • With ConfigService: Reads environment variables automatically
  • Without ConfigService: Uses default values and shows a warning

Usage Options

Option 1: With ConfigModule (Recommended)

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { NestSwaggerModule } from '@bvhoach2393/nest-swagger';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    NestSwaggerModule,
  ],
})
export class AppModule {}

Option 2: Without ConfigModule

import { Module } from '@nestjs/common';
import { NestSwaggerModule } from '@bvhoach2393/nest-swagger';

@Module({
  imports: [NestSwaggerModule],
})
export class AppModule {}

In this case, you should provide configuration manually:

const customConfig: SwaggerConfig = {
  title: 'My API',
  description: 'API Description',
  version: '1.0.0',
  path: 'api-docs',
  apiDocPath: 'api-json',
  tag: 'api'
};

swaggerService.setupSwagger(app, customConfig);

API Reference

NestSwaggerService

Methods

setupSwagger(app: INestApplication, config?: SwaggerConfig): void

Sets up basic Swagger documentation for your NestJS application.

Parameters:

  • app - NestJS application instance
  • config - Optional configuration to override environment variables
setupSwaggerWithAuth(app: INestApplication, config?: SwaggerConfig): void

Sets up Swagger documentation with Bearer authentication support.

Parameters:

  • app - NestJS application instance
  • config - Optional configuration to override environment variables

SwaggerConfig Interface

interface SwaggerConfig {
  title?: string;           // API title
  description?: string;     // API description
  version?: string;         // API version
  path?: string;           // Swagger UI path
  apiDocPath?: string;     // JSON documentation path
  tag?: string;            // Default API tag
}

Running Tests

Unit Tests

npm run test

Test Coverage

npm run test:cov

Test Watch Mode

npm run test:watch

Building the Library

npm run build

Publishing to NPM Registry

1. Build the library

npm run build

2. Test the package locally

npm pack

3. Login to NPM (if not already logged in)

npm login

4. Publish to NPM

npm publish

5. Publish with specific tag (optional)

npm publish --tag beta

Development

Prerequisites

  • Node.js >= 16.0.0
  • npm >= 7.0.0

Setup Development Environment

  1. Clone the repository
  2. Install dependencies: npm install
  3. Run tests: npm run test
  4. Build the library: npm run build

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for a list of changes.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.

Author

bvhoach2393

4. Copy files to dist and publish

npm run publish:lib:swagger


### Version Management

```bash
# Patch version (1.0.0 -> 1.0.1)
npm version patch

# Minor version (1.0.0 -> 1.1.0)
npm version minor

# Major version (1.0.0 -> 2.0.0)
npm version major

Examples

Basic Example

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestSwaggerService } from '@bvhoach2393/nest-swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  const swaggerService = new NestSwaggerService();
  swaggerService.setupSwagger(app);
  
  await app.listen(3000);
  console.log('API Documentation available at: http://localhost:3000/api');
}
bootstrap();

Advanced Example with Authentication

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestSwaggerService } from '@bvhoach2393/nest-swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  const swaggerService = new NestSwaggerService();
  
  swaggerService.setupSwaggerWithAuth(app, {
    title: 'My Secure API',
    description: 'API with Bearer Token and API Key authentication',
    version: '2.0',
    path: 'docs',
    tag: 'secure-api'
  });
  
  await app.listen(3000);
  console.log('API Documentation available at: http://localhost:3000/docs');
}
bootstrap();

Dependencies

Runtime Dependencies

  • @nestjs/swagger: ^8.0.0
  • swagger-ui-express: ^5.0.0

Peer Dependencies

  • @nestjs/common: ^10.0.0 || ^11.0.0
  • @nestjs/core: ^10.0.0 || ^11.0.0
  • reflect-metadata: ^0.1.13 || ^0.2.0

License

MIT

Contributing

  1. Fork the repository
  2. Create your feature branch: git checkout -b feature/my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature/my-feature
  5. Submit a pull request

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.