@zenofolio/hyper-decor 1.0.46 → 1.0.48

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 (60) hide show
  1. package/dist/__internals/creators/request.creator.js +2 -2
  2. package/dist/__internals/creators/routes.creator.d.ts +1 -0
  3. package/dist/__internals/creators/routes.creator.js +2 -0
  4. package/dist/__internals/helpers/imports.helper.d.ts +8 -0
  5. package/dist/__internals/helpers/imports.helper.js +45 -0
  6. package/dist/__internals/helpers/prepare.helper.d.ts +11 -0
  7. package/dist/__internals/helpers/prepare.helper.js +237 -0
  8. package/dist/__internals/utils/function.util.d.ts +1 -4
  9. package/dist/__internals/utils/function.util.js +22 -10
  10. package/dist/decorators/HyperApp.js +2 -185
  11. package/dist/decorators/HyperController.js +2 -1
  12. package/dist/decorators/Scope.js +1 -1
  13. package/dist/decorators/Service.d.ts +5 -0
  14. package/dist/decorators/Service.js +16 -0
  15. package/dist/decorators/index.d.ts +1 -0
  16. package/dist/decorators/index.js +1 -0
  17. package/dist/decorators/types.d.ts +10 -3
  18. package/dist/lib/openapi/collectors/class.collector.d.ts +9 -0
  19. package/dist/lib/openapi/collectors/class.collector.js +53 -0
  20. package/dist/lib/openapi/collectors/index.d.ts +3 -0
  21. package/dist/lib/openapi/collectors/index.js +19 -0
  22. package/dist/lib/openapi/collectors/method.collector.d.ts +3 -0
  23. package/dist/lib/openapi/collectors/method.collector.js +36 -0
  24. package/dist/lib/openapi/collectors/param.collector.d.ts +3 -0
  25. package/dist/lib/openapi/collectors/param.collector.js +27 -0
  26. package/dist/lib/openapi/constants.d.ts +46 -0
  27. package/dist/lib/openapi/constants.js +61 -0
  28. package/dist/lib/openapi/decorators/api-method.decorator.d.ts +3 -0
  29. package/dist/lib/openapi/decorators/api-method.decorator.js +11 -0
  30. package/dist/lib/openapi/decorators/api-parameter.decorator.d.ts +3 -0
  31. package/dist/lib/openapi/decorators/api-parameter.decorator.js +10 -0
  32. package/dist/lib/openapi/decorators/api-request-body.decorator.d.ts +3 -0
  33. package/dist/lib/openapi/decorators/api-request-body.decorator.js +10 -0
  34. package/dist/lib/openapi/decorators/api-response.decodator.d.ts +3 -0
  35. package/dist/lib/openapi/decorators/api-response.decodator.js +10 -0
  36. package/dist/lib/openapi/decorators/api-security.decorator.d.ts +3 -0
  37. package/dist/lib/openapi/decorators/api-security.decorator.js +10 -0
  38. package/dist/lib/openapi/decorators/api-tag.decorator.d.ts +3 -0
  39. package/dist/lib/openapi/decorators/api-tag.decorator.js +10 -0
  40. package/dist/lib/openapi/decorators/index.d.ts +6 -0
  41. package/dist/lib/openapi/decorators/index.js +22 -0
  42. package/dist/lib/openapi/helpers/index.d.ts +6 -0
  43. package/dist/lib/openapi/helpers/index.js +22 -0
  44. package/dist/lib/openapi/helpers/method.helper.d.ts +3 -0
  45. package/dist/lib/openapi/helpers/method.helper.js +20 -0
  46. package/dist/lib/openapi/helpers/parameter.helper.d.ts +3 -0
  47. package/dist/lib/openapi/helpers/parameter.helper.js +16 -0
  48. package/dist/lib/openapi/helpers/request-body.helper.d.ts +3 -0
  49. package/dist/lib/openapi/helpers/request-body.helper.js +9 -0
  50. package/dist/lib/openapi/helpers/response.helper.d.ts +3 -0
  51. package/dist/lib/openapi/helpers/response.helper.js +18 -0
  52. package/dist/lib/openapi/helpers/security.helper.d.ts +3 -0
  53. package/dist/lib/openapi/helpers/security.helper.js +10 -0
  54. package/dist/lib/openapi/helpers/tag.helper.d.ts +3 -0
  55. package/dist/lib/openapi/helpers/tag.helper.js +10 -0
  56. package/dist/lib/openapi/index.d.ts +1 -0
  57. package/dist/lib/openapi/index.js +17 -0
  58. package/dist/lib/openapi/types.d.ts +131 -0
  59. package/dist/lib/openapi/types.js +2 -0
  60. package/package.json +1 -1
