nestjs-extensions
v0.29.0
Published
A collection of useful and opinionated modules to use with Nest framework. 😻
Downloads
26
Maintainers
Readme
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 usesclass-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 }; } }
- Step 1 - use the pipe, it requires a nestjs