
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
decorate-all
Advanced tools
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 thereflect-metadata
package as a peer dependency, so you need to have it installed.
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.
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"
The second parameter of the DecorateAll
decorator takes an options object with the following options:
exclude: string[]
// 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
// 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
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"
FAQs
Decorate all methods of a class using a single class decorator
The npm package decorate-all receives a total of 15,458 weekly downloads. As such, decorate-all popularity was classified as popular.
We found that decorate-all demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.