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

decorate-all

v1.2.1

Published

Decorate all methods of a class using a single class decorator

Downloads

157,678

Readme

decorate-all

A typescript decorator that allows you to apply another decorator to all methods of a class.

npm i decorate-all

Note: since version 1.1.0, this package uses the reflect-metadata package as a peer dependency, so you need to have it installed.

Why

A need for this decorator arose from having to decorate all methods (route handlers) of a controller class with the same parameter decorator to apply the framework-specific metadata. In the project, which is a multi-tenant application, most routes contain the same tenantId path parameter and repeating it over each method turned out to be tedious and error prone.

This decorator is, however, not limited to adding simple metadata decorators. Another use case can be applying a logging decorator (that actually alters the method's implementation) on all methods of a class for debugging or for tracing purposes.

Usage

import { DecorateAll } from 'decorate-all';

const Uppercase = (
    target: any,
    propertyKey: string,
    descriptor: PropertyDescriptor,
) => {
    const original = descriptor.value;
    descriptor.value = function () {
        return original.call(this).toUpperCase();
    };
};

// instead of decorating each method on its own...
class Hello1 {
    @Uppercase
    world() {
        return 'world';
    }

    @Uppercase
    galaxy() {
        return 'galaxy';
    }
}

// ...use the DecorateAll decorator
@DecorateAll(Uppercase)
class Hello2 {
    world() {
        return 'world';
    }
    galaxy() {
        return 'galaxy';
    }
}

const hello1 = new Hello1();
console.log(hello1.world()); // logs "WORLD"
console.log(hello1.galaxy()); // logs "GALAXY"

const hello2 = new Hello2();
console.log(hello2.world()); // logs "WORLD"
console.log(hello2.galaxy()); // logs "GALAXY"

Options

The second parameter of the DecorateAll decorator takes an options object with the following options:

  • exclude: string[]
    array of method names that won't be decorated
// apply the Uppercase decorator to all methods, except 'galaxy'
@DecorateAll(Uppercase, { exclude: ['galaxy'] })
class Hello {
    world() {
        return 'world';
    }
    galaxy() {
        return 'galaxy';
    }
    universe() {
        return 'universe';
    }
}

const hello = new Hello();
console.log(hello.world()); // logs "WORLD"
console.log(hello.galaxy()); // logs "galaxy"
console.log(hello.universe()); // logs "UNIVERSE"
  • excludePrefix: string
    methods with the given prefix won't be decorated
// apply the Uppercase decorator to all methods, except 'galaxy'
@DecorateAll(Uppercase, { excludePrefix: '_' })
class Hello {
    world() {
        return 'world';
    }
    _galaxy() {
        return 'galaxy';
    }
    universe() {
        return 'universe';
    }
}

const hello = new Hello();
console.log(hello.world()); // logs "WORLD"
console.log(hello._galaxy()); // logs "galaxy"
console.log(hello.universe()); // logs "UNIVERSE"
  • deep: boolean
    By default, only the class' own methods are decorated. If you pass deep: true, the decorator will be also applied to inherited methods of any extended class. (It can also be combined with the exclude options).
class Plain {
    hi() {
        return 'hi';
    }
    hello() {
        return 'hello';
    }
}

@DecorateAll(Uppercase, { deep: true })
class Decorated extends Plain {}

const plain = new Plain();
console.log(plain.hi()); // logs "hi"
console.log(plain.hello()); // logs "hello"

const decorated = new Decorated();
console.log(decorated.hi()); // logs "HI"
console.log(decorated.hello()); // logs "HELLO"