forked from nestjs/nest
-
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
1 parent
81be8a0
commit 52e0128
Showing
645 changed files
with
13,202 additions
and
10,929 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,3 @@ | ||
{ | ||
"singleQuote": true | ||
} |
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
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
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
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
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
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 |
---|---|---|
|
@@ -13,4 +13,4 @@ export class CatsService { | |
findAll(): Cat[] { | ||
return this.cats; | ||
} | ||
} | ||
} |
11 changes: 4 additions & 7 deletions
11
examples/01-cats-app/src/modules/cats/dto/create-cat.dto.ts
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 |
---|---|---|
@@ -1,12 +1,9 @@ | ||
import { IsString, IsInt } from 'class-validator'; | ||
|
||
export class CreateCatDto { | ||
@IsString() | ||
readonly name: string; | ||
@IsString() readonly name: string; | ||
|
||
@IsInt() | ||
readonly age: number; | ||
@IsInt() readonly age: number; | ||
|
||
@IsString() | ||
readonly breed: string; | ||
} | ||
@IsString() readonly breed: string; | ||
} |
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ export interface Cat { | |
readonly name: string; | ||
readonly age: number; | ||
readonly breed: string; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
examples/01-cats-app/src/modules/common/decorators/roles.decorator.ts
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { ReflectMetadata } from '@nestjs/common'; | ||
|
||
export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles); | ||
export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles); |
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
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
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
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
26 changes: 20 additions & 6 deletions
26
examples/01-cats-app/src/modules/common/interceptors/exception.interceptor.ts
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 |
---|---|---|
@@ -1,14 +1,28 @@ | ||
import { Interceptor, NestInterceptor, ExecutionContext, HttpStatus } from '@nestjs/common'; | ||
import { | ||
Interceptor, | ||
NestInterceptor, | ||
ExecutionContext, | ||
HttpStatus | ||
} from '@nestjs/common'; | ||
import { HttpException } from '@nestjs/common'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import 'rxjs/add/operator/catch'; | ||
import 'rxjs/add/observable/throw'; | ||
|
||
@Interceptor() | ||
export class ExceptionInterceptor implements NestInterceptor { | ||
intercept(dataOrRequest, context: ExecutionContext, stream$: Observable<any>): Observable<any> { | ||
return stream$.catch((err) => Observable.throw( | ||
new HttpException('Exception interceptor message', HttpStatus.BAD_GATEWAY), | ||
)); | ||
intercept( | ||
dataOrRequest, | ||
context: ExecutionContext, | ||
stream$: Observable<any> | ||
): Observable<any> { | ||
return stream$.catch(err => | ||
Observable.throw( | ||
new HttpException( | ||
'Exception interceptor message', | ||
HttpStatus.BAD_GATEWAY | ||
) | ||
) | ||
); | ||
} | ||
} | ||
} |
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
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
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
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
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
37 changes: 21 additions & 16 deletions
37
examples/01-cats-app/src/modules/common/pipes/validation.pipe.ts
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 |
---|---|---|
@@ -1,25 +1,30 @@ | ||
import { HttpException } from '@nestjs/common'; | ||
import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common'; | ||
import { | ||
PipeTransform, | ||
Pipe, | ||
ArgumentMetadata, | ||
HttpStatus | ||
} from '@nestjs/common'; | ||
import { validate } from 'class-validator'; | ||
import { plainToClass } from 'class-transformer'; | ||
|
||
@Pipe() | ||
export class ValidationPipe implements PipeTransform<any> { | ||
async transform(value, metadata: ArgumentMetadata) { | ||
const { metatype } = metadata; | ||
if (!metatype || !this.toValidate(metatype)) { | ||
return value; | ||
} | ||
const object = plainToClass(metatype, value); | ||
const errors = await validate(object); | ||
if (errors.length > 0) { | ||
throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST); | ||
} | ||
async transform(value, metadata: ArgumentMetadata) { | ||
const { metatype } = metadata; | ||
if (!metatype || !this.toValidate(metatype)) { | ||
return value; | ||
} | ||
|
||
private toValidate(metatype): boolean { | ||
const types = [String, Boolean, Number, Array, Object]; | ||
return !types.find((type) => metatype === type); | ||
const object = plainToClass(metatype, value); | ||
const errors = await validate(object); | ||
if (errors.length > 0) { | ||
throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
private toValidate(metatype): boolean { | ||
const types = [String, Boolean, Number, Array, Object]; | ||
return !types.find(type => metatype === type); | ||
} | ||
} |
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
Oops, something went wrong.