Skip to content
This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
/ nestjs-extensions Public archive

[WIP] A bunch of useful and opinionated filters, modules, pipes... to use with Nest framework. 😻

Notifications You must be signed in to change notification settings

chanlito/nestjs-extensions

Repository files navigation

NestJS Extensions

[WIP] A bunch of useful and opinionated filters, modules, pipes... to use with Nest framework. 😻

Setup

npm install nestjs-extensions@latest

Usage

  • ApplicationExceptionFilter is a nestjs filter use to catch all exceptions & errors in the application.

    import { ApplicationExceptionFilter } from 'nestjs-extensions';
    // ... other imports
    
    const app = await NestFactory.create();
    
    app.useGlobalFilters(new ApplicationExceptionFilter());
  • DtoPipe & Dto is used for validation. Internally it uses class-transformer & class-validator.

    • Step 1 - use the pipe, it requires a nestjs Reflector.
    import { DtoPipe } from 'nestjs-extensions';
    // ... other imports
    
    const app = await NestFactory.create();
    
    app.useGlobalPipes(new DtoPipe(new Reflector()));
    • Step 2 - create a file called create-post.dto.ts
    import { Transform } from 'class-transformer';
    import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
    import { Dto } from 'nestjs-extensions';
    
    @Dto()
    export class CreatePostDto {
      @IsNotEmpty()
      @IsString()
      title!: string;
    
      @IsString()
      @IsOptional()
      description?: string;
    
      @IsNotEmpty()
      @Transform(x => +x)
      count!: number;
    }
    • Step 3 - use it inside your controller
    // ...
    @Controller('posts')
    export class PostsController {
      @Post()
      async createPost(@Body() { title, description, count }: CreatePostDto) {
        return { title, description, count };
      }
    }

About

[WIP] A bunch of useful and opinionated filters, modules, pipes... to use with Nest framework. 😻

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published