@@ -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
  *
@@ -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];
@@ -1,3 +1,4 @@
1
+ import "reflect-metadata";
1
2
  import { Request, Response } from "hyper-express";
2
3
  /**
3
4
  * Helper function to create route decorators for HTTP methods.
@@ -1,6 +1,7 @@
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
  /**
@@ -17,6 +18,7 @@ function createRouteDecorator(method, resolver) {
17
18
  key: constants_1.KEY_PARAMS_ROUTE,
18
19
  targetResolver: (target) => { var _a; return (_a = target.constructor) !== null && _a !== void 0 ? _a : target; },
19
20
  options: (data, Target, propertyKey, descriptor) => {
21
+ // add openAPI data here
20
22
  var _a;
21
23
  const handler = descriptor.value;
22
24
  if (typeof handler !== "function")
@@ -0,0 +1,8 @@
1
+ import { 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[]): Promise<void>;
@@ -0,0 +1,45 @@
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
+ /**
15
+ * Prepare imports for the target class.
16
+ *
17
+ * @param target
18
+ * @param imports
19
+ */
20
+ function prepareImports(target, imports) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ var _a;
23
+ for (const service of imports) {
24
+ const _class = tsyringe_1.container.resolve(service);
25
+ if (!_class)
26
+ continue;
27
+ const isSingleton = ((_a = _class.isSingleton) === null || _a === void 0 ? void 0 : _a.call(_class)) === true;
28
+ if (isSingleton) {
29
+ if (isInitialized(_class))
30
+ continue;
31
+ tsyringe_1.container.registerInstance(service, _class);
32
+ }
33
+ if (typeof _class.onInit === "function") {
34
+ yield _class.onInit();
35
+ if (isSingleton)
36
+ setInitialized(_class);
37
+ }
38
+ }
39
+ });
40
+ }
41
+ ////////////////////////
42
+ /// Utils
43
+ ////////////////////////
44
+ const isInitialized = (target) => Reflect.get(target, "____initialized") === true;
45
+ const setInitialized = (target) => Reflect.set(target, "____initialized", true);
@@ -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>;
@@ -0,0 +1,237 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.prepareApplication = prepareApplication;
16
+ const hyper_express_1 = require("hyper-express");
17
+ const collectors_1 = require("../../collectors");
18
+ const role_transform_1 = __importDefault(require("../transform/role.transform"));
19
+ const scope_transfrom_1 = __importDefault(require("../transform/scope.transfrom"));
20
+ const object_util_1 = require("../utils/object.util");
21
+ const decorator_base_1 = require("../decorator-base");
22
+ const constants_1 = require("../constants");
23
+ const middleware_transform_1 = __importDefault(require("../transform/middleware.transform"));
24
+ const tsyringe_1 = require("tsyringe");
25
+ const path_util_1 = require("../utils/path.util");
26
+ const imports_helper_1 = require("./imports.helper");
27
+ const if_router = (current) => {
28
+ if (!current || !((current === null || current === void 0 ? void 0 : current.prototype) instanceof hyper_express_1.Router))
29
+ return new hyper_express_1.Router();
30
+ return new current();
31
+ };
32
+ /**
33
+ * Extract the data from the target class.
34
+ *
35
+ * @param target
36
+ * @returns
37
+ */
38
+ function getData(target) {
39
+ var _a, _b, _c, _d, _e;
40
+ const app = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_APP, target);
41
+ const module = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_MODULE, target);
42
+ const controller = (0, decorator_base_1.getDecorData)(constants_1.KEY_TYPE_CONTROLLER, target);
43
+ const middlewares = (0, middleware_transform_1.default)((_a = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_MIDDLEWARES, target)) !== null && _a !== void 0 ? _a : []);
44
+ const scopes = (_b = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_SCOPE, target)) !== null && _b !== void 0 ? _b : [];
45
+ const roles = (_c = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_ROLE, target)) !== null && _c !== void 0 ? _c : [];
46
+ const routes = (_d = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_ROUTE, target)) !== null && _d !== void 0 ? _d : {
47
+ routes: [],
48
+ };
49
+ const params = (_e = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_PARAM, target)) !== null && _e !== void 0 ? _e : {};
50
+ const pass = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_PASS, target);
51
+ return {
52
+ app,
53
+ module,
54
+ controller,
55
+ middlewares,
56
+ scopes,
57
+ roles,
58
+ routes,
59
+ params,
60
+ pass,
61
+ };
62
+ }
63
+ /**
64
+ * Prepare the application with the given options.
65
+ *
66
+ * @param options
67
+ * @param Target
68
+ * @param app
69
+ * @param log
70
+ */
71
+ function prepareApplication(options, Target, app, log) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ var _a, _b;
74
+ const data = getData(Target);
75
+ const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : "/";
76
+ const imports = (_b = options.imports) !== null && _b !== void 0 ? _b : [];
77
+ yield (0, imports_helper_1.prepareImports)(Target, imports);
78
+ if (data.middlewares.length) {
79
+ app.use(...data.middlewares);
80
+ log("middleware", `${Target.name} with middlewares: ${data.middlewares
81
+ .map((e) => e.name)
82
+ .join(", ")}`);
83
+ }
84
+ (0, scope_transfrom_1.default)(data.scopes, (middleware, scopes) => {
85
+ collectors_1.ScopeStore.addAll(scopes);
86
+ app.use(middleware);
87
+ log("middleware", `${Target.name} with scopes: ${data.scopes.join(", ")}`);
88
+ });
89
+ (0, role_transform_1.default)(data.roles, (middleware) => {
90
+ app.use(middleware);
91
+ log("middleware", `${Target.name} with roles: ${data.roles.join(", ")}`);
92
+ });
93
+ const routers = yield Promise.all(options.modules.map((module) => __awaiter(this, void 0, void 0, function* () {
94
+ var _a, _b, _c, _d;
95
+ const data = getData(module);
96
+ const path = (_b = (_a = data.module) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : "/";
97
+ const imports = (_d = (_c = data.module) === null || _c === void 0 ? void 0 : _c.imports) !== null && _d !== void 0 ? _d : [];
98
+ return prepareTarget({
99
+ target: module,
100
+ router: if_router(module),
101
+ namespace: `${Target.name}/${module.name}`,
102
+ instance: app,
103
+ prefix: path,
104
+ imports: imports,
105
+ log,
106
+ });
107
+ })));
108
+ routers.forEach(({ router, path }) => {
109
+ app.use((0, path_util_1.join)(prefix, path), router);
110
+ });
111
+ });
112
+ }
113
+ /**
114
+ * Prepare the target class
115
+ * HyperModule or HyperController can be used as target.
116
+ *
117
+ * @param param0
118
+ * @returns
119
+ */
120
+ function prepareTarget(_a) {
121
+ return __awaiter(this, arguments, void 0, function* ({ target, router, prefix = "/", namespace = "", instance, imports = [], log, }) {
122
+ var _b, _c, _d, _e, _f, _g, _h, _j;
123
+ const _router = router !== null && router !== void 0 ? router : if_router(target);
124
+ const data = getData(target);
125
+ const modules = (_c = (_b = data.module) === null || _b === void 0 ? void 0 : _b.modules) !== null && _c !== void 0 ? _c : [];
126
+ const controllers = (_e = (_d = data.module) === null || _d === void 0 ? void 0 : _d.controllers) !== null && _e !== void 0 ? _e : [];
127
+ const routes = (_f = data.routes) !== null && _f !== void 0 ? _f : [];
128
+ const middlewares = (_g = data.middlewares) !== null && _g !== void 0 ? _g : [];
129
+ const scopes = (_h = data.scopes) !== null && _h !== void 0 ? _h : [];
130
+ const roles = (_j = data.roles) !== null && _j !== void 0 ? _j : [];
131
+ ////////////////////////////////
132
+ /// Prepare Imports
133
+ ////////////////////////////////
134
+ yield (0, imports_helper_1.prepareImports)(target, imports);
135
+ ////////////////////////////////
136
+ /// Attach Middlewares
137
+ ////////////////////////////////
138
+ _router.use(...middlewares);
139
+ (0, scope_transfrom_1.default)(scopes, (middleware, scopes) => {
140
+ collectors_1.ScopeStore.addAll(scopes);
141
+ _router.use(middleware);
142
+ });
143
+ (0, role_transform_1.default)(roles, (middleware) => _router.use(middleware));
144
+ ////////////////////////////////
145
+ /// Prepare Modules
146
+ ////////////////////////////////
147
+ yield (0, object_util_1.$each)(modules, (module) => __awaiter(this, void 0, void 0, function* () {
148
+ const moduleData = getData(module);
149
+ if (!moduleData.module)
150
+ return;
151
+ const router = yield prepareTarget({
152
+ target: module,
153
+ imports: moduleData.module.imports,
154
+ namespace: `${namespace}/${module.name}`,
155
+ prefix: moduleData.module.path,
156
+ instance: tsyringe_1.container.resolve(module),
157
+ log,
158
+ });
159
+ _router.use(router.path, router.router);
160
+ }));
161
+ // ////////////////////////////////
162
+ // /// Prepare Controllers
163
+ // ////////////////////////////////
164
+ yield (0, object_util_1.$each)(controllers, (controller) => __awaiter(this, void 0, void 0, function* () {
165
+ const data = getData(controller);
166
+ const controllerData = data.controller;
167
+ if (!controllerData)
168
+ return;
169
+ const router = yield prepareTarget({
170
+ target: controller,
171
+ namespace: `${namespace}/${controller.name}`,
172
+ prefix: controllerData.path,
173
+ imports: controllerData.imports,
174
+ instance: tsyringe_1.container.resolve(controller),
175
+ log,
176
+ });
177
+ _router.use(router.path, router.router);
178
+ }));
179
+ ////////////////////////////////
180
+ /// Prepare Routes
181
+ ////////////////////////////////
182
+ yield (0, object_util_1.$each)(Array.from(routes.routes), (route) => __awaiter(this, void 0, void 0, function* () {
183
+ if (typeof route !== "object")
184
+ return;
185
+ yield prepareRoutes({
186
+ target: target,
187
+ router: _router,
188
+ route,
189
+ namespace,
190
+ instance,
191
+ prefix,
192
+ log,
193
+ });
194
+ }));
195
+ return {
196
+ router: _router,
197
+ path: prefix,
198
+ };
199
+ });
200
+ }
201
+ /**
202
+ * Prepare the routes for the target class.
203
+ *
204
+ * @param param0
205
+ * @returns
206
+ */
207
+ function prepareRoutes(_a) {
208
+ return __awaiter(this, arguments, void 0, function* ({ target, router, route, instance, namespace, log, }) {
209
+ var _b, _c, _d;
210
+ const { method, path, handler, propertyKey } = route;
211
+ const metadata = getData(handler);
212
+ const params = (_d = (_c = (_b = metadata.params) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c[propertyKey]) !== null && _d !== void 0 ? _d : [];
213
+ const $fn = Reflect.get(router, method);
214
+ const hasParams = params.length > 0;
215
+ if (!$fn)
216
+ return;
217
+ const middlewares = [...metadata.middlewares];
218
+ (0, role_transform_1.default)(metadata.roles, (middleware) => middlewares.push(middleware));
219
+ (0, scope_transfrom_1.default)(metadata.scopes, (middleware, scopes) => {
220
+ middlewares.push(middleware);
221
+ collectors_1.ScopeStore.addAll(scopes);
222
+ });
223
+ log("routes", `${namespace}/${propertyKey} ${method.toUpperCase()} { ${path} }`);
224
+ if (!hasParams) {
225
+ $fn.call(router, path, ...middlewares, handler.bind(instance));
226
+ }
227
+ else {
228
+ $fn.call(router, path, ...middlewares, (req, res) => __awaiter(this, void 0, void 0, function* () {
229
+ const args = yield Promise.all(params.map((param) => __awaiter(this, void 0, void 0, function* () {
230
+ const { resolver, key } = param;
231
+ return yield resolver(req, res);
232
+ })));
233
+ return handler.bind(instance)(...args, req, res);
234
+ }));
235
+ }
236
+ });
237
+ }
@@ -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;
@@ -8,23 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.HyperApp = void 0;
16
13
  require("reflect-metadata");
17
14
  const hyper_express_1 = require("hyper-express");
18
15
  const constants_1 = require("../__internals/constants");
19
16
  const decorator_base_1 = require("../__internals/decorator-base");
20
- const path_util_1 = require("../__internals/utils/path.util");
21
- const scope_transfrom_1 = __importDefault(require("../__internals/transform/scope.transfrom"));
22
- const role_transform_1 = __importDefault(require("../__internals/transform/role.transform"));
23
- const object_util_1 = require("../__internals/utils/object.util");
24
17
  const merge_metadata_1 = require("../__internals/helpers/merge-metadata");
25
- const tsyringe_1 = require("tsyringe");
26
- const collectors_1 = require("../collectors");
27
- const middleware_transform_1 = __importDefault(require("../__internals/transform/middleware.transform"));
18
+ const prepare_helper_1 = require("../__internals/helpers/prepare.helper");
28
19
  /**
29
20
  * Decorator to define the main application class with assigned modules.
30
21
  * @param modules - List of modules to be used in the application.
@@ -60,7 +51,7 @@ const HyperApp = (options) => (0, decorator_base_1.DecoratorHelper)({
60
51
  applyOptions(Target) {
61
52
  return __awaiter(this, void 0, void 0, function* () {
62
53
  var _a;
63
- yield applyAppOptions(options, Target, this, this.log.bind(this));
54
+ yield (0, prepare_helper_1.prepareApplication)(options, Target, this, this.log.bind(this));
64
55
  const target = Reflect.construct(Target, this.listArguments);
65
56
  (_a = target === null || target === void 0 ? void 0 : target.onPrepare) === null || _a === void 0 ? void 0 : _a.call(target);
66
57
  this.showLogs();
@@ -98,177 +89,3 @@ exports.HyperApp = HyperApp;
98
89
  //////////////////////////////////
99
90
  /// Private methods
100
91
  //////////////////////////////////
101
- const if_router = (current) => {
102
- if (!current || !((current === null || current === void 0 ? void 0 : current.prototype) instanceof hyper_express_1.Router))
103
- return new hyper_express_1.Router();
104
- return new current();
105
- };
106
- function getData(target) {
107
- var _a, _b, _c, _d, _e;
108
- const app = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_APP, target);
109
- const module = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_MODULE, target);
110
- const controller = (0, decorator_base_1.getDecorData)(constants_1.KEY_TYPE_CONTROLLER, target);
111
- const middlewares = (0, middleware_transform_1.default)((_a = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_MIDDLEWARES, target)) !== null && _a !== void 0 ? _a : []);
112
- const scopes = (_b = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_SCOPE, target)) !== null && _b !== void 0 ? _b : [];
113
- const roles = (_c = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_ROLE, target)) !== null && _c !== void 0 ? _c : [];
114
- const routes = (_d = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_ROUTE, target)) !== null && _d !== void 0 ? _d : {
115
- routes: [],
116
- };
117
- const params = (_e = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_PARAM, target)) !== null && _e !== void 0 ? _e : {};
118
- const pass = (0, decorator_base_1.getDecorData)(constants_1.KEY_PARAMS_PASS, target);
119
- return {
120
- app,
121
- module,
122
- controller,
123
- middlewares,
124
- scopes,
125
- roles,
126
- routes,
127
- params,
128
- pass,
129
- };
130
- }
131
- function applyAppOptions(options, Target, app, log) {
132
- return __awaiter(this, void 0, void 0, function* () {
133
- var _a, _b;
134
- const data = getData(Target);
135
- const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : "/";
136
- const services = (_b = options.imports) !== null && _b !== void 0 ? _b : [];
137
- if (data.middlewares.length) {
138
- app.use(...data.middlewares);
139
- log("middleware", `${Target.name} with middlewares: ${data.middlewares
140
- .map((e) => e.name)
141
- .join(", ")}`);
142
- }
143
- (0, scope_transfrom_1.default)(data.scopes, (middleware, scopes) => {
144
- collectors_1.ScopeStore.addAll(scopes);
145
- app.use(middleware);
146
- log("middleware", `${Target.name} with scopes: ${data.scopes.join(", ")}`);
147
- });
148
- (0, role_transform_1.default)(data.roles, (middleware) => {
149
- app.use(middleware);
150
- log("middleware", `${Target.name} with roles: ${data.roles.join(", ")}`);
151
- });
152
- const routers = yield Promise.all(options.modules.map((module) => __awaiter(this, void 0, void 0, function* () {
153
- var _a, _b;
154
- const path = (_b = (_a = getData(module).module) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : "/";
155
- return prepareTarget({
156
- target: module,
157
- router: if_router(module),
158
- namespace: `${Target.name}/${module.name}`,
159
- instance: app,
160
- prefix: path,
161
- log,
162
- });
163
- })));
164
- routers.forEach(({ router, path }) => {
165
- app.use((0, path_util_1.join)(prefix, path), router);
166
- });
167
- });
168
- }
169
- function prepareTarget(_a) {
170
- return __awaiter(this, arguments, void 0, function* ({ target, router, prefix = "/", namespace = "", instance, log, }) {
171
- var _b, _c, _d, _e, _f, _g, _h, _j;
172
- const _router = router !== null && router !== void 0 ? router : if_router(target);
173
- const data = getData(target);
174
- const modules = (_c = (_b = data.module) === null || _b === void 0 ? void 0 : _b.modules) !== null && _c !== void 0 ? _c : [];
175
- const controllers = (_e = (_d = data.module) === null || _d === void 0 ? void 0 : _d.controllers) !== null && _e !== void 0 ? _e : [];
176
- const routes = (_f = data.routes) !== null && _f !== void 0 ? _f : [];
177
- const middlewares = (_g = data.middlewares) !== null && _g !== void 0 ? _g : [];
178
- const scopes = (_h = data.scopes) !== null && _h !== void 0 ? _h : [];
179
- const roles = (_j = data.roles) !== null && _j !== void 0 ? _j : [];
180
- ////////////////////////////////
181
- /// Attach Middlewares
182
- ////////////////////////////////
183
- _router.use(...middlewares);
184
- (0, scope_transfrom_1.default)(scopes, (middleware, scopes) => {
185
- collectors_1.ScopeStore.addAll(scopes);
186
- _router.use(middleware);
187
- });
188
- (0, role_transform_1.default)(roles, (middleware) => _router.use(middleware));
189
- ////////////////////////////////
190
- /// Prepare Modules
191
- ////////////////////////////////
192
- yield (0, object_util_1.$each)(modules, (module) => __awaiter(this, void 0, void 0, function* () {
193
- const moduleData = getData(module);
194
- if (!moduleData.module)
195
- return;
196
- const router = yield prepareTarget({
197
- target: module,
198
- namespace: `${namespace}/${module.name}`,
199
- prefix: moduleData.module.path,
200
- instance: tsyringe_1.container.resolve(module),
201
- log,
202
- });
203
- _router.use(router.path, router.router);
204
- }));
205
- // ////////////////////////////////
206
- // /// Prepare Controllers
207
- // ////////////////////////////////
208
- yield (0, object_util_1.$each)(controllers, (controller) => __awaiter(this, void 0, void 0, function* () {
209
- const data = getData(controller);
210
- const controllerData = data.controller;
211
- if (!controllerData)
212
- return;
213
- const router = yield prepareTarget({
214
- target: controller,
215
- namespace: `${namespace}/${controller.name}`,
216
- prefix: controllerData.path,
217
- instance: tsyringe_1.container.resolve(controller),
218
- log,
219
- });
220
- _router.use(router.path, router.router);
221
- }));
222
- ////////////////////////////////
223
- /// Prepare Routes
224
- ////////////////////////////////
225
- yield (0, object_util_1.$each)(Array.from(routes.routes), (route) => __awaiter(this, void 0, void 0, function* () {
226
- if (typeof route !== "object")
227
- return;
228
- yield prepareRoutes({
229
- target: target,
230
- router: _router,
231
- route,
232
- namespace,
233
- instance,
234
- prefix,
235
- log,
236
- });
237
- }));
238
- return {
239
- router: _router,
240
- path: prefix,
241
- };
242
- });
243
- }
244
- function prepareRoutes(_a) {
245
- return __awaiter(this, arguments, void 0, function* ({ target, router, route, instance, namespace, log, }) {
246
- var _b, _c, _d;
247
- const { method, path, handler, propertyKey } = route;
248
- const metadata = getData(handler);
249
- const params = (_d = (_c = (_b = metadata.params) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c[propertyKey]) !== null && _d !== void 0 ? _d : [];
250
- const $fn = Reflect.get(router, method);
251
- const hasParams = params.length > 0;
252
- if (!$fn)
253
- return;
254
- const middlewares = [...metadata.middlewares];
255
- (0, role_transform_1.default)(metadata.roles, (middleware) => middlewares.push(middleware));
256
- (0, scope_transfrom_1.default)(metadata.scopes, (middleware, scopes) => {
257
- middlewares.push(middleware);
258
- collectors_1.ScopeStore.addAll(scopes);
259
- });
260
- log("routes", `${namespace}/${propertyKey} ${method.toUpperCase()} { ${path} }`);
261
- if (!hasParams) {
262
- $fn.call(router, path, ...middlewares, handler.bind(instance));
263
- }
264
- else {
265
- $fn.call(router, path, ...middlewares, (req, res) => __awaiter(this, void 0, void 0, function* () {
266
- const args = yield Promise.all(params.map((param) => __awaiter(this, void 0, void 0, function* () {
267
- const { resolver, key } = param;
268
- return yield resolver(req, res);
269
- })));
270
- return handler.bind(instance)(...args, req, res);
271
- }));
272
- }
273
- });
274
- }
@@ -4,7 +4,7 @@ exports.HyperController = void 0;
4
4
  const decorator_base_1 = require("../__internals/decorator-base");
5
5
  const constants_1 = require("../__internals/constants");
6
6
  const HyperController = (options) => {
7
- var _a, _b, _c;
7
+ var _a, _b, _c, _d;
8
8
  const isString = typeof options === "string";
9
9
  return (0, decorator_base_1.DecoratorHelper)({
10
10
  type: constants_1.KEY_TYPE_CONTROLLER,
@@ -13,6 +13,7 @@ const HyperController = (options) => {
13
13
  path: isString ? options : (_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : "/",
14
14
  roles: isString ? [] : (_b = options === null || options === void 0 ? void 0 : options.roles) !== null && _b !== void 0 ? _b : [],
15
15
  scopes: isString ? [] : (_c = options === null || options === void 0 ? void 0 : options.scopes) !== null && _c !== void 0 ? _c : [],
16
+ imports: isString ? [] : (_d = options === null || options === void 0 ? void 0 : options.imports) !== null && _d !== void 0 ? _d : [],
16
17
  },
17
18
  });
18
19
  };
@@ -12,7 +12,7 @@ const stores_1 = __importDefault(require("../__internals/stores"));
12
12
  * Scope decorator for defining access scopes.
13
13
  */
14
14
  const Scope = (scopes) => (target, propertyKey, descriptorOrIndex) => {
15
- const { isProperty, isMethod } = (0, who_helper_1.default)(target, propertyKey, descriptorOrIndex);
15
+ const { isProperty } = (0, who_helper_1.default)(target, propertyKey, descriptorOrIndex);
16
16
  if (isProperty) {
17
17
  throw new Error(`Scope decorator cannot be used as parameter decorator in ${target.constructor.name}.${propertyKey}`);
18
18
  }
@@ -0,0 +1,5 @@
1
+ interface ServiceOptions {
2
+ singleton?: boolean;
3
+ }
4
+ export declare const Service: ({ singleton, }: ServiceOptions) => ClassDecorator;
5
+ export {};