@zenofolio/hyper-decor 1.0.45 → 1.0.47
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/helpers/imports.helper.d.ts +8 -0
- package/dist/__internals/helpers/imports.helper.js +45 -0
- package/dist/__internals/transform/middleware.transform.js +6 -5
- package/dist/decorators/HyperApp.js +28 -4
- package/dist/decorators/HyperController.js +2 -1
- package/dist/decorators/Service.d.ts +5 -0
- package/dist/decorators/Service.js +16 -0
- package/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.js +1 -0
- package/dist/decorators/types.d.ts +10 -3
- package/package.json +1 -1
|
@@ -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);
|
|
@@ -5,14 +5,15 @@ const tsyringe_1 = require("tsyringe");
|
|
|
5
5
|
function middlewareTransformer(list) {
|
|
6
6
|
return list
|
|
7
7
|
.map((middleware) => {
|
|
8
|
+
if (isClass(middleware)) {
|
|
9
|
+
const instance = tsyringe_1.container.resolve(middleware);
|
|
10
|
+
return instance.handle.bind(instance);
|
|
11
|
+
}
|
|
8
12
|
if (typeof middleware === "function") {
|
|
9
|
-
if (middleware.constructor) {
|
|
10
|
-
const instance = tsyringe_1.container.resolve(middleware);
|
|
11
|
-
return instance.handle.bind(instance);
|
|
12
|
-
}
|
|
13
13
|
return middleware;
|
|
14
14
|
}
|
|
15
15
|
return null;
|
|
16
16
|
})
|
|
17
|
-
.filter((middleware) => middleware);
|
|
17
|
+
.filter((middleware) => !!middleware);
|
|
18
18
|
}
|
|
19
|
+
const isClass = (fn) => typeof fn === "function" && `${fn}`.indexOf("class") === 0;
|
|
@@ -25,6 +25,7 @@ const merge_metadata_1 = require("../__internals/helpers/merge-metadata");
|
|
|
25
25
|
const tsyringe_1 = require("tsyringe");
|
|
26
26
|
const collectors_1 = require("../collectors");
|
|
27
27
|
const middleware_transform_1 = __importDefault(require("../__internals/transform/middleware.transform"));
|
|
28
|
+
const imports_helper_1 = require("../__internals/helpers/imports.helper");
|
|
28
29
|
/**
|
|
29
30
|
* Decorator to define the main application class with assigned modules.
|
|
30
31
|
* @param modules - List of modules to be used in the application.
|
|
@@ -133,7 +134,8 @@ function applyAppOptions(options, Target, app, log) {
|
|
|
133
134
|
var _a, _b;
|
|
134
135
|
const data = getData(Target);
|
|
135
136
|
const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : "/";
|
|
136
|
-
const
|
|
137
|
+
const imports = (_b = options.imports) !== null && _b !== void 0 ? _b : [];
|
|
138
|
+
yield (0, imports_helper_1.prepareImports)(Target, imports);
|
|
137
139
|
if (data.middlewares.length) {
|
|
138
140
|
app.use(...data.middlewares);
|
|
139
141
|
log("middleware", `${Target.name} with middlewares: ${data.middlewares
|
|
@@ -150,14 +152,17 @@ function applyAppOptions(options, Target, app, log) {
|
|
|
150
152
|
log("middleware", `${Target.name} with roles: ${data.roles.join(", ")}`);
|
|
151
153
|
});
|
|
152
154
|
const routers = yield Promise.all(options.modules.map((module) => __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
var _a, _b;
|
|
154
|
-
const
|
|
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 : [];
|
|
155
159
|
return prepareTarget({
|
|
156
160
|
target: module,
|
|
157
161
|
router: if_router(module),
|
|
158
162
|
namespace: `${Target.name}/${module.name}`,
|
|
159
163
|
instance: app,
|
|
160
164
|
prefix: path,
|
|
165
|
+
imports: imports,
|
|
161
166
|
log,
|
|
162
167
|
});
|
|
163
168
|
})));
|
|
@@ -166,8 +171,15 @@ function applyAppOptions(options, Target, app, log) {
|
|
|
166
171
|
});
|
|
167
172
|
});
|
|
168
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Prepare the target class
|
|
176
|
+
* HyperModule or HyperController can be used as target.
|
|
177
|
+
*
|
|
178
|
+
* @param param0
|
|
179
|
+
* @returns
|
|
180
|
+
*/
|
|
169
181
|
function prepareTarget(_a) {
|
|
170
|
-
return __awaiter(this, arguments, void 0, function* ({ target, router, prefix = "/", namespace = "", instance, log, }) {
|
|
182
|
+
return __awaiter(this, arguments, void 0, function* ({ target, router, prefix = "/", namespace = "", instance, imports = [], log, }) {
|
|
171
183
|
var _b, _c, _d, _e, _f, _g, _h, _j;
|
|
172
184
|
const _router = router !== null && router !== void 0 ? router : if_router(target);
|
|
173
185
|
const data = getData(target);
|
|
@@ -178,6 +190,10 @@ function prepareTarget(_a) {
|
|
|
178
190
|
const scopes = (_h = data.scopes) !== null && _h !== void 0 ? _h : [];
|
|
179
191
|
const roles = (_j = data.roles) !== null && _j !== void 0 ? _j : [];
|
|
180
192
|
////////////////////////////////
|
|
193
|
+
/// Prepare Imports
|
|
194
|
+
////////////////////////////////
|
|
195
|
+
yield (0, imports_helper_1.prepareImports)(target, imports);
|
|
196
|
+
////////////////////////////////
|
|
181
197
|
/// Attach Middlewares
|
|
182
198
|
////////////////////////////////
|
|
183
199
|
_router.use(...middlewares);
|
|
@@ -195,6 +211,7 @@ function prepareTarget(_a) {
|
|
|
195
211
|
return;
|
|
196
212
|
const router = yield prepareTarget({
|
|
197
213
|
target: module,
|
|
214
|
+
imports: moduleData.module.imports,
|
|
198
215
|
namespace: `${namespace}/${module.name}`,
|
|
199
216
|
prefix: moduleData.module.path,
|
|
200
217
|
instance: tsyringe_1.container.resolve(module),
|
|
@@ -214,6 +231,7 @@ function prepareTarget(_a) {
|
|
|
214
231
|
target: controller,
|
|
215
232
|
namespace: `${namespace}/${controller.name}`,
|
|
216
233
|
prefix: controllerData.path,
|
|
234
|
+
imports: controllerData.imports,
|
|
217
235
|
instance: tsyringe_1.container.resolve(controller),
|
|
218
236
|
log,
|
|
219
237
|
});
|
|
@@ -241,6 +259,12 @@ function prepareTarget(_a) {
|
|
|
241
259
|
};
|
|
242
260
|
});
|
|
243
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* Prepare the routes for the target class.
|
|
264
|
+
*
|
|
265
|
+
* @param param0
|
|
266
|
+
* @returns
|
|
267
|
+
*/
|
|
244
268
|
function prepareRoutes(_a) {
|
|
245
269
|
return __awaiter(this, arguments, void 0, function* ({ target, router, route, instance, namespace, log, }) {
|
|
246
270
|
var _b, _c, _d;
|
|
@@ -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
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Service = void 0;
|
|
4
|
+
const tsyringe_1 = require("tsyringe");
|
|
5
|
+
const Service = ({ singleton = true, }) => {
|
|
6
|
+
return (target) => {
|
|
7
|
+
const instance = tsyringe_1.container.resolve(target);
|
|
8
|
+
if (singleton) {
|
|
9
|
+
tsyringe_1.container.registerInstance(target, instance);
|
|
10
|
+
}
|
|
11
|
+
return function (...args) {
|
|
12
|
+
return instance;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.Service = Service;
|
package/dist/decorators/index.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { MiddlewareHandler, MiddlewareNext, Request, Response, ServerConstructorOptions } from "hyper-express";
|
|
2
|
+
export interface OnInit {
|
|
3
|
+
onInit(): Promise<any>;
|
|
4
|
+
}
|
|
5
|
+
export interface IsSingleton {
|
|
6
|
+
isSingleton(): boolean;
|
|
7
|
+
}
|
|
2
8
|
export type Constructor<R extends any = any> = new (...args: any[]) => R;
|
|
9
|
+
export type ImportType = Constructor<Partial<OnInit> & Partial<IsSingleton>>;
|
|
3
10
|
export type ConstructorDecorator = (target: Constructor, kay?: any, descriptor?: PropertyDescriptor) => Constructor;
|
|
4
11
|
export type HyperClassDecorator<T> = (options?: T) => ConstructorDecorator;
|
|
5
12
|
export type HyperMethodDecorator<T> = (options?: T) => (target: any, key?: any, descriptor?: PropertyDescriptor) => void;
|
|
@@ -18,7 +25,7 @@ export interface HyperAppMetadata {
|
|
|
18
25
|
logger?: (...args: any[]) => void;
|
|
19
26
|
logs?: LogSpaces;
|
|
20
27
|
modules: Constructor[];
|
|
21
|
-
imports?:
|
|
28
|
+
imports?: ImportType[];
|
|
22
29
|
options?: ServerConstructorOptions;
|
|
23
30
|
}
|
|
24
31
|
export interface HyperAppDecorator {
|
|
@@ -31,14 +38,14 @@ export type HyperModuleMetadata = {
|
|
|
31
38
|
scopes?: string[];
|
|
32
39
|
modules?: Constructor[];
|
|
33
40
|
controllers?: Constructor[];
|
|
34
|
-
imports?:
|
|
41
|
+
imports?: ImportType[];
|
|
35
42
|
};
|
|
36
43
|
export type HyperModuleDecorator = HyperClassDecorator<HyperModuleMetadata>;
|
|
37
44
|
export type HyperControllerMetadata = {
|
|
38
45
|
path?: string;
|
|
39
46
|
roles?: string[];
|
|
40
47
|
scopes?: string[];
|
|
41
|
-
imports?:
|
|
48
|
+
imports?: ImportType[];
|
|
42
49
|
};
|
|
43
50
|
export type HyperControllerDecorator = HyperClassDecorator<HyperControllerMetadata | string>;
|
|
44
51
|
export type ParameterResolver = (req: Request, res: Response) => any | Promise<any>;
|