Run time decorate ( + JS support)
Apply decoration in run time

Overview

Apply run time decoration is strong advantage of SugoiJS which gives us the flexibility to apply decorators in run-time, either if it because of conditions or lazy-loading.
Applying decorators in run-time provides us the ability to use decorators by pure JS files as well.

Usage

Decorate - Class decorator

For applying decorator method on a call we are able to call the sugoiJS decorate method
decorateProperty(methodToApply, target)

Example

1
// Using ES6
2
import {Catch, decorate} from '@sugoi/core';
3
// Using nodeJS
4
const {Catch, decorate} = require('@sugoi/core');
5
6
class Validator(){
7
static validate(item){
8
if(!item.valid){
9
throw new Error('Invalid')
10
}
11
}
12
}
13
decorate(Catch(()=>false),Validator);
Copied!

Decorate - Property decorator

For applying decorator method on class property we are able to call the sugoiJS decorateProperty method
decorateProperty(methodToApply, target, property)

Example

1
// Using ES6
2
import {Catch, decorateProperty} from '@sugoi/core';
3
// Using nodeJS
4
const {Catch, decorateProperty} = require('@sugoi/core');
5
6
class Validator(){
7
static validate(item){
8
if(!item.valid){
9
throw new Error('Invalid')
10
}
11
}
12
}
13
decorateProperty(Catch(()=>false), Validator, 'validate');
Copied!
Last modified 2yr ago