@habibovulugbek/nest-circuit-breaker
TypeScript icon, indicating that this package has built-in type declarations

0.0.17 • Public • Published

Circuit Breaker for NestJS

Before installation

To install this package, run the following commands:

Installation

$ pnpm add --save-prod @habibovulugbek/nest-circuit-breaker

If you want to use the RpcExceptionFilter, ExpressExceptionFilter or FastifyExceptionFilter filter with the ExceptionInterceptor interceptor, you must install additional packages:

$ pnpm add --save-prod @nestjs/common reflect-metadata rxjs

Getting started

You can use the RpcExceptionFilter filter with the ExceptionInterceptor interceptor in the main.ts file to handle all exceptions in the rpc application:

// main.ts
import type { NatsOptions } from '@nestjs/microservices'
import { NestFactory } from '@nestjs/core'
import { Transport } from '@nestjs/microservices'
import { RpcExceptionFilter, ExceptionInterceptor } from '@sello-lab/exceptions'
import { AppModule } from './app.module'

setImmediate(async (): Promise<void> => {
  const app = await NestFactory.createMicroservice<NatsOptions>(AppModule, {
    transport: Transport.NATS,
    options: {
      servers: ['nats://127.0.0.1:4222'],
    },
  })

  app.useGlobalFilters(new RpcExceptionFilter())
  app.useGlobalInterceptors(new ExceptionInterceptor())

  await app.listen()
})

You can use the ExpressExceptionFilter filter with the ExceptionInterceptor interceptor in the main.ts file to handle all exceptions in the express application:

// main.ts
import type { NestExpressApplication } from '@nestjs/platform-express'
import { NestFactory } from '@nestjs/core'
import { ExpressAdapter } from '@nestjs/platform-express'
import { ExpressExceptionFilter, ExceptionInterceptor } from '@sello-lab/exceptions'
import { AppModule } from './app.module'

setImmediate(async (): Promise<void> => {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, new ExpressAdapter())

  app.useGlobalFilters(new ExpressExceptionFilter())
  app.useGlobalInterceptors(new ExceptionInterceptor())

  await app.listen(3000, '127.0.0.1')
})

You can use the FastifyExceptionFilter filter with the ExceptionInterceptor interceptor in the main.ts file to handle all exceptions in the fastify application:

// main.ts
import type { NestFastifyApplication } from '@nestjs/platform-fastify'
import { NestFactory } from '@nestjs/core'
import { FastifyAdapter } from '@nestjs/platform-fastify'
import { FastifyExceptionFilter, ExceptionInterceptor } from '@sello-lab/exceptions'
import { AppModule } from './app.module'

setImmediate(async (): Promise<void> => {
  const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter())

  app.useGlobalFilters(new FastifyExceptionFilter())
  app.useGlobalInterceptors(new ExceptionInterceptor())

  await app.listen(3000, '127.0.0.1')
})

There are 11 exceptions in this package. You can use them in your project.

Enjoy!

Package Sidebar

Install

npm i @habibovulugbek/nest-circuit-breaker

Weekly Downloads

1

Version

0.0.17

License

MIT

Unpacked Size

20.1 kB

Total Files

20

Last publish

Collaborators

  • habibovulugbek