0.1.2 • Published 4 years ago

@amiralblendic/nestjs-addons v0.1.2

Weekly downloads
4
License
MIT
Repository
-
Last release
4 years ago

@amiralblendic/nestjs-addons

This package contains modules, providers and gateways to quickly extend the functionnalities of your nestjs application.

LoggingModule - Simple Logging module

To use this module or any of its components, you must import it as part of one of your module

import { Module } from '@nestjs/common';
import { LoggingModule } from '@amiralblendic/nestjs-addons/dist/logging';

@Module({
  imports: [LoggingModule],
})
export class ExampleModule {}

LoggingInterceptor

Decorate your controllers with the following so that the LoggingModule becomes aware of the successful requests dispatched to your controller.

import { UseInterceptors, Controller, Get } from '@nestjs/common';
import { LoggingInterceptor } from '@amiralblendic/nestjs-addons/dist/logging';

@Controller('example')
@UseInterceptors(LoggingInterceptor)
export class ExampleController {
  @Get()
  getExample(): string {
    return 'example';
  }
}

This will allow all requests dispatched to ExampleController to be intercepted and logged by the LoggingModule.

A LoggingInterceptor alone is not able to catch HttpExceptions and hence is not able to log errors or unsucessful requests. To enable this feature, you must use LoggingExceptionFilter.

LoggingExceptionFilter

Decorate your controllers with the following so that the LoggingModule becomes aware of the unsuccessful requests dispatched to your controller.

import {
  UseInterceptors,
  UseFilters,
  Controller,
  Get,
  UnauthorizedException,
} from '@nestjs/common';
import {
  LoggingInterceptor,
  LoggingExceptionFilter,
} from '@amiralblendic/nestjs-addons/dist/logging';

@Controller('example')
@UseInterceptors(LoggingInterceptor)
@UseFilters()
export class ExampleController {
  @Get()
  getExample(): string {
    return 'example';
  }

  @Get('unauthorized')
  getUnauthorized(): string {
    throw new UnauthorizedException();
  }
}

Now, whenever a HTTP GET /example/unauthorized is issued, the LoggingModule will be able to get the UnauthorizedException and to log it.

However there is an even easier way to use this LoggingModule and its features

GlobalLoggingInterceptor & GlobalLoggingExceptionFilter

You can import GlobalLoggingInterceptor and GlobalLoggingExceptionFilter and directly use them at your own module's level.

WARNING: this will enable the LoggingModule globally, so use at your own risks

import { Module } from '@nestjs/common';
import {
  LoggingModule,
  GlobalLoggingInterceptor,
  GlobalLoggingExceptionFilter,
} from '@amiralblendic/nestjs-addons/dist/logging';

@Module({
  controllers: [
    /* ... */
  ],
  providers: [GlobalLoggingInterceptor, GlobalLoggingExceptionFilter],
  imports: [LoggingModule],
})
export class ExampleModule {}

With this setup, all your routes will be taken into account by the LoggingModule. This is the recommended way of using this module, but this is not the only one.

StatsModule - Simple metrics gateway for your NestJS app

This module is useless if LoggingModule is being used since it consumes all its data from it.

StatsGateway

This component is a websockets gateway to expose multiple KPIs of your NestJS app, gathered from the LoggingModule.

Setup

The setup is fairly simple: you only have to import the StatsGateway and pass it in the providers array of the module of your choice.

For this example, we will use the default and recommended LoggingModule + StatsGateway setup.

import { Module } from '@nestjs/common';
import {
  LoggingModule,
  GlobalLoggingInterceptor,
  GlobalLoggingExceptionFilter,
} from '@amiralblendic/nestjs-addons/dist/logging';
import { StatsGateway } from '@amiralblendic/nestjs-addons/dist/stats';

@Module({
  controllers: [ExampleController],
  providers: [
    GlobalLoggingInterceptor,
    GlobalLoggingExceptionFilter,
    StatsGateway,
  ],
  imports: [TodosModule, LoggingModule],
})
export class ExampleModule {}

Now you should be able to open a socket-io/ws connection to the /stats path (i.e. if your NestJS app is running on https://example.com/, then you should open a connection to https://example.com/stats).