@eggjs/controller-decorator 4.0.0-beta.6 → 4.0.0-beta.8

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 (55) hide show
  1. package/dist/index.d.ts +247 -23
  2. package/dist/index.js +749 -28
  3. package/package.json +6 -6
  4. package/dist/builder/ControllerMetaBuilderFactory.d.ts +0 -11
  5. package/dist/builder/ControllerMetaBuilderFactory.js +0 -37
  6. package/dist/builder/index.js +0 -3
  7. package/dist/decorator/Acl.d.ts +0 -4
  8. package/dist/decorator/Acl.js +0 -24
  9. package/dist/decorator/Context.d.ts +0 -4
  10. package/dist/decorator/Context.js +0 -16
  11. package/dist/decorator/Middleware.d.ts +0 -6
  12. package/dist/decorator/Middleware.js +0 -57
  13. package/dist/decorator/http/HTTPController.d.ts +0 -6
  14. package/dist/decorator/http/HTTPController.js +0 -23
  15. package/dist/decorator/http/HTTPMethod.d.ts +0 -6
  16. package/dist/decorator/http/HTTPMethod.js +0 -21
  17. package/dist/decorator/http/HTTPParam.d.ts +0 -12
  18. package/dist/decorator/http/HTTPParam.js +0 -77
  19. package/dist/decorator/http/Host.d.ts +0 -6
  20. package/dist/decorator/http/Host.js +0 -27
  21. package/dist/decorator/http/index.js +0 -6
  22. package/dist/decorator/index.js +0 -10
  23. package/dist/impl/http/HTTPControllerMetaBuilder.d.ts +0 -13
  24. package/dist/impl/http/HTTPControllerMetaBuilder.js +0 -63
  25. package/dist/impl/http/HTTPControllerMethodMetaBuilder.d.ts +0 -15
  26. package/dist/impl/http/HTTPControllerMethodMetaBuilder.js +0 -80
  27. package/dist/impl/http/index.js +0 -4
  28. package/dist/impl/index.js +0 -5
  29. package/dist/model/HTTPControllerMeta.d.ts +0 -25
  30. package/dist/model/HTTPControllerMeta.js +0 -53
  31. package/dist/model/HTTPCookies.d.ts +0 -6
  32. package/dist/model/HTTPCookies.js +0 -7
  33. package/dist/model/HTTPMethodMeta.d.ts +0 -59
  34. package/dist/model/HTTPMethodMeta.js +0 -102
  35. package/dist/model/HTTPRequest.d.ts +0 -8
  36. package/dist/model/HTTPRequest.js +0 -5
  37. package/dist/model/HTTPResponse.d.ts +0 -11
  38. package/dist/model/HTTPResponse.js +0 -5
  39. package/dist/model/index.js +0 -7
  40. package/dist/util/ControllerInfoUtil.d.ts +0 -20
  41. package/dist/util/ControllerInfoUtil.js +0 -48
  42. package/dist/util/ControllerMetadataUtil.d.ts +0 -9
  43. package/dist/util/ControllerMetadataUtil.js +0 -21
  44. package/dist/util/HTTPInfoUtil.d.ts +0 -20
  45. package/dist/util/HTTPInfoUtil.js +0 -53
  46. package/dist/util/HTTPPriorityUtil.d.ts +0 -22
  47. package/dist/util/HTTPPriorityUtil.js +0 -38
  48. package/dist/util/MethodInfoUtil.d.ts +0 -23
  49. package/dist/util/MethodInfoUtil.js +0 -62
  50. package/dist/util/index.js +0 -10
  51. package/dist/util/validator/ControllerValidator.d.ts +0 -8
  52. package/dist/util/validator/ControllerValidator.js +0 -16
  53. package/dist/util/validator/MethodValidator.d.ts +0 -8
  54. package/dist/util/validator/MethodValidator.js +0 -31
  55. package/dist/util/validator/index.js +0 -4
