Library for aspect-oriented programming with JavaScript, which takes advantage of ECMAScript 2016 decorators syntax.
For further reading on decorators, take a look at the spec.
Blog post, introduction to the AOP and the library could be found here.
The library offers the following combinations of advices and joint points:
beforeMethod(MethodSelector)
- invoked before method callafterMethod(MethodSelector)
- invoked after method callaroundMethod(MethodSelector)
- invoked around method callonThrowOfMethod(MethodSelector)
- invoked on throw of method call
beforeStaticMethod(MethodSelector)
- invoked before static method callafterStaticMethod(MethodSelector)
- invoked after static method callaroundStaticMethod(MethodSelector)
- invoked around static method callonThrowOfStaticMethod(MethodSelector)
- invoked on throw of static method call
beforeSetter(MemberSelector)
- invoked before setter callafterSetter(MemberSelector)
- invoked after setter callaroundSetter(MemberSelector)
- invoked around setter callonThrowOfSetter(MemberSelector)
- invoked on throw of setter callbeforeGetter(MemberSelector)
- invoked before getter callafterGetter(MemberSelector)
- invoked after getter callaroundGetter(MemberSelector)
- invoked around getter callonThrowOfGetter(MemberSelector)
- invoked on throw of getter call
export interface MethodSelector {
classNamePattern: RegExp;
methodNamePattern: RegExp;
}
export interface MemberSelector {
classNamePattern: RegExp;
fieldNamePattern: RegExp;
}
export class Metadata {
public method: MethodMetadata;
public className: string;
}
export class MethodMetadata {
public proceed: boolean;
public name: string;
public args: any[];
public context: any;
public result: any;
public exception: any;
}
import {beforeMethod, Wove, Metadata} from 'aspect.js';
class LoggerAspect {
@beforeMethod({
classNamePattern: /^Article/,
methodNamePattern: /^(get|set)/
})
invokeBeforeMethod(meta: Metadata) {
console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
}
}
class Article {
id: number;
title: string;
content: string;
}
@Wove()
class ArticleCollection {
articles: Article[] = [];
getArticle(id: number) {
console.log(`Setting article with id: ${id}.`);
return this.articles.filter(a => {
return a.id === id;
}).pop();
}
setArticle(article: Article) {
console.log(`Setting article with id: ${article.id}.`);
this.articles.push(article);
}
}
new ArticleCollection().getArticle(1);
// Result:
// Inside of the logger. Called ArticleCollection.getArticle with args: 1.
// Setting article with id: 1.
The library is in early stage of development. It's API most likely will change.
git clone https://github.com/mgechev/aop.js --depth 1
npm install -g ts-node
ts-node demo/index.ts
- Tests
- Type annotations and DTS generation
- Implement the following joint points:
- Method execution
- Static method execution
- Constructor execution
- Filed get
- Field set
- Aspect factories
- Generic aspects
- Implement the following advices:
- Before
- After
- Throwing
- Returning
- Around
MIT