aspect-ts
v1.0.6
Published
aspect oriented programming for typescript and javascript
Downloads
4
Readme
aspectTS
A simple typescript Aspect implementation for AOP (Aspect Oriented Programming)
Install
yarn install
Development
For development two tasks are available:
- yarn test - Runs the tests
- yarn build - Transpile the code into lib directory
Usage
The package exposes 5 methods :
- before
Allow to run an advice before a chosen method.
const x = new MyClass(); // assuming MyClass has a method doIt();
before(x, 'doIt', () => {
console.log('this runs before doIt()');
});
- after
Allow to run an advice after a chosen method, regardless the success or the failure of that method
const x = new MyClass(); // assuming MyClass has a method doIt();
after(x, 'doIt', () => {
console.log('this runs after doIt()');
});
- afterReturn
Allow to run an advice after a chosen method, if and only if the method successfully returns
const x = new MyClass(); // assuming MyClass has a method doIt();
afterReturn(x, 'doIt', () => {
console.log('doIt() method passed successfully');
});
- afterThrow
Allow to run an advice after a chosen method, if and only if the method throw an error
const x = new MyClass(); // assuming MyClass has a method doIt();
afterThrow(x, 'doIt', () => {
console.log('doIt() method thrown an error');
});
- around
Allow to run an advice before and after a chosen method. This is the most powerful kind of advice as it can controls whether or not to run the original method as well as the arguments given to it.
const x = new MyClass(); // assuming MyClass has a method doIt();
around(x, 'doIt', (nextAdvice) => {
console.log('this runs before doIt()');
nextAdvice(); // next advice might be another around or the doIt() method itself
console.log('this runs after doIt()');
});
// with arguments:
const x = new MyClass(); // assuming MyClass has a method doIt(value);
around(x, 'doIt', (value, nextAdvice) => {
console.log('this runs before doIt()');
nextAdvice(value); // next advice might be another around or the doIt() method itself
console.log('this runs after doIt()');
});