@eggjs/controller-decorator 4.0.0-beta.8 → 4.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +1 -1
  2. package/dist/builder/ControllerMetaBuilderFactory.d.ts +11 -0
  3. package/dist/builder/ControllerMetaBuilderFactory.js +37 -0
  4. package/dist/builder/index.d.ts +1 -0
  5. package/dist/builder/index.js +3 -0
  6. package/dist/decorator/Acl.d.ts +4 -0
  7. package/dist/decorator/Acl.js +24 -0
  8. package/dist/decorator/Context.d.ts +4 -0
  9. package/dist/decorator/Context.js +16 -0
  10. package/dist/decorator/Middleware.d.ts +6 -0
  11. package/dist/decorator/Middleware.js +56 -0
  12. package/dist/decorator/http/HTTPController.d.ts +6 -0
  13. package/dist/decorator/http/HTTPController.js +24 -0
  14. package/dist/decorator/http/HTTPMethod.d.ts +6 -0
  15. package/dist/decorator/http/HTTPMethod.js +22 -0
  16. package/dist/decorator/http/HTTPParam.d.ts +159 -0
  17. package/dist/decorator/http/HTTPParam.js +223 -0
  18. package/dist/decorator/http/Host.d.ts +6 -0
  19. package/dist/decorator/http/Host.js +27 -0
  20. package/dist/decorator/http/index.d.ts +5 -0
  21. package/dist/decorator/http/index.js +7 -0
  22. package/dist/decorator/index.d.ts +14 -0
  23. package/dist/decorator/index.js +16 -0
  24. package/dist/decorator/mcp/Extra.d.ts +4 -0
  25. package/dist/decorator/mcp/Extra.js +13 -0
  26. package/dist/decorator/mcp/MCPController.d.ts +6 -0
  27. package/dist/decorator/mcp/MCPController.js +24 -0
  28. package/dist/decorator/mcp/MCPPrompt.d.ts +8 -0
  29. package/dist/decorator/mcp/MCPPrompt.js +31 -0
  30. package/dist/decorator/mcp/MCPResource.d.ts +6 -0
  31. package/dist/decorator/mcp/MCPResource.js +21 -0
  32. package/dist/decorator/mcp/MCPTool.d.ts +8 -0
  33. package/dist/decorator/mcp/MCPTool.js +31 -0
  34. package/dist/decorator/mcp/index.d.ts +5 -0
  35. package/dist/decorator/mcp/index.js +7 -0
  36. package/dist/impl/http/HTTPControllerMetaBuilder.d.ts +14 -0
  37. package/dist/impl/http/HTTPControllerMetaBuilder.js +60 -0
  38. package/dist/impl/http/HTTPControllerMethodMetaBuilder.d.ts +16 -0
  39. package/dist/impl/http/HTTPControllerMethodMetaBuilder.js +81 -0
  40. package/dist/impl/http/index.d.ts +2 -0
  41. package/dist/impl/http/index.js +4 -0
  42. package/dist/impl/index.d.ts +8 -0
  43. package/dist/impl/index.js +10 -0
  44. package/dist/impl/mcp/MCPControllerMetaBuilder.d.ts +15 -0
  45. package/dist/impl/mcp/MCPControllerMetaBuilder.js +67 -0
  46. package/dist/impl/mcp/MCPControllerPromptMetaBuilder.d.ts +13 -0
  47. package/dist/impl/mcp/MCPControllerPromptMetaBuilder.js +37 -0
  48. package/dist/impl/mcp/MCPControllerResourceMetaBuilder.d.ts +13 -0
  49. package/dist/impl/mcp/MCPControllerResourceMetaBuilder.js +35 -0
  50. package/dist/impl/mcp/MCPControllerToolMetaBuilder.d.ts +13 -0
  51. package/dist/impl/mcp/MCPControllerToolMetaBuilder.js +37 -0
  52. package/dist/impl/mcp/index.d.ts +4 -0
  53. package/dist/impl/mcp/index.js +6 -0
  54. package/dist/index.d.ts +41 -247
  55. package/dist/index.js +41 -749
  56. package/dist/model/HTTPControllerMeta.d.ts +27 -0
  57. package/dist/model/HTTPControllerMeta.js +58 -0
  58. package/dist/model/HTTPCookies.d.ts +2 -0
  59. package/dist/model/HTTPCookies.js +3 -0
  60. package/dist/model/HTTPMethodMeta.d.ts +60 -0
  61. package/dist/model/HTTPMethodMeta.js +104 -0
  62. package/dist/model/HTTPResponse.d.ts +7 -0
  63. package/dist/model/HTTPResponse.js +6 -0
  64. package/dist/model/MCPControllerMeta.d.ts +29 -0
  65. package/dist/model/MCPControllerMeta.js +51 -0
  66. package/dist/model/MCPPromptMeta.d.ts +28 -0
  67. package/dist/model/MCPPromptMeta.js +26 -0
  68. package/dist/model/MCPResourceMeta.d.ts +28 -0
  69. package/dist/model/MCPResourceMeta.js +28 -0
  70. package/dist/model/MCPToolMeta.d.ts +26 -0
  71. package/dist/model/MCPToolMeta.js +24 -0
  72. package/dist/model/index.d.ts +8 -0
  73. package/dist/model/index.js +10 -0
  74. package/dist/util/ControllerInfoUtil.d.ts +22 -0
  75. package/dist/util/ControllerInfoUtil.js +54 -0
  76. package/dist/util/ControllerMetadataUtil.d.ts +9 -0
  77. package/dist/util/ControllerMetadataUtil.js +21 -0
  78. package/dist/util/HTTPInfoUtil.d.ts +20 -0
  79. package/dist/util/HTTPInfoUtil.js +53 -0
  80. package/dist/util/HTTPPriorityUtil.d.ts +23 -0
  81. package/dist/util/HTTPPriorityUtil.js +39 -0
  82. package/dist/util/MCPInfoUtil.d.ts +52 -0
  83. package/dist/util/MCPInfoUtil.js +87 -0
  84. package/dist/util/MethodInfoUtil.d.ts +25 -0
  85. package/dist/util/MethodInfoUtil.js +68 -0
  86. package/dist/util/index.d.ts +9 -0
  87. package/dist/util/index.js +11 -0
  88. package/dist/util/validator/ControllerValidator.d.ts +8 -0
  89. package/dist/util/validator/ControllerValidator.js +16 -0
  90. package/dist/util/validator/MethodValidator.d.ts +8 -0
  91. package/dist/util/validator/MethodValidator.js +31 -0
  92. package/dist/util/validator/index.d.ts +2 -0
  93. package/dist/util/validator/index.js +4 -0
  94. package/package.json +36 -39
