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

@rjavlonn/toolkit

v1.0.0

Published

Nest js tool kit

Downloads

11

Readme

Nest toolkit

Nest utils library


@Output() Decorator

@Output({...}) allows you to customize data returned by the controller layer.
It combines the usage of @SerializeOptions({...}) and @UseInterceptors(...).

For a better understanding of how SerializeOptions and UseInterceptors work, checkout the documentation on the official NestJS Website.

Configuration

To configure the output you can provide 3 properties:

| key | Type | description |
|---|---|---| | class | Function | The decorated DTO Class you want to use. | | transform | TransformOptions? | class-transformer transform options | | validate | ValidationOptions? | class-validator validation options |

Usage

@Output({...}) decorator is a method decorator for controllers only:

    @Get('/:id/endpoint')
    @Output({
        class: ResourceDTO,
        transform: {
            groups: [Groups.Public],
            excludeExtraneousValues: true
        },
        transform: {
            always: true
        }
    })
    public async method(
        @Param('id') id: string,
        @Criteria() criteria?: QueryCriteria<Resource>,
    ): Promise<Resource> {
        criteria = criteria || new QueryCriteria<Resource>({
            survey: { id }
        });

        return await this.resourceService.one(criteria);
    }
}

@Catch Decorator

Default use case

From your custom controller :

class ControllerA {
    get() {
        try {
            // find data
        } catch (e) {
            throw new InternalServerErrorException(e);
        }
    }
}

You can now use the Catch decorator, by default it will map an InternalServerErrorException

class ControllerA {
    @Catch()
    get() {
        // find data
    }
}

Classic use case

From your custom controller :

class ControllerA {
    get() {
        try {
            // find data
        } catch (e) {
            if (e instanceof MyCustomErrorA)
                throw new ConflictException(e);
            if (e instanceof MyCustomErrorB)
                throw new NotFoundException(e);
            throw InternalServerErrorException(e);
        }
    }
}

You can now use the Catch decorator.

class ControllerA {
    @Catch({
        [MyCustomErrorA.name]: ConflictException,
        [MyCustomErrorB.name]: NotFoundException
    })
    get() {
        // find data
    }
}

If MyCustomErrorA is throws, it will be mapped on ConflictException

If MyCustomErrorB is throws, it will be mapped on NotFoundException

Others will be mapped on InternalServerErrorException

Advanced use case

From your custom controller :

class ControllerA {
    get() {
        try {
            // find data
        } catch (e) {
            if (e instanceof MyCustomErrorA) {
                console.log('Show me what you got.');
                throw new ConflictException(e);            
            }
            if (e instanceof MyCustomErrorB)
                throw new NotFoundException(e);
            throw InternalServerErrorException(e);
        }
    }
}

You can now use the Catch decorator.

class HTTPExceptionFactory implements IHttpExceptionFactory {
    public create(e) {
        console.log('Show me what you got.');
        return new ConflictException(e);
    }
}

class ControllerA {
    @Catch({
        [MyCustomErrorA.name]: HTTPExceptionFactory,
        [MyCustomErrorB.name]: NotFoundException
    })
    get() {
        // find data
    }
}

If MyCustomErrorA is throws, it will be mapped on HTTPExceptionFactory and then call the create method. Here you can customize your business rules related to the error.

If MyCustomErrorB is throws, it will be mapped on NotFoundException

Others will be mapped on InternalServerErrorException

Configuration

The second parameter of the decorator takes a CatchOption configuration object with the following properties :

| key | Type | Default | description |
|--------------------------|------------|---------|-------------------------------------------------------------------------------------------------| | reply | Boolean | true | Use an ExceptionFilter to reply to the incoming HttpRequest with the mapped HttpException | | logClientErrorsAsWarning | Boolean | false | Will use logger.warn event for errors mapped to client errors HTTP codes |