This repository was archived by the owner on Mar 22, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ # NestJS Extensions
2+
3+ [ WIP] A bunch of useful and opinionated filters, modules, pipes... to use with Nest framework. 😻
4+
5+ ## Setup
6+
7+ ``` bash
8+ npm install nestjs-extensions@latest
9+ ```
10+
11+ ## Usage
12+
13+ * ` ApplicationExceptionFilter ` is a nestjs filter use to catch all exceptions & errors in the application.
14+
15+ ``` ts
16+ import { ApplicationExceptionFilter } from ' nestjs-extensions' ;
17+ // ... other imports
18+
19+ const app = await NestFactory .create ();
20+
21+ app .useGlobalFilters (new ApplicationExceptionFilter ());
22+ ```
23+
24+ * ` DtoPipe ` & ` Dto ` is used for validation. Internally it uses ` class-transformer ` & ` class-validator ` .
25+
26+ * * Step 1* - use the pipe, it requires a nestjs ` Reflector ` .
27+
28+ ``` ts
29+ import { DtoPipe } from ' nestjs-extensions' ;
30+ // ... other imports
31+
32+ const app = await NestFactory .create ();
33+
34+ app .useGlobalPipes (new DtoPipe (new Reflector ()));
35+ ```
36+
37+ * * Step 2* - create a file called ` create-post.dto.ts `
38+
39+ ``` ts
40+ import { Transform } from ' class-transformer' ;
41+ import { IsNotEmpty , IsOptional , IsString } from ' class-validator' ;
42+ import { Dto } from ' nestjs-extensions' ;
43+
44+ @Dto ()
45+ export class CreatePostDto {
46+ @IsNotEmpty ()
47+ @IsString ()
48+ title! : string ;
49+
50+ @IsString ()
51+ @IsOptional ()
52+ description? : string ;
53+
54+ @IsNotEmpty ()
55+ @Transform (x => + x )
56+ count! : number ;
57+ }
58+ ```
59+
60+ * * Step 3* - use it inside your controller
61+
62+ ``` ts
63+ // ...
64+ @Controller (' posts' )
65+ export class PostsController {
66+ @Post ()
67+ async createPost(@Body () { title , description , count }: CreatePostDto ) {
68+ return { title , description , count };
69+ }
70+ }
71+ ```
You can’t perform that action at this time.
0 commit comments