@zenofolio/hyper-decor 1.0.4 → 1.0.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 (189) hide show
  1. package/.agent/rules/philosophy.md +103 -0
  2. package/.agent/rules/writing.md +32 -0
  3. package/README.md +107 -142
  4. package/dist/__internals/constants.d.ts +1 -58
  5. package/dist/__internals/constants.js +3 -65
  6. package/dist/__internals/helpers/lifecycle.helper.d.ts +3 -0
  7. package/dist/__internals/helpers/lifecycle.helper.js +25 -0
  8. package/dist/__internals/helpers/merge-metadata.d.ts +2 -3
  9. package/dist/__internals/helpers/merge-metadata.js +43 -23
  10. package/dist/__internals/helpers/prepare.helper.d.ts +5 -0
  11. package/dist/__internals/helpers/prepare.helper.js +392 -0
  12. package/dist/__internals/stores/hyper.store.d.ts +5 -0
  13. package/dist/__internals/stores/hyper.store.js +5 -0
  14. package/dist/__internals/stores/index.d.ts +4 -15
  15. package/dist/__internals/stores/index.js +18 -35
  16. package/dist/__internals/stores/meta.store.d.ts +34 -0
  17. package/dist/__internals/stores/meta.store.js +107 -0
  18. package/dist/__internals/stores/scope.store.d.ts +14 -0
  19. package/dist/__internals/stores/scope.store.js +29 -0
  20. package/dist/__internals/stores/serivces.store.d.ts +1 -0
  21. package/dist/__internals/stores/{store.interface.js → serivces.store.js} +2 -0
  22. package/dist/__internals/transform/middleware.transform.d.ts +3 -0
  23. package/dist/__internals/transform/middleware.transform.js +19 -0
  24. package/dist/__internals/transform/role.transform.d.ts +1 -1
  25. package/dist/__internals/transform/role.transform.js +10 -7
  26. package/dist/__internals/transform/scope.transfrom.d.ts +5 -7
  27. package/dist/__internals/transform/scope.transfrom.js +53 -39
  28. package/dist/__internals/transform/transform.registry.d.ts +33 -0
  29. package/dist/__internals/transform/transform.registry.js +59 -0
  30. package/dist/__internals/types.d.ts +53 -4
  31. package/dist/__internals/utils/function.util.d.ts +1 -4
  32. package/dist/__internals/utils/function.util.js +22 -10
  33. package/dist/common/bootstrap.d.ts +6 -2
  34. package/dist/common/bootstrap.js +51 -8
  35. package/dist/common/helpers/index.d.ts +1 -0
  36. package/dist/common/helpers/index.js +1 -0
  37. package/dist/common/helpers/scopes.d.ts +3 -3
  38. package/dist/common/helpers/scopes.js +7 -8
  39. package/dist/common/helpers/state.d.ts +17 -0
  40. package/dist/common/helpers/state.js +44 -0
  41. package/dist/common/message-bus.d.ts +11 -0
  42. package/dist/common/message-bus.js +51 -0
  43. package/dist/common/testing.d.ts +56 -0
  44. package/dist/common/testing.js +193 -0
  45. package/dist/common/transport.d.ts +10 -0
  46. package/dist/common/transport.js +59 -0
  47. package/dist/constants.d.ts +1 -0
  48. package/dist/constants.js +2 -1
  49. package/dist/decorators.d.ts +1 -0
  50. package/dist/{common/openapi/index.js → decorators.js} +1 -2
  51. package/dist/extension.js +11 -2
  52. package/dist/index.d.ts +11 -3
  53. package/dist/index.js +11 -3
  54. package/dist/lib/event/meta.store.d.ts +4 -0
  55. package/dist/lib/event/meta.store.js +5 -0
  56. package/dist/lib/openapi/collectors/class.collector.d.ts +5 -0
  57. package/dist/lib/openapi/collectors/class.collector.js +10 -0
  58. package/dist/lib/openapi/collectors/index.d.ts +3 -0
  59. package/dist/lib/openapi/collectors/index.js +19 -0
  60. package/dist/lib/openapi/collectors/method.collector.d.ts +7 -0
  61. package/dist/lib/openapi/collectors/method.collector.js +101 -0
  62. package/dist/lib/openapi/collectors/schema.collector.d.ts +6 -0
  63. package/dist/lib/openapi/collectors/schema.collector.js +29 -0
  64. package/dist/lib/openapi/constants.d.ts +47 -0
  65. package/dist/lib/openapi/constants.js +61 -0
  66. package/dist/lib/openapi/decorators.d.ts +16 -0
  67. package/dist/lib/openapi/decorators.js +93 -0
  68. package/dist/lib/openapi/index.d.ts +30 -0
  69. package/dist/lib/openapi/index.js +136 -0
  70. package/dist/lib/openapi/metadata.d.ts +7 -0
  71. package/dist/lib/openapi/metadata.js +8 -0
  72. package/dist/lib/openapi/metadata.registry.d.ts +29 -0
  73. package/dist/lib/openapi/metadata.registry.js +41 -0
  74. package/dist/lib/openapi/types.d.ts +131 -0
  75. package/dist/lib/server/decorators/File.d.ts +37 -0
  76. package/dist/{decorators → lib/server/decorators}/File.js +62 -75
  77. package/dist/lib/server/decorators/Http.d.ts +12 -0
  78. package/dist/lib/server/decorators/Http.js +56 -0
  79. package/dist/lib/server/decorators/HyperApp.d.ts +7 -0
  80. package/dist/lib/server/decorators/HyperApp.js +14 -0
  81. package/dist/lib/server/decorators/HyperController.d.ts +6 -0
  82. package/dist/lib/server/decorators/HyperController.js +15 -0
  83. package/dist/lib/server/decorators/HyperModule.d.ts +6 -0
  84. package/dist/lib/server/decorators/HyperModule.js +13 -0
  85. package/dist/lib/server/decorators/HyperService.d.ts +12 -0
  86. package/dist/lib/server/decorators/HyperService.js +30 -0
  87. package/dist/lib/server/decorators/Messaging.d.ts +8 -0
  88. package/dist/lib/server/decorators/Messaging.js +19 -0
  89. package/dist/lib/server/decorators/Middleware.d.ts +18 -0
  90. package/dist/lib/server/decorators/Middleware.js +52 -0
  91. package/dist/lib/server/decorators/Output.d.ts +6 -0
  92. package/dist/lib/server/decorators/Output.js +14 -0
  93. package/dist/lib/server/decorators/Pass.d.ts +10 -0
  94. package/dist/lib/server/decorators/Pass.js +13 -0
  95. package/dist/lib/server/decorators/Role.d.ts +7 -0
  96. package/dist/lib/server/decorators/Role.js +14 -0
  97. package/dist/lib/server/decorators/Routes.d.ts +14 -0
  98. package/dist/lib/server/decorators/Routes.js +39 -0
  99. package/dist/lib/server/decorators/Scope.d.ts +7 -0
  100. package/dist/lib/server/decorators/Scope.js +14 -0
  101. package/dist/{decorators → lib/server/decorators}/index.d.ts +9 -4
  102. package/dist/{decorators → lib/server/decorators}/index.js +9 -4
  103. package/dist/lib/server/decorators/metadata.d.ts +1 -0
  104. package/dist/lib/server/decorators/metadata.js +5 -0
  105. package/dist/lib/server/decorators/types.d.ts +125 -0
  106. package/dist/lib/server/decorators/types.js +6 -0
  107. package/dist/{exeptions → lib/server/exeptions}/HyperException.d.ts +2 -1
  108. package/dist/{exeptions → lib/server/exeptions}/HyperException.js +2 -1
  109. package/dist/{exeptions → lib/server/exeptions}/HyperFileException.js +1 -1
  110. package/dist/{exeptions → lib/server/exeptions}/NotRoleException.js +1 -1
  111. package/dist/{exeptions → lib/server/exeptions}/NotScopeException.js +1 -1
  112. package/dist/lib/tree/tree.d.ts +36 -0
  113. package/dist/lib/tree/tree.js +106 -0
  114. package/dist/type.d.ts +13 -2
  115. package/hyper-express-decorators.d.ts +1 -0
  116. package/package.json +81 -60
  117. package/scripts/clean.js +56 -0
  118. package/scripts/test-server.ts +85 -0
  119. package/tsconfig.json +18 -13
  120. package/vitest.config.mjs +30 -0
  121. package/.mocharc.js +0 -5
  122. package/dist/__internals/creators/request.creator.d.ts +0 -12
  123. package/dist/__internals/creators/request.creator.js +0 -53
  124. package/dist/__internals/creators/routes.creator.d.ts +0 -10
  125. package/dist/__internals/creators/routes.creator.js +0 -37
  126. package/dist/__internals/decorator-base.d.ts +0 -30
  127. package/dist/__internals/decorator-base.js +0 -86
  128. package/dist/__internals/store.d.ts +0 -10
  129. package/dist/__internals/store.js +0 -17
  130. package/dist/__internals/stores/middleware.store.d.ts +0 -7
  131. package/dist/__internals/stores/middleware.store.js +0 -19
  132. package/dist/__internals/stores/params.store.d.ts +0 -21
  133. package/dist/__internals/stores/params.store.js +0 -65
  134. package/dist/__internals/stores/routes.store.d.ts +0 -17
  135. package/dist/__internals/stores/routes.store.js +0 -43
  136. package/dist/__internals/stores/store.interface.d.ts +0 -8
  137. package/dist/__internals/transform/method.transform.d.ts +0 -2
  138. package/dist/__internals/transform/method.transform.js +0 -20
  139. package/dist/__internals/transform/pass.transfrom.d.ts +0 -1
  140. package/dist/__internals/transform/pass.transfrom.js +0 -2
  141. package/dist/__internals/utils/mixin.utils.d.ts +0 -11
  142. package/dist/__internals/utils/mixin.utils.js +0 -34
  143. package/dist/__internals/utils/router.d.ts +0 -1
  144. package/dist/__internals/utils/router.js +0 -2
  145. package/dist/common/openapi/collect-class-data.d.ts +0 -21
  146. package/dist/common/openapi/collect-class-data.js +0 -45
  147. package/dist/common/openapi/collect-function-data.d.ts +0 -32
  148. package/dist/common/openapi/collect-function-data.js +0 -70
  149. package/dist/common/openapi/index.d.ts +0 -2
  150. package/dist/decorators/File.d.ts +0 -65
  151. package/dist/decorators/Http.d.ts +0 -55
  152. package/dist/decorators/Http.js +0 -93
  153. package/dist/decorators/HyperApp.d.ts +0 -7
  154. package/dist/decorators/HyperApp.js +0 -262
  155. package/dist/decorators/HyperController.d.ts +0 -2
  156. package/dist/decorators/HyperController.js +0 -19
  157. package/dist/decorators/HyperModule.d.ts +0 -5
  158. package/dist/decorators/HyperModule.js +0 -14
  159. package/dist/decorators/Middleware.d.ts +0 -24
  160. package/dist/decorators/Middleware.js +0 -51
  161. package/dist/decorators/Pass.d.ts +0 -12
  162. package/dist/decorators/Pass.js +0 -29
  163. package/dist/decorators/Role.d.ts +0 -6
  164. package/dist/decorators/Role.js +0 -34
  165. package/dist/decorators/Routes.d.ts +0 -14
  166. package/dist/decorators/Routes.js +0 -21
  167. package/dist/decorators/Scope.d.ts +0 -6
  168. package/dist/decorators/Scope.js +0 -25
  169. package/dist/decorators/types.d.ts +0 -89
  170. /package/dist/{decorators → lib/openapi}/types.js +0 -0
  171. /package/dist/{exeptions → lib/server/exeptions}/DuplicateControllerPathException.d.ts +0 -0
  172. /package/dist/{exeptions → lib/server/exeptions}/DuplicateControllerPathException.js +0 -0
  173. /package/dist/{exeptions → lib/server/exeptions}/DuplicatedException.d.ts +0 -0
  174. /package/dist/{exeptions → lib/server/exeptions}/DuplicatedException.js +0 -0
  175. /package/dist/{exeptions → lib/server/exeptions}/DuplicatedHandlerException.d.ts +0 -0
  176. /package/dist/{exeptions → lib/server/exeptions}/DuplicatedHandlerException.js +0 -0
  177. /package/dist/{exeptions → lib/server/exeptions}/HyperFileException.d.ts +0 -0
  178. /package/dist/{exeptions → lib/server/exeptions}/MethodNotFountException.d.ts +0 -0
  179. /package/dist/{exeptions → lib/server/exeptions}/MethodNotFountException.js +0 -0
  180. /package/dist/{exeptions → lib/server/exeptions}/NotPropertyException.d.ts +0 -0
  181. /package/dist/{exeptions → lib/server/exeptions}/NotPropertyException.js +0 -0
  182. /package/dist/{exeptions → lib/server/exeptions}/NotRoleException.d.ts +0 -0
  183. /package/dist/{exeptions → lib/server/exeptions}/NotScopeException.d.ts +0 -0
  184. /package/dist/{exeptions → lib/server/exeptions}/WrongPlaceException.d.ts +0 -0
  185. /package/dist/{exeptions → lib/server/exeptions}/WrongPlaceException.js +0 -0
  186. /package/dist/{exeptions → lib/server/exeptions}/index.d.ts +0 -0
  187. /package/dist/{exeptions → lib/server/exeptions}/index.js +0 -0
  188. /package/dist/{exeptions → lib/server/exeptions}/types.d.ts +0 -0
  189. /package/dist/{exeptions → lib/server/exeptions}/types.js +0 -0
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var _a, _b;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Metadata = void 0;
5
+ require("reflect-metadata");
6
+ /**
7
+ * Global Symbols for cross-package consistency
8
+ */
9
+ const METADATA_STORE = Symbol.for('hyper:store');
10
+ const DISCOVERY_INDEX = Symbol.for('hyper:index');
11
+ const globalRegistry = globalThis;
12
+ /**
13
+ * Global stores ensure that even different copies of the package
14
+ * share the same metadata and discovery registry.
15
+ */
16
+ const globalStore = (_a = globalRegistry[METADATA_STORE]) !== null && _a !== void 0 ? _a : (globalRegistry[METADATA_STORE] = new WeakMap());
17
+ const globalIndex = (_b = globalRegistry[DISCOVERY_INDEX]) !== null && _b !== void 0 ? _b : (globalRegistry[DISCOVERY_INDEX] = new Set());
18
+ class Metadata {
19
+ /**
20
+ * Resolve the real metadata target.
21
+ * If an instance is passed, use its constructor.
22
+ */
23
+ static resolveTarget(target) {
24
+ return typeof target === 'function' ? target : target.constructor;
25
+ }
26
+ /**
27
+ * Track class constructors for discovery.
28
+ */
29
+ static track(target) {
30
+ if (typeof target === 'function') {
31
+ globalIndex.add(target);
32
+ }
33
+ }
34
+ /**
35
+ * Get metadata for a target.
36
+ * Auto-initializes the metadata object if missing.
37
+ */
38
+ static get(target) {
39
+ const resolved = this.resolveTarget(target);
40
+ let meta = globalStore.get(resolved);
41
+ if (!meta) {
42
+ meta = {};
43
+ globalStore.set(resolved, meta);
44
+ this.track(resolved);
45
+ }
46
+ return meta;
47
+ }
48
+ /**
49
+ * Replace the full metadata object for a target.
50
+ */
51
+ static rawSet(target, value) {
52
+ const resolved = this.resolveTarget(target);
53
+ globalStore.set(resolved, value);
54
+ this.track(resolved);
55
+ }
56
+ /**
57
+ * Create a scoped metadata accessor.
58
+ */
59
+ static prefix(name) {
60
+ const resolveKey = (key) => {
61
+ if (typeof key === 'string' || typeof key === 'symbol' || key === undefined) {
62
+ return key;
63
+ }
64
+ if (typeof key === 'object' && key !== null) {
65
+ if (key.kind === 'class')
66
+ return undefined;
67
+ if (key.name)
68
+ return key.name;
69
+ }
70
+ return undefined;
71
+ };
72
+ return {
73
+ set: (target, propertyKey, data) => {
74
+ var _a, _b, _c, _d;
75
+ const root = Metadata.get(target);
76
+ const scoped = ((_a = root[name]) !== null && _a !== void 0 ? _a : (root[name] = {}));
77
+ const key = resolveKey(propertyKey);
78
+ if (key === undefined) {
79
+ Object.assign(((_b = scoped.common) !== null && _b !== void 0 ? _b : (scoped.common = {})), data);
80
+ return;
81
+ }
82
+ const methods = ((_c = scoped.methods) !== null && _c !== void 0 ? _c : (scoped.methods = {}));
83
+ Object.assign(((_d = methods[key]) !== null && _d !== void 0 ? _d : (methods[key] = {})), data);
84
+ },
85
+ get: (target, propertyKey) => {
86
+ var _a, _b, _c;
87
+ const root = Metadata.get(target);
88
+ const scoped = root[name];
89
+ if (!scoped) {
90
+ return {};
91
+ }
92
+ const key = resolveKey(propertyKey);
93
+ if (key === undefined) {
94
+ return ((_a = scoped.common) !== null && _a !== void 0 ? _a : (scoped.common = {}));
95
+ }
96
+ return (_c = (_b = scoped.methods) === null || _b === void 0 ? void 0 : _b[key]) !== null && _c !== void 0 ? _c : {};
97
+ },
98
+ };
99
+ }
100
+ /**
101
+ * Get all tracked classes.
102
+ */
103
+ static getTrackedClasses() {
104
+ return [...globalIndex];
105
+ }
106
+ }
107
+ exports.Metadata = Metadata;
@@ -0,0 +1,14 @@
1
+ import { ScopeMap } from "../../lib/server/decorators";
2
+ /**
3
+ * This class is a store for the scopes that are defined in the application.
4
+ *
5
+ */
6
+ export declare class ScopeStore {
7
+ private static scopes;
8
+ static add(scope: ScopeMap): void;
9
+ static addAll(scopes: ScopeMap[]): void;
10
+ static getScopes(): ScopeMap[];
11
+ static getScopeNames(): string[];
12
+ static isEmpty(): boolean;
13
+ static clear(): void;
14
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScopeStore = void 0;
4
+ /**
5
+ * This class is a store for the scopes that are defined in the application.
6
+ *
7
+ */
8
+ class ScopeStore {
9
+ static add(scope) {
10
+ this.scopes.set(scope.scope, scope); // Evita duplicados basados en el nombre del scope
11
+ }
12
+ static addAll(scopes) {
13
+ scopes.forEach((scope) => this.add(scope));
14
+ }
15
+ static getScopes() {
16
+ return Array.from(this.scopes.values());
17
+ }
18
+ static getScopeNames() {
19
+ return Array.from(this.scopes.keys());
20
+ }
21
+ static isEmpty() {
22
+ return this.scopes.size === 0;
23
+ }
24
+ static clear() {
25
+ this.scopes.clear();
26
+ }
27
+ }
28
+ exports.ScopeStore = ScopeStore;
29
+ ScopeStore.scopes = new Map();
@@ -0,0 +1 @@
1
+ export declare const serviceStore: Set<any>;
@@ -1,2 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serviceStore = void 0;
4
+ exports.serviceStore = new Set();
@@ -0,0 +1,3 @@
1
+ import { MiddlewareHandler } from "hyper-express";
2
+ import { MiddlewareType } from "../../lib/server/decorators";
3
+ export default function middlewareTransformer(list: MiddlewareType[]): MiddlewareHandler[];
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = middlewareTransformer;
4
+ const tsyringe_1 = require("tsyringe");
5
+ function middlewareTransformer(list) {
6
+ return list
7
+ .map((middleware, idx) => {
8
+ if (isClass(middleware)) {
9
+ const instance = tsyringe_1.container.resolve(middleware);
10
+ return instance.handle.bind(instance);
11
+ }
12
+ if (typeof middleware === "function") {
13
+ return middleware;
14
+ }
15
+ return null;
16
+ })
17
+ .filter((middleware) => !!middleware);
18
+ }
19
+ const isClass = (fn) => typeof fn === "function" && `${fn}`.indexOf("class") === 0;
@@ -1,5 +1,5 @@
1
1
  import { MiddlewareHandler } from "hyper-express";
2
- import { RoleType } from "../../decorators";
2
+ import { RoleType } from "../../lib/server/decorators";
3
3
  export default function roleTransform(list: RoleType[], callback?: (middleware: MiddlewareHandler, roles: {
4
4
  role: string;
5
5
  description: string;
@@ -1,18 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = roleTransform;
4
- const exeptions_1 = require("../../exeptions");
5
- const helpers_1 = require("../../common/helpers");
4
+ const exeptions_1 = require("../../lib/server/exeptions");
5
+ const role_1 = require("../../common/helpers/role");
6
+ const constants_1 = require("../../constants");
6
7
  function roleTransform(list, callback) {
7
8
  const { roles, names, isEmtpy } = resolveRoles(list);
8
9
  const middleware = (req, res, next) => {
9
- // if scopes is empty, then we don't need to check for scopes
10
+ var _a;
10
11
  if (isEmtpy)
11
12
  return next();
12
- // get the user scopes
13
- const requestRoles = (0, helpers_1.getRoles)(req);
14
- // find the first scope that is not in the userScopes
15
- const role = roles.find((scope) => requestRoles === null || requestRoles === void 0 ? void 0 : requestRoles.includes(scope.role));
13
+ const requestRoles = (_a = (0, role_1.getRoles)(req)) !== null && _a !== void 0 ? _a : [];
14
+ if (requestRoles.length === 0 && isEmtpy)
15
+ return next();
16
+ if (requestRoles.includes(constants_1.FULL_ACCESS))
17
+ return next();
18
+ const role = roles.some((scope) => requestRoles.includes(scope.role));
16
19
  if (role) {
17
20
  return next();
18
21
  }
@@ -1,7 +1,5 @@
1
- import { MiddlewareHandler } from "hyper-express";
2
- import { ScopeType } from "../../decorators";
3
- export default function scopeTransfrom(listScopes: ScopeType[], callback?: (middleware: MiddlewareHandler, scopes: {
4
- scope: string;
5
- description: string;
6
- message: string | null;
7
- }[], names: Set<string>) => void): MiddlewareHandler;
1
+ import { MiddlewareHandler } from "hyper-express/types";
2
+ import { ScopeMap, ScopeType } from "../../lib/server/decorators";
3
+ type Callback = (middleware: MiddlewareHandler, scopes: ScopeMap[], names: Set<string>) => void;
4
+ export default function scopeTransfrom(listScopes: ScopeType[], ...callback: Callback[]): MiddlewareHandler;
5
+ export {};
@@ -1,26 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = scopeTransfrom;
4
- const exeptions_1 = require("../../exeptions");
4
+ const exeptions_1 = require("../../lib/server/exeptions");
5
5
  const helpers_1 = require("../../common/helpers");
6
- function scopeTransfrom(listScopes, callback) {
7
- const { scopes, scopeNames, isEmtpy } = resolveScopes(listScopes);
6
+ const constants_1 = require("../../constants");
7
+ function scopeTransfrom(listScopes, ...callback) {
8
+ const { scopes, scopeNames, isEmpty } = resolveScopes(listScopes);
8
9
  const middleware = (req, res, next) => {
9
10
  var _a;
10
- // if scopes is empty, then we don't need to check for scopes
11
- if (isEmtpy)
11
+ if (isEmpty)
12
12
  return next();
13
- // get the user scopes
14
- const userScopes = (0, helpers_1.getScopes)(req);
15
- // find the first scope that is not in the userScopes
16
- const error = scopes.find((scope) => !(userScopes === null || userScopes === void 0 ? void 0 : userScopes.includes(scope.scope)));
17
- if (error) {
18
- return next(new exeptions_1.NotScopeException((_a = error.message) !== null && _a !== void 0 ? _a : `You don't have the required scopes to access this resource`, userScopes, Array.from(scopeNames)));
13
+ const userScopesRaw = (0, helpers_1.getScopes)(req);
14
+ if (!userScopesRaw || userScopesRaw.length === 0) {
15
+ return next(new exeptions_1.NotScopeException(`FORBIDDEN`, [], Array.from(scopeNames)));
16
+ }
17
+ if (userScopesRaw.includes(constants_1.FULL_ACCESS))
18
+ return next();
19
+ for (const scope of scopes) {
20
+ if (!userScopesRaw.includes(scope.scope)) {
21
+ return next(new exeptions_1.NotScopeException((_a = scope.message) !== null && _a !== void 0 ? _a : `FORBIDDEN`, userScopesRaw, Array.from(scopeNames)));
22
+ }
19
23
  }
20
24
  return next();
21
25
  };
22
- if (scopeNames.size > 0 && callback) {
23
- callback(middleware, scopes, scopeNames);
26
+ if (!isEmpty && callback.length > 0) {
27
+ for (const cb of callback)
28
+ cb(middleware, scopes, scopeNames);
24
29
  }
25
30
  return middleware;
26
31
  }
@@ -35,38 +40,47 @@ const resolveScopes = (scopes) => {
35
40
  var _a, _b;
36
41
  const $scopes = {};
37
42
  for (const scope of scopes) {
38
- if (typeof scope === "string") {
39
- $scopes[scope] = {
40
- scope,
41
- description: "",
42
- message: null,
43
+ const list = parseScope(scope);
44
+ if (list.length === 0)
45
+ continue;
46
+ for (const s of list) {
47
+ $scopes[s.scope] = {
48
+ scope: s.scope,
49
+ description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
50
+ message: (_b = s.message) !== null && _b !== void 0 ? _b : `You don't have the required scopes to access this resource`,
43
51
  };
44
52
  }
45
- else if (Array.isArray(scope)) {
46
- for (const s of scope) {
47
- switch (typeof s) {
48
- case "string":
49
- $scopes[s] = {
50
- scope: s,
51
- description: "",
52
- message: null,
53
- };
54
- break;
55
- case "object":
56
- $scopes[s.scope] = {
57
- scope: s.scope,
58
- description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
59
- message: (_b = s.message) !== null && _b !== void 0 ? _b : null,
60
- };
61
- break;
62
- }
63
- }
64
- }
65
53
  }
66
54
  const values = Object.values($scopes);
67
55
  return {
68
56
  scopes: values,
69
57
  scopeNames: new Set(Object.keys($scopes)),
70
- isEmtpy: values.length === 0,
58
+ isEmpty: values.length === 0,
71
59
  };
72
60
  };
61
+ /**
62
+ * Parse the scope to a standard format
63
+ *
64
+ * @param scope
65
+ * @returns
66
+ */
67
+ const parseScope = (scope) => {
68
+ switch (typeof scope) {
69
+ case "string":
70
+ return [
71
+ {
72
+ scope,
73
+ description: "",
74
+ },
75
+ ];
76
+ case "object":
77
+ if (Array.isArray(scope)) {
78
+ if (scope.length === 0)
79
+ return [];
80
+ return scope.map((s) => parseScope(s)).flat();
81
+ }
82
+ else {
83
+ return [scope];
84
+ }
85
+ }
86
+ };
@@ -0,0 +1,33 @@
1
+ import { Request, Response } from "hyper-express";
2
+ export interface TransformContext<T = any, S = any> {
3
+ data: T;
4
+ schema: S;
5
+ options: any;
6
+ req: Request;
7
+ res: Response;
8
+ from: string;
9
+ }
10
+ export type TransformHandler<T = any, S = any> = (ctx: TransformContext<T, S>) => any | Promise<any>;
11
+ export interface ITransformer<T = any, S = any> {
12
+ transform: TransformHandler<T, S>;
13
+ getOpenApiSchema?: (schema: S) => any;
14
+ }
15
+ export type TransformerInput = TransformHandler | ITransformer;
16
+ declare class TransformRegistry {
17
+ private transformers;
18
+ /**
19
+ * Register a transformer (function or object with transform method).
20
+ */
21
+ register(input: TransformerInput): void;
22
+ /**
23
+ * Iterates through registered transformers to process the data.
24
+ * Returns the transformed data or the original data if no transformer matched.
25
+ */
26
+ resolve(ctx: TransformContext): Promise<any>;
27
+ /**
28
+ * Queries transformers to get an OpenAPI schema representation.
29
+ */
30
+ getOpenApiSchema(schema: any): any | undefined;
31
+ }
32
+ export declare const transformRegistry: TransformRegistry;
33
+ export {};
@@ -0,0 +1,59 @@
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.transformRegistry = void 0;
13
+ class TransformRegistry {
14
+ constructor() {
15
+ this.transformers = [];
16
+ }
17
+ /**
18
+ * Register a transformer (function or object with transform method).
19
+ */
20
+ register(input) {
21
+ if (typeof input === 'function') {
22
+ this.transformers.push({ transform: input });
23
+ }
24
+ else {
25
+ this.transformers.push(input);
26
+ }
27
+ }
28
+ /**
29
+ * Iterates through registered transformers to process the data.
30
+ * Returns the transformed data or the original data if no transformer matched.
31
+ */
32
+ resolve(ctx) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ let currentData = ctx.data;
35
+ for (const transformer of this.transformers) {
36
+ const result = yield transformer.transform(Object.assign(Object.assign({}, ctx), { data: currentData }));
37
+ // If transformer returns non-undefined, we assume it's the new data (or unchanged but recognized)
38
+ if (result !== undefined) {
39
+ currentData = result;
40
+ }
41
+ }
42
+ return currentData;
43
+ });
44
+ }
45
+ /**
46
+ * Queries transformers to get an OpenAPI schema representation.
47
+ */
48
+ getOpenApiSchema(schema) {
49
+ for (const transformer of this.transformers) {
50
+ if (transformer.getOpenApiSchema) {
51
+ const result = transformer.getOpenApiSchema(schema);
52
+ if (result !== undefined)
53
+ return result;
54
+ }
55
+ }
56
+ return undefined;
57
+ }
58
+ }
59
+ exports.transformRegistry = new TransformRegistry();
@@ -1,7 +1,56 @@
1
- import { RouteMetadata } from "../decorators";
2
- export interface RouterList {
3
- type: string;
4
- routes: Set<RouteMetadata>;
1
+ import { HyperAppMetadata, HyperModuleMetadata, HyperControllerMetadata, HyperParameterMetadata, RouteMetadata, Constructor, MiddlewareType, ScopeType, RoleType } from "../lib/server/decorators/types";
2
+ /**
3
+ * 🛠️ Shared Class-Level Metadata Keys
4
+ */
5
+ export interface HyperBaseCommon {
6
+ middlewares?: MiddlewareType[];
7
+ scopes?: ScopeType[];
8
+ roles?: RoleType[];
9
+ pass?: unknown;
10
+ }
11
+ /**
12
+ * 📦 Hierarchical Metadata Structure for Components
13
+ */
14
+ export type HyperCommonMetadata = (HyperAppMetadata & HyperBaseCommon & {
15
+ type: 'app';
16
+ }) | (HyperModuleMetadata & HyperBaseCommon & {
17
+ type: 'module';
18
+ }) | (HyperControllerMetadata & HyperBaseCommon & {
19
+ type: 'controller';
20
+ }) | (HyperBaseCommon & {
21
+ type: 'service';
22
+ });
23
+ /**
24
+ * 📡 Hierarchical Metadata Structure for Methods
25
+ */
26
+ export interface HyperMethodMetadata {
27
+ route?: RouteMetadata;
28
+ params?: HyperParameterMetadata;
29
+ middlewares?: MiddlewareType[];
30
+ scopes?: ScopeType[];
31
+ roles?: RoleType[];
32
+ onMessage?: {
33
+ topic: string;
34
+ };
35
+ output?: unknown;
36
+ reflection?: {
37
+ params?: (Constructor | Function)[];
38
+ output?: Constructor | Function;
39
+ };
40
+ }
41
+ /**
42
+ * 🌳 Root storage for a prefix segment (e.g. 'hyper')
43
+ */
44
+ export interface HyperPrefixRoot {
45
+ common: HyperCommonMetadata;
46
+ methods: Record<string, HyperMethodMetadata>;
47
+ }
48
+ /**
49
+ * 🏦 The global metadata store structure per class constructor
50
+ */
51
+ export interface HyperMetadataStore {
52
+ server?: HyperPrefixRoot;
53
+ openapi?: unknown;
5
54
  }
6
55
  export type HyperApplicationPrivate<T> = T & {
7
56
  prepare(): Promise<void>;
@@ -1,4 +1 @@
1
- /**
2
- * Extracts argument names from a function.
3
- */
4
- export declare const extreactArgsNames: (fn: (...args: any[]) => any) => string[] | null;
1
+ export declare const extractArgsNames: (fn: (...args: any[]) => any, replacer?: string) => string[] | null;
@@ -1,20 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extreactArgsNames = void 0;
4
- /**
5
- * Extracts argument names from a function.
6
- */
7
- const extreactArgsNames = (fn) => {
3
+ exports.extractArgsNames = void 0;
4
+ const extractArgsNames = (fn, replacer = "param") => {
8
5
  try {
6
+ // Convertimos la función en un string
9
7
  const str = fn.toString();
10
- const args = str
8
+ // Usamos una expresión regular para capturar los nombres de los parámetros
9
+ const argNames = [];
10
+ const regex = /(\w+|\{[^}]+\}|\[[^\]]+\])/g;
11
+ const matches = str
11
12
  .slice(str.indexOf("(") + 1, str.indexOf(")"))
12
- .split(",")
13
- .map((arg) => arg.trim());
14
- return args;
13
+ .match(regex);
14
+ // Si se encuentran coincidencias, las filtramos y las limpiamos
15
+ if (matches) {
16
+ matches.forEach((arg, index) => {
17
+ // Si el argumento es desestructurado, asignamos un nombre genérico
18
+ if (arg.includes("{") || arg.includes("[")) {
19
+ argNames.push(`${replacer}${index}`);
20
+ }
21
+ else {
22
+ argNames.push(arg.trim());
23
+ }
24
+ });
25
+ }
26
+ return argNames.length > 0 ? argNames : null;
15
27
  }
16
28
  catch (error) {
17
29
  return null;
18
30
  }
19
31
  };
20
- exports.extreactArgsNames = extreactArgsNames;
32
+ exports.extractArgsNames = extractArgsNames;
@@ -1,3 +1,7 @@
1
1
  import { IHyperApp, IHyperApplication } from "../type";
2
- export default function createApplication<T extends IHyperApplication>(app: new (...args: any[]) => T): Promise<IHyperApp<T>>;
3
- export { createApplication };
2
+ /**
3
+ * Creates a new HyperApplication.
4
+ *
5
+ * @param app
6
+ */
7
+ export declare function createApplication<T extends IHyperApplication>(application: new (...args: any[]) => T): Promise<IHyperApp<T>>;
@@ -9,17 +9,60 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.default = createApplication;
13
12
  exports.createApplication = createApplication;
14
13
  const tsyringe_1 = require("tsyringe");
15
- function createApplication(app) {
14
+ const message_bus_1 = require("./message-bus");
15
+ const stores_1 = require("../__internals/stores");
16
+ const prepare_helper_1 = require("../__internals/helpers/prepare.helper");
17
+ const transform_registry_1 = require("../__internals/transform/transform.registry");
18
+ /**
19
+ * Creates a new HyperApplication.
20
+ *
21
+ * @param app
22
+ */
23
+ function createApplication(application) {
16
24
  return __awaiter(this, void 0, void 0, function* () {
17
- var _a;
18
- const instance = tsyringe_1.container.resolve(app);
19
- if (instance.prepare) {
20
- yield instance.prepare();
25
+ const metadata = stores_1.HyperMeta.get(application);
26
+ if (metadata.type !== "app") {
27
+ throw new Error("Application must be decorated with @HyperApp");
21
28
  }
22
- (_a = instance === null || instance === void 0 ? void 0 : instance.onPrepare) === null || _a === void 0 ? void 0 : _a.call(instance);
23
- return instance;
29
+ const logger = metadata.logger || console.log;
30
+ const logWrapper = (space, msg) => {
31
+ var _a;
32
+ if ((_a = metadata.logs) === null || _a === void 0 ? void 0 : _a[space]) {
33
+ logger(`[${space.toUpperCase()}] ${msg}`);
34
+ }
35
+ };
36
+ const appServer = yield (0, prepare_helper_1.prepareApplication)(metadata, application, logWrapper);
37
+ const appInstance = tsyringe_1.container.resolve(application);
38
+ const appProxy = new Proxy(appInstance, {
39
+ get(target, prop) {
40
+ if (prop === "useTransform") {
41
+ return (transformer) => {
42
+ transform_registry_1.transformRegistry.register(transformer);
43
+ return appProxy;
44
+ };
45
+ }
46
+ if (prop === "emit") {
47
+ return (topic, data) => __awaiter(this, void 0, void 0, function* () {
48
+ const bus = tsyringe_1.container.resolve(message_bus_1.MessageBus);
49
+ yield bus.emit(topic, data);
50
+ });
51
+ }
52
+ // Prioritize application instance methods/props
53
+ if (prop in target) {
54
+ const value = target[prop];
55
+ return typeof value === "function" ? value.bind(target) : value;
56
+ }
57
+ // Fallback to hyper-express server
58
+ const serverValue = appServer[prop];
59
+ return typeof serverValue === "function" ? serverValue.bind(appServer) : serverValue;
60
+ },
61
+ });
62
+ if ("onPrepare" in appInstance && typeof appInstance.onPrepare === "function") {
63
+ const onPrepare = appInstance.onPrepare.bind(appInstance);
64
+ yield onPrepare();
65
+ }
66
+ return appProxy;
24
67
  });
25
68
  }
@@ -1,2 +1,3 @@
1
1
  export * from "./role";
2
2
  export * from "./scopes";
3
+ export * from "./state";
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./role"), exports);
18
18
  __exportStar(require("./scopes"), exports);
19
+ __exportStar(require("./state"), exports);