@eggjs/controller-decorator 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.
Files changed (118) hide show
  1. package/README.md +12 -0
  2. package/dist/builder/ControllerMetaBuilderFactory.d.ts +11 -0
  3. package/dist/builder/ControllerMetaBuilderFactory.js +37 -0
  4. package/dist/builder/index.js +3 -0
  5. package/dist/decorator/Acl.d.ts +4 -0
  6. package/dist/decorator/Acl.js +24 -0
  7. package/dist/decorator/Context.d.ts +4 -0
  8. package/dist/decorator/Context.js +16 -0
  9. package/dist/decorator/Middleware.d.ts +6 -0
  10. package/dist/decorator/Middleware.js +57 -0
  11. package/dist/decorator/http/HTTPController.d.ts +6 -0
  12. package/dist/decorator/http/HTTPController.js +23 -0
  13. package/dist/decorator/http/HTTPMethod.d.ts +6 -0
  14. package/dist/decorator/http/HTTPMethod.js +21 -0
  15. package/dist/decorator/http/HTTPParam.d.ts +12 -0
  16. package/dist/decorator/http/HTTPParam.js +77 -0
  17. package/dist/decorator/http/Host.d.ts +6 -0
  18. package/dist/decorator/http/Host.js +27 -0
  19. package/dist/decorator/http/index.js +6 -0
  20. package/dist/decorator/index.js +10 -0
  21. package/dist/impl/http/HTTPControllerMetaBuilder.d.ts +13 -0
  22. package/dist/impl/http/HTTPControllerMetaBuilder.js +63 -0
  23. package/dist/impl/http/HTTPControllerMethodMetaBuilder.d.ts +15 -0
  24. package/dist/impl/http/HTTPControllerMethodMetaBuilder.js +80 -0
  25. package/dist/impl/http/index.js +4 -0
  26. package/dist/impl/index.js +5 -0
  27. package/dist/index.d.ts +24 -0
  28. package/dist/index.js +31 -0
  29. package/dist/model/HTTPControllerMeta.d.ts +25 -0
  30. package/dist/model/HTTPControllerMeta.js +53 -0
  31. package/dist/model/HTTPCookies.d.ts +6 -0
  32. package/dist/model/HTTPCookies.js +7 -0
  33. package/dist/model/HTTPMethodMeta.d.ts +59 -0
  34. package/dist/model/HTTPMethodMeta.js +102 -0
  35. package/dist/model/HTTPRequest.d.ts +8 -0
  36. package/dist/model/HTTPRequest.js +5 -0
  37. package/dist/model/HTTPResponse.d.ts +11 -0
  38. package/dist/model/HTTPResponse.js +5 -0
  39. package/dist/model/index.js +7 -0
  40. package/dist/util/ControllerInfoUtil.d.ts +20 -0
  41. package/dist/util/ControllerInfoUtil.js +48 -0
  42. package/dist/util/ControllerMetadataUtil.d.ts +9 -0
  43. package/dist/util/ControllerMetadataUtil.js +21 -0
  44. package/dist/util/HTTPInfoUtil.d.ts +20 -0
  45. package/dist/util/HTTPInfoUtil.js +53 -0
  46. package/dist/util/HTTPPriorityUtil.d.ts +22 -0
  47. package/dist/util/HTTPPriorityUtil.js +38 -0
  48. package/dist/util/MethodInfoUtil.d.ts +23 -0
  49. package/dist/util/MethodInfoUtil.js +62 -0
  50. package/dist/util/index.js +10 -0
  51. package/dist/util/validator/ControllerValidator.d.ts +8 -0
  52. package/dist/util/validator/ControllerValidator.js +16 -0
  53. package/dist/util/validator/MethodValidator.d.ts +8 -0
  54. package/dist/util/validator/MethodValidator.js +31 -0
  55. package/dist/util/validator/index.js +4 -0
  56. package/package.json +26 -28
  57. package/src/builder/ControllerMetaBuilderFactory.d.ts +0 -7
  58. package/src/builder/ControllerMetaBuilderFactory.js +0 -41
  59. package/src/builder/index.d.ts +0 -1
  60. package/src/builder/index.js +0 -2
  61. package/src/decorator/Acl.d.ts +0 -1
  62. package/src/decorator/Acl.js +0 -22
  63. package/src/decorator/Context.d.ts +0 -1
  64. package/src/decorator/Context.js +0 -11
  65. package/src/decorator/Middleware.d.ts +0 -2
  66. package/src/decorator/Middleware.js +0 -69
  67. package/src/decorator/http/HTTPController.d.ts +0 -2
  68. package/src/decorator/http/HTTPController.js +0 -29
  69. package/src/decorator/http/HTTPMethod.d.ts +0 -2
  70. package/src/decorator/http/HTTPMethod.js +0 -17
  71. package/src/decorator/http/HTTPParam.d.ts +0 -8
  72. package/src/decorator/http/HTTPParam.js +0 -75
  73. package/src/decorator/http/Host.d.ts +0 -2
  74. package/src/decorator/http/Host.js +0 -25
  75. package/src/decorator/http/index.d.ts +0 -4
  76. package/src/decorator/http/index.js +0 -5
  77. package/src/decorator/index.d.ts +0 -4
  78. package/src/decorator/index.js +0 -5
  79. package/src/impl/http/HTTPControllerMetaBuilder.d.ts +0 -9
  80. package/src/impl/http/HTTPControllerMetaBuilder.js +0 -57
  81. package/src/impl/http/HTTPControllerMethodMetaBuilder.d.ts +0 -11
  82. package/src/impl/http/HTTPControllerMethodMetaBuilder.js +0 -100
  83. package/src/impl/http/index.d.ts +0 -2
  84. package/src/impl/http/index.js +0 -3
  85. package/src/impl/index.d.ts +0 -1
  86. package/src/impl/index.js +0 -2
  87. package/src/index.d.ts +0 -6
  88. package/src/index.js +0 -7
  89. package/src/model/HTTPControllerMeta.d.ts +0 -22
  90. package/src/model/HTTPControllerMeta.js +0 -59
  91. package/src/model/HTTPCookies.d.ts +0 -3
  92. package/src/model/HTTPCookies.js +0 -4
  93. package/src/model/HTTPMethodMeta.d.ts +0 -56
  94. package/src/model/HTTPMethodMeta.js +0 -124
  95. package/src/model/HTTPRequest.d.ts +0 -7
  96. package/src/model/HTTPRequest.js +0 -3
  97. package/src/model/HTTPResponse.d.ts +0 -10
  98. package/src/model/HTTPResponse.js +0 -3
  99. package/src/model/index.d.ts +0 -5
  100. package/src/model/index.js +0 -6
  101. package/src/util/ControllerInfoUtil.d.ts +0 -17
  102. package/src/util/ControllerInfoUtil.js +0 -46
  103. package/src/util/ControllerMetadataUtil.d.ts +0 -5
  104. package/src/util/ControllerMetadataUtil.js +0 -20
  105. package/src/util/HTTPInfoUtil.d.ts +0 -16
  106. package/src/util/HTTPInfoUtil.js +0 -64
  107. package/src/util/HTTPPriorityUtil.d.ts +0 -19
  108. package/src/util/HTTPPriorityUtil.js +0 -42
  109. package/src/util/MethodInfoUtil.d.ts +0 -20
  110. package/src/util/MethodInfoUtil.js +0 -72
  111. package/src/util/index.d.ts +0 -6
  112. package/src/util/index.js +0 -7
  113. package/src/util/validator/ControllerValidator.d.ts +0 -4
  114. package/src/util/validator/ControllerValidator.js +0 -15
  115. package/src/util/validator/MethodValidator.d.ts +0 -4
  116. package/src/util/validator/MethodValidator.js +0 -31
  117. package/src/util/validator/index.d.ts +0 -2
  118. package/src/util/validator/index.js +0 -3
