# @eggjs/aop-plugin

> tegg aop plugin

Latest version **4.1.0** (published 2026-10-07) · 0 weekly downloads

## Install

```sh
npm install @eggjs/aop-plugin
pnpm add @eggjs/aop-plugin
yarn add @eggjs/aop-plugin
bun add @eggjs/aop-plugin
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2026-10-07 |
| First published | 2025-10-23 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22.18.0 |
| Dependencies | 7 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| Maintainers | gxkl, eggjs-admin, fengmk2, atian25, hyj1991, killagu, coolme200, akitasummer, elrrrrrrr, niania |
| Keywords | aop, decorator, egg, tegg, typescript |

## Links

- npm: https://www.npmjs.com/package/@eggjs/aop-plugin
- Repository: https://github.com/eggjs/egg
- Homepage: https://github.com/eggjs/egg/tree/next/tegg/plugin/aop
- Issues: https://github.com/eggjs/egg/issues
- npm.io page: https://npm.io/package/@eggjs/aop-plugin

## Dependencies (7)

- [@eggjs/metadata](https://npm.io/package/@eggjs/metadata.md) 4.1.0
- [@eggjs/lifecycle](https://npm.io/package/@eggjs/lifecycle.md) 4.1.0
- [@eggjs/aop-runtime](https://npm.io/package/@eggjs/aop-runtime.md) 4.1.0
- [@eggjs/tegg-runtime](https://npm.io/package/@eggjs/tegg-runtime.md) 4.1.0
- [@eggjs/aop-decorator](https://npm.io/package/@eggjs/aop-decorator.md) 4.1.0
- [@eggjs/module-common](https://npm.io/package/@eggjs/module-common.md) 4.1.0
- [@eggjs/core-decorator](https://npm.io/package/@eggjs/core-decorator.md) 4.1.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2026-10-07
- 4.0.2-rc.1 (rc) — 2026-10-06
- 4.0.2-beta.27 (beta) — 2026-09-20
- 4.0.2 — 2026-10-06
- 4.0.2-rc.0 — 2026-09-23
- 4.0.2-beta.26 — 2026-08-07
- 4.0.2-beta.25 — 2026-07-13
- 4.0.2-beta.24 — 2026-07-12
- 4.0.2-beta.23 — 2026-07-11
- 4.0.2-beta.22 — 2026-07-11
- 4.0.2-beta.20 — 2026-07-09
- 4.0.2-beta.19 — 2026-06-29
- 4.0.2-beta.17 — 2026-06-28
- 4.0.2-beta.16 — 2026-06-26
- 4.0.2-beta.15 — 2026-06-26
- … 24 more at https://npm.io/package/@eggjs/aop-plugin/versions

## README

# @eggjs/aop-plugin

[![NPM version][npm-image]][npm-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/@eggjs/aop-plugin.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/@eggjs/aop-plugin.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/aop-plugin
[snyk-image]: https://snyk.io/test/npm/@eggjs/aop-plugin/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/aop-plugin
[download-image]: https://img.shields.io/npm/dm/@eggjs/aop-plugin.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/aop-plugin

## Usage

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

## Advice

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

**注意：Advice 也是一种 Prototype，可以通过 initType 来指定不同的生命周期，默认为 Singleton。**

如需在调用过程中保存状态，可以将状态通过 `AdviceContext` 的 get/set 来保存。

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

const FOO_STATE_SYMBOL = Symbol('AdviceExample#state');

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

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

  // 在函数成功后执行
  async afterReturn(ctx: AdviceContext, result: any): Promise<void> {
    // 将会打印 23333
    console.log(ctx.get(FOO_STATE_SYMBOL));
  }

  // 在函数成功后执行
  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`

```ts
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 指定到。**

```ts
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 {}
```

---
_Source: https://npm.io/package/@eggjs/aop-plugin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
