@eggjs/tegg-controller-plugin 4.0.0-beta.4 → 4.0.0-beta.6
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/README.md +15 -3
- package/dist/app/middleware/tegg_root_proto.d.ts +6 -0
- package/dist/app/middleware/tegg_root_proto.js +12 -0
- package/dist/app.d.ts +27 -0
- package/dist/app.js +67 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/lib/AppLoadUnitControllerHook.d.ts +14 -0
- package/dist/lib/AppLoadUnitControllerHook.js +29 -0
- package/dist/lib/ControllerLoadUnit.d.ts +25 -0
- package/dist/lib/ControllerLoadUnit.js +63 -0
- package/dist/lib/ControllerLoadUnitHandler.d.ts +16 -0
- package/dist/lib/ControllerLoadUnitHandler.js +29 -0
- package/dist/lib/ControllerLoadUnitInstance.d.ts +21 -0
- package/dist/lib/ControllerLoadUnitInstance.js +40 -0
- package/dist/lib/ControllerMetadataManager.d.ts +12 -0
- package/dist/lib/ControllerMetadataManager.js +23 -0
- package/dist/lib/ControllerRegister.d.ts +9 -0
- package/dist/lib/ControllerRegister.js +4 -0
- package/dist/lib/ControllerRegisterFactory.d.ts +16 -0
- package/dist/lib/ControllerRegisterFactory.js +24 -0
- package/dist/lib/EggControllerLoader.d.ts +11 -0
- package/dist/lib/EggControllerLoader.js +29 -0
- package/dist/lib/EggControllerPrototypeHook.d.ts +9 -0
- package/dist/lib/EggControllerPrototypeHook.js +13 -0
- package/dist/lib/RootProtoManager.d.ts +12 -0
- package/dist/lib/RootProtoManager.js +28 -0
- package/dist/lib/errors.d.ts +9 -0
- package/dist/lib/errors.js +16 -0
- package/dist/lib/impl/http/Acl.d.ts +6 -0
- package/dist/lib/impl/http/Acl.js +29 -0
- package/dist/lib/impl/http/HTTPControllerRegister.d.ts +22 -0
- package/dist/lib/impl/http/HTTPControllerRegister.js +60 -0
- package/dist/lib/impl/http/HTTPMethodRegister.d.ts +23 -0
- package/dist/lib/impl/http/HTTPMethodRegister.js +139 -0
- package/dist/lib/impl/http/Req.d.ts +9 -0
- package/dist/lib/impl/http/Req.js +16 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.js +4 -0
- package/package.json +55 -46
- package/app/middleware/tegg_root_proto.d.ts +0 -3
- package/app/middleware/tegg_root_proto.js +0 -8
- package/app.d.ts +0 -13
- package/app.js +0 -75
- package/index.d.ts +0 -13
- package/index.js +0 -2
- package/lib/AppLoadUnitControllerHook.d.ts +0 -10
- package/lib/AppLoadUnitControllerHook.js +0 -26
- package/lib/ControllerLoadUnit.d.ts +0 -21
- package/lib/ControllerLoadUnit.js +0 -72
- package/lib/ControllerLoadUnitHandler.d.ts +0 -12
- package/lib/ControllerLoadUnitHandler.js +0 -27
- package/lib/ControllerLoadUnitInstance.d.ts +0 -17
- package/lib/ControllerLoadUnitInstance.js +0 -34
- package/lib/ControllerMetadataManager.d.ts +0 -8
- package/lib/ControllerMetadataManager.js +0 -26
- package/lib/ControllerRegister.d.ts +0 -5
- package/lib/ControllerRegister.js +0 -2
- package/lib/ControllerRegisterFactory.d.ts +0 -12
- package/lib/ControllerRegisterFactory.js +0 -19
- package/lib/EggControllerLoader.d.ts +0 -7
- package/lib/EggControllerLoader.js +0 -31
- package/lib/EggControllerPrototypeHook.d.ts +0 -5
- package/lib/EggControllerPrototypeHook.js +0 -10
- package/lib/RootProtoManager.d.ts +0 -8
- package/lib/RootProtoManager.js +0 -32
- package/lib/errors.d.ts +0 -5
- package/lib/errors.js +0 -12
- package/lib/impl/http/Acl.d.ts +0 -2
- package/lib/impl/http/Acl.js +0 -32
- package/lib/impl/http/HTTPControllerRegister.d.ts +0 -18
- package/lib/impl/http/HTTPControllerRegister.js +0 -59
- package/lib/impl/http/HTTPMethodRegister.d.ts +0 -19
- package/lib/impl/http/HTTPMethodRegister.js +0 -161
- package/lib/impl/http/Req.d.ts +0 -5
- package/lib/impl/http/Req.js +0 -13
package/README.md
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
# @eggjs/tegg-controller-plugin
|
|
2
2
|
|
|
3
|
+
[![NPM version][npm-image]][npm-url]
|
|
4
|
+
[![Known Vulnerabilities][snyk-image]][snyk-url]
|
|
5
|
+
[![npm download][download-image]][download-url]
|
|
6
|
+
[](https://nodejs.org/en/download/)
|
|
7
|
+
|
|
8
|
+
[npm-image]: https://img.shields.io/npm/v/@eggjs/tegg-controller-plugin.svg?style=flat-square
|
|
9
|
+
[npm-url]: https://npmjs.org/package/@eggjs/tegg-controller-plugin
|
|
10
|
+
[snyk-image]: https://snyk.io/test/npm/@eggjs/tegg-controller-plugin/badge.svg?style=flat-square
|
|
11
|
+
[snyk-url]: https://snyk.io/test/npm/@eggjs/tegg-controller-plugin
|
|
12
|
+
[download-image]: https://img.shields.io/npm/dm/@eggjs/tegg-controller-plugin.svg?style=flat-square
|
|
13
|
+
[download-url]: https://npmjs.org/package/@eggjs/tegg-controller-plugin
|
|
14
|
+
|
|
3
15
|
使用注解的方式来开发 egg 中的 Controller
|
|
4
16
|
|
|
5
17
|
## Install
|
|
6
18
|
|
|
7
19
|
```shell
|
|
8
20
|
# tegg 注解
|
|
9
|
-
npm i --save @eggjs/tegg
|
|
21
|
+
npm i --save @eggjs/tegg@beta
|
|
10
22
|
# tegg 插件
|
|
11
|
-
npm i --save @eggjs/tegg-plugin
|
|
23
|
+
npm i --save @eggjs/tegg-plugin@beta
|
|
12
24
|
# tegg controller 插件
|
|
13
|
-
npm i --save @eggjs/tegg-controller-plugin
|
|
25
|
+
npm i --save @eggjs/tegg-controller-plugin@beta
|
|
14
26
|
```
|
|
15
27
|
|
|
16
28
|
## Prepare
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ROOT_PROTO } from "@eggjs/egg-module-common";
|
|
2
|
+
|
|
3
|
+
//#region src/app/middleware/tegg_root_proto.ts
|
|
4
|
+
var tegg_root_proto_default = () => {
|
|
5
|
+
return async function teggRootProto(ctx, next) {
|
|
6
|
+
ctx[ROOT_PROTO] = ctx.app.rootProtoManager.getRootProto(ctx);
|
|
7
|
+
return next();
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { tegg_root_proto_default as default };
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { RootProtoManager } from "./lib/RootProtoManager.js";
|
|
2
|
+
import { ControllerRegisterFactory } from "./lib/ControllerRegisterFactory.js";
|
|
3
|
+
import { ControllerMetaBuilderFactory } from "@eggjs/tegg";
|
|
4
|
+
import { Application, ILifecycleBoot } from "egg";
|
|
5
|
+
|
|
6
|
+
//#region src/app.d.ts
|
|
7
|
+
declare class ControllerAppBootHook implements ILifecycleBoot {
|
|
8
|
+
private readonly app;
|
|
9
|
+
private readonly loadUnitHook;
|
|
10
|
+
private readonly controllerRegisterFactory;
|
|
11
|
+
private controllerLoadUnitHandler;
|
|
12
|
+
private readonly controllerPrototypeHook;
|
|
13
|
+
constructor(app: Application);
|
|
14
|
+
configWillLoad(): void;
|
|
15
|
+
prepareMiddleware(middlewareNames: string[]): string[];
|
|
16
|
+
didLoad(): Promise<void>;
|
|
17
|
+
beforeClose(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
declare module 'egg' {
|
|
20
|
+
interface Application {
|
|
21
|
+
rootProtoManager: RootProtoManager;
|
|
22
|
+
controllerRegisterFactory: ControllerRegisterFactory;
|
|
23
|
+
controllerMetaBuilderFactory: typeof ControllerMetaBuilderFactory;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { ControllerAppBootHook as default };
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { CONTROLLER_LOAD_UNIT, ControllerLoadUnit } from "./lib/ControllerLoadUnit.js";
|
|
2
|
+
import { RootProtoManager } from "./lib/RootProtoManager.js";
|
|
3
|
+
import { ControllerRegisterFactory } from "./lib/ControllerRegisterFactory.js";
|
|
4
|
+
import { ControllerMetadataManager } from "./lib/ControllerMetadataManager.js";
|
|
5
|
+
import { AppLoadUnitControllerHook } from "./lib/AppLoadUnitControllerHook.js";
|
|
6
|
+
import { HTTPControllerRegister } from "./lib/impl/http/HTTPControllerRegister.js";
|
|
7
|
+
import { ControllerLoadUnitHandler } from "./lib/ControllerLoadUnitHandler.js";
|
|
8
|
+
import { EggControllerPrototypeHook } from "./lib/EggControllerPrototypeHook.js";
|
|
9
|
+
import { EggControllerLoader } from "./lib/EggControllerLoader.js";
|
|
10
|
+
import "@eggjs/tegg-metadata";
|
|
11
|
+
import { ControllerMetaBuilderFactory, ControllerType } from "@eggjs/tegg";
|
|
12
|
+
import { ModuleLoadUnitInstance } from "@eggjs/tegg-runtime";
|
|
13
|
+
|
|
14
|
+
//#region src/app.ts
|
|
15
|
+
var ControllerAppBootHook = class {
|
|
16
|
+
app;
|
|
17
|
+
loadUnitHook;
|
|
18
|
+
controllerRegisterFactory;
|
|
19
|
+
controllerLoadUnitHandler;
|
|
20
|
+
controllerPrototypeHook;
|
|
21
|
+
constructor(app) {
|
|
22
|
+
this.app = app;
|
|
23
|
+
this.controllerRegisterFactory = new ControllerRegisterFactory(this.app);
|
|
24
|
+
this.app.rootProtoManager = new RootProtoManager();
|
|
25
|
+
this.app.controllerRegisterFactory = this.controllerRegisterFactory;
|
|
26
|
+
this.app.controllerMetaBuilderFactory = ControllerMetaBuilderFactory;
|
|
27
|
+
this.loadUnitHook = new AppLoadUnitControllerHook(this.controllerRegisterFactory, this.app.rootProtoManager);
|
|
28
|
+
this.controllerPrototypeHook = new EggControllerPrototypeHook();
|
|
29
|
+
}
|
|
30
|
+
configWillLoad() {
|
|
31
|
+
this.app.loadUnitLifecycleUtil.registerLifecycle(this.loadUnitHook);
|
|
32
|
+
this.app.eggPrototypeLifecycleUtil.registerLifecycle(this.controllerPrototypeHook);
|
|
33
|
+
this.app.loaderFactory.registerLoader(CONTROLLER_LOAD_UNIT, (unitPath) => {
|
|
34
|
+
return new EggControllerLoader(unitPath);
|
|
35
|
+
});
|
|
36
|
+
this.controllerRegisterFactory.registerControllerRegister(ControllerType.HTTP, HTTPControllerRegister.create);
|
|
37
|
+
this.app.loadUnitFactory.registerLoadUnitCreator(CONTROLLER_LOAD_UNIT, (ctx) => {
|
|
38
|
+
return new ControllerLoadUnit(`tegg-app-controller:${ctx.unitPath}`, ctx.unitPath, ctx.loader, this.app.eggPrototypeFactory, this.app.eggPrototypeCreatorFactory);
|
|
39
|
+
});
|
|
40
|
+
this.app.loadUnitInstanceFactory.registerLoadUnitInstanceClass(CONTROLLER_LOAD_UNIT, (ctx) => {
|
|
41
|
+
return new ModuleLoadUnitInstance(ctx.loadUnit);
|
|
42
|
+
});
|
|
43
|
+
this.prepareMiddleware(this.app.config.coreMiddleware);
|
|
44
|
+
}
|
|
45
|
+
prepareMiddleware(middlewareNames) {
|
|
46
|
+
if (!middlewareNames.includes("teggCtxLifecycleMiddleware")) middlewareNames.unshift("teggCtxLifecycleMiddleware");
|
|
47
|
+
const index = middlewareNames.indexOf("teggCtxLifecycleMiddleware");
|
|
48
|
+
middlewareNames.splice(index, 0, "teggRootProto");
|
|
49
|
+
return middlewareNames;
|
|
50
|
+
}
|
|
51
|
+
async didLoad() {
|
|
52
|
+
await this.app.moduleHandler.ready();
|
|
53
|
+
this.controllerLoadUnitHandler = new ControllerLoadUnitHandler(this.app);
|
|
54
|
+
await this.controllerLoadUnitHandler.ready();
|
|
55
|
+
HTTPControllerRegister.instance?.doRegister(this.app.rootProtoManager);
|
|
56
|
+
}
|
|
57
|
+
async beforeClose() {
|
|
58
|
+
if (this.controllerLoadUnitHandler) await this.controllerLoadUnitHandler.destroy();
|
|
59
|
+
this.app.loadUnitLifecycleUtil.deleteLifecycle(this.loadUnitHook);
|
|
60
|
+
this.app.eggPrototypeLifecycleUtil.deleteLifecycle(this.controllerPrototypeHook);
|
|
61
|
+
ControllerMetadataManager.instance.clear();
|
|
62
|
+
HTTPControllerRegister.clean();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
export { ControllerAppBootHook as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RootProtoManager } from "./RootProtoManager.js";
|
|
2
|
+
import { ControllerRegisterFactory } from "./ControllerRegisterFactory.js";
|
|
3
|
+
import { LoadUnit, LoadUnitLifecycleContext } from "@eggjs/tegg-metadata";
|
|
4
|
+
import { LifecycleHook } from "@eggjs/tegg";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/AppLoadUnitControllerHook.d.ts
|
|
7
|
+
declare class AppLoadUnitControllerHook implements LifecycleHook<LoadUnitLifecycleContext, LoadUnit> {
|
|
8
|
+
private readonly controllerRegisterFactory;
|
|
9
|
+
private readonly rootProtoManager;
|
|
10
|
+
constructor(controllerRegisterFactory: ControllerRegisterFactory, rootProtoManager: RootProtoManager);
|
|
11
|
+
postCreate(_: LoadUnitLifecycleContext, obj: LoadUnit): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { AppLoadUnitControllerHook };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import "./RootProtoManager.js";
|
|
2
|
+
import "./ControllerRegisterFactory.js";
|
|
3
|
+
import { ControllerMetadataManager } from "./ControllerMetadataManager.js";
|
|
4
|
+
import "@eggjs/tegg-metadata";
|
|
5
|
+
import { CONTROLLER_META_DATA } from "@eggjs/tegg";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/AppLoadUnitControllerHook.ts
|
|
8
|
+
var AppLoadUnitControllerHook = class {
|
|
9
|
+
controllerRegisterFactory;
|
|
10
|
+
rootProtoManager;
|
|
11
|
+
constructor(controllerRegisterFactory, rootProtoManager) {
|
|
12
|
+
this.controllerRegisterFactory = controllerRegisterFactory;
|
|
13
|
+
this.rootProtoManager = rootProtoManager;
|
|
14
|
+
}
|
|
15
|
+
async postCreate(_, obj) {
|
|
16
|
+
const iterator = obj.iterateEggPrototype();
|
|
17
|
+
for (const proto of iterator) {
|
|
18
|
+
const metadata = proto.getMetaData(CONTROLLER_META_DATA);
|
|
19
|
+
if (!metadata) continue;
|
|
20
|
+
const register = this.controllerRegisterFactory.getControllerRegister(proto, metadata);
|
|
21
|
+
if (!register) throw new Error(`not find controller implement for ${String(proto.name)} which type is ${metadata.type}`);
|
|
22
|
+
ControllerMetadataManager.instance.addController(metadata);
|
|
23
|
+
await register.register(this.rootProtoManager, obj);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { AppLoadUnitControllerHook };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { EggPrototype, EggPrototypeCreatorFactory, EggPrototypeFactory, LoadUnit, Loader } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { Id, QualifierInfo } from "@eggjs/tegg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/ControllerLoadUnit.d.ts
|
|
5
|
+
declare const CONTROLLER_LOAD_UNIT = "app#controller";
|
|
6
|
+
declare class ControllerLoadUnit implements LoadUnit {
|
|
7
|
+
private readonly loader;
|
|
8
|
+
id: Id;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly type = "app#controller";
|
|
11
|
+
readonly unitPath: string;
|
|
12
|
+
private eggPrototypeFactory;
|
|
13
|
+
private eggPrototypeCreatorFactory;
|
|
14
|
+
private protoMap;
|
|
15
|
+
constructor(name: string, unitPath: string, loader: Loader, eggPrototypeFactory: EggPrototypeFactory, eggPrototypeCreatorFactory: typeof EggPrototypeCreatorFactory);
|
|
16
|
+
init(): Promise<void>;
|
|
17
|
+
containPrototype(proto: EggPrototype): boolean;
|
|
18
|
+
getEggPrototype(name: string, qualifiers: QualifierInfo[]): EggPrototype[];
|
|
19
|
+
registerEggPrototype(proto: EggPrototype): void;
|
|
20
|
+
deletePrototype(proto: EggPrototype): void;
|
|
21
|
+
destroy(): Promise<void>;
|
|
22
|
+
iterateEggPrototype(): IterableIterator<EggPrototype>;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { CONTROLLER_LOAD_UNIT, ControllerLoadUnit };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { EggPrototypeFactory } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { IdenticalUtil } from "@eggjs/tegg";
|
|
3
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/ControllerLoadUnit.ts
|
|
6
|
+
const CONTROLLER_LOAD_UNIT = "app#controller";
|
|
7
|
+
var ControllerLoadUnit = class {
|
|
8
|
+
loader;
|
|
9
|
+
id;
|
|
10
|
+
name;
|
|
11
|
+
type = CONTROLLER_LOAD_UNIT;
|
|
12
|
+
unitPath;
|
|
13
|
+
eggPrototypeFactory;
|
|
14
|
+
eggPrototypeCreatorFactory;
|
|
15
|
+
protoMap = /* @__PURE__ */ new Map();
|
|
16
|
+
constructor(name, unitPath, loader, eggPrototypeFactory, eggPrototypeCreatorFactory) {
|
|
17
|
+
this.id = IdenticalUtil.createLoadUnitId(name);
|
|
18
|
+
this.name = name;
|
|
19
|
+
this.unitPath = unitPath;
|
|
20
|
+
this.loader = loader;
|
|
21
|
+
this.eggPrototypeFactory = eggPrototypeFactory;
|
|
22
|
+
this.eggPrototypeCreatorFactory = eggPrototypeCreatorFactory;
|
|
23
|
+
}
|
|
24
|
+
async init() {
|
|
25
|
+
const clazzList = await this.loader.load();
|
|
26
|
+
for (const clazz of clazzList) {
|
|
27
|
+
const protos = await this.eggPrototypeCreatorFactory.createProto(clazz, this);
|
|
28
|
+
for (const proto of protos) this.eggPrototypeFactory.registerPrototype(proto, this);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
containPrototype(proto) {
|
|
32
|
+
return !!this.protoMap.get(proto.name)?.find((t) => t === proto);
|
|
33
|
+
}
|
|
34
|
+
getEggPrototype(name, qualifiers) {
|
|
35
|
+
return this.protoMap.get(name)?.filter((proto) => proto.verifyQualifiers(qualifiers)) || [];
|
|
36
|
+
}
|
|
37
|
+
registerEggPrototype(proto) {
|
|
38
|
+
MapUtil.getOrStore(this.protoMap, proto.name, []).push(proto);
|
|
39
|
+
}
|
|
40
|
+
deletePrototype(proto) {
|
|
41
|
+
const protos = this.protoMap.get(proto.name);
|
|
42
|
+
if (protos) {
|
|
43
|
+
const index = protos.indexOf(proto);
|
|
44
|
+
if (index !== -1) protos.splice(index, 1);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async destroy() {
|
|
48
|
+
for (const namedProtos of this.protoMap.values()) {
|
|
49
|
+
const protos = namedProtos.slice();
|
|
50
|
+
for (const proto of protos) EggPrototypeFactory.instance.deletePrototype(proto, this);
|
|
51
|
+
}
|
|
52
|
+
this.protoMap.clear();
|
|
53
|
+
}
|
|
54
|
+
iterateEggPrototype() {
|
|
55
|
+
return Array.from(this.protoMap.values()).reduce((p, c) => {
|
|
56
|
+
p = p.concat(c);
|
|
57
|
+
return p;
|
|
58
|
+
}, []).values();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
export { CONTROLLER_LOAD_UNIT, ControllerLoadUnit };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LoadUnit } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { LoadUnitInstance } from "@eggjs/tegg-runtime";
|
|
3
|
+
import { Application } from "egg";
|
|
4
|
+
import { Base } from "sdk-base";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/ControllerLoadUnitHandler.d.ts
|
|
7
|
+
declare class ControllerLoadUnitHandler extends Base {
|
|
8
|
+
private readonly app;
|
|
9
|
+
controllerLoadUnit: LoadUnit;
|
|
10
|
+
controllerLoadUnitInstance: LoadUnitInstance;
|
|
11
|
+
constructor(app: Application);
|
|
12
|
+
_init(): Promise<void>;
|
|
13
|
+
destroy(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { ControllerLoadUnitHandler };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CONTROLLER_LOAD_UNIT } from "./ControllerLoadUnit.js";
|
|
2
|
+
import "@eggjs/tegg-metadata";
|
|
3
|
+
import "@eggjs/tegg-runtime";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { Base } from "sdk-base";
|
|
6
|
+
|
|
7
|
+
//#region src/lib/ControllerLoadUnitHandler.ts
|
|
8
|
+
var ControllerLoadUnitHandler = class extends Base {
|
|
9
|
+
app;
|
|
10
|
+
controllerLoadUnit;
|
|
11
|
+
controllerLoadUnitInstance;
|
|
12
|
+
constructor(app) {
|
|
13
|
+
super({ initMethod: "_init" });
|
|
14
|
+
this.app = app;
|
|
15
|
+
}
|
|
16
|
+
async _init() {
|
|
17
|
+
const controllerDir = path.join(this.app.config.baseDir, "app/controller");
|
|
18
|
+
const loader = this.app.loaderFactory.createLoader(controllerDir, CONTROLLER_LOAD_UNIT);
|
|
19
|
+
this.controllerLoadUnit = await this.app.loadUnitFactory.createLoadUnit(controllerDir, CONTROLLER_LOAD_UNIT, loader);
|
|
20
|
+
this.controllerLoadUnitInstance = await this.app.loadUnitInstanceFactory.createLoadUnitInstance(this.controllerLoadUnit);
|
|
21
|
+
}
|
|
22
|
+
async destroy() {
|
|
23
|
+
if (this.controllerLoadUnit) await this.app.loadUnitFactory.destroyLoadUnit(this.controllerLoadUnit);
|
|
24
|
+
if (this.controllerLoadUnitInstance) await this.app.loadUnitInstanceFactory.destroyLoadUnitInstance(this.controllerLoadUnitInstance);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { ControllerLoadUnitHandler };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EggPrototype, LoadUnit } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { EggObjectName } from "@eggjs/tegg";
|
|
3
|
+
import { EggObject, LoadUnitInstance, LoadUnitInstanceLifecycleContext, LoadUnitInstanceLifecycleUtil } from "@eggjs/tegg-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/ControllerLoadUnitInstance.d.ts
|
|
6
|
+
declare class ControllerLoadUnitInstance implements LoadUnitInstance {
|
|
7
|
+
readonly loadUnit: LoadUnit;
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
private protoToCreateMap;
|
|
11
|
+
private loadUnitInstanceLifecycleUtil;
|
|
12
|
+
constructor(loadUnit: LoadUnit, loadUnitInstanceLifecycleUtil: typeof LoadUnitInstanceLifecycleUtil);
|
|
13
|
+
iterateProtoToCreate(): IterableIterator<[EggObjectName, EggPrototype]>;
|
|
14
|
+
addProtoToCreate(): void;
|
|
15
|
+
deleteProtoToCreate(): void;
|
|
16
|
+
init(ctx: LoadUnitInstanceLifecycleContext): Promise<void>;
|
|
17
|
+
getOrCreateEggObject(): Promise<EggObject>;
|
|
18
|
+
getEggObject(): EggObject;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ControllerLoadUnitInstance };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import "@eggjs/tegg-metadata";
|
|
2
|
+
import { IdenticalUtil } from "@eggjs/tegg";
|
|
3
|
+
import { LoadUnitInstanceLifecycleUtil } from "@eggjs/tegg-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/ControllerLoadUnitInstance.ts
|
|
6
|
+
var ControllerLoadUnitInstance = class {
|
|
7
|
+
loadUnit;
|
|
8
|
+
id;
|
|
9
|
+
name;
|
|
10
|
+
protoToCreateMap = /* @__PURE__ */ new Map();
|
|
11
|
+
loadUnitInstanceLifecycleUtil;
|
|
12
|
+
constructor(loadUnit, loadUnitInstanceLifecycleUtil) {
|
|
13
|
+
this.loadUnit = loadUnit;
|
|
14
|
+
this.name = loadUnit.name;
|
|
15
|
+
this.id = IdenticalUtil.createLoadUnitInstanceId(loadUnit.id);
|
|
16
|
+
this.loadUnitInstanceLifecycleUtil = loadUnitInstanceLifecycleUtil;
|
|
17
|
+
}
|
|
18
|
+
iterateProtoToCreate() {
|
|
19
|
+
return this.protoToCreateMap.entries();
|
|
20
|
+
}
|
|
21
|
+
addProtoToCreate() {
|
|
22
|
+
throw new Error("controller load unit not allow have singleton proto");
|
|
23
|
+
}
|
|
24
|
+
deleteProtoToCreate() {
|
|
25
|
+
throw new Error("controller load unit not allow have singleton proto");
|
|
26
|
+
}
|
|
27
|
+
async init(ctx) {
|
|
28
|
+
await this.loadUnitInstanceLifecycleUtil.objectPreCreate(ctx, this);
|
|
29
|
+
await this.loadUnitInstanceLifecycleUtil.objectPostCreate(ctx, this);
|
|
30
|
+
}
|
|
31
|
+
async getOrCreateEggObject() {
|
|
32
|
+
throw new Error("controller load unit not allow have singleton proto");
|
|
33
|
+
}
|
|
34
|
+
getEggObject() {
|
|
35
|
+
throw new Error("controller load unit not allow have singleton proto");
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { ControllerLoadUnitInstance };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ControllerMetadata } from "@eggjs/tegg";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/ControllerMetadataManager.d.ts
|
|
4
|
+
declare class ControllerMetadataManager {
|
|
5
|
+
private readonly controllers;
|
|
6
|
+
static instance: ControllerMetadataManager;
|
|
7
|
+
constructor();
|
|
8
|
+
addController(metadata: ControllerMetadata): void;
|
|
9
|
+
clear(): void;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ControllerMetadataManager };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "@eggjs/tegg";
|
|
2
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/ControllerMetadataManager.ts
|
|
5
|
+
var ControllerMetadataManager = class ControllerMetadataManager {
|
|
6
|
+
controllers = /* @__PURE__ */ new Map();
|
|
7
|
+
static instance = new ControllerMetadataManager();
|
|
8
|
+
constructor() {
|
|
9
|
+
this.controllers = /* @__PURE__ */ new Map();
|
|
10
|
+
}
|
|
11
|
+
addController(metadata) {
|
|
12
|
+
const typeControllers = MapUtil.getOrStore(this.controllers, metadata.type, []);
|
|
13
|
+
if (typeControllers.filter((c) => c.controllerName === metadata.controllerName).length) throw new Error(`duplicate controller name ${metadata.controllerName}`);
|
|
14
|
+
if (typeControllers.filter((c) => c.protoName === metadata.protoName).length) throw new Error(`duplicate proto name ${String(metadata.protoName)}`);
|
|
15
|
+
typeControllers.push(metadata);
|
|
16
|
+
}
|
|
17
|
+
clear() {
|
|
18
|
+
this.controllers.clear();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ControllerMetadataManager };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RootProtoManager } from "./RootProtoManager.js";
|
|
2
|
+
import { LoadUnit } from "@eggjs/tegg-metadata";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/ControllerRegister.d.ts
|
|
5
|
+
interface ControllerRegister {
|
|
6
|
+
register(rootProtoManager: RootProtoManager, loadUnit?: LoadUnit): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { ControllerRegister };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ControllerRegister } from "./ControllerRegister.js";
|
|
2
|
+
import { EggPrototype } from "@eggjs/tegg-metadata";
|
|
3
|
+
import { ControllerMetadata, ControllerTypeLike } from "@eggjs/tegg";
|
|
4
|
+
import { Application } from "egg";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/ControllerRegisterFactory.d.ts
|
|
7
|
+
type RegisterCreator = (proto: EggPrototype, controllerMeta: ControllerMetadata, app: Application) => ControllerRegister;
|
|
8
|
+
declare class ControllerRegisterFactory {
|
|
9
|
+
private readonly app;
|
|
10
|
+
private registerCreatorMap;
|
|
11
|
+
constructor(app: Application);
|
|
12
|
+
registerControllerRegister(type: ControllerTypeLike, creator: RegisterCreator): void;
|
|
13
|
+
getControllerRegister(proto: EggPrototype, metadata: ControllerMetadata): ControllerRegister | undefined;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { ControllerRegisterFactory, RegisterCreator };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import "./ControllerRegister.js";
|
|
2
|
+
import "@eggjs/tegg-metadata";
|
|
3
|
+
import "@eggjs/tegg";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/ControllerRegisterFactory.ts
|
|
6
|
+
var ControllerRegisterFactory = class {
|
|
7
|
+
app;
|
|
8
|
+
registerCreatorMap;
|
|
9
|
+
constructor(app) {
|
|
10
|
+
this.app = app;
|
|
11
|
+
this.registerCreatorMap = /* @__PURE__ */ new Map();
|
|
12
|
+
}
|
|
13
|
+
registerControllerRegister(type, creator) {
|
|
14
|
+
this.registerCreatorMap.set(type, creator);
|
|
15
|
+
}
|
|
16
|
+
getControllerRegister(proto, metadata) {
|
|
17
|
+
const creator = this.registerCreatorMap.get(metadata.type);
|
|
18
|
+
if (!creator) return;
|
|
19
|
+
return creator(proto, metadata, this.app);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { ControllerRegisterFactory };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EggProtoImplClass } from "@eggjs/tegg";
|
|
2
|
+
import { Loader } from "@eggjs/tegg-types";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/EggControllerLoader.d.ts
|
|
5
|
+
declare class EggControllerLoader implements Loader {
|
|
6
|
+
private readonly controllerDir;
|
|
7
|
+
constructor(controllerDir: string);
|
|
8
|
+
load(): Promise<EggProtoImplClass[]>;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { EggControllerLoader };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { globby } from "globby";
|
|
3
|
+
import { LoaderUtil } from "@eggjs/tegg-loader";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/EggControllerLoader.ts
|
|
6
|
+
var EggControllerLoader = class {
|
|
7
|
+
controllerDir;
|
|
8
|
+
constructor(controllerDir) {
|
|
9
|
+
this.controllerDir = controllerDir;
|
|
10
|
+
}
|
|
11
|
+
async load() {
|
|
12
|
+
const filePattern = LoaderUtil.filePattern();
|
|
13
|
+
let files;
|
|
14
|
+
try {
|
|
15
|
+
files = (await globby(filePattern, { cwd: this.controllerDir })).map((file) => path.join(this.controllerDir, file));
|
|
16
|
+
} catch {
|
|
17
|
+
files = [];
|
|
18
|
+
}
|
|
19
|
+
const protoClassList = [];
|
|
20
|
+
for (const file of files) {
|
|
21
|
+
const fileClazzList = await LoaderUtil.loadFile(file);
|
|
22
|
+
for (const clazz of fileClazzList) protoClassList.push(clazz);
|
|
23
|
+
}
|
|
24
|
+
return protoClassList;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
export { EggControllerLoader };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EggPrototype, EggPrototypeLifecycleContext } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { LifecycleHook } from "@eggjs/tegg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/EggControllerPrototypeHook.d.ts
|
|
5
|
+
declare class EggControllerPrototypeHook implements LifecycleHook<EggPrototypeLifecycleContext, EggPrototype> {
|
|
6
|
+
postCreate(ctx: EggPrototypeLifecycleContext): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { EggControllerPrototypeHook };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "@eggjs/tegg-metadata";
|
|
2
|
+
import { ControllerMetaBuilderFactory, ControllerMetadataUtil } from "@eggjs/tegg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/EggControllerPrototypeHook.ts
|
|
5
|
+
var EggControllerPrototypeHook = class {
|
|
6
|
+
async postCreate(ctx) {
|
|
7
|
+
const metadata = ControllerMetaBuilderFactory.build(ctx.clazz);
|
|
8
|
+
if (metadata) ControllerMetadataUtil.setControllerMetadata(ctx.clazz, metadata);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { EggControllerPrototypeHook };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EggPrototype } from "@eggjs/tegg-metadata";
|
|
2
|
+
import { EggContext } from "@eggjs/tegg";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/RootProtoManager.d.ts
|
|
5
|
+
type GetRootProtoCallback = (ctx: EggContext) => EggPrototype | undefined;
|
|
6
|
+
declare class RootProtoManager {
|
|
7
|
+
protoMap: Map<string, GetRootProtoCallback[]>;
|
|
8
|
+
registerRootProto(method: string, cb: GetRootProtoCallback, host: string): void;
|
|
9
|
+
getRootProto(ctx: EggContext): EggPrototype | undefined;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { GetRootProtoCallback, RootProtoManager };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import "@eggjs/tegg-metadata";
|
|
2
|
+
import "@eggjs/tegg";
|
|
3
|
+
import { MapUtil } from "@eggjs/tegg-common-util";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/RootProtoManager.ts
|
|
6
|
+
var RootProtoManager = class {
|
|
7
|
+
protoMap = /* @__PURE__ */ new Map();
|
|
8
|
+
registerRootProto(method, cb, host) {
|
|
9
|
+
host = host || "";
|
|
10
|
+
MapUtil.getOrStore(this.protoMap, method + host, []).push(cb);
|
|
11
|
+
}
|
|
12
|
+
getRootProto(ctx) {
|
|
13
|
+
const hostCbList = this.protoMap.get(ctx.method + ctx.host);
|
|
14
|
+
if (hostCbList) for (const cb of hostCbList) {
|
|
15
|
+
const proto = cb(ctx);
|
|
16
|
+
if (proto) return proto;
|
|
17
|
+
}
|
|
18
|
+
const cbList = this.protoMap.get(ctx.method);
|
|
19
|
+
if (!cbList) return;
|
|
20
|
+
for (const cb of cbList) {
|
|
21
|
+
const proto = cb(ctx);
|
|
22
|
+
if (proto) return proto;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { RootProtoManager };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TeggError } from "@eggjs/tegg-metadata";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/errors.ts
|
|
4
|
+
var ErrorCodes = /* @__PURE__ */ function(ErrorCodes$1) {
|
|
5
|
+
ErrorCodes$1["ROUTER_CONFLICT"] = "ROUTER_CONFLICT";
|
|
6
|
+
return ErrorCodes$1;
|
|
7
|
+
}(ErrorCodes || {});
|
|
8
|
+
/** 路由冲突错误 */
|
|
9
|
+
var RouterConflictError = class extends TeggError {
|
|
10
|
+
constructor(msg) {
|
|
11
|
+
super(msg, ErrorCodes.ROUTER_CONFLICT);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { RouterConflictError };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { EggContext, HTTPControllerMeta, HTTPMethodMeta, Next } from "@eggjs/tegg";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/impl/http/Acl.d.ts
|
|
4
|
+
declare function aclMiddlewareFactory(controllerMeta: HTTPControllerMeta, methodMeta: HTTPMethodMeta): (ctx: EggContext, next: Next) => Promise<void>;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { aclMiddlewareFactory };
|