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

4.0.5 • Public • Published

packaged @ts-ioc/aop

This repo is for distribution on npm. The source for this module is in the main repo.

@ts-ioc/aop is AOP base on Ioc container, via @ts-ioc/core, typescript decorator.

Install

npm install @ts-ioc/aop
import { AopModule } from '@ts-ioc/aop';
// in server
import { ContainerBuilder } from '@ts-ioc/platform-server'
// in browser
import { ContainerBuilder } from '@ts-ioc/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 '@ts-ioc/core';

import { Joinpoint, Around, Aspect , Pointcut } from '@ts-ioc/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);
    }
}

Container Interface

see more interface. all document is typescript .d.ts.

Documentation is available on the @ts-ioc/aop docs site.

License

MIT © Houjun

Package Sidebar

Install

npm i @ts-ioc/aop

Weekly Downloads

76

Version

4.0.5

License

MIT

Unpacked Size

1.25 MB

Total Files

285

Last publish

Collaborators

  • houjun