Skip to content

Commit 334e13d

Browse files
authored
refactor: add types package (#205)
<!-- Thank you for your pull request. Please review below requirements. Bug fixes and new features should include tests and possibly benchmarks. Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md 感谢您贡献代码。请确认下列 checklist 的完成情况。 Bug 修复和新功能必须包含测试,必要时请附上性能测试。 Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md --> ##### Checklist <!-- Remove items that do not apply. For completed items, change [ ] to [x]. --> - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) <!-- Provide affected core subsystem(s). --> ##### Description of change <!-- Provide a description of the change below this comment. --> <!-- - any feature? - close https://github.com/eggjs/egg/ISSUE_URL -->
1 parent 5612b8f commit 334e13d

File tree

303 files changed

+1808
-1500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

303 files changed

+1808
-1500
lines changed

core/aop-decorator/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from '@eggjs/tegg-types/aop';
12
export * from './src/decorator/Advice';
23
export * from './src/decorator/Pointcut';
34
export * from './src/decorator/Crosscut';

core/aop-decorator/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"dependencies": {
1919
"@eggjs/core-decorator": "^3.37.1",
2020
"@eggjs/tegg-common-util": "^3.37.1",
21-
"@eggjs/tegg-metadata": "^3.37.1"
21+
"@eggjs/tegg-metadata": "^3.37.1",
22+
"@eggjs/tegg-types": "^3.37.1"
2223
},
2324
"scripts": {
2425
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",

core/aop-decorator/src/AspectMetaBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { EggProtoImplClass } from '@eggjs/tegg-types';
12
import { CrosscutAdviceFactory } from './CrosscutAdviceFactory';
2-
import { EggProtoImplClass } from '@eggjs/core-decorator';
33
import { Aspect, AspectBuilder } from './model/Aspect';
44
import { PointcutAdviceInfoUtil } from './util/PointcutAdviceInfoUtil';
55

core/aop-decorator/src/CrosscutAdviceFactory.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import assert from 'assert';
2-
import { EggProtoImplClass } from '@eggjs/core-decorator';
3-
import { IAdvice } from './decorator/Advice';
1+
import assert from 'node:assert';
2+
import type { EggProtoImplClass, IAdvice, AdviceInfo } from '@eggjs/tegg-types';
43
import { CrosscutInfoUtil } from './util/CrosscutInfoUtil';
5-
import { AdviceInfo } from './model/Aspect';
64

75
export class CrosscutAdviceFactory {
86
private readonly crosscutAdviceClazzList: Array<EggProtoImplClass<IAdvice>> = [];

core/aop-decorator/src/decorator/Advice.ts

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,8 @@
1-
import {
2-
AccessLevel,
3-
EggProtoImplClass,
4-
ObjectInitType,
5-
Prototype,
6-
PrototypeParams, PrototypeUtil,
7-
} from '@eggjs/core-decorator';
8-
import { AdviceInfoUtil } from '../util/AdviceInfoUtil';
1+
import { Prototype, PrototypeUtil } from '@eggjs/core-decorator';
92
import { StackUtil } from '@eggjs/tegg-common-util';
10-
11-
export interface AdviceContext<T = object, K = any> {
12-
that: T;
13-
method: PropertyKey;
14-
args: any[];
15-
adviceParams?: K;
16-
}
17-
18-
/**
19-
* execute order:
20-
* 1. beforeCall
21-
* 1. around
22-
* 1. afterReturn/afterThrow
23-
* 1. afterFinally
24-
*/
25-
export interface IAdvice<T = object, K = any> {
26-
/**
27-
* call before function
28-
* @param ctx
29-
*/
30-
beforeCall?(ctx: AdviceContext<T, K>): Promise<void>;
31-
32-
/**
33-
* call after function succeed
34-
*/
35-
afterReturn?(ctx: AdviceContext<T, K>, result: any): Promise<void>;
36-
37-
/**
38-
* call after function throw error
39-
*/
40-
afterThrow?(ctx: AdviceContext<T, K>, error: Error): Promise<void>;
41-
42-
/**
43-
* always call after function done
44-
*/
45-
afterFinally?(ctx: AdviceContext<T, K>): Promise<void>;
46-
47-
/**
48-
* execute the function
49-
* the only one can modify the function return value
50-
*/
51-
around?(ctx: AdviceContext<T, K>, next: () => Promise<any>): Promise<any>;
52-
}
3+
import { AccessLevel, ObjectInitType } from '@eggjs/tegg-types';
4+
import type { EggProtoImplClass, IAdvice, PrototypeParams } from '@eggjs/tegg-types';
5+
import { AdviceInfoUtil } from '../util/AdviceInfoUtil';
536

547
const defaultAdviceParam = {
558
accessLevel: AccessLevel.PUBLIC,

core/aop-decorator/src/decorator/Crosscut.ts

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
1-
import { EggProtoImplClass } from '@eggjs/core-decorator';
2-
import { IAdvice } from './Advice';
3-
import { CrosscutInfo, CrosscutInfoUtil } from '../util/CrosscutInfoUtil';
4-
import {
5-
ClassPointInfo,
6-
CustomPointcutCallback,
7-
CustomPointInfo,
8-
NamePointInfo,
9-
PointcutType,
10-
} from '../model/PointcutInfo';
11-
12-
export interface CrosscutOptions {
13-
// 默认值 100
14-
order?: number;
15-
adviceParams?: any;
16-
}
1+
import { PointcutType } from '@eggjs/tegg-types';
2+
import type { CrosscutInfo, EggProtoImplClass, IAdvice, CrosscutParam, CrosscutOptions } from '@eggjs/tegg-types';
3+
import { CrosscutInfoUtil } from '../util/CrosscutInfoUtil';
4+
import { ClassPointInfo, CustomPointInfo, NamePointInfo } from '../model/PointcutInfo';
175

186
const defaultCrossOptions = {
197
order: 100,
208
};
219

22-
// TODO type check for methodName
23-
export interface ClassCrosscutParam {
24-
type: PointcutType.CLASS;
25-
clazz: EggProtoImplClass;
26-
methodName: PropertyKey;
27-
}
28-
29-
export interface NameCrosscutParam {
30-
type: PointcutType.NAME;
31-
className: RegExp;
32-
methodName: RegExp;
33-
}
34-
35-
36-
export interface CustomCrosscutParam {
37-
type: PointcutType.CUSTOM;
38-
callback: CustomPointcutCallback;
39-
}
40-
41-
export type CrosscutParam = ClassCrosscutParam | NameCrosscutParam | CustomCrosscutParam;
42-
4310
export function Crosscut(param: CrosscutParam, options?: CrosscutOptions) {
4411
return function(constructor: EggProtoImplClass<IAdvice>) {
4512
let crosscutInfo: CrosscutInfo;

core/aop-decorator/src/decorator/Pointcut.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import { IAdvice } from './Advice';
2-
import { EggProtoImplClass } from '@eggjs/core-decorator';
1+
import type { EggProtoImplClass, IAdvice, PointcutOptions } from '@eggjs/tegg-types';
32
import { PointcutAdviceInfoUtil } from '../util/PointcutAdviceInfoUtil';
43
import assert from 'assert';
54
import { AdviceInfoUtil } from '../util/AdviceInfoUtil';
65

7-
export interface PointcutOptions<K = any> {
8-
// default is 1000
9-
order?: number;
10-
adviceParams?: K;
11-
}
12-
136
const defaultPointcutOptions = {
147
order: 1000,
158
};

core/aop-decorator/src/model/Aspect.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
import { EggProtoImplClass } from '@eggjs/core-decorator';
2-
import { IAdvice } from '../decorator/Advice';
3-
4-
export interface AdviceInfo {
5-
clazz: EggProtoImplClass<IAdvice>;
6-
order: number;
7-
adviceParams: any;
8-
}
9-
10-
export interface AspectAdvice {
11-
name: string;
12-
clazz: EggProtoImplClass<IAdvice>;
13-
adviceParams: any;
14-
}
1+
import type { AdviceInfo, AspectAdvice, EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';
152

163
export class Aspect {
174
readonly clazz: EggProtoImplClass;

core/aop-decorator/src/model/PointcutInfo.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
1-
import { EggProtoImplClass } from '@eggjs/core-decorator';
2-
3-
export enum PointcutType {
4-
/**
5-
* use class type to match
6-
*/
7-
CLASS = 'CLASS',
8-
/**
9-
* use regexp to match className and methodName
10-
*/
11-
NAME = 'NAME',
12-
/**
13-
* use custom function to match
14-
*/
15-
CUSTOM = 'CUSTOM',
16-
}
17-
18-
export interface PointcutInfo {
19-
type: PointcutType;
20-
21-
match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
22-
}
1+
import { PointcutType } from '@eggjs/tegg-types';
2+
import type { CustomPointcutCallback, EggProtoImplClass, PointcutInfo } from '@eggjs/tegg-types';
233

244
export class ClassPointInfo implements PointcutInfo {
255
readonly type = PointcutType.CLASS;
@@ -56,8 +36,6 @@ export class NamePointInfo implements PointcutInfo {
5636
}
5737
}
5838

59-
export type CustomPointcutCallback = (clazz: EggProtoImplClass, method: PropertyKey) => boolean;
60-
6139
export class CustomPointInfo implements PointcutInfo {
6240
readonly type = PointcutType.CUSTOM;
6341
readonly cb: CustomPointcutCallback;

core/aop-decorator/src/util/AdviceInfoUtil.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
2-
import { IAdvice } from '../decorator/Advice';
1+
import { MetadataUtil } from '@eggjs/core-decorator';
2+
import type { EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';
33

44
export const IS_ADVICE = Symbol.for('EggPrototype#isAdvice');
55

0 commit comments

Comments
 (0)