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