1.7.2 • Published 1 year ago

@eddaic/nestjs-decorators v1.7.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

nestjs-decorators

A variety of decorators, pipes and transformers intended for use with NestJS framework.

Included with each decorator are Swagger ApiProperty definitions to reduce boiler plate in DTOs.

Installation

npm install @eddaic/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.

import { ParseBigIntPipe } from '@eddaic/nestjs-decorators';
import { Controller, Get, Param } from '@nestjs/common';

@Controller()
export class AppController {
  @Get(':id')
  async getId(@Param('id', ParseBigIntPipe) id: bigint): bigint {
    return id;
  }
}

ApiPropertyBigInt, ApiPropertyBigIntOptional, IsBigInt, TransformBigInt

Validate bigint values or transform decimal values to bigint through a rounding policy.

import {
  ApiPropertyBigInt,
  ApiPropertyBigIntOptional,
  IsBigInt,
  RoundingPolicy,
  TransformBigInt,
} from '@eddaic/nestjs-decorators';
import { IsNumber, IsOptional } from 'class-validator';

export class MyDto {
  @IsBigInt()
  @ApiPropertyBigInt()
  id: bigint;

  @IsBigInt()
  @IsOptional()
  @ApiPropertyBigIntOptional()
  limit?: bigint;

  @IsNumber()
  @TransformBigInt({ rounding: RoundingPolicy.CEIL })
  @ApiPropertyBigInt()
  ceil: bigint;
}

ApiPropertyInt, TransformInt

Transform decimal values to integer number values through a rounding policy.

import {
  ApiPropertyInt,
  RoundingPolicy,
  TransformInt,
} from '@eddaic/nestjs-decorators';
import { IsNumber } from 'class-validator';

export class MyDto {
  @IsNumber()
  @TransformInt({ rounding: RoundingPolicy.FLOOR })
  @ApiPropertyInt()
  floor: number;
}

Transforming Arrays

import {
  ApiPropertyInt,
  RoundingPolicy,
  TransformInt,
} from '@eddaic/nestjs-decorators';
import { IsNumber } from 'class-validator';

export class MyDto {
  @IsNumber({ each: true })
  @TransformInt({ each: true, rounding: RoundingPolicy.FLOOR })
  @ApiPropertyInt({ isArray: true })
  floors: number[];
}

ApiPropertyDate, ApiPropertyDateOptional

import {
  ApiPropertyDate,
  ApiPropertyDateOptional,
} from '@eddaic/nestjs-decorators';
import { IsDate, IsOptional } from 'class-validator';

export class MyDto {
  @IsDate()
  @ApiPropertyDate()
  date: date;

  @IsDate()
  @IsOptional()
  @ApiPropertyDateOptional()
  optional_date?: date;
}

SplitString

Returns string array using specified separator (defaults to ,). Supporting RegExp also.

import { SplitString } from '@eddaic/nestjs-decorators';
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';

export class MyDto {
  @IsString({ each: true })
  @SplitString({ separator: '|' })
  @ApiProperty({ isArray: true, type: 'string' })
  strings: string[];
}

Combined with TransformInt

import { SplitString, TransformInt } from '@eddaic/nestjs-decorators';
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber } from 'class-validator';

export class MyDto {
  @IsNumber({ each: true })
  @SplitString({ separator: ',' })
  @TransformInt({ each: true })
  @ApiPropertyInt({ isArray: true })
  integers: number[];
}

Trim

import { Trim } from '@eddaic/nestjs-decorators';
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsNotEmpty } from 'class-validator';

export class MyDto {
  @IsString()
  @IsNotEmpty()
  @Trim()
  @ApiProperty()
  nonEmptyString: string;
}
1.9.0

1 year ago

2.3.0

8 months ago

2.2.0

9 months ago

2.1.1

9 months ago

2.5.0

7 months ago

2.4.0

8 months ago

1.10.0

10 months ago

2.1.0

9 months ago

2.0.0

9 months ago

1.8.0

1 year ago

1.7.2

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.0

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.1

1 year ago