@tsdi/aop
v6.0.48
Published
AOP base on Ioc container, via typescript decorator
Downloads
97
Maintainers
Readme
packaged @tsdi/aop
This repo is for distribution on npm
. The source for this module is in the
main repo.
@tsdi/aop
is AOP base on Ioc container, via @tsdi/core, typescript decorator.
Install
npm install @tsdi/aop
import { AopModule } from '@tsdi/aop';
// in server
import { ContainerBuilder } from '@tsdi/platform-server'
// in browser
import { ContainerBuilder } from '@tsdi/platform-browser'
let builder = new ContainerBuilder();
let container = build.create();
container.use(AopModule);
Documentation
AOP
It's a dynamic aop base on ioc.
define a Aspect class, must with decorator:
- @Aspect
define advice decorator have
@Before(matchstring|RegExp)
@After(matchstring|RegExp)
@Around(matchstring|RegExp)
@AfterThrowing(matchstring|RegExp)
@AfterReturning(matchstring|RegExp)
@Pointcut(matchstring|RegExp)
see simples
import { TypeMetadata, IClassMethodDecorator, createClassMethodDecorator} from '@tsdi/core';
import { Joinpoint, Around, Aspect , Pointcut } from '@tsdi/aop';
export const Authorization: IClassMethodDecorator<TypeMetadata> = createClassMethodDecorator<TypeMetadata>('Authorization');
// auth check simple.
@Aspect
export class AuthAspect {
// pointcut for method has @Authorization decorator.
@Pointcut('@annotation(Authorization)', 'authAnnotation')
auth(joinPoint: Joinpoint, authAnnotation:MethodMetadata[]) {
console.log('aspect annotation Before log, method name:', joinPoint.fullName, ' state:', joinPoint.state, ' returning:', joinPoint.returning, ' throwing:', joinPoint.throwing);
}
}
@Aspect
export class SecrityAspect {
// before AuthAspect.auth check some.
@Before('execution(AuthAspect.auth)', 'authAnnotation')
sessionCheck(authAnnotation:MethodMetadata[],joinPoint: Joinpoint) {
console.log('aspect execution check session secrity Before AnnotationAspect.auth, method name:', joinPoint.fullName, ' state:', joinPoint.state, ' returning:', joinPoint.returning, ' throwing:', joinPoint.throwing);
}
}
// Log simple
@Singleton
@Aspect
export class DebugLog {
@Before(/\w+Controller.\w+$/)
// @Before('execution(*Controller.*)')
Beforlog(joinPoint: Joinpoint) {
console.log('aspect Before log:', joinPoint.fullName);
}
@Around('execution(*Controller.*)')
log(joinPoint: Joinpoint) {
console.log('aspect Around log, method name:', joinPoint.fullName, ' state:', joinPoint.state, ' Args:', joinPoint.args, ' returning:', joinPoint.returning, ' throwing:', joinPoint.throwing);
}
}
Documentation
Documentation is available on the
- @tsdi/ioc document.
- @tsdi/aop document.
- @tsdi/core document.
- @tsdi/boot document.
- @tsdi/components document.
- @tsdi/compiler document.
- @tsdi/activities document.
- @tsdi/pack document.
- @tsdi/typeorm-adapter document.
- @tsdi/unit document.
- @tsdi/unit-console document.
- @tsdi/cli document.
packages
@tsdi/cli @tsdi/ioc @tsdi/aop @tsdi/core @tsdi/boot @tsdi/components @tsdi/compiler @tsdi/activities @tsdi/pack @tsdi/typeorm-adapter @tsdi/unit @tsdi/unit-console
License
MIT © Houjun