npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eggjs/tegg-aop-plugin

v3.51.2

Published

tegg aop plugin

Downloads

919

Readme

@eggjs/tegg-aop-plugin

Usage

// plugin.js
export.aopModule = {
  enable: true,
  package: '@eggjs/tegg-aop-plugin',
};

Advice

使用 @Advice 注解来申明一个实现,可以用来监听、拦截方法执行。

注意:Advice 也是一种 Prototype,可以通过 initType 来指定不同的生命周期。

import { Advice, IAdvice } from '@eggjs/tegg/aop';

@Advice()
export class AdviceExample implements IAdvice {
  // Advice 中可以正常的注入其他的对象
  @Inject()
  private readonly callTrace: CallTrace;

  // 在函数执行前执行
  async beforeCall(ctx: AdviceContext): Promise<void> {
    // ...
  }

  // 在函数成功后执行
  async afterReturn(ctx: AdviceContext, result: any): Promise<void> {
    // ...
  }

  // 在函数成功后执行
  async afterThrow(ctx: AdviceContext, error: Error): Promise<void> {
    // ...
  }

  // 在函数退出时执行
  async afterFinally(ctx: AdviceContext): Promise<void> {
  }

  // 类似 koa 中间件的模式
  // block = next
  async around(ctx: AdviceContext, next: () => Promise<any>): Promise<any> {
  }
}

Pointcut

使用 @Pointcut 在某个类特定的方法上申明一个 Advice

import { Pointcut } from '@eggjs/tegg/aop';
import { ContextProto } from '@eggjs/tegg';

@ContextProto()
export class Hello {

  // 创建 Hello.hello 的切面 AdviceExample,并传递 adviceParams 给 AdviceExample
  // AdviceExample 的切面函数可以通过 ctx.adviceParams 拿到注解传入的参数
  @Pointcut(AdviceExample, { adviceParams: { foo: 'bar' } })
  async hello(name: string) {
    return `hello ${name}`;
  }
}

Crosscut

使用 @Crosscut 来声明一个通用的 Advice,有三种模式

  • 指定类和方法
  • 通过正则指定类和方法
  • 通过回调来指定类和方法

注意:egg 中的对象无法被 Crosscut 指定到。

import { Crosscut, Advice, IAdvice } from '@eggjs/tegg/aop';

// 通过类型来指定
// 创建 CrosscutClassAdviceExample.hello 的切面 CrosscutExample,并传递 adviceParams 给 CrosscutExample
// CrosscutExample 的切面函数可以通过 ctx.adviceParams 拿到注解传入的参数
@Crosscut({
  type: PointcutType.CLASS,
  clazz: CrosscutExample,
  methodName: 'hello',
}, { adviceParams: { foo: 'bar' } })
@Advice()
export class CrosscutClassAdviceExample implements IAdvice {
}

// 通过正则来指定
@Crosscut({
  type: PointcutType.NAME,
  className: /crosscut.*/i,
  methodName: /hello/,
})
@Advice()
export class CrosscutNameAdviceExample implements IAdvice {
}

// 通过回调来指定
@Crosscut({
  type: PointcutType.CUSTOM,
  callback:  (clazz: EggProtoImplClass, method: PropertyKey) => {
    return clazz === CrosscutExample && method === 'hello';
  }
})
@Advice()
****export class CrosscutCustomAdviceExample implements IAdvice {
}