Skip to content

Commit

Permalink
refactor: add types package (#205)
Browse files Browse the repository at this point in the history
<!--
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
-->
  • Loading branch information
gxkl committed Apr 17, 2024
1 parent 5612b8f commit 334e13d
Show file tree
Hide file tree
Showing 303 changed files with 1,808 additions and 1,500 deletions.
1 change: 1 addition & 0 deletions core/aop-decorator/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '@eggjs/tegg-types/aop';
export * from './src/decorator/Advice';
export * from './src/decorator/Pointcut';
export * from './src/decorator/Crosscut';
Expand Down
3 changes: 2 additions & 1 deletion core/aop-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"dependencies": {
"@eggjs/core-decorator": "^3.37.1",
"@eggjs/tegg-common-util": "^3.37.1",
"@eggjs/tegg-metadata": "^3.37.1"
"@eggjs/tegg-metadata": "^3.37.1",
"@eggjs/tegg-types": "^3.37.1"
},
"scripts": {
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
Expand Down
2 changes: 1 addition & 1 deletion core/aop-decorator/src/AspectMetaBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { EggProtoImplClass } from '@eggjs/tegg-types';
import { CrosscutAdviceFactory } from './CrosscutAdviceFactory';
import { EggProtoImplClass } from '@eggjs/core-decorator';
import { Aspect, AspectBuilder } from './model/Aspect';
import { PointcutAdviceInfoUtil } from './util/PointcutAdviceInfoUtil';

Expand Down
6 changes: 2 additions & 4 deletions core/aop-decorator/src/CrosscutAdviceFactory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import assert from 'assert';
import { EggProtoImplClass } from '@eggjs/core-decorator';
import { IAdvice } from './decorator/Advice';
import assert from 'node:assert';
import type { EggProtoImplClass, IAdvice, AdviceInfo } from '@eggjs/tegg-types';
import { CrosscutInfoUtil } from './util/CrosscutInfoUtil';
import { AdviceInfo } from './model/Aspect';

export class CrosscutAdviceFactory {
private readonly crosscutAdviceClazzList: Array<EggProtoImplClass<IAdvice>> = [];
Expand Down
55 changes: 4 additions & 51 deletions core/aop-decorator/src/decorator/Advice.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,8 @@
import {
AccessLevel,
EggProtoImplClass,
ObjectInitType,
Prototype,
PrototypeParams, PrototypeUtil,
} from '@eggjs/core-decorator';
import { AdviceInfoUtil } from '../util/AdviceInfoUtil';
import { Prototype, PrototypeUtil } from '@eggjs/core-decorator';
import { StackUtil } from '@eggjs/tegg-common-util';

export interface AdviceContext<T = object, K = any> {
that: T;
method: PropertyKey;
args: any[];
adviceParams?: K;
}

/**
* execute order:
* 1. beforeCall
* 1. around
* 1. afterReturn/afterThrow
* 1. afterFinally
*/
export interface IAdvice<T = object, K = any> {
/**
* call before function
* @param ctx
*/
beforeCall?(ctx: AdviceContext<T, K>): Promise<void>;

/**
* call after function succeed
*/
afterReturn?(ctx: AdviceContext<T, K>, result: any): Promise<void>;

/**
* call after function throw error
*/
afterThrow?(ctx: AdviceContext<T, K>, error: Error): Promise<void>;

/**
* always call after function done
*/
afterFinally?(ctx: AdviceContext<T, K>): Promise<void>;

/**
* execute the function
* the only one can modify the function return value
*/
around?(ctx: AdviceContext<T, K>, next: () => Promise<any>): Promise<any>;
}
import { AccessLevel, ObjectInitType } from '@eggjs/tegg-types';
import type { EggProtoImplClass, IAdvice, PrototypeParams } from '@eggjs/tegg-types';
import { AdviceInfoUtil } from '../util/AdviceInfoUtil';

const defaultAdviceParam = {
accessLevel: AccessLevel.PUBLIC,
Expand Down
41 changes: 4 additions & 37 deletions core/aop-decorator/src/decorator/Crosscut.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import { EggProtoImplClass } from '@eggjs/core-decorator';
import { IAdvice } from './Advice';
import { CrosscutInfo, CrosscutInfoUtil } from '../util/CrosscutInfoUtil';
import {
ClassPointInfo,
CustomPointcutCallback,
CustomPointInfo,
NamePointInfo,
PointcutType,
} from '../model/PointcutInfo';

export interface CrosscutOptions {
// 默认值 100
order?: number;
adviceParams?: any;
}
import { PointcutType } from '@eggjs/tegg-types';
import type { CrosscutInfo, EggProtoImplClass, IAdvice, CrosscutParam, CrosscutOptions } from '@eggjs/tegg-types';
import { CrosscutInfoUtil } from '../util/CrosscutInfoUtil';
import { ClassPointInfo, CustomPointInfo, NamePointInfo } from '../model/PointcutInfo';

const defaultCrossOptions = {
order: 100,
};

// TODO type check for methodName
export interface ClassCrosscutParam {
type: PointcutType.CLASS;
clazz: EggProtoImplClass;
methodName: PropertyKey;
}

export interface NameCrosscutParam {
type: PointcutType.NAME;
className: RegExp;
methodName: RegExp;
}


export interface CustomCrosscutParam {
type: PointcutType.CUSTOM;
callback: CustomPointcutCallback;
}

export type CrosscutParam = ClassCrosscutParam | NameCrosscutParam | CustomCrosscutParam;

export function Crosscut(param: CrosscutParam, options?: CrosscutOptions) {
return function(constructor: EggProtoImplClass<IAdvice>) {
let crosscutInfo: CrosscutInfo;
Expand Down
9 changes: 1 addition & 8 deletions core/aop-decorator/src/decorator/Pointcut.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { IAdvice } from './Advice';
import { EggProtoImplClass } from '@eggjs/core-decorator';
import type { EggProtoImplClass, IAdvice, PointcutOptions } from '@eggjs/tegg-types';
import { PointcutAdviceInfoUtil } from '../util/PointcutAdviceInfoUtil';
import assert from 'assert';
import { AdviceInfoUtil } from '../util/AdviceInfoUtil';

export interface PointcutOptions<K = any> {
// default is 1000
order?: number;
adviceParams?: K;
}

const defaultPointcutOptions = {
order: 1000,
};
Expand Down
15 changes: 1 addition & 14 deletions core/aop-decorator/src/model/Aspect.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import { EggProtoImplClass } from '@eggjs/core-decorator';
import { IAdvice } from '../decorator/Advice';

export interface AdviceInfo {
clazz: EggProtoImplClass<IAdvice>;
order: number;
adviceParams: any;
}

export interface AspectAdvice {
name: string;
clazz: EggProtoImplClass<IAdvice>;
adviceParams: any;
}
import type { AdviceInfo, AspectAdvice, EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';

export class Aspect {
readonly clazz: EggProtoImplClass;
Expand Down
26 changes: 2 additions & 24 deletions core/aop-decorator/src/model/PointcutInfo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import { EggProtoImplClass } from '@eggjs/core-decorator';

export enum PointcutType {
/**
* use class type to match
*/
CLASS = 'CLASS',
/**
* use regexp to match className and methodName
*/
NAME = 'NAME',
/**
* use custom function to match
*/
CUSTOM = 'CUSTOM',
}

export interface PointcutInfo {
type: PointcutType;

match(clazz: EggProtoImplClass, method: PropertyKey): boolean;
}
import { PointcutType } from '@eggjs/tegg-types';
import type { CustomPointcutCallback, EggProtoImplClass, PointcutInfo } from '@eggjs/tegg-types';

export class ClassPointInfo implements PointcutInfo {
readonly type = PointcutType.CLASS;
Expand Down Expand Up @@ -56,8 +36,6 @@ export class NamePointInfo implements PointcutInfo {
}
}

export type CustomPointcutCallback = (clazz: EggProtoImplClass, method: PropertyKey) => boolean;

export class CustomPointInfo implements PointcutInfo {
readonly type = PointcutType.CUSTOM;
readonly cb: CustomPointcutCallback;
Expand Down
4 changes: 2 additions & 2 deletions core/aop-decorator/src/util/AdviceInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
import { IAdvice } from '../decorator/Advice';
import { MetadataUtil } from '@eggjs/core-decorator';
import type { EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';

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

Expand Down
7 changes: 3 additions & 4 deletions core/aop-decorator/src/util/AspectInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
import { IAdvice } from '../decorator/Advice';
import { MetadataUtil } from '@eggjs/core-decorator';
import { ASPECT_LIST } from '@eggjs/tegg-types';
import type { EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';
import { Aspect } from '../model/Aspect';

export const ASPECT_LIST = Symbol.for('EggPrototype#aspectList');

export class AspectInfoUtil {
static setAspectList(aspectList: Array<Aspect>, clazz: EggProtoImplClass<IAdvice>) {
MetadataUtil.defineMetaData(ASPECT_LIST, aspectList, clazz);
Expand Down
15 changes: 3 additions & 12 deletions core/aop-decorator/src/util/CrosscutInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { AdviceInfo } from '../model/Aspect';
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator';
import { IAdvice } from '../decorator/Advice';
import { PointcutInfo } from '../model/PointcutInfo';

export const CROSSCUT_INFO_LIST = Symbol.for('EggPrototype#crosscutInfoList');
export const IS_CROSSCUT_ADVICE = Symbol.for('EggPrototype#isCrosscutAdvice');

export interface CrosscutInfo {
pointcutInfo: PointcutInfo;
adviceInfo: AdviceInfo;
}
import { MetadataUtil } from '@eggjs/core-decorator';
import { IS_CROSSCUT_ADVICE, CROSSCUT_INFO_LIST } from '@eggjs/tegg-types';
import type { CrosscutInfo, EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';

export class CrosscutInfoUtil {
static setIsCrosscutAdvice(isCrosscutAdvice: boolean, clazz: EggProtoImplClass<IAdvice>) {
Expand Down
9 changes: 3 additions & 6 deletions core/aop-decorator/src/util/PointcutAdviceInfoUtil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
EggProtoImplClass, MetadataUtil,
} from '@eggjs/core-decorator';
import { AdviceInfo } from '../model/Aspect';

export const POINTCUT_ADVICE_INFO_LIAR = Symbol.for('EggPrototype#pointcutAdviceInfoList');
import { MetadataUtil } from '@eggjs/core-decorator';
import { POINTCUT_ADVICE_INFO_LIAR } from '@eggjs/tegg-types';
import type { AdviceInfo, EggProtoImplClass } from '@eggjs/tegg-types';

interface PointcutAdviceInfo {
method: PropertyKey;
Expand Down
7 changes: 4 additions & 3 deletions core/aop-decorator/test/AspectMetaBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import assert from 'node:assert';
import { PrototypeUtil } from '@eggjs/core-decorator';
import { CrosscutAdviceFactory } from '../src/CrosscutAdviceFactory';
import {
CrosscutClassAdviceExample,
CrosscutCustomAdviceExample, CrosscutExample,
CrosscutCustomAdviceExample,
CrosscutExample,
CrosscutNameAdviceExample,
} from './fixtures/CrosscutExample';
import {
Expand All @@ -11,7 +14,6 @@ import {
PointcutExample,
} from './fixtures/PointcutExample';
import { AspectMetaBuilder } from '../src/AspectMetaBuilder';
import assert from 'assert';
import {
ChildExample,
CrosscutNoOverwriteParentExample,
Expand All @@ -21,7 +23,6 @@ import {
PointcutAdviceOverwriteChildExample,
PointcutAdviceOverwriteParentExample,
} from './fixtures/InheritExample';
import { PrototypeUtil } from '@eggjs/core-decorator';

describe('test/AspectMetaBuild.test.ts', () => {
const crosscutAdviceFactory = new CrosscutAdviceFactory();
Expand Down
6 changes: 4 additions & 2 deletions core/aop-decorator/test/fixtures/CrosscutExample.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ContextProto, EggProtoImplClass } from '@eggjs/core-decorator';
import { Advice, Crosscut, IAdvice, PointcutType } from '../..';
import { ContextProto } from '@eggjs/core-decorator';
import { PointcutType } from '@eggjs/tegg-types';
import type { EggProtoImplClass, IAdvice } from '@eggjs/tegg-types';
import { Advice, Crosscut } from '../..';

@ContextProto()
export class CrosscutExample {
Expand Down
5 changes: 3 additions & 2 deletions core/aop-decorator/test/fixtures/InheritExample.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ContextProto } from '@eggjs/core-decorator';
import { Advice, AdviceContext, IAdvice } from '../../src/decorator/Advice';
import { PointcutType } from '@eggjs/tegg-types';
import type { AdviceContext, IAdvice } from '@eggjs/tegg-types';
import { Advice } from '../../src/decorator/Advice';
import { Pointcut } from '../../src/decorator/Pointcut';
import { Crosscut } from '../../src/decorator/Crosscut';
import { PointcutType } from '../../src/model/PointcutInfo';

@Advice()
export class PointcutAdviceNoOverwriteParentExample implements IAdvice {
Expand Down
3 changes: 2 additions & 1 deletion core/aop-decorator/test/fixtures/PointcutExample.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ContextProto } from '@eggjs/core-decorator';
import { Advice, AdviceContext, IAdvice, Pointcut } from '../..';
import type { AdviceContext, IAdvice } from '@eggjs/tegg-types';
import { Advice, Pointcut } from '../..';

@Advice()
export class PointcutAdviceBeforeCallExample implements IAdvice {
Expand Down
1 change: 1 addition & 0 deletions core/aop-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@eggjs/tegg-lifecycle": "^3.37.1",
"@eggjs/tegg-metadata": "^3.37.1",
"@eggjs/tegg-runtime": "^3.37.1",
"@eggjs/tegg-types": "^3.37.1",
"koa-compose": "^4.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion core/aop-runtime/src/AspectExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AdviceContext, AspectAdvice, IAdvice } from '@eggjs/aop-decorator';
import type { AdviceContext, AspectAdvice, IAdvice } from '@eggjs/tegg-types';
import compose from 'koa-compose';
import type { Middleware } from 'koa-compose';

Expand Down
6 changes: 3 additions & 3 deletions core/aop-runtime/src/EggObjectAopHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LifecycleHook } from '@eggjs/tegg-lifecycle';
import { EggObject, EggObjectLifeCycleContext } from '@eggjs/tegg-runtime';
import { Aspect, ASPECT_LIST } from '@eggjs/aop-decorator';
import { ASPECT_LIST } from '@eggjs/tegg-types';
import type { EggObject, EggObjectLifeCycleContext, LifecycleHook } from '@eggjs/tegg-types';
import { Aspect } from '@eggjs/aop-decorator';
import { AspectExecutor } from './AspectExecutor';

export class EggObjectAopHook implements LifecycleHook<EggObjectLifeCycleContext, EggObject> {
Expand Down
3 changes: 1 addition & 2 deletions core/aop-runtime/src/EggPrototypeCrossCutHook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LifecycleHook } from '@eggjs/tegg-lifecycle';
import type { EggPrototype, EggPrototypeLifecycleContext, LifecycleHook } from '@eggjs/tegg-types';
import { CrosscutAdviceFactory, CrosscutInfoUtil } from '@eggjs/aop-decorator';
import { EggPrototype, EggPrototypeLifecycleContext } from '@eggjs/tegg-metadata';

export class EggPrototypeCrossCutHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
private readonly crosscutAdviceFactory: CrosscutAdviceFactory;
Expand Down
18 changes: 10 additions & 8 deletions core/aop-runtime/src/LoadUnitAopHook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { LifecycleHook } from '@eggjs/tegg-lifecycle';
import { CrosscutAdviceFactory, AspectMetaBuilder, AspectInfoUtil } from '@eggjs/aop-decorator';
import { EggProtoImplClass, PrototypeUtil } from '@eggjs/core-decorator';
import { EggPrototype, LoadUnit, LoadUnitLifecycleContext, TeggError } from '@eggjs/tegg-metadata';

export interface EggPrototypeWithClazz extends EggPrototype {
clazz?: EggProtoImplClass;
}
import { AspectInfoUtil, AspectMetaBuilder, CrosscutAdviceFactory } from '@eggjs/aop-decorator';
import { PrototypeUtil } from '@eggjs/core-decorator';
import { TeggError } from '@eggjs/tegg-metadata';
import type {
EggPrototype,
EggPrototypeWithClazz,
LifecycleHook,
LoadUnit,
LoadUnitLifecycleContext,
} from '@eggjs/tegg-types';

export class LoadUnitAopHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
private readonly crosscutAdviceFactory: CrosscutAdviceFactory;
Expand Down
10 changes: 5 additions & 5 deletions core/aop-runtime/test/aop-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'path';
import assert from 'node:assert';
import path from 'node:path';
import mm from 'mm';
import assert from 'assert';
import { EggObjectLifecycleUtil, LoadUnitInstance, LoadUnitInstanceFactory } from '@eggjs/tegg-runtime';
import { EggObjectLifecycleUtil, LoadUnitInstanceFactory } from '@eggjs/tegg-runtime';
import { EggPrototypeLifecycleUtil, LoadUnitFactory, LoadUnitLifecycleUtil } from '@eggjs/tegg-metadata';
import type { LoadUnitInstance } from '@eggjs/tegg-types';
import { CrosscutAdviceFactory } from '@eggjs/aop-decorator';
import { EggTestContext } from '../../test-util';
import { CoreTestHelper, EggTestContext } from '../../test-util';
import { CallTrace, Hello, crosscutAdviceParams, pointcutAdviceParams } from './fixtures/modules/hello_succeed/Hello';
import { CoreTestHelper } from '../../test-util/CoreTestHelper';
import { EggObjectAopHook } from '../src/EggObjectAopHook';
import { LoadUnitAopHook } from '../src/LoadUnitAopHook';
import { EggPrototypeCrossCutHook } from '../src/EggPrototypeCrossCutHook';
Expand Down
Loading

0 comments on commit 334e13d

Please sign in to comment.