Class

NestBootstraper

NestBootstraper()

Constructor

# new NestBootstraper()

The NestBootstraper class provides a chainable API for configuring a NestJS application instance. It includes built-in methods for enabling CORS, Helmet security, Swagger documentation, global pipes, filters, interceptors, and starting the server.

This class promotes consistency and reduces repetitive setup code across multiple NestJS projects.

A fluent, static bootstrap class for initializing and configuring a NestJS application.

View Source factory/NestBootstraper.ts, line 54

Example
```ts
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { MyLogger } from "./MyLogger";
import { NestBootstraper } from "@decaf-ts/for-nest";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  await NestBootstraper
    .initialize(app)
    .enableLogger(new MyLogger())
    .enableCors(["http://localhost:4200"])
    .useHelmet()
    .setupSwagger({
      title: "OpenAPI by TradeMarkā„¢",
      description: "TradeMarkā„¢ API documentation",
      version: "1.0.0",
      path: "api",
      persistAuthorization: true,
      topbarBgColor: "#2C3E50",
      topbarIconPath: "/assets/logo.svg",
      faviconPath: "/assets/favicon.ico"
    })
    .useGlobalFilters()
    .useGlobalPipes(...)
    .useGlobalInterceptors(...)
    .start(3000);
}

bootstrap();
```

Members

# static logger

Ensures that a valid Logger instance is always available for logging bootstrap-related messages.

Returns the current logger instance, creating a default one if not set.

View Source factory/NestBootstraper.ts, line 67

Methods

# static enableCors(originsopt, allowMethodsopt) → {NestBootstraper}

Allows defining either a wildcard origin ("*") or a list of allowed origins. Automatically accepts local development requests and those without origin headers. Throws a CorsError for unauthorized origins.

Enables Cross-Origin Resource Sharing (CORS) for the application.

Parameters:
Name Type Attributes Default Description
origins '*' | Array.<string> <optional>
[]

List of allowed origins or "*" to allow all.

allowMethods Array.<string> <optional>
['GET', 'POST', 'PUT', 'DELETE']

Allowed HTTP methods.

View Source factory/NestBootstraper.ts, line 354

Returns the class for chaining configuration.

# static enableLogger(customLoggeropt) → {NestBootstraper}

If a custom logger is provided, it replaces the default logger. Otherwise, a new logger named "NestBootstrap" is used. This logger is also registered with the NestJS application.

Enables or replaces the global logger for the NestJS application.

Parameters:
Name Type Attributes Description
customLogger Logger <optional>

Optional custom logger instance.

View Source factory/NestBootstraper.ts, line 338

Returns the class for chaining.

# static initialize(app) → {NestBootstraper}

Binds the provided NestJS app instance to the bootstrapper, enabling chained configuration methods.

Initializes the bootstrapper with a given NestJS application.

Parameters:
Name Type Description
app INestApplication

The NestJS application instance to initialize.

View Source factory/NestBootstraper.ts, line 324

Returns the class for chaining configuration methods.

# static setupSwagger(options) → {NestBootstraper}

Uses the SwaggerBuilder utility to configure API documentation with detailed customization for title, version, paths, and colors. Swagger is automatically exposed at the configured path.

Configures and initializes Swagger UI for API documentation.

Parameters:
Name Type Description
options SwaggerSetupOptions

Swagger configuration options.

View Source factory/NestBootstraper.ts, line 382

Returns the class for chaining configuration.

# async static start(portopt, hostopt, logopt) → {Promise.<void>}

Listens on the specified port and optionally a host. Once started, logs the application URL for easy access. The startup process resolves once the application is successfully running.

Starts the NestJS application and binds it to the given port and host.

Parameters:
Name Type Attributes Default Description
port number <optional>
3000

Port number to listen on.

host string <optional>

Optional host or IP address to bind to.

log boolean <optional>
true

Whether to log the application URL upon startup.

View Source factory/NestBootstraper.ts, line 440

Resolves once the application starts successfully.

Promise.<void>

# static useGlobalFilters(…filters)

If no filters are provided, it automatically registers a default set of standard exception filters for common error types like HttpException, ValidationException, ConflictException, and others.

Registers one or more global exception filters.

Parameters:
Name Type Attributes Description
filters Array.<ExceptionFilter> <repeatable>

Optional filters to apply globally.

View Source factory/NestBootstraper.ts, line 409

# static useGlobalInterceptors(…interceptors) → {NestBootstraper}

Interceptors allow advanced request/response manipulation such as serialization, logging, or transformation. Multiple interceptors can be added for modular configuration.

Registers global interceptors for request and response transformation.

Parameters:
Name Type Attributes Description
interceptors Array.<NestInterceptor> <repeatable>

Interceptor instances to register.

View Source factory/NestBootstraper.ts, line 423

Returns the class for chaining configuration.

# static useGlobalPipes(…pipes) → {NestBootstraper}

Enables request payload validation and transformation globally across the entire NestJS application. Multiple pipes can be chained together for modular input validation.

Registers one or more global validation pipes.

Parameters:
Name Type Attributes Description
pipes Array.<PipeTransform> <repeatable>

Pipe instances to register globally.

View Source factory/NestBootstraper.ts, line 396

Returns the class for chaining.

# static useHelmet(optionsopt) → {NestBootstraper}

Dynamically loads the helmet package if available and registers it as middleware to improve HTTP header security. If not installed, logs a warning and continues execution without throwing errors.

Applies the Helmet middleware for enhanced security.

Parameters:
Name Type Attributes Description
options Record.<string, any> <optional>

Optional configuration passed to Helmet.

View Source factory/NestBootstraper.ts, line 368

Returns the class for chaining configuration.