package/dist/index.js CHANGED
@@ -1,31 +1,752 @@
1
- import { ControllerInfoUtil } from "./util/ControllerInfoUtil.js";
2
- import { ControllerValidator } from "./util/validator/ControllerValidator.js";
3
- import { MethodInfoUtil } from "./util/MethodInfoUtil.js";
4
- import { MethodValidator } from "./util/validator/MethodValidator.js";
5
- import { ControllerMetadataUtil } from "./util/ControllerMetadataUtil.js";
6
- import { HTTPInfoUtil } from "./util/HTTPInfoUtil.js";
7
- import { HTTPPriorityUtil } from "./util/HTTPPriorityUtil.js";
8
- import "./util/index.js";
9
- import { ControllerMetaBuilderFactory } from "./builder/ControllerMetaBuilderFactory.js";
10
- import "./builder/index.js";
11
- import { Host } from "./decorator/http/Host.js";
12
- import { HTTPController } from "./decorator/http/HTTPController.js";
13
- import { HTTPMethod } from "./decorator/http/HTTPMethod.js";
14
- import { Cookies, HTTPBody, HTTPHeaders, HTTPParam, HTTPQueries, HTTPQuery, Request } from "./decorator/http/HTTPParam.js";
15
- import { Acl } from "./decorator/Acl.js";
16
- import { Context } from "./decorator/Context.js";
17
- import { Middleware } from "./decorator/Middleware.js";
18
- import "./decorator/index.js";
19
- import { BodyParamMeta, CookiesParamMeta, HTTPMethodMeta, HeadersParamMeta, ParamMeta, ParamMetaUtil, PathParamMeta, QueriesParamMeta, QueryParamMeta, RequestParamMeta } from "./model/HTTPMethodMeta.js";
20
- import { HTTPControllerMeta } from "./model/HTTPControllerMeta.js";
21
- import { HTTPCookies } from "./model/HTTPCookies.js";
22
- import { HTTPRequest } from "./model/HTTPRequest.js";
23
- import { HTTPResponse } from "./model/HTTPResponse.js";
24
- import "./model/index.js";
25
- import { HTTPControllerMethodMetaBuilder } from "./impl/http/HTTPControllerMethodMetaBuilder.js";
26
- import { HTTPControllerMetaBuilder } from "./impl/http/HTTPControllerMetaBuilder.js";
27
- import "./impl/index.js";
1
+ import { AdviceInfoUtil, Pointcut } from "@eggjs/aop-decorator";
2
+ import { ClassUtil } from "@eggjs/tegg-metadata";
3
+ import { AccessLevel, CONTROLLER_ACL, CONTROLLER_AOP_MIDDLEWARES, CONTROLLER_HOST, CONTROLLER_HTTP_PATH, CONTROLLER_META_DATA, CONTROLLER_METHOD_METHOD_MAP, CONTROLLER_METHOD_PARAM_NAME_MAP, CONTROLLER_METHOD_PARAM_TYPE_MAP, CONTROLLER_METHOD_PATH_MAP, CONTROLLER_METHOD_PRIORITY, CONTROLLER_MIDDLEWARES, CONTROLLER_NAME, CONTROLLER_TYPE, ControllerType, HTTPParamType, METHOD_ACL, METHOD_AOP_MIDDLEWARES, METHOD_AOP_REGISTER_MAP, METHOD_CONTEXT_INDEX, METHOD_CONTROLLER_HOST, METHOD_CONTROLLER_TYPE_MAP, METHOD_MIDDLEWARES } from "@eggjs/tegg-types";
4
+ import { MetadataUtil, PrototypeUtil, SingletonProto } from "@eggjs/core-decorator";
5
+ import { MapUtil, ObjectUtils, StackUtil } from "@eggjs/tegg-common-util";
6
+ import pathToRegexp, { parse } from "path-to-regexp";
7
+ import assert from "node:assert";
8
+ import { isClass } from "is-type-of";
9
+ import path from "node:path";
10
+ import { Cookies as Cookies$1 } from "@eggjs/cookies";
28
11
 
29
12
  export * from "@eggjs/tegg-types/controller-decorator"
30
13
 
