npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decorators-utils

v1.2.5

Published

Using decorator to apply AOP (Aspect Oriented Programming)

Downloads

41

Readme

AOP Decorators

The module contain decorators that help us to apply Aspect Oriented Programming (AOP) into NodeJs project. It uses Winston as based logger

Notes:

  • It will process from top to bottom.
class SampleClass {
    @IsString()
    @Transform((x) => x.concat(' transformed'))
    @Mapping('renamedFieldName')
    @MaxStringSize(5)
    private sampleProperty: boolean;
}

// The flow will be:
//  - Validate isString
//  - Transform data
//  - Mapping field name
//  - Validate String length

Table of Contents

Installation

npm install decorators-utils

or

yarn add decorators-utils

Usage

@logContext()
class SampleClass {
  public logger: ILogger = new LoggerImpl();

  @validate
  @logInputParams()
  public sampleMethod(@required() _params: any): any {
    this.logger.info('dummy message');
  }
}

Validator

The module supports testing for string, number, boolean ,object, array, and Test to support custom Test

String

Possible options for String: IsEmail, IsMatched, IsString, MaxStringSize, MinStringSize

//Define class schema
class SampleClass {
    @IsString()
    @IsMatched(/^dog/)
    @IsEmail()
    @MaxStringSize(5)
    @MinStringSize(1)
    private sampleProperty: string;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: 'sample' });

//Sample returned error
{
  message: 'sampleProperty must be string',
  name: 'IS_STRING',
  path: 'sampleProperty',
}

Number

Possible options for String: IsLarger, IsLess, IsNumber, MaxDigits, MinDigits

//Define class schema
class SampleClass {
    @IsLarger(2)
    @IsLess(5)
    @IsNumber()
    @MaxDigits(3)
    @MinDigits(1)
    private sampleProperty: number;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: 123 });

//Sample returned error
{
  message: 'sampleProperty must be number',
  name: 'IS_NUMBER',
  path: 'sampleProperty',
}

Boolean

Possible options for String: IsBoolean

//Define class schema
class SampleClass {
    @IsBoolean()
    private sampleProperty: boolean;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: true });

//Sample returned error
{
  message: 'sampleProperty must be boolean',
  name: 'IS_BOOLEAN',
  path: 'sampleProperty',
}

Object

Possible options for String: IsValidObject

//Define class schema
class ChildClass {
  @IsString()
  private requestId: string
}

class SampleClass {
    @IsValidObject()
    private sampleProperty: ChildClass;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: { requestId:'string' } });

//Sample returned error
{
  message: 'requestId must be string',
  name: 'IS_VALID_OBJECT',
  path: 'sampleProperty.requestId',
}

Array

Possible options for String: IsValidArray, ArraySize

//Define class schema
class ChildClass {
  @IsString()
  private requestId: string
}

class SampleClass {
    @IsValidArray(ChildClass)
    private sampleProperty: ChildClass[];
}

class SampleClass {
    @IsValidArray(String)
    private sampleProperty: String[];
}

class SampleClass {
    @IsValidArray(Number)
    private sampleProperty: Number[];
}

class SampleClass {
    @IsValidArray(Boolean)
    private sampleProperty: Boolean[];
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: [{ requestId:'string' }] });

//Sample returned error
{
  message: 'must be a valid array element',
  name: 'IS_VALID_ARRAY',
  path: 'sampleProperty[2]',
};
class SampleClass {
    @ArraySize({ max: 2, min: 1 })
    private sampleProperty: Boolean[];
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: [{ requestId:'string' }] });

//Sample returned error
{
  message: 'sampleProperty size must be less than 2, greater than 1',
  name: 'IS_VALID_ARRAY',
  path: 'sampleProperty',
};

Custom Test

//Define class schema
const canConvertStringToNum = (value: string): boolean => {
    return !!parseInt(value);
};

class SampleClass {
  @Test(canConvertStringToNum)
  private sampleProperty: string;

  public getData(): void {
    console.log(this.sampleProperty);
  }
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: '123' });

//Sample returned error
{
  message: 'sampleProperty custom test failed',
  name: 'canConvertStringToNum',
  path: 'sampleProperty',
};

Transform

modify the input data

//Define class schema
class SampleClass {
    @Transform((x) => x.concat(' transformed'))
    private sampleProperty: boolean;
}

//Run function to validate data based on the schema above
const data = { sampleProperty: 'sampleData' }
validateSchema(SampleClass, data);

//New data
console.log(data.sampleProperty) // => sampleData transformed

Mapping

Change field name

//Define class schema
class SampleClass {
    @Mapping('renamedFieldName')
    private sampleProperty: boolean;
}

//Run function to validate data based on the schema above
const data = { sampleProperty: 'sampleData' }
validateSchema(SampleClass, data);

//New data
console.log(data.renamedFieldName) // => sampleData transformed

LogContext

@logContext()
class SampleClass {
  public logger: ILogger = new LoggerImpl();

  public sampleMethod(_params: any): any {
    this.logger.info('dummy message');
  }
}

The message will be [SampleClass] [sampleMethod] dummy message

LogInputParam

class SampleClass {
  public logger: ILogger = new LoggerImpl();

  @logInputParams()
  public sampleMethod(_params: any): any {
    this.logger.info('dummy message');
  }
}

When the method is called, an additional log record will appear method is called with param with the input param value

Parameters

class SampleClass {
  public logger: ILogger = new LoggerImpl();

  @validate
  public sampleMethod(@required() _params: any): any {}
}

Although _params is a required parameters, it still accept undefined value. It will throw error when @required(error), or even return boolean value (true/false) by @required(false)

CHANGELOG

CHANGELOG