package/README.md CHANGED
@@ -14,4 +14,4 @@
14
14
 
15
15
  ## Usage
16
16
 
17
- Please read [@eggjs/tegg-controller-plugin](../../plugin/controller/README.md)
17
+ Please read [@eggjs/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 @@
1
+ import { ControllerMetaBuilderFactory } from "./ControllerMetaBuilderFactory.js";
@@ -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 InjectContext(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
3
+ //#endregion
4
+ export { InjectContext };
@@ -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 InjectContext() {
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 { InjectContext };
@@ -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,56 @@
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
+ const MiddlewareType = {
10
+ AOP: "AOP",
11
+ MiddlewareFunc: "MiddlewareFunc"
12
+ };
13
+ function isAop(mw) {
14
+ return isClass(mw) && AdviceInfoUtil.isAdvice(mw);
15
+ }
16
+ function isAopTypeOrMiddlewareType(middlewares) {
17
+ const adviceCount = middlewares.filter((t) => isAop(t)).length;
18
+ if (adviceCount) {
19
+ if (adviceCount === middlewares.length) return MiddlewareType.AOP;
20
+ throw new Error("AOP and MiddlewareFunc can not be mixed");
21
+ }
22
+ return MiddlewareType.MiddlewareFunc;
23
+ }
24
+ function Middleware(...middlewares) {
25
+ function functionTypeClassMiddleware(constructor) {
26
+ middlewares.forEach((mid) => {
27
+ ControllerInfoUtil.addControllerMiddleware(mid, constructor);
28
+ });
29
+ }
30
+ function aopTypeClassMiddleware(constructor) {
31
+ for (const aopAdvice of middlewares) ControllerInfoUtil.addControllerAopMiddleware(aopAdvice, constructor);
32
+ }
33
+ function functionTypeMethodMiddleware(target, propertyKey) {
34
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
35
+ const controllerClazz = target.constructor;
36
+ const methodName = propertyKey;
37
+ middlewares.forEach((mid) => {
38
+ MethodInfoUtil.addMethodMiddleware(mid, controllerClazz, methodName);
39
+ });
40
+ }
41
+ function aopTypeMethodMiddleware(target, propertyKey) {
42
+ const controllerClazz = target.constructor;
43
+ const methodName = propertyKey;
44
+ for (const aopAdvice of middlewares) MethodInfoUtil.addMethodAopMiddleware(aopAdvice, controllerClazz, methodName);
45
+ }
46
+ return function(target, propertyKey) {
47
+ const type = isAopTypeOrMiddlewareType(middlewares);
48
+ if (propertyKey === void 0) if (type === MiddlewareType.AOP) aopTypeClassMiddleware(target);
49
+ else functionTypeClassMiddleware(target);
50
+ else if (type === MiddlewareType.AOP) aopTypeMethodMiddleware(target, propertyKey);
51
+ else functionTypeMethodMiddleware(target, propertyKey);
52
+ };
53
+ }
54
+
55
+ //#endregion
56
+ 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,24 @@
1
+ import { ControllerInfoUtil } from "../../util/ControllerInfoUtil.js";
2
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
3
+ import "../../util/index.js";
4
+ import { PrototypeUtil, SingletonProto } from "@eggjs/core-decorator";
5
+ import { AccessLevel, ControllerType } from "@eggjs/tegg-types";
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
+ if (param?.timeout) ControllerInfoUtil.setControllerTimeout(param.timeout, constructor);
15
+ SingletonProto({
16
+ accessLevel: AccessLevel.PUBLIC,
17
+ name: param?.protoName
18
+ })(constructor);
19
+ PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5));
20
+ };
21
+ }
22
+
23
+ //#endregion
24
+ 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,22 @@
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.timeout) MethodInfoUtil.setMethodTimeout(param.timeout, controllerClazz, methodName);
17
+ if (param.priority !== void 0) HTTPInfoUtil.setHTTPMethodPriority(param.priority, controllerClazz, methodName);
18
+ };
19
+ }
20
+
21
+ //#endregion
22
+ export { HTTPMethod };
@@ -0,0 +1,159 @@
1
+ import { InjectContext } from "../Context.js";
2
+ import { HTTPParamParams, HTTPQueriesParams, HTTPQueryParams } from "@eggjs/tegg-types";
3
+
4
+ //#region src/decorator/http/HTTPParam.d.ts
5
+
6
+ /**
7
+ * Inject the request body.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPBody } from 'egg';
12
+ *
13
+ * @HTTPController()
14
+ * export class FooController {
15
+ * @HTTPMethod({
16
+ * path: '/foo',
17
+ * method: HTTPMethodEnum.GET,
18
+ * })
19
+ * // POST /foo -H 'Content-Type: application/json' -d '{"foo": "bar"}'
20
+ * // body = { "foo": "bar" }
21
+ * async bar(@HTTPBody() body: any): Promise<void> {
22
+ * console.log(body);
23
+ * }
24
+ * }
25
+ * ```
26
+ */
27
+ declare function HTTPBody(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
28
+ /**
29
+ * Inject the request headers.
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPHeaders, type IncomingHttpHeaders } from 'egg';
34
+ *
35
+ * @HTTPController()
36
+ * export class FooController {
37
+ * @HTTPMethod({
38
+ * path: '/foo',
39
+ * method: HTTPMethodEnum.GET,
40
+ * })
41
+ * // GET /foo -H 'X-Custom: custom'
42
+ * // headers['x-custom'] = 'custom'
43
+ * async bar(@HTTPHeaders() headers: IncomingHttpHeaders): Promise<void> {
44
+ * console.log(headers);
45
+ * }
46
+ * }
47
+ * ```
48
+ */
49
+ declare function HTTPHeaders(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
50
+ /**
51
+ * Inject the request query string, the value is string type.
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPQuery } from 'egg';
56
+ *
57
+ * @HTTPController()
58
+ * export class FooController {
59
+ * @HTTPMethod({
60
+ * path: '/foo',
61
+ * method: HTTPMethodEnum.GET,
62
+ * })
63
+ * // GET /foo?user=asd
64
+ * // user = 'asd'
65
+ * async bar(@HTTPQuery() user?: string): Promise<void> {
66
+ * console.log(user);
67
+ * }
68
+ * }
69
+ * ```
70
+ */
71
+ declare function HTTPQuery(param?: HTTPQueryParams): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
72
+ /**
73
+ * Inject the request query strings, all value are Array type.
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPQueries } from 'egg';
78
+ *
79
+ * @HTTPController()
80
+ * export class FooController {
81
+ * @HTTPMethod({
82
+ * path: '/foo',
83
+ * method: HTTPMethodEnum.GET,
84
+ * })
85
+ * // GET /foo?user=asd&user=fgh
86
+ * // user = ['asd', 'fgh']
87
+ * async bar(@HTTPQueries({ name: 'user' }) users?: string[]): Promise<void> {
88
+ * console.log(users);
89
+ * }
90
+ * }
91
+ * ```
92
+ */
93
+ declare function HTTPQueries(param?: HTTPQueriesParams): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
94
+ /**
95
+ * Inject the request path parameter, the value is string type.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPParam } from 'egg';
100
+ *
101
+ * @HTTPController()
102
+ * export class FooController {
103
+ * @HTTPMethod({
104
+ * path: '/foo/:id',
105
+ * method: HTTPMethodEnum.GET,
106
+ * })
107
+ * // GET /foo/123
108
+ * // id = '123'
109
+ * async bar(@HTTPParam() id: string): Promise<void> {
110
+ * console.log(id);
111
+ * }
112
+ * }
113
+ * ```
114
+ */
115
+ declare function HTTPParam(param?: HTTPParamParams): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
116
+ /**
117
+ * Inject the request object.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPRequest } from 'egg';
122
+ *
123
+ * @HTTPController()
124
+ * export class FooController {
125
+ * @HTTPMethod({
126
+ * path: '/foo',
127
+ * method: HTTPMethodEnum.GET,
128
+ * })
129
+ * async bar(@HTTPRequest() request: Request): Promise<void> {
130
+ * console.log(request);
131
+ * }
132
+ * }
133
+ * ```
134
+ */
135
+ declare function HTTPRequest(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
136
+ /**
137
+ * Inject the request cookies.
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPCookies, type Cookies } from 'egg';
142
+ *
143
+ * @HTTPController()
144
+ * export class FooController {
145
+ * @HTTPMethod({
146
+ * path: '/foo',
147
+ * method: HTTPMethodEnum.GET,
148
+ * })
149
+ * // GET /foo -H 'Cookie: foo=bar; bar=baz'
150
+ * // cookies = cookies
151
+ * async bar(@HTTPCookies() cookies: Cookies): Promise<void> {
152
+ * console.log(cookies);
153
+ * }
154
+ * }
155
+ * ```
156
+ */
157
+ declare function HTTPCookies(): (target: any, propertyKey: PropertyKey, parameterIndex: number) => void;
158
+ //#endregion
159
+ export { HTTPBody, HTTPCookies, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, HTTPRequest };
@@ -0,0 +1,223 @@
1
+ import { HTTPInfoUtil } from "../../util/HTTPInfoUtil.js";
2
+ import "../../util/index.js";
3
+ import { InjectContext } from "../Context.js";
4
+ import { HTTPParamType } from "@eggjs/tegg-types";
5
+ import { ObjectUtils } from "@eggjs/tegg-common-util";
6
+ import assert from "node:assert";
7
+
8
+ //#region src/decorator/http/HTTPParam.ts
9
+ /**
10
+ * Inject the request body.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPBody } from 'egg';
15
+ *
16
+ * @HTTPController()
17
+ * export class FooController {
18
+ * @HTTPMethod({
19
+ * path: '/foo',
20
+ * method: HTTPMethodEnum.GET,
21
+ * })
22
+ * // POST /foo -H 'Content-Type: application/json' -d '{"foo": "bar"}'
23
+ * // body = { "foo": "bar" }
24
+ * async bar(@HTTPBody() body: any): Promise<void> {
25
+ * console.log(body);
26
+ * }
27
+ * }
28
+ * ```
29
+ */
30
+ function HTTPBody() {
31
+ return function(target, propertyKey, parameterIndex) {
32
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
33
+ const methodName = propertyKey;
34
+ const controllerClazz = target.constructor;
35
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.BODY, parameterIndex, controllerClazz, methodName);
36
+ };
37
+ }
38
+ /**
39
+ * Inject the request headers.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPHeaders, type IncomingHttpHeaders } from 'egg';
44
+ *
45
+ * @HTTPController()
46
+ * export class FooController {
47
+ * @HTTPMethod({
48
+ * path: '/foo',
49
+ * method: HTTPMethodEnum.GET,
50
+ * })
51
+ * // GET /foo -H 'X-Custom: custom'
52
+ * // headers['x-custom'] = 'custom'
53
+ * async bar(@HTTPHeaders() headers: IncomingHttpHeaders): Promise<void> {
54
+ * console.log(headers);
55
+ * }
56
+ * }
57
+ * ```
58
+ */
59
+ function HTTPHeaders() {
60
+ return function(target, propertyKey, parameterIndex) {
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.HEADERS, parameterIndex, controllerClazz, methodName);
65
+ };
66
+ }
67
+ /**
68
+ * Inject the request query string, the value is string type.
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPQuery } from 'egg';
73
+ *
74
+ * @HTTPController()
75
+ * export class FooController {
76
+ * @HTTPMethod({
77
+ * path: '/foo',
78
+ * method: HTTPMethodEnum.GET,
79
+ * })
80
+ * // GET /foo?user=asd
81
+ * // user = 'asd'
82
+ * async bar(@HTTPQuery() user?: string): Promise<void> {
83
+ * console.log(user);
84
+ * }
85
+ * }
86
+ * ```
87
+ */
88
+ function HTTPQuery(param) {
89
+ return function(target, propertyKey, parameterIndex) {
90
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
91
+ const methodName = propertyKey;
92
+ const controllerClazz = target.constructor;
93
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
94
+ const name = param?.name || argNames[parameterIndex];
95
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.QUERY, parameterIndex, controllerClazz, methodName);
96
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
97
+ };
98
+ }
99
+ /**
100
+ * Inject the request query strings, all value are Array type.
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPQueries } from 'egg';
105
+ *
106
+ * @HTTPController()
107
+ * export class FooController {
108
+ * @HTTPMethod({
109
+ * path: '/foo',
110
+ * method: HTTPMethodEnum.GET,
111
+ * })
112
+ * // GET /foo?user=asd&user=fgh
113
+ * // user = ['asd', 'fgh']
114
+ * async bar(@HTTPQueries({ name: 'user' }) users?: string[]): Promise<void> {
115
+ * console.log(users);
116
+ * }
117
+ * }
118
+ * ```
119
+ */
120
+ function HTTPQueries(param) {
121
+ return function(target, propertyKey, parameterIndex) {
122
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
123
+ const methodName = propertyKey;
124
+ const controllerClazz = target.constructor;
125
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
126
+ const name = param?.name || argNames[parameterIndex];
127
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.QUERIES, parameterIndex, controllerClazz, methodName);
128
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
129
+ };
130
+ }
131
+ /**
132
+ * Inject the request path parameter, the value is string type.
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPParam } from 'egg';
137
+ *
138
+ * @HTTPController()
139
+ * export class FooController {
140
+ * @HTTPMethod({
141
+ * path: '/foo/:id',
142
+ * method: HTTPMethodEnum.GET,
143
+ * })
144
+ * // GET /foo/123
145
+ * // id = '123'
146
+ * async bar(@HTTPParam() id: string): Promise<void> {
147
+ * console.log(id);
148
+ * }
149
+ * }
150
+ * ```
151
+ */
152
+ function HTTPParam(param) {
153
+ return function(target, propertyKey, parameterIndex) {
154
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
155
+ const methodName = propertyKey;
156
+ const controllerClazz = target.constructor;
157
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
158
+ const name = param?.name || argNames[parameterIndex];
159
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.PARAM, parameterIndex, controllerClazz, methodName);
160
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
161
+ };
162
+ }
163
+ /**
164
+ * Inject the request object.
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPRequest } from 'egg';
169
+ *
170
+ * @HTTPController()
171
+ * export class FooController {
172
+ * @HTTPMethod({
173
+ * path: '/foo',
174
+ * method: HTTPMethodEnum.GET,
175
+ * })
176
+ * async bar(@HTTPRequest() request: Request): Promise<void> {
177
+ * console.log(request);
178
+ * }
179
+ * }
180
+ * ```
181
+ */
182
+ function HTTPRequest() {
183
+ return function(target, propertyKey, parameterIndex) {
184
+ const [nodeMajor] = process.versions.node.split(".").map((v) => Number(v));
185
+ assert(nodeMajor >= 16, `[controller/${target.name}] expect node version >=16, but now is ${nodeMajor}`);
186
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
187
+ const methodName = propertyKey;
188
+ const controllerClazz = target.constructor;
189
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.REQUEST, parameterIndex, controllerClazz, methodName);
190
+ };
191
+ }
192
+ /**
193
+ * Inject the request cookies.
194
+ *
195
+ * @example
196
+ * ```typescript
197
+ * import { HTTPController, HTTPMethod, HTTPMethodEnum, HTTPCookies, type Cookies } from 'egg';
198
+ *
199
+ * @HTTPController()
200
+ * export class FooController {
201
+ * @HTTPMethod({
202
+ * path: '/foo',
203
+ * method: HTTPMethodEnum.GET,
204
+ * })
205
+ * // GET /foo -H 'Cookie: foo=bar; bar=baz'
206
+ * // cookies = cookies
207
+ * async bar(@HTTPCookies() cookies: Cookies): Promise<void> {
208
+ * console.log(cookies);
209
+ * }
210
+ * }
211
+ * ```
212
+ */
213
+ function HTTPCookies() {
214
+ return function(target, propertyKey, parameterIndex) {
215
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
216
+ const methodName = propertyKey;
217
+ const controllerClazz = target.constructor;
218
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.COOKIES, parameterIndex, controllerClazz, methodName);
219
+ };
220
+ }
221
+
222
+ //#endregion
223
+ export { HTTPBody, HTTPCookies, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, HTTPRequest };
@@ -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,5 @@
1
+ import { Host } from "./Host.js";
2
+ import { HTTPController } from "./HTTPController.js";
3
+ import { HTTPMethod } from "./HTTPMethod.js";
4
+ import { InjectContext } from "../Context.js";
5
+ import { HTTPBody, HTTPCookies, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, HTTPRequest } from "./HTTPParam.js";