@eddaic/nestjs-decorators
Version:
Additional decorators intended for use with NestJS framework.
185 lines (146 loc) • 3.89 kB
Markdown
# nestjs-decorators
A variety of decorators, pipes and transformers intended for use with [NestJS](https://nestjs.com/) framework.
Included with each decorator are Swagger `ApiProperty` definitions to reduce boiler plate in DTOs.
## Installation
```sh
npm install /nestjs-decorators
```
## Decorators
- `ApiPropertyBigInt`
- `ApiPropertyBigIntOptional`
- `ApiPropertyDate`
- `ApiPropertyDateOptional`
- `ApiPropertyInt`
- `ApiPropertyIntOptional`
- `IsBigInt`
- `ParseBigIntPipe`
- `SplitString`
- `TransformBigInt`
- `TransformInt`
- `Trim`
## Examples
### `ParseBigIntPipe`
Pipe parameters as bigint values. A `BadRequestException` is thrown if unable to tranform.
```typescript
import { ParseBigIntPipe } from '@eddaic/nestjs-decorators';
import { Controller, Get, Param } from '@nestjs/common';
export class AppController {
async getId( id: bigint): bigint {
return id;
}
}
```
### `ApiPropertyBigInt`, `ApiPropertyBigIntOptional`, `IsBigInt`, `TransformBigInt`
Validate `bigint` values or transform decimal values to `bigint` through a rounding policy.
```typescript
import {
ApiPropertyBigInt,
ApiPropertyBigIntOptional,
IsBigInt,
RoundingPolicy,
TransformBigInt,
} from '@eddaic/nestjs-decorators';
import { IsNumber, IsOptional } from 'class-validator';
export class MyDto {
id: bigint;
limit?: bigint;
ceil: bigint;
}
```
### `ApiPropertyInt`, `TransformInt`
Transform decimal values to integer `number` values through a rounding policy.
```typescript
import {
ApiPropertyInt,
RoundingPolicy,
TransformInt,
} from '@eddaic/nestjs-decorators';
import { IsNumber } from 'class-validator';
export class MyDto {
floor: number;
}
```
#### Transforming Arrays
```typescript
import {
ApiPropertyInt,
RoundingPolicy,
TransformInt,
} from '@eddaic/nestjs-decorators';
import { IsNumber } from 'class-validator';
export class MyDto {
floors: number[];
}
```
### `ApiPropertyDate`, `ApiPropertyDateOptional`
```typescript
import {
ApiPropertyDate,
ApiPropertyDateOptional,
} from '@eddaic/nestjs-decorators';
import { IsDate, IsOptional } from 'class-validator';
export class MyDto {
date: date;
optional_date?: date;
}
```
### `SplitString`
Returns string array using specified separator (defaults to `,`). Supporting `RegExp` also.
```typescript
import { SplitString } from '@eddaic/nestjs-decorators';
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';
export class MyDto {
strings: string[];
}
```
#### Combined with `TransformInt`
```typescript
import { SplitString, TransformInt } from '@eddaic/nestjs-decorators';
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber } from 'class-validator';
export class MyDto {
integers: number[];
}
```
### `Trim`
```typescript
import { Trim } from '@eddaic/nestjs-decorators';
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';
export class MyDto {
nonEmptyString: string;
}
```