package/README.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @eggjs/controller-decorator
2
2
 
3
+ [![NPM version][npm-image]][npm-url]
4
+ [![Known Vulnerabilities][snyk-image]][snyk-url]
5
+ [![npm download][download-image]][download-url]
6
+ [![Node.js Version](https://img.shields.io/node/v/@eggjs/controller-decorator.svg?style=flat)](https://nodejs.org/en/download/)
7
+
8
+ [npm-image]: https://img.shields.io/npm/v/@eggjs/controller-decorator.svg?style=flat-square
9
+ [npm-url]: https://npmjs.org/package/@eggjs/controller-decorator
10
+ [snyk-image]: https://snyk.io/test/npm/@eggjs/controller-decorator/badge.svg?style=flat-square
11
+ [snyk-url]: https://snyk.io/test/npm/@eggjs/controller-decorator
12
+ [download-image]: https://img.shields.io/npm/dm/@eggjs/controller-decorator.svg?style=flat-square
13
+ [download-url]: https://npmjs.org/package/@eggjs/controller-decorator
14
+
3
15
  ## Usage
4
16
 
5
17
  Please read [@eggjs/tegg-controller-plugin](../../plugin/controller/README.md)
@@ -0,0 +1,11 @@
1
+ import { ControllerMetaBuilder, ControllerMetaBuilderCreator, ControllerMetadata, ControllerTypeLike, EggProtoImplClass } from "@eggjs/tegg-types";
2
+
3
+ //#region src/builder/ControllerMetaBuilderFactory.d.ts
4
+ declare class ControllerMetaBuilderFactory {
5
+ private static builderCreatorMap;
6
+ static registerControllerMetaBuilder(controllerType: ControllerTypeLike, controllerBuilderCreator: ControllerMetaBuilderCreator): void;
7
+ static createControllerMetaBuilder(clazz: EggProtoImplClass, controllerType?: ControllerTypeLike): ControllerMetaBuilder | undefined;
8
+ static build(clazz: EggProtoImplClass, controllerType?: ControllerTypeLike): ControllerMetadata | undefined;
9
+ }
10
+ //#endregion
11
+ export { ControllerMetaBuilderFactory };
@@ -0,0 +1,37 @@
1
+ import { ControllerInfoUtil } from "../util/ControllerInfoUtil.js";
2
+ import { MethodInfoUtil } from "../util/MethodInfoUtil.js";
3
+ import "../util/index.js";
4
+ import { Pointcut } from "@eggjs/aop-decorator";
5
+
6
+ //#region src/builder/ControllerMetaBuilderFactory.ts
7
+ var ControllerMetaBuilderFactory = class ControllerMetaBuilderFactory {
8
+ static builderCreatorMap = /* @__PURE__ */ new Map();
9
+ static registerControllerMetaBuilder(controllerType, controllerBuilderCreator) {
10
+ this.builderCreatorMap.set(controllerType, controllerBuilderCreator);
11
+ }
12
+ static createControllerMetaBuilder(clazz, controllerType) {
13
+ if (!controllerType) controllerType = ControllerInfoUtil.getControllerType(clazz);
14
+ if (!controllerType) return;
15
+ const creator = this.builderCreatorMap.get(controllerType);
16
+ if (!creator) throw new Error(`not found controller meta builder for type ${controllerType}`);
17
+ return creator(clazz);
18
+ }
19
+ static build(clazz, controllerType) {
20
+ const builder = ControllerMetaBuilderFactory.createControllerMetaBuilder(clazz, controllerType);
21
+ if (!builder) return;
22
+ const metadata = builder.build();
23
+ if (!metadata) return;
24
+ const controllerAopMws = ControllerInfoUtil.getControllerAopMiddlewares(clazz);
25
+ for (const { name } of metadata.methods) {
26
+ const methodAopMws = MethodInfoUtil.getMethodAopMiddlewares(clazz, name);
27
+ if (MethodInfoUtil.shouldRegisterAopMiddlewarePointCut(clazz, name)) {
28
+ for (const mw of [...methodAopMws, ...controllerAopMws].reverse()) Pointcut(mw)(clazz.prototype, name);
29
+ MethodInfoUtil.registerAopMiddlewarePointcut(clazz, name);
30
+ }
31
+ }
32
+ return metadata;
33
+ }
34
+ };
35
+
36
+ //#endregion
37
+ export { ControllerMetaBuilderFactory };
@@ -0,0 +1,3 @@
1
+ import { ControllerMetaBuilderFactory } from "./ControllerMetaBuilderFactory.js";
2
+
3
+ export { };
@@ -0,0 +1,4 @@
1
+ //#region src/decorator/Acl.d.ts
2
+ declare function Acl(code?: string): (target: any, propertyKey?: PropertyKey) => void;
3
+ //#endregion
4
+ export { Acl };
@@ -0,0 +1,24 @@
1
+ import { ControllerInfoUtil } from "../util/ControllerInfoUtil.js";
2
+ import { MethodInfoUtil } from "../util/MethodInfoUtil.js";
3
+ import "../util/index.js";
4
+ import assert from "node:assert";
5
+
6
+ //#region src/decorator/Acl.ts
7
+ function Acl(code) {
8
+ function classAcl(constructor) {
9
+ ControllerInfoUtil.setControllerAcl(code, constructor);
10
+ }
11
+ function methodAcl(target, propertyKey) {
12
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
13
+ const controllerClazz = target.constructor;
14
+ const methodName = propertyKey;
15
+ MethodInfoUtil.setMethodAcl(code, controllerClazz, methodName);
16
+ }
17
+ return function(target, propertyKey) {
18
+ if (propertyKey === void 0) classAcl(target);
19
+ else methodAcl(target, propertyKey);
20
+ };
21
+ }
22
+
23
+ //#endregion
24
+ export { Acl };
@@ -0,0 +1,4 @@
1
+ //#region src/decorator/Context.d.ts
2
+ declare function Context(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
3
+ //#endregion
4
+ export { Context };
@@ -0,0 +1,16 @@
1
+ import { MethodInfoUtil } from "../util/MethodInfoUtil.js";
2
+ import "../util/index.js";
3
+ import assert from "node:assert";
4
+
5
+ //#region src/decorator/Context.ts
6
+ function Context() {
7
+ return function(target, propertyKey, parameterIndex) {
8
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
9
+ const methodName = propertyKey;
10
+ const controllerClazz = target.constructor;
11
+ MethodInfoUtil.setMethodContextIndexInArgs(parameterIndex, controllerClazz, methodName);
12
+ };
13
+ }
14
+
15
+ //#endregion
16
+ export { Context };
@@ -0,0 +1,6 @@
1
+ import { EggProtoImplClass, IAdvice, MiddlewareFunc } from "@eggjs/tegg-types";
2
+
3
+ //#region src/decorator/Middleware.d.ts
4
+ declare function Middleware(...middlewares: Array<MiddlewareFunc> | Array<EggProtoImplClass<IAdvice>>): (target: any, propertyKey?: PropertyKey) => void;
5
+ //#endregion
6
+ export { Middleware };
@@ -0,0 +1,57 @@
1
+ import { ControllerInfoUtil } from "../util/ControllerInfoUtil.js";
2
+ import { MethodInfoUtil } from "../util/MethodInfoUtil.js";
3
+ import "../util/index.js";
4
+ import { AdviceInfoUtil } from "@eggjs/aop-decorator";
5
+ import assert from "node:assert";
6
+ import { isClass } from "is-type-of";
7
+
8
+ //#region src/decorator/Middleware.ts
9
+ var MiddlewareType = /* @__PURE__ */ function(MiddlewareType$1) {
10
+ MiddlewareType$1["AOP"] = "AOP";
11
+ MiddlewareType$1["MiddlewareFunc"] = "MiddlewareFunc";
12
+ return MiddlewareType$1;
13
+ }(MiddlewareType || {});
14
+ function isAop(mw) {
15
+ return isClass(mw) && AdviceInfoUtil.isAdvice(mw);
16
+ }
17
+ function isAopTypeOrMiddlewareType(middlewares) {
18
+ const adviceCount = middlewares.filter((t) => isAop(t)).length;
19
+ if (adviceCount) {
20
+ if (adviceCount === middlewares.length) return MiddlewareType.AOP;
21
+ throw new Error("AOP and MiddlewareFunc can not be mixed");
22
+ }
23
+ return MiddlewareType.MiddlewareFunc;
24
+ }
25
+ function Middleware(...middlewares) {
26
+ function functionTypeClassMiddleware(constructor) {
27
+ middlewares.forEach((mid) => {
28
+ ControllerInfoUtil.addControllerMiddleware(mid, constructor);
29
+ });
30
+ }
31
+ function aopTypeClassMiddleware(constructor) {
32
+ for (const aopAdvice of middlewares) ControllerInfoUtil.addControllerAopMiddleware(aopAdvice, constructor);
33
+ }
34
+ function functionTypeMethodMiddleware(target, propertyKey) {
35
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
36
+ const controllerClazz = target.constructor;
37
+ const methodName = propertyKey;
38
+ middlewares.forEach((mid) => {
39
+ MethodInfoUtil.addMethodMiddleware(mid, controllerClazz, methodName);
40
+ });
41
+ }
42
+ function aopTypeMethodMiddleware(target, propertyKey) {
43
+ const controllerClazz = target.constructor;
44
+ const methodName = propertyKey;
45
+ for (const aopAdvice of middlewares) MethodInfoUtil.addMethodAopMiddleware(aopAdvice, controllerClazz, methodName);
46
+ }
47
+ return function(target, propertyKey) {
48
+ const type = isAopTypeOrMiddlewareType(middlewares);
49
+ if (propertyKey === void 0) if (type === MiddlewareType.AOP) aopTypeClassMiddleware(target);
50
+ else functionTypeClassMiddleware(target);
51
+ else if (type === MiddlewareType.AOP) aopTypeMethodMiddleware(target, propertyKey);
52
+ else functionTypeMethodMiddleware(target, propertyKey);
53
+ };
54
+ }
55
+
56
+ //#endregion
57
+ export { Middleware };
@@ -0,0 +1,6 @@
1
+ import { EggProtoImplClass, HTTPControllerParams } from "@eggjs/tegg-types";
2
+
3
+ //#region src/decorator/http/HTTPController.d.ts
4
+ declare function HTTPController(param?: HTTPControllerParams): (constructor: EggProtoImplClass) => void;
5
+ //#endregion
6
+ export { HTTPController };
@@ -0,0 +1,23 @@
1
+ import { ControllerInfoUtil } from "../../util/ControllerInfoUtil.js";
2
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
3
+ import "../../util/index.js";
4
+ import { AccessLevel, ControllerType } from "@eggjs/tegg-types";
5
+ import { PrototypeUtil, SingletonProto } from "@eggjs/core-decorator";
6
+ import { StackUtil } from "@eggjs/tegg-common-util";
7
+
8
+ //#region src/decorator/http/HTTPController.ts
9
+ function HTTPController(param) {
10
+ return function(constructor) {
11
+ ControllerInfoUtil.setControllerType(constructor, ControllerType.HTTP);
12
+ if (param?.controllerName) ControllerInfoUtil.setControllerName(constructor, param.controllerName);
13
+ if (param?.path) HTTPInfoUtil.setHTTPPath(param.path, constructor);
14
+ SingletonProto({
15
+ accessLevel: AccessLevel.PUBLIC,
16
+ name: param?.protoName
17
+ })(constructor);
18
+ PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5));
19
+ };
20
+ }
21
+
22
+ //#endregion
23
+ export { HTTPController };
@@ -0,0 +1,6 @@
1
+ import { HTTPMethodParams } from "@eggjs/tegg-types";
2
+
3
+ //#region src/decorator/http/HTTPMethod.d.ts
4
+ declare function HTTPMethod(param: HTTPMethodParams): (target: any, propertyKey: PropertyKey) => void;
5
+ //#endregion
6
+ export { HTTPMethod };
@@ -0,0 +1,21 @@
1
+ import { MethodInfoUtil } from "../../util/MethodInfoUtil.js";
2
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
3
+ import "../../util/index.js";
4
+ import { ControllerType } from "@eggjs/tegg-types";
5
+ import assert from "node:assert";
6
+
7
+ //#region src/decorator/http/HTTPMethod.ts
8
+ function HTTPMethod(param) {
9
+ return function(target, propertyKey) {
10
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
11
+ const controllerClazz = target.constructor;
12
+ const methodName = propertyKey;
13
+ MethodInfoUtil.setMethodControllerType(controllerClazz, methodName, ControllerType.HTTP);
14
+ HTTPInfoUtil.setHTTPMethodPath(param.path, controllerClazz, methodName);
15
+ HTTPInfoUtil.setHTTPMethodMethod(param.method, controllerClazz, methodName);
16
+ if (param.priority !== void 0) HTTPInfoUtil.setHTTPMethodPriority(param.priority, controllerClazz, methodName);
17
+ };
18
+ }
19
+
20
+ //#endregion
21
+ export { HTTPMethod };
@@ -0,0 +1,12 @@
1
+ import { HTTPParamParams, HTTPQueriesParams, HTTPQueryParams } from "@eggjs/tegg-types";
2
+
3
+ //#region src/decorator/http/HTTPParam.d.ts
4
+ declare function HTTPBody(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
5
+ declare function HTTPHeaders(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
6
+ declare function HTTPQuery(param?: HTTPQueryParams): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
7
+ declare function HTTPQueries(param?: HTTPQueriesParams): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
8
+ declare function HTTPParam(param?: HTTPParamParams): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
9
+ declare function Request(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
10
+ declare function Cookies(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
11
+ //#endregion
12
+ export { Cookies, HTTPBody, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, Request };
@@ -0,0 +1,77 @@
1
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
2
+ import "../../util/index.js";
3
+ import { HTTPParamType } from "@eggjs/tegg-types";
4
+ import { ObjectUtils } from "@eggjs/tegg-common-util";
5
+ import assert from "node:assert";
6
+
7
+ //#region src/decorator/http/HTTPParam.ts
8
+ function HTTPBody() {
9
+ return function(target, propertyKey, parameterIndex) {
10
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
11
+ const methodName = propertyKey;
12
+ const controllerClazz = target.constructor;
13
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.BODY, parameterIndex, controllerClazz, methodName);
14
+ };
15
+ }
16
+ function HTTPHeaders() {
17
+ return function(target, propertyKey, parameterIndex) {
18
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
19
+ const methodName = propertyKey;
20
+ const controllerClazz = target.constructor;
21
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.HEADERS, parameterIndex, controllerClazz, methodName);
22
+ };
23
+ }
24
+ function HTTPQuery(param) {
25
+ return function(target, propertyKey, parameterIndex) {
26
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
27
+ const methodName = propertyKey;
28
+ const controllerClazz = target.constructor;
29
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
30
+ const name = param?.name || argNames[parameterIndex];
31
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.QUERY, parameterIndex, controllerClazz, methodName);
32
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
33
+ };
34
+ }
35
+ function HTTPQueries(param) {
36
+ return function(target, propertyKey, parameterIndex) {
37
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
38
+ const methodName = propertyKey;
39
+ const controllerClazz = target.constructor;
40
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
41
+ const name = param?.name || argNames[parameterIndex];
42
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.QUERIES, parameterIndex, controllerClazz, methodName);
43
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
44
+ };
45
+ }
46
+ function HTTPParam(param) {
47
+ return function(target, propertyKey, parameterIndex) {
48
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
49
+ const methodName = propertyKey;
50
+ const controllerClazz = target.constructor;
51
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
52
+ const name = param?.name || argNames[parameterIndex];
53
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.PARAM, parameterIndex, controllerClazz, methodName);
54
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
55
+ };
56
+ }
57
+ function Request() {
58
+ return function(target, propertyKey, parameterIndex) {
59
+ const [nodeMajor] = process.versions.node.split(".").map((v) => Number(v));
60
+ assert(nodeMajor >= 16, `[controller/${target.name}] expect node version >=16, but now is ${nodeMajor}`);
61
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
62
+ const methodName = propertyKey;
63
+ const controllerClazz = target.constructor;
64
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.REQUEST, parameterIndex, controllerClazz, methodName);
65
+ };
66
+ }
67
+ function Cookies() {
68
+ return function(target, propertyKey, parameterIndex) {
69
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
70
+ const methodName = propertyKey;
71
+ const controllerClazz = target.constructor;
72
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.COOKIES, parameterIndex, controllerClazz, methodName);
73
+ };
74
+ }
75
+
76
+ //#endregion
77
+ export { Cookies, HTTPBody, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, Request };
@@ -0,0 +1,6 @@
1
+ import { HostType } from "@eggjs/tegg-types";
2
+
3
+ //#region src/decorator/http/Host.d.ts
4
+ declare function Host(host: HostType): (target: any, propertyKey?: PropertyKey) => void;
5
+ //#endregion
6
+ export { Host };
@@ -0,0 +1,27 @@
1
+ import { ControllerInfoUtil } from "../../util/ControllerInfoUtil.js";
2
+ import { MethodInfoUtil } from "../../util/MethodInfoUtil.js";
3
+ import "../../util/index.js";
4
+ import assert from "node:assert";
5
+
6
+ //#region src/decorator/http/Host.ts
7
+ function Host(host) {
8
+ function parseHost() {
9
+ return Array.isArray(host) ? host : [host];
10
+ }
11
+ function classHost(constructor) {
12
+ ControllerInfoUtil.addControllerHosts(parseHost(), constructor);
13
+ }
14
+ function methodHost(target, propertyKey) {
15
+ assert(typeof propertyKey === "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
16
+ const controllerClazz = target.constructor;
17
+ const methodName = propertyKey;
18
+ MethodInfoUtil.setMethodHosts(parseHost(), controllerClazz, methodName);
19
+ }
20
+ return function(target, propertyKey) {
21
+ if (propertyKey === void 0) classHost(target);
22
+ else methodHost(target, propertyKey);
23
+ };
24
+ }
25
+
26
+ //#endregion
27
+ export { Host };
@@ -0,0 +1,6 @@
1
+ import { Host } from "./Host.js";
2
+ import { HTTPController } from "./HTTPController.js";
3
+ import { HTTPMethod } from "./HTTPMethod.js";
4
+ import { Cookies, HTTPBody, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, Request } from "./HTTPParam.js";
5
+
6
+ export { };
@@ -0,0 +1,10 @@
1
+ import { Host } from "./http/Host.js";
2
+ import { HTTPController } from "./http/HTTPController.js";
3
+ import { HTTPMethod } from "./http/HTTPMethod.js";
4
+ import { Cookies, HTTPBody, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, Request } from "./http/HTTPParam.js";
5
+ import "./http/index.js";
6
+ import { Acl } from "./Acl.js";
7
+ import { Context } from "./Context.js";
8
+ import { Middleware } from "./Middleware.js";
9
+
10
+ export { };
@@ -0,0 +1,13 @@
1
+ import { HTTPControllerMeta } from "../../model/HTTPControllerMeta.js";
2
+ import { EggProtoImplClass } from "@eggjs/tegg-types";
3
+
4
+ //#region src/impl/http/HTTPControllerMetaBuilder.d.ts
5
+ declare class HTTPControllerMetaBuilder {
6
+ private readonly clazz;
7
+ constructor(clazz: EggProtoImplClass);
8
+ private buildMethod;
9
+ build(): HTTPControllerMeta;
10
+ static create(clazz: EggProtoImplClass): HTTPControllerMetaBuilder;
11
+ }
12
+ //#endregion
13
+ export { HTTPControllerMetaBuilder };
@@ -0,0 +1,63 @@
1
+ import { ControllerInfoUtil } from "../../util/ControllerInfoUtil.js";
2
+ import { ControllerValidator } from "../../util/validator/ControllerValidator.js";
3
+ import { ControllerMetadataUtil } from "../../util/ControllerMetadataUtil.js";
4
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
5
+ import "../../util/index.js";
6
+ import { ControllerMetaBuilderFactory } from "../../builder/ControllerMetaBuilderFactory.js";
7
+ import "../../builder/index.js";
8
+ import { HTTPControllerMeta } from "../../model/HTTPControllerMeta.js";
9
+ import "../../model/index.js";
10
+ import { HTTPControllerMethodMetaBuilder } from "./HTTPControllerMethodMetaBuilder.js";
11
+ import { ClassUtil } from "@eggjs/tegg-metadata";
12
+ import { ControllerType } from "@eggjs/tegg-types";
13
+ import { PrototypeUtil } from "@eggjs/core-decorator";
14
+ import { ObjectUtils } from "@eggjs/tegg-common-util";
15
+ import assert from "node:assert";
16
+
17
+ //#region src/impl/http/HTTPControllerMetaBuilder.ts
18
+ var HTTPControllerMetaBuilder = class HTTPControllerMetaBuilder {
19
+ clazz;
20
+ constructor(clazz) {
21
+ this.clazz = clazz;
22
+ }
23
+ buildMethod() {
24
+ const methodNames = ObjectUtils.getProperties(this.clazz.prototype);
25
+ const methods = [];
26
+ for (const methodName of methodNames) {
27
+ const methodMeta = new HTTPControllerMethodMetaBuilder(this.clazz, methodName).build();
28
+ if (methodMeta) methods.push(methodMeta);
29
+ }
30
+ return methods;
31
+ }
32
+ build() {
33
+ ControllerValidator.validate(this.clazz);
34
+ const controllerType = ControllerInfoUtil.getControllerType(this.clazz);
35
+ assert.equal(controllerType, ControllerType.HTTP, "invalidate controller type");
36
+ const httpPath = HTTPInfoUtil.getHTTPPath(this.clazz);
37
+ const httpMiddlewares = ControllerInfoUtil.getControllerMiddlewares(this.clazz);
38
+ const methods = this.buildMethod();
39
+ const clazzName = this.clazz.name;
40
+ const controllerName = ControllerInfoUtil.getControllerName(this.clazz) || clazzName;
41
+ const protoName = PrototypeUtil.getProperty(this.clazz).name;
42
+ const needAcl = ControllerInfoUtil.hasControllerAcl(this.clazz);
43
+ const aclCode = ControllerInfoUtil.getControllerAcl(this.clazz);
44
+ const hosts = ControllerInfoUtil.getControllerHosts(this.clazz);
45
+ const metadata = new HTTPControllerMeta(clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode, hosts);
46
+ ControllerMetadataUtil.setControllerMetadata(this.clazz, metadata);
47
+ for (const method of metadata.methods) {
48
+ const realPath = metadata.getMethodRealPath(method);
49
+ if (!realPath.startsWith("/")) {
50
+ const desc = ClassUtil.classDescription(this.clazz);
51
+ throw new Error(`class ${desc} method ${method.name} path ${realPath} not start with /`);
52
+ }
53
+ }
54
+ return metadata;
55
+ }
56
+ static create(clazz) {
57
+ return new HTTPControllerMetaBuilder(clazz);
58
+ }
59
+ };
60
+ ControllerMetaBuilderFactory.registerControllerMetaBuilder(ControllerType.HTTP, HTTPControllerMetaBuilder.create);
61
+
62
+ //#endregion
63
+ export { HTTPControllerMetaBuilder };
@@ -0,0 +1,15 @@
1
+ import { HTTPMethodMeta } from "../../model/HTTPMethodMeta.js";
2
+ import { EggProtoImplClass } from "@eggjs/tegg-types";
3
+
4
+ //#region src/impl/http/HTTPControllerMethodMetaBuilder.d.ts
5
+ declare class HTTPControllerMethodMetaBuilder {
6
+ private readonly clazz;
7
+ private readonly methodName;
8
+ constructor(clazz: EggProtoImplClass, methodName: string);
9
+ private checkParamDecorators;
10
+ private buildParamType;
11
+ getPriority(): number;
12
+ build(): HTTPMethodMeta | undefined;
13
+ }
14
+ //#endregion
15
+ export { HTTPControllerMethodMetaBuilder };
@@ -0,0 +1,80 @@
1
+ import { MethodInfoUtil } from "../../util/MethodInfoUtil.js";
2
+ import { MethodValidator } from "../../util/validator/MethodValidator.js";
3
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
4
+ import { HTTPPriorityUtil } from "../../util/HTTPPriorityUtil.js";
5
+ import "../../util/index.js";
6
+ import { HTTPMethodMeta, ParamMetaUtil } from "../../model/HTTPMethodMeta.js";
7
+ import "../../model/index.js";
8
+ import { ClassUtil } from "@eggjs/tegg-metadata";
9
+ import path from "node:path";
10
+
11
+ //#region src/impl/http/HTTPControllerMethodMetaBuilder.ts
12
+ var HTTPControllerMethodMetaBuilder = class {
13
+ clazz;
14
+ methodName;
15
+ constructor(clazz, methodName) {
16
+ this.clazz = clazz;
17
+ this.methodName = methodName;
18
+ }
19
+ checkParamDecorators() {
20
+ const functionLength = this.clazz.prototype[this.methodName].length;
21
+ const paramIndexList = HTTPInfoUtil.getParamIndexList(this.clazz, this.methodName);
22
+ const contextIndex = MethodInfoUtil.getMethodContextIndex(this.clazz, this.methodName);
23
+ const hasAnnotationParamCount = typeof contextIndex === "undefined" ? paramIndexList.length : paramIndexList.length + 1;
24
+ const maxParamCount = Math.max(functionLength, hasAnnotationParamCount);
25
+ for (let i = 0; i < maxParamCount; ++i) {
26
+ if (i === contextIndex) continue;
27
+ if (!HTTPInfoUtil.getHTTPMethodParamType(i, this.clazz, this.methodName)) {
28
+ const classDesc = ClassUtil.classDescription(this.clazz);
29
+ throw new Error(`${classDesc}:${this.methodName} param ${i} has no http param type, Please add @HTTPBody, @HTTPParam, @HTTPQuery, @HTTPQueries`);
30
+ }
31
+ }
32
+ }
33
+ buildParamType(httpPath) {
34
+ this.checkParamDecorators();
35
+ const paramTypeMap = /* @__PURE__ */ new Map();
36
+ const paramIndexList = HTTPInfoUtil.getParamIndexList(this.clazz, this.methodName);
37
+ for (const paramIndex of paramIndexList) {
38
+ const paramType = HTTPInfoUtil.getHTTPMethodParamType(paramIndex, this.clazz, this.methodName);
39
+ const paramName = HTTPInfoUtil.getHTTPMethodParamName(paramIndex, this.clazz, this.methodName);
40
+ const paramMeta = ParamMetaUtil.createParam(paramType, paramName);
41
+ try {
42
+ paramMeta.validate(httpPath);
43
+ } catch (e) {
44
+ e.message = `build controller ${ClassUtil.classDescription(this.clazz)} method ${this.methodName} param ${paramName} failed: ${e.message}`;
45
+ throw e;
46
+ }
47
+ paramTypeMap.set(paramIndex, paramMeta);
48
+ }
49
+ return paramTypeMap;
50
+ }
51
+ getPriority() {
52
+ const priority = HTTPInfoUtil.getHTTPMethodPriority(this.clazz, this.methodName);
53
+ if (priority !== void 0) return priority;
54
+ const controllerPath = HTTPInfoUtil.getHTTPPath(this.clazz);
55
+ const methodPath = HTTPInfoUtil.getHTTPMethodPath(this.clazz, this.methodName);
56
+ const realPath = controllerPath ? path.posix.join(controllerPath, methodPath) : methodPath;
57
+ const defaultPriority = HTTPPriorityUtil.calcPathPriority(realPath);
58
+ if (defaultPriority > HTTPPriorityUtil.DEFAULT_PRIORITY) throw new Error(`path ${realPath} is too long, should set priority manually`);
59
+ return defaultPriority;
60
+ }
61
+ build() {
62
+ MethodValidator.validate(this.clazz, this.methodName);
63
+ if (!MethodInfoUtil.getMethodControllerType(this.clazz, this.methodName)) return;
64
+ const httpMethod = HTTPInfoUtil.getHTTPMethodMethod(this.clazz, this.methodName);
65
+ const parentPath = HTTPInfoUtil.getHTTPPath(this.clazz);
66
+ const httpPath = HTTPInfoUtil.getHTTPMethodPath(this.clazz, this.methodName);
67
+ const contextIndex = MethodInfoUtil.getMethodContextIndex(this.clazz, this.methodName);
68
+ const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName);
69
+ const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName);
70
+ const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName);
71
+ const hosts = MethodInfoUtil.getMethodHosts(this.clazz, this.methodName);
72
+ const realPath = parentPath ? path.posix.join(parentPath, httpPath) : httpPath;
73
+ const paramTypeMap = this.buildParamType(realPath);
74
+ const priority = this.getPriority();
75
+ return new HTTPMethodMeta(this.methodName, httpPath, httpMethod, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, hosts);
76
+ }
77
+ };
78
+
79
+ //#endregion
80
+ export { HTTPControllerMethodMetaBuilder };
@@ -0,0 +1,4 @@
1
+ import { HTTPControllerMethodMetaBuilder } from "./HTTPControllerMethodMetaBuilder.js";
2
+ import { HTTPControllerMetaBuilder } from "./HTTPControllerMetaBuilder.js";
3
+
4
+ export { };
@@ -0,0 +1,5 @@
1
+ import { HTTPControllerMethodMetaBuilder } from "./http/HTTPControllerMethodMetaBuilder.js";
2
+ import { HTTPControllerMetaBuilder } from "./http/HTTPControllerMetaBuilder.js";
3
+ import "./http/index.js";
4
+
5
+ export { };
@@ -0,0 +1,24 @@
1
+ import { ControllerMetaBuilderFactory } from "./builder/ControllerMetaBuilderFactory.js";
2
+ import { Host } from "./decorator/http/Host.js";
3
+ import { HTTPController } from "./decorator/http/HTTPController.js";
4
+ import { HTTPMethod } from "./decorator/http/HTTPMethod.js";
5
+ import { Cookies, HTTPBody, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, Request } from "./decorator/http/HTTPParam.js";
6
+ import { Acl } from "./decorator/Acl.js";
7
+ import { Context } from "./decorator/Context.js";
8
+ import { Middleware } from "./decorator/Middleware.js";
9
+ import { BodyParamMeta, CookiesParamMeta, HTTPMethodMeta, HeadersParamMeta, ParamMeta, ParamMetaUtil, PathParamMeta, QueriesParamMeta, QueryParamMeta, RequestParamMeta } from "./model/HTTPMethodMeta.js";
10
+ import { HTTPControllerMeta } from "./model/HTTPControllerMeta.js";
11
+ import { HTTPCookies } from "./model/HTTPCookies.js";
12
+ import { HTTPRequest } from "./model/HTTPRequest.js";
13
+ import { HTTPResponse } from "./model/HTTPResponse.js";
14
+ import { HTTPControllerMetaBuilder } from "./impl/http/HTTPControllerMetaBuilder.js";
15
+ import { HTTPControllerMethodMetaBuilder } from "./impl/http/HTTPControllerMethodMetaBuilder.js";
16
+ import { ControllerValidator } from "./util/validator/ControllerValidator.js";
17
+ import { MethodValidator } from "./util/validator/MethodValidator.js";
18
+ import { ControllerInfoUtil } from "./util/ControllerInfoUtil.js";
19
+ import { ControllerMetadataUtil } from "./util/ControllerMetadataUtil.js";
20
+ import { HTTPInfoUtil } from "./util/HTTPInfoUtil.js";
21
+ import { HTTPPriorityUtil } from "./util/HTTPPriorityUtil.js";
22
+ import { MethodInfoUtil } from "./util/MethodInfoUtil.js";
23
+ export * from "@eggjs/tegg-types/controller-decorator";
24
+ export { Acl, BodyParamMeta, Context, ControllerInfoUtil, ControllerMetaBuilderFactory, ControllerMetadataUtil, ControllerValidator, Cookies, CookiesParamMeta, HTTPBody, HTTPController, HTTPControllerMeta, HTTPControllerMetaBuilder, HTTPControllerMethodMetaBuilder, HTTPCookies, HTTPHeaders, HTTPInfoUtil, HTTPMethod, HTTPMethodMeta, HTTPParam, HTTPPriorityUtil, HTTPQueries, HTTPQuery, HTTPRequest, HTTPResponse, HeadersParamMeta, Host, MethodInfoUtil, MethodValidator, Middleware, ParamMeta, ParamMetaUtil, PathParamMeta, QueriesParamMeta, QueryParamMeta, Request, RequestParamMeta };