{"name":"@eggjs/aop-plugin","version":"4.0.2-beta.12","keywords":["aop","decorator","egg","tegg","typescript"],"_id":"@eggjs/aop-plugin@4.0.2-beta.12","maintainers":[{"name":"gxkl","email":"gxkl203@gmail.com"},{"name":"eggjs-admin","email":"fengmk2+eggjs@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"atian25","email":"atian25@qq.com"},{"name":"hyj1991","email":"yeekwanvong@gmail.com"},{"name":"killagu","email":"killa07071201@gmail.com"},{"name":"coolme200","email":"2001-wms@163.com"},{"name":"akitasummer","email":"644171127@qq.com"},{"name":"niania","email":"ljracm@gmail.com"}],"homepage":"https://github.com/eggjs/egg/tree/next/tegg/plugin/aop","bugs":{"url":"https://github.com/eggjs/egg/issues"},"dist":{"shasum":"1f296abcd61e9e2796469d914c560eb0045841d2","tarball":"http://registry-npm.ywwl.com/@eggjs/aop-plugin/download/@eggjs/aop-plugin-4.0.2-beta.12.tgz","fileCount":11,"integrity":"sha512-tdAdJrKHa/a/APnYBaDQyij/IUonRVsIMPC0BxV4Blg6yU+uULSE2kAH5iaAzB+X0u7THee8UxNUda5jbP5ILw==","signatures":[{"sig":"MEQCIG3ynNAFus/xfbKP9VsiAbBVxF7GMfj624yqo6NTxcmPAiBcOo+AUblGsJAXdzsmLv3qwU/yhqaqYiYobYRc5qCiLA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@eggjs%2faop-plugin@4.0.2-beta.12","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":11286,"size":4439,"noattachment":false,"key":"/@eggjs/aop-plugin/-/@eggjs/aop-plugin-4.0.2-beta.12.tgz"},"main":"./dist/index.js","type":"module","_from":"file:eggjs-aop-plugin-4.0.2-beta.12.tgz","types":"./dist/index.d.ts","module":"./dist/index.js","engines":{"node":">=22.18.0"},"exports":{".":"./dist/index.js","./app":"./dist/app.js","./types":"./dist/types.js","./package.json":"./package.json","./lib/AopContextHook":"./dist/lib/AopContextHook.js"},"scripts":{"typecheck":"tsgo --noEmit"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:e4d1ead1-e23e-456b-a32e-6c99d4992c40"}},"_resolved":"/tmp/c77650cd6b2f2cf18e2b1ea995b4b9c9/eggjs-aop-plugin-4.0.2-beta.12.tgz","eggPlugin":{"name":"teggAop","dependencies":["tegg"]},"_integrity":"sha512-tdAdJrKHa/a/APnYBaDQyij/IUonRVsIMPC0BxV4Blg6yU+uULSE2kAH5iaAzB+X0u7THee8UxNUda5jbP5ILw==","repository":{"url":"git+https://github.com/eggjs/egg.git","type":"git","directory":"tegg/plugin/aop"},"_npmVersion":"11.13.0","description":"tegg aop plugin","directories":{},"_nodeVersion":"24.16.0","dependencies":{"@eggjs/metadata":"4.0.2-beta.12","@eggjs/lifecycle":"4.0.2-beta.12","@eggjs/aop-runtime":"4.0.2-beta.12","@eggjs/tegg-runtime":"4.0.2-beta.12","@eggjs/aop-decorator":"4.0.2-beta.12","@eggjs/module-common":"4.0.2-beta.12","@eggjs/core-decorator":"4.0.2-beta.12"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"egg":"4.1.2-beta.12","typescript":"^5.9.3","@eggjs/mock":"7.0.2-beta.12","@eggjs/tegg":"4.0.2-beta.12","@types/node":"^24.10.2","@eggjs/tegg-config":"4.0.2-beta.12","@eggjs/tegg-plugin":"4.0.2-beta.12"},"peerDependencies":{"egg":"4.1.2-beta.12","@eggjs/tegg-plugin":"4.0.2-beta.12"},"_npmOperationalInternal":{"tmp":"tmp/aop-plugin_4.0.2-beta.12_1782223954365_0.9364702573190169","host":"s3://npm-registry-packages-npm-production"},"readme":"# @eggjs/aop-plugin\n\n[![NPM version][npm-image]][npm-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/@eggjs/aop-plugin.svg?style=flat)](https://nodejs.org/en/download/)\n\n[npm-image]: https://img.shields.io/npm/v/@eggjs/aop-plugin.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@eggjs/aop-plugin\n[snyk-image]: https://snyk.io/test/npm/@eggjs/aop-plugin/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/@eggjs/aop-plugin\n[download-image]: https://img.shields.io/npm/dm/@eggjs/aop-plugin.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@eggjs/aop-plugin\n\n## Usage\n\n```js\n// plugin.js\nexport.teggAop = {\n  enable: true,\n  package: '@eggjs/aop-plugin',\n};\n```\n\n## Advice\n\n使用 `@Advice` 注解来申明一个实现，可以用来监听、拦截方法执行。\n\n**注意：Advice 也是一种 Prototype，可以通过 initType 来指定不同的生命周期，默认为 Singleton。**\n\n如需在调用过程中保存状态，可以将状态通过 `AdviceContext` 的 get/set 来保存。\n\n```ts\nimport { Advice, IAdvice } from '@eggjs/tegg/aop';\n\nconst FOO_STATE_SYMBOL = Symbol('AdviceExample#state');\n\n@Advice()\nexport class AdviceExample implements IAdvice {\n  // Advice 中可以正常的注入其他的对象\n  @Inject()\n  private readonly callTrace: CallTrace;\n\n  // 在函数执行前执行\n  async beforeCall(ctx: AdviceContext): Promise<void> {\n    // ...\n    ctx.set(FOO_STATE_SYMBOL, 23333);\n  }\n\n  // 在函数成功后执行\n  async afterReturn(ctx: AdviceContext, result: any): Promise<void> {\n    // 将会打印 23333\n    console.log(ctx.get(FOO_STATE_SYMBOL));\n  }\n\n  // 在函数成功后执行\n  async afterThrow(ctx: AdviceContext, error: Error): Promise<void> {\n    // ...\n  }\n\n  // 在函数退出时执行\n  async afterFinally(ctx: AdviceContext): Promise<void> {}\n\n  // 类似 koa 中间件的模式\n  // block = next\n  async around(ctx: AdviceContext, next: () => Promise<any>): Promise<any> {}\n}\n```\n\n## Pointcut\n\n使用 `@Pointcut` 在某个类特定的方法上申明一个 `Advice`\n\n```ts\nimport { Pointcut } from '@eggjs/tegg/aop';\nimport { ContextProto } from '@eggjs/tegg';\n\n@ContextProto()\nexport class Hello {\n  // 创建 Hello.hello 的切面 AdviceExample，并传递 adviceParams 给 AdviceExample\n  // AdviceExample 的切面函数可以通过 ctx.adviceParams 拿到注解传入的参数\n  @Pointcut(AdviceExample, { adviceParams: { foo: 'bar' } })\n  async hello(name: string) {\n    return `hello ${name}`;\n  }\n}\n```\n\n## Crosscut\n\n使用 `@Crosscut` 来声明一个通用的 `Advice`，有三种模式\n\n- 指定类和方法\n- 通过正则指定类和方法\n- 通过回调来指定类和方法\n\n**注意：egg 中的对象无法被 Crosscut 指定到。**\n\n```ts\nimport { Crosscut, Advice, IAdvice } from '@eggjs/tegg/aop';\n\n// 通过类型来指定\n// 创建 CrosscutClassAdviceExample.hello 的切面 CrosscutExample，并传递 adviceParams 给 CrosscutExample\n// CrosscutExample 的切面函数可以通过 ctx.adviceParams 拿到注解传入的参数\n@Crosscut(\n  {\n    type: PointcutType.CLASS,\n    clazz: CrosscutExample,\n    methodName: 'hello',\n  },\n  { adviceParams: { foo: 'bar' } },\n)\n@Advice()\nexport class CrosscutClassAdviceExample implements IAdvice {}\n\n// 通过正则来指定\n@Crosscut({\n  type: PointcutType.NAME,\n  className: /crosscut.*/i,\n  methodName: /hello/,\n})\n@Advice()\nexport class CrosscutNameAdviceExample implements IAdvice {}\n\n// 通过回调来指定\n@Crosscut({\n  type: PointcutType.CUSTOM,\n  callback: (clazz: EggProtoImplClass, method: PropertyKey) => {\n    return clazz === CrosscutExample && method === 'hello';\n  },\n})\n@Advice()\nexport class CrosscutCustomAdviceExample implements IAdvice {}\n```\n","publish_time":1782223954494,"_cnpm_publish_time":1782223954494,"dist-tags":{"beta":"4.0.2-beta.27","latest":"4.1.0","rc":"4.0.2-rc.1"}}