31
- 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 };
14
+ //#region src/util/ControllerInfoUtil.ts
15
+ var ControllerInfoUtil = class {
16
+ static addControllerMiddleware(middleware, clazz) {
17
+ MetadataUtil.initOwnArrayMetaData(CONTROLLER_MIDDLEWARES, clazz, []).push(middleware);
18
+ }
19
+ static addControllerAopMiddleware(middleware, clazz) {
20
+ MetadataUtil.initOwnArrayMetaData(CONTROLLER_AOP_MIDDLEWARES, clazz, []).push(middleware);
21
+ }
22
+ static getControllerMiddlewares(clazz) {
23
+ return MetadataUtil.getMetaData(CONTROLLER_MIDDLEWARES, clazz) || [];
24
+ }
25
+ static getControllerAopMiddlewares(clazz) {
26
+ return MetadataUtil.getMetaData(CONTROLLER_AOP_MIDDLEWARES, clazz) || [];
27
+ }
28
+ static setControllerType(clazz, controllerType) {
29
+ MetadataUtil.defineMetaData(CONTROLLER_TYPE, controllerType, clazz);
30
+ }
31
+ static setControllerName(clazz, controllerName) {
32
+ MetadataUtil.defineMetaData(CONTROLLER_NAME, controllerName, clazz);
33
+ }
34
+ static getControllerName(clazz) {
35
+ return MetadataUtil.getMetaData(CONTROLLER_NAME, clazz);
36
+ }
37
+ static getControllerType(clazz) {
38
+ return MetadataUtil.getMetaData(CONTROLLER_TYPE, clazz);
39
+ }
40
+ static setControllerAcl(code, clazz) {
41
+ MetadataUtil.defineMetaData(CONTROLLER_ACL, code, clazz);
42
+ }
43
+ static hasControllerAcl(clazz) {
44
+ return MetadataUtil.hasMetaData(CONTROLLER_ACL, clazz);
45
+ }
46
+ static getControllerAcl(clazz) {
47
+ return MetadataUtil.getMetaData(CONTROLLER_ACL, clazz);
48
+ }
49
+ static addControllerHosts(hosts, clazz) {
50
+ MetadataUtil.defineMetaData(CONTROLLER_HOST, hosts, clazz);
51
+ }
52
+ static getControllerHosts(clazz) {
53
+ return MetadataUtil.getMetaData(CONTROLLER_HOST, clazz);
54
+ }
55
+ };
56
+
57
+ //#endregion
58
+ //#region src/util/validator/ControllerValidator.ts
59
+ var ControllerValidator = class {
60
+ static validate(clazz) {
61
+ const controllerType = ControllerInfoUtil.getControllerType(clazz);
62
+ if (ControllerInfoUtil.getControllerMiddlewares(clazz).length && !controllerType) {
63
+ const desc = ClassUtil.classDescription(clazz);
64
+ throw new Error(`${desc} @Middleware should use with controller decorator`);
65
+ }
66
+ }
67
+ };
68
+
69
+ //#endregion
70
+ //#region src/util/MethodInfoUtil.ts
71
+ var MethodInfoUtil = class {
72
+ static setMethodControllerType(clazz, methodName, controllerType) {
73
+ MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_TYPE_MAP, clazz, /* @__PURE__ */ new Map()).set(methodName, controllerType);
74
+ }
75
+ static getMethodControllerType(clazz, methodName) {
76
+ return MetadataUtil.getMetaData(METHOD_CONTROLLER_TYPE_MAP, clazz)?.get(methodName);
77
+ }
78
+ static setMethodContextIndexInArgs(index, clazz, methodName) {
79
+ MetadataUtil.initOwnMapMetaData(METHOD_CONTEXT_INDEX, clazz, /* @__PURE__ */ new Map()).set(methodName, index);
80
+ }
81
+ static getMethodContextIndex(clazz, methodName) {
82
+ return MetadataUtil.getMetaData(METHOD_CONTEXT_INDEX, clazz)?.get(methodName);
83
+ }
84
+ static addMethodMiddleware(middleware, clazz, methodName) {
85
+ const methodMiddlewareMap = MetadataUtil.initOwnMapMetaData(METHOD_MIDDLEWARES, clazz, /* @__PURE__ */ new Map());
86
+ MapUtil.getOrStore(methodMiddlewareMap, methodName, []).push(middleware);
87
+ }
88
+ static getMethodMiddlewares(clazz, methodName) {
89
+ return MetadataUtil.getMetaData(METHOD_MIDDLEWARES, clazz)?.get(methodName) || [];
90
+ }
91
+ static addMethodAopMiddleware(middleware, clazz, methodName) {
92
+ const methodMiddlewareMap = MetadataUtil.initOwnMapMetaData(METHOD_AOP_MIDDLEWARES, clazz, /* @__PURE__ */ new Map());
93
+ MapUtil.getOrStore(methodMiddlewareMap, methodName, []).push(middleware);
94
+ }
95
+ static getMethodAopMiddlewares(clazz, methodName) {
96
+ return MetadataUtil.getMetaData(METHOD_AOP_MIDDLEWARES, clazz)?.get(methodName) || [];
97
+ }
98
+ static setMethodAcl(code, clazz, methodName) {
99
+ MetadataUtil.initOwnMapMetaData(METHOD_ACL, clazz, /* @__PURE__ */ new Map()).set(methodName, code);
100
+ }
101
+ static hasMethodAcl(clazz, methodName) {
102
+ return !!MetadataUtil.getMetaData(METHOD_ACL, clazz)?.has(methodName);
103
+ }
104
+ static getMethodAcl(clazz, methodName) {
105
+ return MetadataUtil.getMetaData(METHOD_ACL, clazz)?.get(methodName);
106
+ }
107
+ static setMethodHosts(hosts, clazz, methodName) {
108
+ MetadataUtil.initOwnMapMetaData(METHOD_CONTROLLER_HOST, clazz, /* @__PURE__ */ new Map()).set(methodName, hosts);
109
+ }
110
+ static getMethodHosts(clazz, methodName) {
111
+ return MetadataUtil.getMetaData(METHOD_CONTROLLER_HOST, clazz)?.get(methodName);
112
+ }
113
+ static getMethods(clazz) {
114
+ const methodControllerMap = MetadataUtil.getMetaData(METHOD_CONTROLLER_TYPE_MAP, clazz);
115
+ return Array.from(methodControllerMap?.keys() || []);
116
+ }
117
+ static shouldRegisterAopMiddlewarePointCut(clazz, methodName) {
118
+ const methodControllerMap = MetadataUtil.getMetaData(METHOD_AOP_REGISTER_MAP, clazz);
119
+ return !(methodControllerMap && methodControllerMap.get(methodName));
120
+ }
121
+ static registerAopMiddlewarePointcut(clazz, methodName) {
122
+ MetadataUtil.initOwnMapMetaData(METHOD_AOP_REGISTER_MAP, clazz, /* @__PURE__ */ new Map()).set(methodName, true);
123
+ }
124
+ };
125
+
126
+ //#endregion
127
+ //#region src/util/validator/MethodValidator.ts
128
+ var MethodValidator = class {
129
+ static validate(clazz, methodName) {
130
+ const methodControllerType = MethodInfoUtil.getMethodControllerType(clazz, methodName);
131
+ const methodMiddlewares = MethodInfoUtil.getMethodMiddlewares(clazz, methodName);
132
+ const contextIndex = MethodInfoUtil.getMethodContextIndex(clazz, methodName);
133
+ if (!methodControllerType) {
134
+ if (methodMiddlewares.length) {
135
+ const desc = ClassUtil.classDescription(clazz);
136
+ throw new Error(`${desc}:${methodName} @Middleware should use with method decorator`);
137
+ }
138
+ if (contextIndex !== void 0) {
139
+ const desc = ClassUtil.classDescription(clazz);
140
+ throw new Error(`${desc}:${methodName} @Context should use with method decorator`);
141
+ }
142
+ return;
143
+ }
144
+ const controllerType = ControllerInfoUtil.getControllerType(clazz);
145
+ if (methodControllerType !== controllerType) {
146
+ const desc = ClassUtil.classDescription(clazz);
147
+ throw new Error(`${desc}:${methodName} method decorator ${methodControllerType} can not be used with ${controllerType}`);
148
+ }
149
+ }
150
+ };
151
+
152
+ //#endregion
153
+ //#region src/util/ControllerMetadataUtil.ts
154
+ var ControllerMetadataUtil = class ControllerMetadataUtil {
155
+ static setControllerMetadata(clazz, metaData) {
156
+ MetadataUtil.defineMetaData(CONTROLLER_META_DATA, metaData, clazz);
157
+ }
158
+ static getControllerMetadata(clazz) {
159
+ let metadata = MetadataUtil.getOwnMetaData(CONTROLLER_META_DATA, clazz);
160
+ if (metadata) return metadata;
161
+ metadata = ControllerMetaBuilderFactory.build(clazz);
162
+ if (metadata) ControllerMetadataUtil.setControllerMetadata(clazz, metadata);
163
+ return metadata;
164
+ }
165
+ };
166
+
167
+ //#endregion
168
+ //#region src/util/HTTPInfoUtil.ts
169
+ var HTTPInfoUtil = class {
170
+ static setHTTPPath(path$1, clazz) {
171
+ MetadataUtil.defineMetaData(CONTROLLER_HTTP_PATH, path$1, clazz);
172
+ }
173
+ static getHTTPPath(clazz) {
174
+ return MetadataUtil.getMetaData(CONTROLLER_HTTP_PATH, clazz);
175
+ }
176
+ static setHTTPMethodPath(path$1, clazz, methodName) {
177
+ MetadataUtil.initOwnMapMetaData(CONTROLLER_METHOD_PATH_MAP, clazz, /* @__PURE__ */ new Map()).set(methodName, path$1);
178
+ }
179
+ static getHTTPMethodPath(clazz, methodName) {
180
+ return MetadataUtil.getMetaData(CONTROLLER_METHOD_PATH_MAP, clazz)?.get(methodName);
181
+ }
182
+ static setHTTPMethodMethod(method, clazz, methodName) {
183
+ MetadataUtil.initOwnMapMetaData(CONTROLLER_METHOD_METHOD_MAP, clazz, /* @__PURE__ */ new Map()).set(methodName, method);
184
+ }
185
+ static getHTTPMethodMethod(clazz, methodName) {
186
+ return MetadataUtil.getMetaData(CONTROLLER_METHOD_METHOD_MAP, clazz)?.get(methodName);
187
+ }
188
+ static setHTTPMethodParamType(paramType, parameterIndex, clazz, methodName) {
189
+ const methodParamMap = MetadataUtil.initOwnMapMetaData(CONTROLLER_METHOD_PARAM_TYPE_MAP, clazz, /* @__PURE__ */ new Map());
190
+ MapUtil.getOrStore(methodParamMap, methodName, /* @__PURE__ */ new Map()).set(parameterIndex, paramType);
191
+ }
192
+ static getParamIndexList(clazz, methodName) {
193
+ const paramMap = MetadataUtil.getMetaData(CONTROLLER_METHOD_PARAM_TYPE_MAP, clazz)?.get(methodName);
194
+ if (!paramMap) return [];
195
+ return Array.from(paramMap.keys());
196
+ }
197
+ static getHTTPMethodParamType(parameterIndex, clazz, methodName) {
198
+ return (MetadataUtil.getMetaData(CONTROLLER_METHOD_PARAM_TYPE_MAP, clazz)?.get(methodName))?.get(parameterIndex);
199
+ }
200
+ static setHTTPMethodParamName(paramName, parameterIndex, clazz, methodName) {
201
+ const methodParamNameMap = MetadataUtil.initOwnMapMetaData(CONTROLLER_METHOD_PARAM_NAME_MAP, clazz, /* @__PURE__ */ new Map());
202
+ MapUtil.getOrStore(methodParamNameMap, methodName, /* @__PURE__ */ new Map()).set(parameterIndex, paramName);
203
+ }
204
+ static getHTTPMethodParamName(parameterIndex, clazz, methodName) {
205
+ return (MetadataUtil.getMetaData(CONTROLLER_METHOD_PARAM_NAME_MAP, clazz)?.get(methodName))?.get(parameterIndex);
206
+ }
207
+ static getHTTPMethodPriority(clazz, methodName) {
208
+ return MetadataUtil.getMetaData(CONTROLLER_METHOD_PRIORITY, clazz)?.get(methodName);
209
+ }
210
+ static setHTTPMethodPriority(priority, clazz, methodName) {
211
+ MetadataUtil.initOwnMapMetaData(CONTROLLER_METHOD_PRIORITY, clazz, /* @__PURE__ */ new Map()).set(methodName, priority);
212
+ }
213
+ };
214
+
215
+ //#endregion
216
+ //#region src/util/HTTPPriorityUtil.ts
217
+ var HTTPPriorityUtil = class {
218
+ static DEFAULT_PRIORITY = 1e5;
219
+ static TOKEN_PRIORITY = 1e3;
220
+ /**
221
+ * | Path | RegExp index | priority |
222
+ * | --- | --- | --- |
223
+ * | /* | [0] | 0 |
224
+ * | /hello/:name | [1] | 1000 |
225
+ * | /hello/world/message/:message | [3] | 3000 |
226
+ * | /hello/:name/message/:message | [1, 3] | 4000 |
227
+ * | /hello/world | [] | 100000/Infinity? |
228
+ *
229
+ * priority = hasRegExp
230
+ * : regexpIndex.reduce((p,c) => p + c * 1000, 0)
231
+ * : 100000;
232
+ * @param {string} path -
233
+ */
234
+ static calcPathPriority(path$1) {
235
+ const tokens = parse(path$1);
236
+ let priority = 0;
237
+ let hasRegExp = false;
238
+ let index = 0;
239
+ let token;
240
+ while (token = tokens.shift()) if (typeof token === "string") index += token.split("/").length - 1;
241
+ else {
242
+ hasRegExp = true;
243
+ priority += index++ * this.TOKEN_PRIORITY;
244
+ }
245
+ if (!hasRegExp) return this.DEFAULT_PRIORITY;
246
+ return priority;
247
+ }
248
+ };
249
+
250
+ //#endregion
251
+ //#region src/builder/ControllerMetaBuilderFactory.ts
252
+ var ControllerMetaBuilderFactory = class ControllerMetaBuilderFactory {
253
+ static builderCreatorMap = /* @__PURE__ */ new Map();
254
+ static registerControllerMetaBuilder(controllerType, controllerBuilderCreator) {
255
+ this.builderCreatorMap.set(controllerType, controllerBuilderCreator);
256
+ }
257
+ static createControllerMetaBuilder(clazz, controllerType) {
258
+ if (!controllerType) controllerType = ControllerInfoUtil.getControllerType(clazz);
259
+ if (!controllerType) return;
260
+ const creator = this.builderCreatorMap.get(controllerType);
261
+ if (!creator) throw new Error(`not found controller meta builder for type ${controllerType}`);
262
+ return creator(clazz);
263
+ }
264
+ static build(clazz, controllerType) {
265
+ const builder = ControllerMetaBuilderFactory.createControllerMetaBuilder(clazz, controllerType);
266
+ if (!builder) return;
267
+ const metadata = builder.build();
268
+ if (!metadata) return;
269
+ const controllerAopMws = ControllerInfoUtil.getControllerAopMiddlewares(clazz);
270
+ for (const { name } of metadata.methods) {
271
+ const methodAopMws = MethodInfoUtil.getMethodAopMiddlewares(clazz, name);
272
+ if (MethodInfoUtil.shouldRegisterAopMiddlewarePointCut(clazz, name)) {
273
+ for (const mw of [...methodAopMws, ...controllerAopMws].reverse()) Pointcut(mw)(clazz.prototype, name);
274
+ MethodInfoUtil.registerAopMiddlewarePointcut(clazz, name);
275
+ }
276
+ }
277
+ return metadata;
278
+ }
279
+ };
280
+
281
+ //#endregion
282
+ //#region src/decorator/http/Host.ts
283
+ function Host(host) {
284
+ function parseHost() {
285
+ return Array.isArray(host) ? host : [host];
286
+ }
287
+ function classHost(constructor) {
288
+ ControllerInfoUtil.addControllerHosts(parseHost(), constructor);
289
+ }
290
+ function methodHost(target, propertyKey) {
291
+ assert(typeof propertyKey === "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
292
+ const controllerClazz = target.constructor;
293
+ const methodName = propertyKey;
294
+ MethodInfoUtil.setMethodHosts(parseHost(), controllerClazz, methodName);
295
+ }
296
+ return function(target, propertyKey) {
297
+ if (propertyKey === void 0) classHost(target);
298
+ else methodHost(target, propertyKey);
299
+ };
300
+ }
301
+
302
+ //#endregion
303
+ //#region src/decorator/http/HTTPController.ts
304
+ function HTTPController(param) {
305
+ return function(constructor) {
306
+ ControllerInfoUtil.setControllerType(constructor, ControllerType.HTTP);
307
+ if (param?.controllerName) ControllerInfoUtil.setControllerName(constructor, param.controllerName);
308
+ if (param?.path) HTTPInfoUtil.setHTTPPath(param.path, constructor);
309
+ SingletonProto({
310
+ accessLevel: AccessLevel.PUBLIC,
311
+ name: param?.protoName
312
+ })(constructor);
313
+ PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5));
314
+ };
315
+ }
316
+
317
+ //#endregion
318
+ //#region src/decorator/http/HTTPMethod.ts
319
+ function HTTPMethod(param) {
320
+ return function(target, propertyKey) {
321
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
322
+ const controllerClazz = target.constructor;
323
+ const methodName = propertyKey;
324
+ MethodInfoUtil.setMethodControllerType(controllerClazz, methodName, ControllerType.HTTP);
325
+ HTTPInfoUtil.setHTTPMethodPath(param.path, controllerClazz, methodName);
326
+ HTTPInfoUtil.setHTTPMethodMethod(param.method, controllerClazz, methodName);
327
+ if (param.priority !== void 0) HTTPInfoUtil.setHTTPMethodPriority(param.priority, controllerClazz, methodName);
328
+ };
329
+ }
330
+
331
+ //#endregion
332
+ //#region src/decorator/http/HTTPParam.ts
333
+ function HTTPBody() {
334
+ return function(target, propertyKey, parameterIndex) {
335
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
336
+ const methodName = propertyKey;
337
+ const controllerClazz = target.constructor;
338
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.BODY, parameterIndex, controllerClazz, methodName);
339
+ };
340
+ }
341
+ function HTTPHeaders() {
342
+ return function(target, propertyKey, parameterIndex) {
343
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
344
+ const methodName = propertyKey;
345
+ const controllerClazz = target.constructor;
346
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.HEADERS, parameterIndex, controllerClazz, methodName);
347
+ };
348
+ }
349
+ function HTTPQuery(param) {
350
+ return function(target, propertyKey, parameterIndex) {
351
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
352
+ const methodName = propertyKey;
353
+ const controllerClazz = target.constructor;
354
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
355
+ const name = param?.name || argNames[parameterIndex];
356
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.QUERY, parameterIndex, controllerClazz, methodName);
357
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
358
+ };
359
+ }
360
+ function HTTPQueries(param) {
361
+ return function(target, propertyKey, parameterIndex) {
362
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
363
+ const methodName = propertyKey;
364
+ const controllerClazz = target.constructor;
365
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
366
+ const name = param?.name || argNames[parameterIndex];
367
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.QUERIES, parameterIndex, controllerClazz, methodName);
368
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
369
+ };
370
+ }
371
+ function HTTPParam(param) {
372
+ return function(target, propertyKey, parameterIndex) {
373
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
374
+ const methodName = propertyKey;
375
+ const controllerClazz = target.constructor;
376
+ const argNames = ObjectUtils.getFunctionArgNameList(target[propertyKey]);
377
+ const name = param?.name || argNames[parameterIndex];
378
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.PARAM, parameterIndex, controllerClazz, methodName);
379
+ HTTPInfoUtil.setHTTPMethodParamName(name, parameterIndex, controllerClazz, methodName);
380
+ };
381
+ }
382
+ function Request$1() {
383
+ return function(target, propertyKey, parameterIndex) {
384
+ const [nodeMajor] = process.versions.node.split(".").map((v) => Number(v));
385
+ assert(nodeMajor >= 16, `[controller/${target.name}] expect node version >=16, but now is ${nodeMajor}`);
386
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
387
+ const methodName = propertyKey;
388
+ const controllerClazz = target.constructor;
389
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.REQUEST, parameterIndex, controllerClazz, methodName);
390
+ };
391
+ }
392
+ function Cookies() {
393
+ return function(target, propertyKey, parameterIndex) {
394
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
395
+ const methodName = propertyKey;
396
+ const controllerClazz = target.constructor;
397
+ HTTPInfoUtil.setHTTPMethodParamType(HTTPParamType.COOKIES, parameterIndex, controllerClazz, methodName);
398
+ };
399
+ }
400
+
401
+ //#endregion
402
+ //#region src/decorator/Acl.ts
403
+ function Acl(code) {
404
+ function classAcl(constructor) {
405
+ ControllerInfoUtil.setControllerAcl(code, constructor);
406
+ }
407
+ function methodAcl(target, propertyKey) {
408
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
409
+ const controllerClazz = target.constructor;
410
+ const methodName = propertyKey;
411
+ MethodInfoUtil.setMethodAcl(code, controllerClazz, methodName);
412
+ }
413
+ return function(target, propertyKey) {
414
+ if (propertyKey === void 0) classAcl(target);
415
+ else methodAcl(target, propertyKey);
416
+ };
417
+ }
418
+
419
+ //#endregion
420
+ //#region src/decorator/Context.ts
421
+ function Context() {
422
+ return function(target, propertyKey, parameterIndex) {
423
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
424
+ const methodName = propertyKey;
425
+ const controllerClazz = target.constructor;
426
+ MethodInfoUtil.setMethodContextIndexInArgs(parameterIndex, controllerClazz, methodName);
427
+ };
428
+ }
429
+
430
+ //#endregion
431
+ //#region src/decorator/Middleware.ts
432
+ var MiddlewareType = /* @__PURE__ */ function(MiddlewareType$1) {
433
+ MiddlewareType$1["AOP"] = "AOP";
434
+ MiddlewareType$1["MiddlewareFunc"] = "MiddlewareFunc";
435
+ return MiddlewareType$1;
436
+ }(MiddlewareType || {});
437
+ function isAop(mw) {
438
+ return isClass(mw) && AdviceInfoUtil.isAdvice(mw);
439
+ }
440
+ function isAopTypeOrMiddlewareType(middlewares) {
441
+ const adviceCount = middlewares.filter((t) => isAop(t)).length;
442
+ if (adviceCount) {
443
+ if (adviceCount === middlewares.length) return MiddlewareType.AOP;
444
+ throw new Error("AOP and MiddlewareFunc can not be mixed");
445
+ }
446
+ return MiddlewareType.MiddlewareFunc;
447
+ }
448
+ function Middleware(...middlewares) {
449
+ function functionTypeClassMiddleware(constructor) {
450
+ middlewares.forEach((mid) => {
451
+ ControllerInfoUtil.addControllerMiddleware(mid, constructor);
452
+ });
453
+ }
454
+ function aopTypeClassMiddleware(constructor) {
455
+ for (const aopAdvice of middlewares) ControllerInfoUtil.addControllerAopMiddleware(aopAdvice, constructor);
456
+ }
457
+ function functionTypeMethodMiddleware(target, propertyKey) {
458
+ assert.equal(typeof propertyKey, "string", `[controller/${target.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
459
+ const controllerClazz = target.constructor;
460
+ const methodName = propertyKey;
461
+ middlewares.forEach((mid) => {
462
+ MethodInfoUtil.addMethodMiddleware(mid, controllerClazz, methodName);
463
+ });
464
+ }
465
+ function aopTypeMethodMiddleware(target, propertyKey) {
466
+ const controllerClazz = target.constructor;
467
+ const methodName = propertyKey;
468
+ for (const aopAdvice of middlewares) MethodInfoUtil.addMethodAopMiddleware(aopAdvice, controllerClazz, methodName);
469
+ }
470
+ return function(target, propertyKey) {
471
+ const type = isAopTypeOrMiddlewareType(middlewares);
472
+ if (propertyKey === void 0) if (type === MiddlewareType.AOP) aopTypeClassMiddleware(target);
473
+ else functionTypeClassMiddleware(target);
474
+ else if (type === MiddlewareType.AOP) aopTypeMethodMiddleware(target, propertyKey);
475
+ else functionTypeMethodMiddleware(target, propertyKey);
476
+ };
477
+ }
478
+
479
+ //#endregion
480
+ //#region src/model/HTTPMethodMeta.ts
481
+ var ParamMeta = class {
482
+ type;
483
+ };
484
+ var RequestParamMeta = class extends ParamMeta {
485
+ type = HTTPParamType.REQUEST;
486
+ validate() {}
487
+ };
488
+ var BodyParamMeta = class extends ParamMeta {
489
+ type = HTTPParamType.BODY;
490
+ validate() {}
491
+ };
492
+ var HeadersParamMeta = class extends ParamMeta {
493
+ type = HTTPParamType.HEADERS;
494
+ validate() {}
495
+ };
496
+ var QueryParamMeta = class extends ParamMeta {
497
+ type = HTTPParamType.QUERY;
498
+ name;
499
+ constructor(name) {
500
+ super();
501
+ this.name = name;
502
+ }
503
+ validate() {}
504
+ };
505
+ var QueriesParamMeta = class extends ParamMeta {
506
+ type = HTTPParamType.QUERIES;
507
+ name;
508
+ constructor(name) {
509
+ super();
510
+ this.name = name;
511
+ }
512
+ validate() {}
513
+ };
514
+ var PathParamMeta = class extends ParamMeta {
515
+ type = HTTPParamType.PARAM;
516
+ name;
517
+ constructor(name) {
518
+ super();
519
+ this.name = name;
520
+ }
521
+ validate(httpPath) {
522
+ const names = [];
523
+ pathToRegexp(httpPath, names);
524
+ if (!names.find((name) => String(name.name) === this.name)) throw new Error(`can not find param ${this.name} in path ${httpPath}`);
525
+ }
526
+ };
527
+ var CookiesParamMeta = class extends ParamMeta {
528
+ type = HTTPParamType.COOKIES;
529
+ validate() {}
530
+ };
531
+ var HTTPMethodMeta = class {
532
+ name;
533
+ path;
534
+ method;
535
+ middlewares;
536
+ contextParamIndex;
537
+ paramMap;
538
+ priority;
539
+ needAcl;
540
+ aclCode;
541
+ hosts;
542
+ constructor(name, path$1, method, middlewares, contextParamIndex, paramTypeMap, priority, needAcl, aclCode, hosts) {
543
+ this.name = name;
544
+ this.path = path$1;
545
+ this.method = method;
546
+ this.middlewares = middlewares;
547
+ this.contextParamIndex = contextParamIndex;
548
+ this.paramMap = paramTypeMap;
549
+ this.priority = priority;
550
+ this.needAcl = needAcl;
551
+ this.aclCode = aclCode;
552
+ this.hosts = hosts;
553
+ }
554
+ };
555
+ var ParamMetaUtil = class {
556
+ static createParam(type, name) {
557
+ switch (type) {
558
+ case HTTPParamType.PARAM:
559
+ assert(name, "path param must has name");
560
+ return new PathParamMeta(name);
561
+ case HTTPParamType.BODY: return new BodyParamMeta();
562
+ case HTTPParamType.HEADERS: return new HeadersParamMeta();
563
+ case HTTPParamType.QUERIES:
564
+ assert(name, "queries param must has name");
565
+ return new QueriesParamMeta(name);
566
+ case HTTPParamType.QUERY:
567
+ assert(name, "query param must has name");
568
+ return new QueryParamMeta(name);
569
+ case HTTPParamType.REQUEST: return new RequestParamMeta();
570
+ case HTTPParamType.COOKIES: return new CookiesParamMeta();
571
+ default: assert.fail("never arrive");
572
+ }
573
+ }
574
+ };
575
+
576
+ //#endregion
577
+ //#region src/model/HTTPControllerMeta.ts
578
+ var HTTPControllerMeta = class {
579
+ protoName;
580
+ controllerName;
581
+ className;
582
+ type = ControllerType.HTTP;
583
+ path;
584
+ middlewares;
585
+ methods;
586
+ needAcl;
587
+ aclCode;
588
+ hosts;
589
+ constructor(className, protoName, controllerName, path$1, middlewares, methods, needAcl, aclCode, hosts) {
590
+ this.protoName = protoName;
591
+ this.controllerName = controllerName;
592
+ this.className = className;
593
+ this.path = path$1;
594
+ this.middlewares = middlewares;
595
+ this.methods = methods;
596
+ this.needAcl = needAcl;
597
+ this.aclCode = aclCode;
598
+ this.hosts = hosts;
599
+ }
600
+ getMethodRealPath(method) {
601
+ if (this.path) return path.posix.join(this.path, method.path);
602
+ return method.path;
603
+ }
604
+ getMethodHosts(method) {
605
+ if (this.hosts) return this.hosts;
606
+ return method.hosts;
607
+ }
608
+ getMethodName(method) {
609
+ return `${method.method} ${this.controllerName}.${method.name}`;
610
+ }
611
+ getMethodMiddlewares(method) {
612
+ if (this.middlewares.length) return [...this.middlewares, ...method.middlewares];
613
+ return [...method.middlewares];
614
+ }
615
+ hasMethodAcl(method) {
616
+ return method.needAcl || this.needAcl;
617
+ }
618
+ getMethodAcl(method) {
619
+ if (method.aclCode) return method.aclCode;
620
+ return this.aclCode;
621
+ }
622
+ };
623
+
624
+ //#endregion
625
+ //#region src/model/HTTPCookies.ts
626
+ var HTTPCookies = class extends Cookies$1 {};
627
+
628
+ //#endregion
629
+ //#region src/model/HTTPRequest.ts
630
+ var HTTPRequest = class extends (Request || Object) {};
631
+
632
+ //#endregion
633
+ //#region src/model/HTTPResponse.ts
634
+ var HTTPResponse = class extends (Response || Object) {};
635
+
636
+ //#endregion
637
+ //#region src/impl/http/HTTPControllerMethodMetaBuilder.ts
638
+ var HTTPControllerMethodMetaBuilder = class {
639
+ clazz;
640
+ methodName;
641
+ constructor(clazz, methodName) {
642
+ this.clazz = clazz;
643
+ this.methodName = methodName;
644
+ }
645
+ checkParamDecorators() {
646
+ const functionLength = this.clazz.prototype[this.methodName].length;
647
+ const paramIndexList = HTTPInfoUtil.getParamIndexList(this.clazz, this.methodName);
648
+ const contextIndex = MethodInfoUtil.getMethodContextIndex(this.clazz, this.methodName);
649
+ const hasAnnotationParamCount = typeof contextIndex === "undefined" ? paramIndexList.length : paramIndexList.length + 1;
650
+ const maxParamCount = Math.max(functionLength, hasAnnotationParamCount);
651
+ for (let i = 0; i < maxParamCount; ++i) {
652
+ if (i === contextIndex) continue;
653
+ if (!HTTPInfoUtil.getHTTPMethodParamType(i, this.clazz, this.methodName)) {
654
+ const classDesc = ClassUtil.classDescription(this.clazz);
655
+ throw new Error(`${classDesc}:${this.methodName} param ${i} has no http param type, Please add @HTTPBody, @HTTPParam, @HTTPQuery, @HTTPQueries`);
656
+ }
657
+ }
658
+ }
659
+ buildParamType(httpPath) {
660
+ this.checkParamDecorators();
661
+ const paramTypeMap = /* @__PURE__ */ new Map();
662
+ const paramIndexList = HTTPInfoUtil.getParamIndexList(this.clazz, this.methodName);
663
+ for (const paramIndex of paramIndexList) {
664
+ const paramType = HTTPInfoUtil.getHTTPMethodParamType(paramIndex, this.clazz, this.methodName);
665
+ const paramName = HTTPInfoUtil.getHTTPMethodParamName(paramIndex, this.clazz, this.methodName);
666
+ const paramMeta = ParamMetaUtil.createParam(paramType, paramName);
667
+ try {
668
+ paramMeta.validate(httpPath);
669
+ } catch (e) {
670
+ e.message = `build controller ${ClassUtil.classDescription(this.clazz)} method ${this.methodName} param ${paramName} failed: ${e.message}`;
671
+ throw e;
672
+ }
673
+ paramTypeMap.set(paramIndex, paramMeta);
674
+ }
675
+ return paramTypeMap;
676
+ }
677
+ getPriority() {
678
+ const priority = HTTPInfoUtil.getHTTPMethodPriority(this.clazz, this.methodName);
679
+ if (priority !== void 0) return priority;
680
+ const controllerPath = HTTPInfoUtil.getHTTPPath(this.clazz);
681
+ const methodPath = HTTPInfoUtil.getHTTPMethodPath(this.clazz, this.methodName);
682
+ const realPath = controllerPath ? path.posix.join(controllerPath, methodPath) : methodPath;
683
+ const defaultPriority = HTTPPriorityUtil.calcPathPriority(realPath);
684
+ if (defaultPriority > HTTPPriorityUtil.DEFAULT_PRIORITY) throw new Error(`path ${realPath} is too long, should set priority manually`);
685
+ return defaultPriority;
686
+ }
687
+ build() {
688
+ MethodValidator.validate(this.clazz, this.methodName);
689
+ if (!MethodInfoUtil.getMethodControllerType(this.clazz, this.methodName)) return;
690
+ const httpMethod = HTTPInfoUtil.getHTTPMethodMethod(this.clazz, this.methodName);
691
+ const parentPath = HTTPInfoUtil.getHTTPPath(this.clazz);
692
+ const httpPath = HTTPInfoUtil.getHTTPMethodPath(this.clazz, this.methodName);
693
+ const contextIndex = MethodInfoUtil.getMethodContextIndex(this.clazz, this.methodName);
694
+ const middlewares = MethodInfoUtil.getMethodMiddlewares(this.clazz, this.methodName);
695
+ const needAcl = MethodInfoUtil.hasMethodAcl(this.clazz, this.methodName);
696
+ const aclCode = MethodInfoUtil.getMethodAcl(this.clazz, this.methodName);
697
+ const hosts = MethodInfoUtil.getMethodHosts(this.clazz, this.methodName);
698
+ const realPath = parentPath ? path.posix.join(parentPath, httpPath) : httpPath;
699
+ const paramTypeMap = this.buildParamType(realPath);
700
+ const priority = this.getPriority();
701
+ return new HTTPMethodMeta(this.methodName, httpPath, httpMethod, middlewares, contextIndex, paramTypeMap, priority, needAcl, aclCode, hosts);
702
+ }
703
+ };
704
+
705
+ //#endregion
706
+ //#region src/impl/http/HTTPControllerMetaBuilder.ts
707
+ var HTTPControllerMetaBuilder = class HTTPControllerMetaBuilder {
708
+ clazz;
709
+ constructor(clazz) {
710
+ this.clazz = clazz;
711
+ }
712
+ buildMethod() {
713
+ const methodNames = ObjectUtils.getProperties(this.clazz.prototype);
714
+ const methods = [];
715
+ for (const methodName of methodNames) {
716
+ const methodMeta = new HTTPControllerMethodMetaBuilder(this.clazz, methodName).build();
717
+ if (methodMeta) methods.push(methodMeta);
718
+ }
719
+ return methods;
720
+ }
721
+ build() {
722
+ ControllerValidator.validate(this.clazz);
723
+ const controllerType = ControllerInfoUtil.getControllerType(this.clazz);
724
+ assert.equal(controllerType, ControllerType.HTTP, "invalidate controller type");
725
+ const httpPath = HTTPInfoUtil.getHTTPPath(this.clazz);
726
+ const httpMiddlewares = ControllerInfoUtil.getControllerMiddlewares(this.clazz);
727
+ const methods = this.buildMethod();
728
+ const clazzName = this.clazz.name;
729
+ const controllerName = ControllerInfoUtil.getControllerName(this.clazz) || clazzName;
730
+ const protoName = PrototypeUtil.getProperty(this.clazz).name;
731
+ const needAcl = ControllerInfoUtil.hasControllerAcl(this.clazz);
732
+ const aclCode = ControllerInfoUtil.getControllerAcl(this.clazz);
733
+ const hosts = ControllerInfoUtil.getControllerHosts(this.clazz);
734
+ const metadata = new HTTPControllerMeta(clazzName, protoName, controllerName, httpPath, httpMiddlewares, methods, needAcl, aclCode, hosts);
735
+ ControllerMetadataUtil.setControllerMetadata(this.clazz, metadata);
736
+ for (const method of metadata.methods) {
737
+ const realPath = metadata.getMethodRealPath(method);
738
+ if (!realPath.startsWith("/")) {
739
+ const desc = ClassUtil.classDescription(this.clazz);
740
+ throw new Error(`class ${desc} method ${method.name} path ${realPath} not start with /`);
741
+ }
742
+ }
743
+ return metadata;
744
+ }
745
+ static create(clazz) {
746
+ return new HTTPControllerMetaBuilder(clazz);
747
+ }
748
+ };
749
+ ControllerMetaBuilderFactory.registerControllerMetaBuilder(ControllerType.HTTP, HTTPControllerMetaBuilder.create);
750
+
751
+ //#endregion
752
+ 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$1 as Request, RequestParamMeta };