Socket
Socket
Sign inDemoInstall

@nestjs/terminus

Package Overview
Dependencies
Maintainers
3
Versions
90
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/terminus

Terminus integration provides readiness/liveness health checks for NestJS.


Version published
Weekly downloads
770K
increased by4.92%
Maintainers
3
Weekly downloads
 
Created

What is @nestjs/terminus?

@nestjs/terminus is a health check module for NestJS applications. It provides a way to monitor the health of your application by exposing various health indicators and endpoints. This can be useful for ensuring that your application is running smoothly and for integrating with monitoring tools.

What are @nestjs/terminus's main functionalities?

Basic Health Check

This code sets up a basic health check endpoint in a NestJS application. The `HealthCheckService` is used to perform the health check, and the `@HealthCheck` decorator is used to mark the endpoint.

```typescript
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { HealthController } from './health.controller';

@Module({
  imports: [TerminusModule],
  controllers: [HealthController],
})
export class AppModule {}

import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(private health: HealthCheckService) {}

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([]);
  }
}
```

Database Health Check

This code adds a database health check to the health endpoint. The `TypeOrmHealthIndicator` is used to check the health of the database connection.

```typescript
import { TypeOrmHealthIndicator, HealthCheckService, HealthCheck } from '@nestjs/terminus';
import { Controller, Get } from '@nestjs/common';

@Controller('health')
export class HealthController {
  constructor(
    private health: HealthCheckService,
    private db: TypeOrmHealthIndicator,
  ) {}

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([
      async () => this.db.pingCheck('database'),
    ]);
  }
}
```

Custom Health Indicator

This code demonstrates how to create a custom health indicator. The `CustomHealthIndicator` class extends `HealthIndicator` and implements the `isHealthy` method. This custom indicator is then used in the health check endpoint.

```typescript
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
import { Injectable } from '@nestjs/common';

@Injectable()
export class CustomHealthIndicator extends HealthIndicator {
  async isHealthy(key: string): Promise<HealthIndicatorResult> {
    const isHealthy = true; // Custom logic to determine health
    const result = this.getStatus(key, isHealthy);
    if (isHealthy) {
      return result;
    }
    throw new HealthCheckError('Custom health check failed', result);
  }
}

import { Controller, Get } from '@nestjs/common';
import { HealthCheck, HealthCheckService } from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(
    private health: HealthCheckService,
    private customHealthIndicator: CustomHealthIndicator,
  ) {}

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([
      async () => this.customHealthIndicator.isHealthy('custom'),
    ]);
  }
}
```

Other packages similar to @nestjs/terminus

FAQs

Package last updated on 18 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc