@zenofolio/hyper-decor 1.0.4 → 1.0.7

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 (154) hide show
  1. package/README.md +64 -1
  2. package/debug.txt +1 -0
  3. package/dist/__internals/constants.d.ts +5 -1
  4. package/dist/__internals/constants.js +5 -1
  5. package/dist/__internals/creators/request.creator.d.ts +2 -3
  6. package/dist/__internals/creators/request.creator.js +5 -3
  7. package/dist/__internals/creators/routes.creator.d.ts +2 -3
  8. package/dist/__internals/creators/routes.creator.js +5 -3
  9. package/dist/__internals/decorator-base.js +47 -11
  10. package/dist/__internals/helpers/imports.helper.d.ts +8 -0
  11. package/dist/__internals/helpers/imports.helper.js +92 -0
  12. package/dist/__internals/helpers/lifecycle.helper.d.ts +3 -0
  13. package/dist/__internals/helpers/lifecycle.helper.js +25 -0
  14. package/dist/__internals/helpers/merge-metadata.js +16 -20
  15. package/dist/__internals/helpers/prepare.helper.d.ts +11 -0
  16. package/dist/__internals/helpers/prepare.helper.js +348 -0
  17. package/dist/__internals/helpers/tree.helper.d.ts +36 -0
  18. package/dist/__internals/helpers/tree.helper.js +81 -0
  19. package/dist/__internals/stores/service.store.d.ts +1 -0
  20. package/dist/__internals/{transform/pass.transfrom.js → stores/service.store.js} +2 -0
  21. package/dist/__internals/transform/middleware.transform.d.ts +3 -0
  22. package/dist/__internals/transform/middleware.transform.js +19 -0
  23. package/dist/__internals/transform/role.transform.js +8 -5
  24. package/dist/__internals/transform/scope.transfrom.d.ts +5 -7
  25. package/dist/__internals/transform/scope.transfrom.js +52 -38
  26. package/dist/__internals/transform/transform.registry.d.ts +33 -0
  27. package/dist/__internals/transform/transform.registry.js +59 -0
  28. package/dist/__internals/utils/function.util.d.ts +1 -4
  29. package/dist/__internals/utils/function.util.js +22 -10
  30. package/dist/common/bootstrap.js +4 -0
  31. package/dist/common/helpers/index.d.ts +1 -0
  32. package/dist/common/helpers/index.js +1 -0
  33. package/dist/common/helpers/scopes.d.ts +3 -3
  34. package/dist/common/helpers/scopes.js +7 -8
  35. package/dist/common/helpers/state.d.ts +17 -0
  36. package/dist/common/helpers/state.js +44 -0
  37. package/dist/common/message-bus.d.ts +11 -0
  38. package/dist/common/message-bus.js +51 -0
  39. package/dist/common/transport.d.ts +9 -0
  40. package/dist/common/transport.js +46 -0
  41. package/dist/decorators/File.d.ts +1 -1
  42. package/dist/decorators/Http.d.ts +20 -13
  43. package/dist/decorators/Http.js +77 -31
  44. package/dist/decorators/HyperApp.d.ts +0 -4
  45. package/dist/decorators/HyperApp.js +16 -182
  46. package/dist/decorators/HyperController.js +2 -1
  47. package/dist/decorators/HyperService.d.ts +15 -0
  48. package/dist/decorators/HyperService.js +29 -0
  49. package/dist/decorators/Messaging.d.ts +10 -0
  50. package/dist/decorators/Messaging.js +22 -0
  51. package/dist/decorators/Middleware.d.ts +5 -4
  52. package/dist/decorators/Middleware.js +33 -19
  53. package/dist/decorators/Output.d.ts +9 -0
  54. package/dist/decorators/Output.js +18 -0
  55. package/dist/decorators/Pass.d.ts +6 -3
  56. package/dist/decorators/Pass.js +6 -3
  57. package/dist/decorators/Role.js +5 -5
  58. package/dist/decorators/Routes.d.ts +14 -13
  59. package/dist/decorators/Scope.js +3 -3
  60. package/dist/decorators/Transform.d.ts +14 -0
  61. package/dist/decorators/Transform.js +18 -0
  62. package/dist/decorators/index.d.ts +3 -0
  63. package/dist/decorators/index.js +3 -0
  64. package/dist/decorators/types.d.ts +45 -10
  65. package/dist/decorators/types.js +4 -0
  66. package/dist/extension.js +11 -2
  67. package/dist/index.d.ts +6 -0
  68. package/dist/index.js +6 -0
  69. package/dist/lib/openapi/collectors/class.collector.d.ts +9 -0
  70. package/dist/lib/openapi/collectors/class.collector.js +67 -0
  71. package/dist/lib/openapi/collectors/index.d.ts +4 -0
  72. package/dist/lib/openapi/collectors/index.js +20 -0
  73. package/dist/lib/openapi/collectors/method.collector.d.ts +2 -0
  74. package/dist/lib/openapi/collectors/method.collector.js +68 -0
  75. package/dist/lib/openapi/collectors/param.collector.d.ts +2 -0
  76. package/dist/lib/openapi/collectors/param.collector.js +64 -0
  77. package/dist/lib/openapi/collectors/schema.collector.d.ts +11 -0
  78. package/dist/lib/openapi/collectors/schema.collector.js +37 -0
  79. package/dist/lib/openapi/constants.d.ts +46 -0
  80. package/dist/lib/openapi/constants.js +61 -0
  81. package/dist/lib/openapi/decorators/api-bearer-auth.decorator.d.ts +2 -0
  82. package/dist/lib/openapi/decorators/api-bearer-auth.decorator.js +10 -0
  83. package/dist/lib/openapi/decorators/api-method.decorator.d.ts +3 -0
  84. package/dist/lib/openapi/decorators/api-method.decorator.js +11 -0
  85. package/dist/lib/openapi/decorators/api-parameter.decorator.d.ts +3 -0
  86. package/dist/lib/openapi/decorators/api-parameter.decorator.js +10 -0
  87. package/dist/lib/openapi/decorators/api-request-body.decorator.d.ts +3 -0
  88. package/dist/lib/openapi/decorators/api-request-body.decorator.js +10 -0
  89. package/dist/lib/openapi/decorators/api-response.decorator.d.ts +3 -0
  90. package/dist/lib/openapi/decorators/api-response.decorator.js +10 -0
  91. package/dist/lib/openapi/decorators/api-security.decorator.d.ts +3 -0
  92. package/dist/lib/openapi/decorators/api-security.decorator.js +10 -0
  93. package/dist/lib/openapi/decorators/api-tag.decorator.d.ts +3 -0
  94. package/dist/lib/openapi/decorators/api-tag.decorator.js +13 -0
  95. package/dist/lib/openapi/decorators/index.d.ts +7 -0
  96. package/dist/lib/openapi/decorators/index.js +23 -0
  97. package/dist/lib/openapi/helpers/index.d.ts +7 -0
  98. package/dist/lib/openapi/helpers/index.js +23 -0
  99. package/dist/lib/openapi/helpers/method.helper.d.ts +3 -0
  100. package/dist/lib/openapi/helpers/method.helper.js +20 -0
  101. package/dist/lib/openapi/helpers/openapi.helper.d.ts +7 -0
  102. package/dist/lib/openapi/helpers/openapi.helper.js +51 -0
  103. package/dist/lib/openapi/helpers/parameter.helper.d.ts +3 -0
  104. package/dist/lib/openapi/helpers/parameter.helper.js +16 -0
  105. package/dist/lib/openapi/helpers/request-body.helper.d.ts +3 -0
  106. package/dist/lib/openapi/helpers/request-body.helper.js +9 -0
  107. package/dist/lib/openapi/helpers/response.helper.d.ts +3 -0
  108. package/dist/lib/openapi/helpers/response.helper.js +18 -0
  109. package/dist/lib/openapi/helpers/security.helper.d.ts +3 -0
  110. package/dist/lib/openapi/helpers/security.helper.js +17 -0
  111. package/dist/lib/openapi/helpers/tag.helper.d.ts +3 -0
  112. package/dist/lib/openapi/helpers/tag.helper.js +10 -0
  113. package/dist/lib/openapi/index.d.ts +6 -0
  114. package/dist/lib/openapi/index.js +22 -0
  115. package/dist/lib/openapi/metadata.registry.d.ts +29 -0
  116. package/dist/lib/openapi/metadata.registry.js +41 -0
  117. package/dist/lib/openapi/types.d.ts +131 -0
  118. package/dist/stores/index.d.ts +1 -0
  119. package/dist/{common/openapi → stores}/index.js +1 -2
  120. package/dist/stores/scope.store.d.ts +14 -0
  121. package/dist/stores/scope.store.js +29 -0
  122. package/dist/type.d.ts +13 -2
  123. package/hyper-express-decorators.d.ts +1 -0
  124. package/package.json +73 -60
  125. package/scripts/clean.js +56 -0
  126. package/scripts/test-server.ts +85 -0
  127. package/tsconfig.json +18 -13
  128. package/vitest.config.mjs +30 -0
  129. package/vitest.json +0 -0
  130. package/.mocharc.js +0 -5
  131. package/dist/__internals/store.d.ts +0 -10
  132. package/dist/__internals/store.js +0 -17
  133. package/dist/__internals/stores/middleware.store.d.ts +0 -7
  134. package/dist/__internals/stores/middleware.store.js +0 -19
  135. package/dist/__internals/stores/params.store.d.ts +0 -21
  136. package/dist/__internals/stores/params.store.js +0 -65
  137. package/dist/__internals/stores/routes.store.d.ts +0 -17
  138. package/dist/__internals/stores/routes.store.js +0 -43
  139. package/dist/__internals/stores/store.interface.d.ts +0 -8
  140. package/dist/__internals/transform/method.transform.d.ts +0 -2
  141. package/dist/__internals/transform/method.transform.js +0 -20
  142. package/dist/__internals/transform/pass.transfrom.d.ts +0 -1
  143. package/dist/__internals/utils/mixin.utils.d.ts +0 -11
  144. package/dist/__internals/utils/mixin.utils.js +0 -34
  145. package/dist/__internals/utils/router.d.ts +0 -1
  146. package/dist/__internals/utils/router.js +0 -2
  147. package/dist/common/openapi/collect-class-data.d.ts +0 -21
  148. package/dist/common/openapi/collect-class-data.js +0 -45
  149. package/dist/common/openapi/collect-function-data.d.ts +0 -32
  150. package/dist/common/openapi/collect-function-data.js +0 -70
  151. package/dist/common/openapi/index.d.ts +0 -2
  152. /package/dist/__internals/stores/{index.d.ts → metadata.store.d.ts} +0 -0
  153. /package/dist/__internals/stores/{index.js → metadata.store.js} +0 -0
  154. /package/dist/{__internals/stores/store.interface.js → lib/openapi/types.js} +0 -0
package/README.md CHANGED
@@ -144,7 +144,7 @@ class TestController extends CRUD<string> {
144
144
  ```typescript
145
145
  @HyperModule({
146
146
  path: "users",
147
- controllers: [HyperController]
147
+ controllers: [TestController]
148
148
  })
149
149
  class UserModule {}
150
150
  ```
@@ -185,7 +185,70 @@ As a result, we get:
185
185
  - [Middleware](./examples/middleware.ts)
186
186
  - [File](./examples//upload-file.ts)
187
187
 
188
+ ## Services & Dependency Injection
189
+ You can use `@HyperService()` with `tsyringe` to inject dependencies into controllers or other services.
188
190
 
191
+ ```typescript
192
+ import { injectable } from "tsyringe";
193
+
194
+ @injectable()
195
+ @HyperService()
196
+ class UserService {
197
+ getUsers() { return ["User1", "User2"]; }
198
+ }
199
+
200
+ @HyperController("/users")
201
+ class UserController {
202
+ constructor(private userService: UserService) {}
203
+
204
+ @Get("/")
205
+ getUsers(@Res() res: Response) {
206
+ res.json(this.userService.getUsers());
207
+ }
208
+ }
209
+ ```
210
+
211
+ ## Agnostic Body Validation & Transformation (`@Transform`)
212
+ You can use `@Transform` to validate and transform incoming requests agnostic of the validation library (like Zod) while seamlessly syncing with OpenAPI definitions.
213
+
214
+ ```typescript
215
+ const ZodTransformer = {
216
+ transform: ({ data, schema }) => {
217
+ if (schema && schema._type === "zod") {
218
+ // Validate and return the parsed data
219
+ return { ...data, parsed: true };
220
+ }
221
+ return data;
222
+ },
223
+ getOpenApiSchema: (schema) => {
224
+ if (schema._type === "zod") {
225
+ return { type: "object", properties: { /* derived from schema */ } };
226
+ }
227
+ }
228
+ };
229
+
230
+ const app = await createApplication(Application);
231
+ app.useTransform(ZodTransformer); // Register your custom transformer globally
232
+
233
+ @HyperController("/users")
234
+ class UserController {
235
+ @Post("/")
236
+ @Transform({ _type: "zod" /* pass your schema */ })
237
+ createUser(@Body() data: any, @Res() res: Response) {
238
+ res.json(data); // `data` is automatically intercepted and transformed!
239
+ }
240
+ }
241
+ ```
242
+
243
+ ## OpenAPI Generation
244
+ Generate a complete OpenAPI specification out-of-the-box leveraging your application tree and decorators footprint.
245
+
246
+ ```typescript
247
+ import { getOpenAPI } from "@zenofolio/hyper-decor/lib/openapi";
248
+
249
+ const openApiDoc = getOpenAPI(Application);
250
+ console.log(openApiDoc.info.title, openApiDoc.paths);
251
+ ```
189
252
 
190
253
  # All for now
191
254
  More documentation will be added here late.
package/debug.txt ADDED
@@ -0,0 +1 @@
1
+ State: class-before-dep-ok
@@ -3,6 +3,7 @@ export declare const KEY_TYPE_CONTROLLER = "hyper:type:controller";
3
3
  export declare const KEY_TYPE_MODULE = "hyper:type:module";
4
4
  export declare const KEY_TYPE_ROUTE = "hyper:type:route";
5
5
  export declare const KEY_TYPE_SERVICE = "hyper:type:service";
6
+ export declare const KEY_TYPE_GUARD = "hyper:type:guard";
6
7
  export type KeyTypes = typeof KEY_TYPE_APP | typeof KEY_TYPE_CONTROLLER | typeof KEY_TYPE_MODULE | typeof KEY_TYPE_SERVICE | typeof KEY_TYPE_ROUTE;
7
8
  export declare const KEY_PARAMS_APP = "hyper:type:app";
8
9
  export declare const KEY_PARAMS_CONTROLLER = "hyper:type:controller";
@@ -13,7 +14,8 @@ export declare const KEY_PARAMS_MIDDLEWARES = "hyper:type:middlewares";
13
14
  export declare const KEY_PARAMS_SCOPE = "hyper:type:scope";
14
15
  export declare const KEY_PARAMS_ROLE = "hyper:type:role";
15
16
  export declare const KEY_PARAMS_PASS = "hyper:type:pass";
16
- export type KeyParams = typeof KEY_PARAMS_APP | typeof KEY_PARAMS_CONTROLLER | typeof KEY_PARAMS_MODULE | typeof KEY_PARAMS_ROUTE | typeof KEY_PARAMS_PARAM | typeof KEY_PARAMS_MIDDLEWARES | typeof KEY_PARAMS_SCOPE | typeof KEY_PARAMS_ROLE | typeof KEY_PARAMS_PASS;
17
+ export declare const KEY_OUTPUT_SCHEMA = "hyper:output:schema";
18
+ export type KeyParams = typeof KEY_PARAMS_APP | typeof KEY_PARAMS_CONTROLLER | typeof KEY_PARAMS_MODULE | typeof KEY_PARAMS_ROUTE | typeof KEY_PARAMS_PARAM | typeof KEY_PARAMS_MIDDLEWARES | typeof KEY_PARAMS_SCOPE | typeof KEY_PARAMS_ROLE | typeof KEY_PARAMS_PASS | typeof KEY_OUTPUT_SCHEMA;
17
19
  export declare const KEY_STATE_UPDATED = "hyper:state:updated";
18
20
  export declare const KEY_STATE_CREATED = "hyper:state:created";
19
21
  export declare const KEY_STATE_PREPARED = "hyper:state:prepared";
@@ -40,6 +42,7 @@ export declare const METADATA_KEYS: {
40
42
  SCOPES: string;
41
43
  SCOPED: string;
42
44
  MIDDLEWARES: string;
45
+ ON_MESSAGE: string;
43
46
  };
44
47
  export declare const METADATA_STORE_KEYS: {
45
48
  PARAMS: string;
@@ -59,3 +62,4 @@ export declare const METADATA_STATE_KEYS: {
59
62
  CREATED: string;
60
63
  PREPARED: string;
61
64
  };
65
+ export declare const FULL_ACCESS = "*";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.METADATA_STATE_KEYS = exports.METADATA_PARAMS_KEYS = exports.METADATA_METHOD_KEYS = exports.METADATA_STORE_KEYS = exports.METADATA_KEYS = exports.METADATA_HYPER_TYPE = exports.DESIGN_TYPE = exports.DESIGN_RETURNTYPE = exports.DESIGN_PARAMTYPES = exports.KEY_STATE_BY_PASS = exports.KEY_STATE_PREPARED = exports.KEY_STATE_CREATED = exports.KEY_STATE_UPDATED = exports.KEY_PARAMS_PASS = exports.KEY_PARAMS_ROLE = exports.KEY_PARAMS_SCOPE = exports.KEY_PARAMS_MIDDLEWARES = exports.KEY_PARAMS_PARAM = exports.KEY_PARAMS_ROUTE = exports.KEY_PARAMS_MODULE = exports.KEY_PARAMS_CONTROLLER = exports.KEY_PARAMS_APP = exports.KEY_TYPE_SERVICE = exports.KEY_TYPE_ROUTE = exports.KEY_TYPE_MODULE = exports.KEY_TYPE_CONTROLLER = exports.KEY_TYPE_APP = void 0;
3
+ exports.FULL_ACCESS = exports.METADATA_STATE_KEYS = exports.METADATA_PARAMS_KEYS = exports.METADATA_METHOD_KEYS = exports.METADATA_STORE_KEYS = exports.METADATA_KEYS = exports.METADATA_HYPER_TYPE = exports.DESIGN_TYPE = exports.DESIGN_RETURNTYPE = exports.DESIGN_PARAMTYPES = exports.KEY_STATE_BY_PASS = exports.KEY_STATE_PREPARED = exports.KEY_STATE_CREATED = exports.KEY_STATE_UPDATED = exports.KEY_OUTPUT_SCHEMA = exports.KEY_PARAMS_PASS = exports.KEY_PARAMS_ROLE = exports.KEY_PARAMS_SCOPE = exports.KEY_PARAMS_MIDDLEWARES = exports.KEY_PARAMS_PARAM = exports.KEY_PARAMS_ROUTE = exports.KEY_PARAMS_MODULE = exports.KEY_PARAMS_CONTROLLER = exports.KEY_PARAMS_APP = exports.KEY_TYPE_GUARD = exports.KEY_TYPE_SERVICE = exports.KEY_TYPE_ROUTE = exports.KEY_TYPE_MODULE = exports.KEY_TYPE_CONTROLLER = exports.KEY_TYPE_APP = void 0;
4
4
  //////////////////////////////
5
5
  /// Types constants
6
6
  //////////////////////////////
@@ -9,6 +9,7 @@ exports.KEY_TYPE_CONTROLLER = "hyper:type:controller";
9
9
  exports.KEY_TYPE_MODULE = "hyper:type:module";
10
10
  exports.KEY_TYPE_ROUTE = "hyper:type:route";
11
11
  exports.KEY_TYPE_SERVICE = "hyper:type:service";
12
+ exports.KEY_TYPE_GUARD = "hyper:type:guard";
12
13
  //////////////////////////////
13
14
  /// Params constants
14
15
  //////////////////////////////
@@ -21,6 +22,7 @@ exports.KEY_PARAMS_MIDDLEWARES = "hyper:type:middlewares";
21
22
  exports.KEY_PARAMS_SCOPE = "hyper:type:scope";
22
23
  exports.KEY_PARAMS_ROLE = "hyper:type:role";
23
24
  exports.KEY_PARAMS_PASS = "hyper:type:pass";
25
+ exports.KEY_OUTPUT_SCHEMA = "hyper:output:schema";
24
26
  //////////////////////////////
25
27
  /// State constants
26
28
  //////////////////////////////
@@ -51,6 +53,7 @@ exports.METADATA_KEYS = {
51
53
  SCOPES: "hyper:scopes",
52
54
  SCOPED: "hyper:scoped",
53
55
  MIDDLEWARES: "hyper:middleware",
56
+ ON_MESSAGE: "hyper:on_message",
54
57
  };
55
58
  exports.METADATA_STORE_KEYS = {
56
59
  PARAMS: "hyper:store:params",
@@ -70,3 +73,4 @@ exports.METADATA_STATE_KEYS = {
70
73
  CREATED: "hyper:created",
71
74
  PREPARED: "hyper:prepared",
72
75
  };
76
+ exports.FULL_ACCESS = "*";
@@ -1,6 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { Request } from "hyper-express";
3
- import { ByPassKeys } from "../stores/params.store";
2
+ import { Request } from "hyper-express/types";
4
3
  import { ParameterResolver } from "../../decorators";
5
4
  /**
6
5
  * Creates a parameter decorator for handling request data.
@@ -9,4 +8,4 @@ import { ParameterResolver } from "../../decorators";
9
8
  * @param {IParamsResolver} resolver - Resolver function to handle the parameter.
10
9
  * @returns {ParameterDecorator} - The parameter decorator function.
11
10
  */
12
- export default function createParamDecorator(key: keyof Request | ByPassKeys, decoratorName: string, resolver: ParameterResolver): ParameterDecorator;
11
+ export default function createParamDecorator(key: keyof Request | "req" | "res", decoratorName: string, resolver: ParameterResolver, schema?: any, isWholeSource?: boolean): ParameterDecorator;
@@ -5,11 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.default = createParamDecorator;
7
7
  require("reflect-metadata");
8
- const function_util_1 = require("../utils/function.util");
9
8
  const constants_1 = require("../constants");
10
9
  const decorator_base_1 = require("../decorator-base");
11
10
  const who_helper_1 = __importDefault(require("../helpers/who.helper"));
12
11
  const WrongPlaceException_1 = __importDefault(require("../../exeptions/WrongPlaceException"));
12
+ const function_util_1 = require("../utils/function.util");
13
13
  /**
14
14
  * Creates a parameter decorator for handling request data.
15
15
  *
@@ -17,7 +17,7 @@ const WrongPlaceException_1 = __importDefault(require("../../exeptions/WrongPlac
17
17
  * @param {IParamsResolver} resolver - Resolver function to handle the parameter.
18
18
  * @returns {ParameterDecorator} - The parameter decorator function.
19
19
  */
20
- function createParamDecorator(key, decoratorName, resolver) {
20
+ function createParamDecorator(key, decoratorName, resolver, schema, isWholeSource) {
21
21
  const _key = key;
22
22
  return (0, decorator_base_1.DecoratorHelper)({
23
23
  type: constants_1.KEY_TYPE_CONTROLLER,
@@ -27,7 +27,7 @@ function createParamDecorator(key, decoratorName, resolver) {
27
27
  if (!isProperty)
28
28
  throw new WrongPlaceException_1.default(decoratorName, "parameter", `${Target.constructor.name}.${propertyKey}`, Target);
29
29
  const saved = options !== null && options !== void 0 ? options : { params: {} };
30
- const names = (0, function_util_1.extreactArgsNames)(Target[propertyKey]);
30
+ const names = (0, function_util_1.extractArgsNames)(Target[propertyKey]);
31
31
  const types = Reflect.getMetadata(constants_1.DESIGN_PARAMTYPES, Target, propertyKey);
32
32
  const name = names === null || names === void 0 ? void 0 : names[parameterIndex];
33
33
  const type = types === null || types === void 0 ? void 0 : types[parameterIndex];
@@ -42,6 +42,8 @@ function createParamDecorator(key, decoratorName, resolver) {
42
42
  key: _key,
43
43
  method: propertyKey.toString(),
44
44
  resolver,
45
+ schema,
46
+ isWholeSource,
45
47
  });
46
48
  // sort by index
47
49
  saved.params[propertyKey].sort((a, b) => a.index - b.index);
@@ -1,10 +1,9 @@
1
- import { Request, Response } from "hyper-express";
1
+ import "reflect-metadata";
2
2
  /**
3
3
  * Helper function to create route decorators for HTTP methods.
4
4
  *
5
5
  * @param {string} method - The HTTP method (e.g., GET, POST).
6
- * @param {(req: Request, res: Response) => any} [resolver] - Optional resolver for the route.
7
6
  * @returns {(path?: string) => MethodDecorator} - A method decorator for defining routes.
8
7
  *
9
8
  */
10
- export default function createRouteDecorator(method: string, resolver?: (req: Request, res: Response) => any): (path?: string) => MethodDecorator & ClassDecorator;
9
+ export default function createRouteDecorator<T extends any = undefined>(method: string): (path?: string, options?: T) => MethodDecorator & ClassDecorator;
@@ -1,22 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = createRouteDecorator;
4
+ require("reflect-metadata");
4
5
  const constants_1 = require("../constants");
5
6
  const decorator_base_1 = require("../decorator-base");
6
7
  /**
7
8
  * Helper function to create route decorators for HTTP methods.
8
9
  *
9
10
  * @param {string} method - The HTTP method (e.g., GET, POST).
10
- * @param {(req: Request, res: Response) => any} [resolver] - Optional resolver for the route.
11
11
  * @returns {(path?: string) => MethodDecorator} - A method decorator for defining routes.
12
12
  *
13
13
  */
14
- function createRouteDecorator(method, resolver) {
15
- return (path = "/") => (0, decorator_base_1.DecoratorHelper)({
14
+ function createRouteDecorator(method) {
15
+ return (path = "/", options) => (0, decorator_base_1.DecoratorHelper)({
16
16
  type: constants_1.KEY_TYPE_CONTROLLER,
17
17
  key: constants_1.KEY_PARAMS_ROUTE,
18
18
  targetResolver: (target) => { var _a; return (_a = target.constructor) !== null && _a !== void 0 ? _a : target; },
19
19
  options: (data, Target, propertyKey, descriptor) => {
20
+ // add openAPI data here
20
21
  var _a;
21
22
  const handler = descriptor.value;
22
23
  if (typeof handler !== "function")
@@ -30,6 +31,7 @@ function createRouteDecorator(method, resolver) {
30
31
  path,
31
32
  propertyKey,
32
33
  handler: handler,
34
+ options,
33
35
  });
34
36
  return saved;
35
37
  },
@@ -48,15 +48,20 @@ function DecoratorHelper({ key, type, options, targetResolver, onDefineData }, .
48
48
  else {
49
49
  (0, exports.defineDecorData)(key, Object.assign({ type }, options), Target);
50
50
  value = Reflect.decorate(transforms, Target);
51
+ // If the class was wrapped/replaced, apply metadata to the new class too
52
+ if (value && value !== Target) {
53
+ (0, exports.defineDecorData)(key, Object.assign({ type }, options), value);
54
+ }
51
55
  }
52
56
  }
53
57
  (0, tsyringe_1.injectable)()((0, object_util_1.$constructor)(target));
54
58
  return value;
55
59
  };
56
60
  }
61
+ const METADATA_CACHE = new WeakMap();
57
62
  const defineDecorData = (key, options, target, property, descriptor) => {
58
63
  let value = options;
59
- if (typeof options === "function") {
64
+ if (typeof options === "function" && !(options.prototype && options.prototype.constructor === options)) {
60
65
  const old = (0, exports.getDecorData)(key, target, property);
61
66
  value = Object.assign(Object.assign({}, old), options(old, target, property, descriptor));
62
67
  }
@@ -66,21 +71,52 @@ const defineDecorData = (key, options, target, property, descriptor) => {
66
71
  else {
67
72
  Reflect.defineMetadata(key, value, target);
68
73
  }
74
+ // Update Cache
75
+ let targetCache = METADATA_CACHE.get(target);
76
+ if (!targetCache) {
77
+ targetCache = new Map();
78
+ METADATA_CACHE.set(target, targetCache);
79
+ }
80
+ targetCache.set(property ? `${key}:${property}` : key, value);
69
81
  };
70
82
  exports.defineDecorData = defineDecorData;
71
- const getDecorData = (key, target, property) => property
72
- ? Reflect.getMetadata(key, target, property)
73
- : Reflect.getMetadata(key, target);
83
+ const getDecorData = (key, target, property) => {
84
+ const cacheKey = property ? `${key}:${property}` : key;
85
+ const targetCache = METADATA_CACHE.get(target);
86
+ if (targetCache && targetCache.has(cacheKey)) {
87
+ return targetCache.get(cacheKey);
88
+ }
89
+ const value = property
90
+ ? Reflect.getMetadata(key, target, property)
91
+ : Reflect.getMetadata(key, target);
92
+ if (value !== undefined) {
93
+ let tc = METADATA_CACHE.get(target);
94
+ if (!tc) {
95
+ tc = new Map();
96
+ METADATA_CACHE.set(target, tc);
97
+ }
98
+ tc.set(cacheKey, value);
99
+ }
100
+ return value;
101
+ };
74
102
  exports.getDecorData = getDecorData;
75
- const hasDecorData = (key, target, property) => property
76
- ? Reflect.hasMetadata(key, target, property)
77
- : Reflect.hasMetadata(key, target);
103
+ const hasDecorData = (key, target, property) => {
104
+ const cacheKey = property ? `${key}:${property}` : key;
105
+ const targetCache = METADATA_CACHE.get(target);
106
+ if (targetCache && targetCache.has(cacheKey))
107
+ return true;
108
+ return property
109
+ ? Reflect.hasMetadata(key, target, property)
110
+ : Reflect.hasMetadata(key, target);
111
+ };
78
112
  exports.hasDecorData = hasDecorData;
79
113
  const extractDecorData = (target) => {
80
114
  const keys = Reflect.getMetadataKeys(target);
81
- return keys.reduce((acc, key) => {
82
- acc[key] = Reflect.getMetadata(key, target);
83
- return acc;
84
- }, {});
115
+ const result = {};
116
+ for (let i = 0; i < keys.length; i++) {
117
+ const key = keys[i];
118
+ result[key] = (0, exports.getDecorData)(key, target);
119
+ }
120
+ return result;
85
121
  };
86
122
  exports.extractDecorData = extractDecorData;
@@ -0,0 +1,8 @@
1
+ import { IHyperHooks, ImportType } from "../../decorators/types";
2
+ /**
3
+ * Prepare imports for the target class.
4
+ *
5
+ * @param target
6
+ * @param imports
7
+ */
8
+ export declare function prepareImports(_target: any, imports: ImportType[], hooks?: IHyperHooks, context?: any): Promise<void>;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.prepareImports = prepareImports;
13
+ const tsyringe_1 = require("tsyringe");
14
+ const lifecycle_helper_1 = require("./lifecycle.helper");
15
+ const decorator_base_1 = require("../decorator-base");
16
+ const constants_1 = require("../constants");
17
+ const message_bus_1 = require("../../common/message-bus");
18
+ /**
19
+ * Prepare imports for the target class.
20
+ *
21
+ * @param target
22
+ * @param imports
23
+ */
24
+ function prepareImports(_target, imports, hooks, context) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const bus = tsyringe_1.container.resolve(message_bus_1.MessageBus);
27
+ yield Promise.all(imports.map((item) => __awaiter(this, void 0, void 0, function* () {
28
+ let token;
29
+ if (typeof item === "function" || typeof item === "string" || typeof item === "symbol") {
30
+ token = item;
31
+ }
32
+ else if (item && typeof item === "object" && "token" in item) {
33
+ token = item.token;
34
+ if (item.useClass) {
35
+ tsyringe_1.container.register(token, { useClass: item.useClass }, item.options);
36
+ }
37
+ else if ("useValue" in item) {
38
+ tsyringe_1.container.registerInstance(token, item.useValue);
39
+ return;
40
+ }
41
+ else if (item.useFactory) {
42
+ tsyringe_1.container.register(token, { useFactory: item.useFactory }, item.options);
43
+ return;
44
+ }
45
+ else if (item.useToken) {
46
+ tsyringe_1.container.register(token, { useToken: item.useToken }, item.options);
47
+ return;
48
+ }
49
+ }
50
+ if (!token)
51
+ return;
52
+ try {
53
+ const instance = tsyringe_1.container.resolve(token);
54
+ if (!instance)
55
+ return;
56
+ // Skip if already initialized to avoid double work/double subscription
57
+ const alreadyDone = (0, lifecycle_helper_1.isInitialized)(instance);
58
+ if (!alreadyDone) {
59
+ if (hooks === null || hooks === void 0 ? void 0 : hooks.onBeforeInit) {
60
+ yield hooks.onBeforeInit(instance, token, context);
61
+ }
62
+ }
63
+ yield (0, lifecycle_helper_1.initializeInstance)(instance);
64
+ if (!alreadyDone) {
65
+ if (hooks === null || hooks === void 0 ? void 0 : hooks.onAfterInit) {
66
+ yield hooks.onAfterInit(instance, token, context);
67
+ }
68
+ // Handle singleton registration if it's a constructor and not registered
69
+ if (typeof token === "function" && !tsyringe_1.container.isRegistered(token)) {
70
+ const isSingleton = typeof instance.isSingleton === "function" ? instance.isSingleton() : true;
71
+ if (isSingleton) {
72
+ tsyringe_1.container.registerInstance(token, instance);
73
+ }
74
+ }
75
+ // Discovery: Messaging (only on first init)
76
+ const messaging = (0, decorator_base_1.getDecorData)(constants_1.METADATA_KEYS.ON_MESSAGE, typeof token === "function" ? token : instance.constructor);
77
+ if (messaging === null || messaging === void 0 ? void 0 : messaging.length) {
78
+ messaging.forEach((msg) => {
79
+ bus.listen(msg.topic, instance[msg.propertyKey].bind(instance));
80
+ });
81
+ }
82
+ }
83
+ }
84
+ catch (e) {
85
+ // Skip dependencies that cannot be resolved automatically
86
+ // Log a warning to prevent silent failures in production
87
+ const name = typeof token === "function" ? token.name : String(token);
88
+ console.warn(`[HyperDecor] Warn: Could not resolve dependency for token "${name}". It might be missing injectable() or not exported correctly.`);
89
+ }
90
+ })));
91
+ });
92
+ }
@@ -0,0 +1,3 @@
1
+ export declare const isInitialized: (target: any) => boolean;
2
+ export declare const setInitialized: (target: any) => boolean;
3
+ export declare function initializeInstance(instance: any): Promise<void>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.setInitialized = exports.isInitialized = void 0;
13
+ exports.initializeInstance = initializeInstance;
14
+ const isInitialized = (target) => Reflect.get(target, "____initialized") === true;
15
+ exports.isInitialized = isInitialized;
16
+ const setInitialized = (target) => Reflect.set(target, "____initialized", true);
17
+ exports.setInitialized = setInitialized;
18
+ function initializeInstance(instance) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ if (instance && typeof instance.onInit === "function" && !(0, exports.isInitialized)(instance)) {
21
+ yield instance.onInit();
22
+ (0, exports.setInitialized)(instance);
23
+ }
24
+ });
25
+ }
@@ -10,35 +10,31 @@ require("reflect-metadata");
10
10
  * @param options Merge options (e.g., array handling).
11
11
  */
12
12
  function mergeMetadata(target, source, keys, options = {}) {
13
- for (const key of keys) {
13
+ for (let i = 0; i < keys.length; i++) {
14
+ const key = keys[i];
14
15
  const sourceMeta = Reflect.getMetadata(key, source);
15
- const targetMeta = Reflect.getMetadata(key, target);
16
16
  if (sourceMeta === undefined)
17
17
  continue;
18
- let mergedMeta = sourceMeta;
19
- if (typeof sourceMeta === "object" && typeof targetMeta === "object") {
20
- mergedMeta = deepMerge(targetMeta, sourceMeta, options);
21
- }
22
- Reflect.defineMetadata(key, mergedMeta, target);
18
+ const targetMeta = Reflect.getMetadata(key, target);
19
+ Reflect.defineMetadata(key, targetMeta === undefined ? sourceMeta : deepMerge(targetMeta, sourceMeta, options), target);
23
20
  }
24
21
  }
25
- /**
26
- * Performs a deep merge of two objects.
27
- * @param target Target object.
28
- * @param source Source object.
29
- * @param options Merge options.
30
- * @returns Merged object.
31
- */
32
22
  function deepMerge(target, source, options) {
33
23
  if (Array.isArray(target) && Array.isArray(source)) {
34
- return (options.overwriteArrays ? source : Array.from(new Set([...target, ...source])));
24
+ if (options.overwriteArrays)
25
+ return source;
26
+ // Faster deduplication for primitive arrays
27
+ const combined = target.concat(source);
28
+ return Array.from(new Set(combined));
35
29
  }
36
- if (typeof target === "object" && target !== null && typeof source === "object" && source !== null) {
37
- const merged = Object.assign({}, target);
38
- for (const key of Object.keys(source)) {
39
- merged[key] = deepMerge(target[key], source[key], options);
30
+ if (source && typeof source === "object" && target && typeof target === "object") {
31
+ const result = Object.assign({}, target);
32
+ const sourceKeys = Object.keys(source);
33
+ for (let i = 0; i < sourceKeys.length; i++) {
34
+ const k = sourceKeys[i];
35
+ result[k] = deepMerge(result[k], source[k], options);
40
36
  }
41
- return merged;
37
+ return result;
42
38
  }
43
39
  return source;
44
40
  }
@@ -0,0 +1,11 @@
1
+ import { Server } from "hyper-express";
2
+ import { HyperAppMetadata, LogSpaces } from "../../decorators/types";
3
+ /**
4
+ * Prepare the application with the given options.
5
+ *
6
+ * @param options
7
+ * @param Target
8
+ * @param app
9
+ * @param log
10
+ */
11
+ export declare function prepareApplication(options: HyperAppMetadata, Target: any, app: Server, log: (space: keyof LogSpaces, message: string) => void): Promise<void>;