This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chanlito
committed
Mar 8, 2018
1 parent
1ddffff
commit 47c366d
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# NestJS Extensions | ||
|
||
[WIP] A bunch of useful and opinionated filters, modules, pipes... to use with Nest framework. 😻 | ||
|
||
## Setup | ||
|
||
```bash | ||
npm install nestjs-extensions@latest | ||
``` | ||
|
||
## Usage | ||
|
||
* `ApplicationExceptionFilter` is a nestjs filter use to catch all exceptions & errors in the application. | ||
|
||
```ts | ||
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`. | ||
|
||
```ts | ||
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` | ||
|
||
```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 | ||
|
||
```ts | ||
// ... | ||
@Controller('posts') | ||
export class PostsController { | ||
@Post() | ||
async createPost(@Body() { title, description, count }: CreatePostDto) { | ||
return { title, description, count }; | ||
} | ||
} | ||
``` |