@eggjs/tegg-aop-plugin 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [0.2.0](https://github.com/eggjs/tegg/compare/v0.1.19...v0.2.0) (2022-01-20)
7
+
8
+
9
+ ### Features
10
+
11
+ * impl aop ([c53df00](https://github.com/eggjs/tegg/commit/c53df001d1455a0a105689694775d880541d9d2f))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Alibaba Group Holding Limited and other contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # @eggjs/tegg-aop-plugin
2
+
3
+ ## Usage
4
+
5
+ ```js
6
+ // plugin.js
7
+ export.aopModule = {
8
+ enable: true,
9
+ package: '@eggjs/tegg-aop-plugin',
10
+ };
11
+ ```
12
+
13
+ ## Advice
14
+
15
+ 使用 `@Advice` 注解来申明一个实现,可以用来监听、拦截方法执行。
16
+
17
+ **注意:Advice 也是一种 Prototype,可以通过 initType 来指定不同的生命周期。**
18
+
19
+ ```ts
20
+ import { Advice, IAdvice } from '@eggjs/tegg/aop';
21
+
22
+ @Advice()
23
+ export class AdviceExample implements IAdvice {
24
+ // Advice 中可以正常的注入其他的对象
25
+ @Inject()
26
+ private readonly callTrace: CallTrace;
27
+
28
+ // 在函数执行前执行
29
+ async beforeCall(ctx: AdviceContext): Promise<void> {
30
+ // ...
31
+ }
32
+
33
+ // 在函数成功后执行
34
+ async afterReturn(ctx: AdviceContext, result: any): Promise<void> {
35
+ // ...
36
+ }
37
+
38
+ // 在函数成功后执行
39
+ async afterThrow(ctx: AdviceContext, error: Error): Promise<void> {
40
+ // ...
41
+ }
42
+
43
+ // 在函数退出时执行
44
+ async afterFinally(ctx: AdviceContext): Promise<void> {
45
+ }
46
+
47
+ // 类似 koa 中间件的模式
48
+ // block = next
49
+ async around(ctx: AdviceContext, next: () => Promise<any>): Promise<any> {
50
+ }
51
+ }
52
+ ```
53
+
54
+ ## Pointcut
55
+
56
+ 使用 `@Pointcut` 在某个类特定的方法上申明一个 `Advice`
57
+
58
+ ```ts
59
+ import { Pointcut } from '@eggjs/tegg/aop';
60
+ import { ContextProto } from '@eggjs/tegg';
61
+
62
+ @ContextProto()
63
+ export class Hello {
64
+ @Pointcut(AdviceExample)
65
+ async hello(name: string) {
66
+ return `hello ${name}`;
67
+ }
68
+ }
69
+ ```
70
+
71
+ ## Crosscut
72
+
73
+ 使用 `@Crosscut` 来声明一个通用的 `Advice`,有三种模式
74
+ - 指定类和方法
75
+ - 通过正则指定类和方法
76
+ - 通过回调来指定类和方法
77
+
78
+ **注意:egg 中的对象无法被 Crosscut 指定到。**
79
+
80
+ ```ts
81
+ import { Crosscut, Advice, IAdvice } from '@eggjs/tegg/aop';
82
+
83
+ // 通过类型来指定
84
+ @Crosscut({
85
+ type: PointcutType.CLASS,
86
+ clazz: CrosscutExample,
87
+ methodName: 'hello',
88
+ })
89
+ @Advice()
90
+ export class CrosscutClassAdviceExample implements IAdvice {
91
+ }
92
+
93
+ // 通过正则来指定
94
+ @Crosscut({
95
+ type: PointcutType.NAME,
96
+ className: /crosscut.*/i,
97
+ methodName: /hello/,
98
+ })
99
+ @Advice()
100
+ export class CrosscutNameAdviceExample implements IAdvice {
101
+ }
102
+
103
+ // 通过回调来指定
104
+ @Crosscut({
105
+ type: PointcutType.CUSTOM,
106
+ callback: (clazz: EggProtoImplClass, method: PropertyKey) => {
107
+ return clazz === CrosscutExample && method === 'hello';
108
+ }
109
+ })
110
+ @Advice()
111
+ ****export class CrosscutCustomAdviceExample implements IAdvice {
112
+ }
113
+
114
+ ```
package/app.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export default class AopAppHook {
2
+ private readonly app;
3
+ private readonly crosscutAdviceFactory;
4
+ private readonly loadUnitAopHook;
5
+ private readonly eggPrototypeCrossCutHook;
6
+ private readonly eggObjectAopHook;
7
+ constructor(app: any);
8
+ configDidLoad(): void;
9
+ beforeClose(): void;
10
+ }
package/app.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const aop_1 = require("@eggjs/tegg/aop");
4
+ const tegg_aop_runtime_1 = require("@eggjs/tegg-aop-runtime");
5
+ class AopAppHook {
6
+ constructor(app) {
7
+ this.app = app;
8
+ this.crosscutAdviceFactory = new aop_1.CrosscutAdviceFactory();
9
+ this.loadUnitAopHook = new tegg_aop_runtime_1.LoadUnitAopHook(this.crosscutAdviceFactory);
10
+ this.eggPrototypeCrossCutHook = new tegg_aop_runtime_1.EggPrototypeCrossCutHook(this.crosscutAdviceFactory);
11
+ this.eggObjectAopHook = new tegg_aop_runtime_1.EggObjectAopHook();
12
+ }
13
+ configDidLoad() {
14
+ this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.eggPrototypeCrossCutHook);
15
+ this.app.loadUnitLifecycleUtil.registerLifecycle(this.loadUnitAopHook);
16
+ this.app.eggObjectLifecycleUtil.registerLifecycle(this.eggObjectAopHook);
17
+ }
18
+ beforeClose() {
19
+ this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.eggPrototypeCrossCutHook);
20
+ this.app.loadUnitLifecycleUtil.deleteLifecycle(this.loadUnitAopHook);
21
+ this.app.eggObjectLifecycleUtil.deleteLifecycle(this.eggObjectAopHook);
22
+ }
23
+ }
24
+ exports.default = AopAppHook;
25
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiYXBwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQ0EseUNBQXdEO0FBQ3hELDhEQUFzRztBQUV0RyxNQUFxQixVQUFVO0lBUTdCLFlBQVksR0FBRztRQUNiLElBQUksQ0FBQyxHQUFHLEdBQUcsR0FBRyxDQUFDO1FBQ2YsSUFBSSxDQUFDLHFCQUFxQixHQUFHLElBQUksMkJBQXFCLEVBQUUsQ0FBQztRQUN6RCxJQUFJLENBQUMsZUFBZSxHQUFHLElBQUksa0NBQWUsQ0FBQyxJQUFJLENBQUMscUJBQXFCLENBQUMsQ0FBQztRQUN2RSxJQUFJLENBQUMsd0JBQXdCLEdBQUcsSUFBSSwyQ0FBd0IsQ0FBQyxJQUFJLENBQUMscUJBQXFCLENBQUMsQ0FBQztRQUN6RixJQUFJLENBQUMsZ0JBQWdCLEdBQUcsSUFBSSxtQ0FBZ0IsRUFBRSxDQUFDO0lBQ2pELENBQUM7SUFFRCxhQUFhO1FBQ1gsSUFBSSxDQUFDLEdBQUcsQ0FBQyx5QkFBeUIsQ0FBQyxpQkFBaUIsQ0FBQyxJQUFJLENBQUMsd0JBQXdCLENBQUMsQ0FBQztRQUNwRixJQUFJLENBQUMsR0FBRyxDQUFDLHFCQUFxQixDQUFDLGlCQUFpQixDQUFDLElBQUksQ0FBQyxlQUFlLENBQUMsQ0FBQztRQUN2RSxJQUFJLENBQUMsR0FBRyxDQUFDLHNCQUFzQixDQUFDLGlCQUFpQixDQUFDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDO0lBQzNFLENBQUM7SUFFRCxXQUFXO1FBQ1QsSUFBSSxDQUFDLEdBQUcsQ0FBQyx5QkFBeUIsQ0FBQyxlQUFlLENBQUMsSUFBSSxDQUFDLHdCQUF3QixDQUFDLENBQUM7UUFDbEYsSUFBSSxDQUFDLEdBQUcsQ0FBQyxxQkFBcUIsQ0FBQyxlQUFlLENBQUMsSUFBSSxDQUFDLGVBQWUsQ0FBQyxDQUFDO1FBQ3JFLElBQUksQ0FBQyxHQUFHLENBQUMsc0JBQXNCLENBQUMsZUFBZSxDQUFDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDO0lBQ3pFLENBQUM7Q0FDRjtBQTNCRCw2QkEyQkMifQ==
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@eggjs/tegg-aop-plugin",
3
+ "version": "0.2.0",
4
+ "eggPlugin": {
5
+ "name": "aopModule",
6
+ "dependencies": [
7
+ "tegg"
8
+ ]
9
+ },
10
+ "types": "typings/index.d.ts",
11
+ "description": "tegg aop plugin",
12
+ "keywords": [
13
+ "egg",
14
+ "typescript",
15
+ "decorator",
16
+ "aop",
17
+ "tegg"
18
+ ],
19
+ "files": [
20
+ "app.js",
21
+ "app.d.ts"
22
+ ],
23
+ "scripts": {
24
+ "clean": "tsc -b --clean",
25
+ "tsc": "npm run clean && tsc -p ./tsconfig.json",
26
+ "tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json",
27
+ "prepublishOnly": "npm run tsc:pub",
28
+ "autod": "autod"
29
+ },
30
+ "homepage": "https://github.com/eggjs/tegg",
31
+ "bugs": {
32
+ "url": "https://github.com/eggjs/tegg/issues"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git@github.com:eggjs/tegg.git",
37
+ "directory": "plugin/aop"
38
+ },
39
+ "engines": {
40
+ "node": ">=14.0.0"
41
+ },
42
+ "dependencies": {
43
+ "@eggjs/tegg": "^0.2.0",
44
+ "@eggjs/tegg-aop-runtime": "^0.2.0"
45
+ },
46
+ "devDependencies": {
47
+ "@eggjs/tegg-config": "^0.2.0",
48
+ "@eggjs/tegg-plugin": "^0.2.0",
49
+ "egg": "^2.29.4",
50
+ "egg-mock": "^4.0.1"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "gitHead": "320bffc1f6074d7785a020815e0cdc3c27cc654b"
56
+ }