1.4.0 • Published 11 months ago

@emackie/nestjs-decorators v1.4.0

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

nestjs-decorators

DEPRECATED: Moved to @eddiac/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 @emackie/nestjs-decorators

Decorators

  • ApiPropertyBigInt
  • ApiPropertyBigIntOptional
  • ApiPropertyDate
  • ApiPropertyDateOptional
  • ApiPropertyInt
  • ApiPropertyIntOptional
  • IsBigInt
  • ParseBigIntPipe
  • TransformBigInt
  • TransformInt
  • Trim

Examples

ParseBigIntPipe

Pipe parameters as bigint values, raising BadRequestException if unable to tranform.

import { ParseBigIntPipe } from '@emackie/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 '@emackie/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 '@emackie/nestjs-decorators';
import { IsNumber } from 'class-validator';

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

ApiPropertyDate, ApiPropertyDateOptional

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

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

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

Trim

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

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

11 months ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

0.2.0

1 year ago