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

Commit

Permalink
initial readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanlito committed Mar 8, 2018
1 parent 1ddffff commit 47c366d
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions README.md
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 };
}
}
```

0 comments on commit 47c366d

Please sign in to comment.