@tsdi/aop
TypeScript icon, indicating that this package has built-in type declarations

6.0.48 • Public • Published

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

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

Package Sidebar

Install

npm i @tsdi/aop

Weekly Downloads

3

Version

6.0.48

License

MIT

Unpacked Size

739 kB

Total Files

103

Last publish

Collaborators

  • houjun