@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.
- package/.agent/rules/philosophy.md +103 -0
- package/.agent/rules/writing.md +32 -0
- package/README.md +107 -142
- package/dist/__internals/constants.d.ts +1 -58
- package/dist/__internals/constants.js +3 -65
- package/dist/__internals/helpers/lifecycle.helper.d.ts +3 -0
- package/dist/__internals/helpers/lifecycle.helper.js +25 -0
- package/dist/__internals/helpers/merge-metadata.d.ts +2 -3
- package/dist/__internals/helpers/merge-metadata.js +43 -23
- package/dist/__internals/helpers/prepare.helper.d.ts +5 -0
- package/dist/__internals/helpers/prepare.helper.js +392 -0
- package/dist/__internals/stores/hyper.store.d.ts +5 -0
- package/dist/__internals/stores/hyper.store.js +5 -0
- package/dist/__internals/stores/index.d.ts +4 -15
- package/dist/__internals/stores/index.js +18 -35
- package/dist/__internals/stores/meta.store.d.ts +34 -0
- package/dist/__internals/stores/meta.store.js +107 -0
- package/dist/__internals/stores/scope.store.d.ts +14 -0
- package/dist/__internals/stores/scope.store.js +29 -0
- package/dist/__internals/stores/serivces.store.d.ts +1 -0
- package/dist/__internals/stores/{store.interface.js → serivces.store.js} +2 -0
- package/dist/__internals/transform/middleware.transform.d.ts +3 -0
- package/dist/__internals/transform/middleware.transform.js +19 -0
- package/dist/__internals/transform/role.transform.d.ts +1 -1
- package/dist/__internals/transform/role.transform.js +10 -7
- package/dist/__internals/transform/scope.transfrom.d.ts +5 -7
- package/dist/__internals/transform/scope.transfrom.js +53 -39
- package/dist/__internals/transform/transform.registry.d.ts +33 -0
- package/dist/__internals/transform/transform.registry.js +59 -0
- package/dist/__internals/types.d.ts +53 -4
- package/dist/__internals/utils/function.util.d.ts +1 -4
- package/dist/__internals/utils/function.util.js +22 -10
- package/dist/common/bootstrap.d.ts +6 -2
- package/dist/common/bootstrap.js +51 -8
- package/dist/common/helpers/index.d.ts +1 -0
- package/dist/common/helpers/index.js +1 -0
- package/dist/common/helpers/scopes.d.ts +3 -3
- package/dist/common/helpers/scopes.js +7 -8
- package/dist/common/helpers/state.d.ts +17 -0
- package/dist/common/helpers/state.js +44 -0
- package/dist/common/message-bus.d.ts +11 -0
- package/dist/common/message-bus.js +51 -0
- package/dist/common/testing.d.ts +56 -0
- package/dist/common/testing.js +193 -0
- package/dist/common/transport.d.ts +10 -0
- package/dist/common/transport.js +59 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -1
- package/dist/decorators.d.ts +1 -0
- package/dist/{common/openapi/index.js → decorators.js} +1 -2
- package/dist/extension.js +11 -2
- package/dist/index.d.ts +11 -3
- package/dist/index.js +11 -3
- package/dist/lib/event/meta.store.d.ts +4 -0
- package/dist/lib/event/meta.store.js +5 -0
- package/dist/lib/openapi/collectors/class.collector.d.ts +5 -0
- package/dist/lib/openapi/collectors/class.collector.js +10 -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 +7 -0
- package/dist/lib/openapi/collectors/method.collector.js +101 -0
- package/dist/lib/openapi/collectors/schema.collector.d.ts +6 -0
- package/dist/lib/openapi/collectors/schema.collector.js +29 -0
- package/dist/lib/openapi/constants.d.ts +47 -0
- package/dist/lib/openapi/constants.js +61 -0
- package/dist/lib/openapi/decorators.d.ts +16 -0
- package/dist/lib/openapi/decorators.js +93 -0
- package/dist/lib/openapi/index.d.ts +30 -0
- package/dist/lib/openapi/index.js +136 -0
- package/dist/lib/openapi/metadata.d.ts +7 -0
- package/dist/lib/openapi/metadata.js +8 -0
- package/dist/lib/openapi/metadata.registry.d.ts +29 -0
- package/dist/lib/openapi/metadata.registry.js +41 -0
- package/dist/lib/openapi/types.d.ts +131 -0
- package/dist/lib/server/decorators/File.d.ts +37 -0
- package/dist/{decorators → lib/server/decorators}/File.js +62 -75
- package/dist/lib/server/decorators/Http.d.ts +12 -0
- package/dist/lib/server/decorators/Http.js +56 -0
- package/dist/lib/server/decorators/HyperApp.d.ts +7 -0
- package/dist/lib/server/decorators/HyperApp.js +14 -0
- package/dist/lib/server/decorators/HyperController.d.ts +6 -0
- package/dist/lib/server/decorators/HyperController.js +15 -0
- package/dist/lib/server/decorators/HyperModule.d.ts +6 -0
- package/dist/lib/server/decorators/HyperModule.js +13 -0
- package/dist/lib/server/decorators/HyperService.d.ts +12 -0
- package/dist/lib/server/decorators/HyperService.js +30 -0
- package/dist/lib/server/decorators/Messaging.d.ts +8 -0
- package/dist/lib/server/decorators/Messaging.js +19 -0
- package/dist/lib/server/decorators/Middleware.d.ts +18 -0
- package/dist/lib/server/decorators/Middleware.js +52 -0
- package/dist/lib/server/decorators/Output.d.ts +6 -0
- package/dist/lib/server/decorators/Output.js +14 -0
- package/dist/lib/server/decorators/Pass.d.ts +10 -0
- package/dist/lib/server/decorators/Pass.js +13 -0
- package/dist/lib/server/decorators/Role.d.ts +7 -0
- package/dist/lib/server/decorators/Role.js +14 -0
- package/dist/lib/server/decorators/Routes.d.ts +14 -0
- package/dist/lib/server/decorators/Routes.js +39 -0
- package/dist/lib/server/decorators/Scope.d.ts +7 -0
- package/dist/lib/server/decorators/Scope.js +14 -0
- package/dist/{decorators → lib/server/decorators}/index.d.ts +9 -4
- package/dist/{decorators → lib/server/decorators}/index.js +9 -4
- package/dist/lib/server/decorators/metadata.d.ts +1 -0
- package/dist/lib/server/decorators/metadata.js +5 -0
- package/dist/lib/server/decorators/types.d.ts +125 -0
- package/dist/lib/server/decorators/types.js +6 -0
- package/dist/{exeptions → lib/server/exeptions}/HyperException.d.ts +2 -1
- package/dist/{exeptions → lib/server/exeptions}/HyperException.js +2 -1
- package/dist/{exeptions → lib/server/exeptions}/HyperFileException.js +1 -1
- package/dist/{exeptions → lib/server/exeptions}/NotRoleException.js +1 -1
- package/dist/{exeptions → lib/server/exeptions}/NotScopeException.js +1 -1
- package/dist/lib/tree/tree.d.ts +36 -0
- package/dist/lib/tree/tree.js +106 -0
- package/dist/type.d.ts +13 -2
- package/hyper-express-decorators.d.ts +1 -0
- package/package.json +81 -60
- package/scripts/clean.js +56 -0
- package/scripts/test-server.ts +85 -0
- package/tsconfig.json +18 -13
- package/vitest.config.mjs +30 -0
- package/.mocharc.js +0 -5
- package/dist/__internals/creators/request.creator.d.ts +0 -12
- package/dist/__internals/creators/request.creator.js +0 -53
- package/dist/__internals/creators/routes.creator.d.ts +0 -10
- package/dist/__internals/creators/routes.creator.js +0 -37
- package/dist/__internals/decorator-base.d.ts +0 -30
- package/dist/__internals/decorator-base.js +0 -86
- package/dist/__internals/store.d.ts +0 -10
- package/dist/__internals/store.js +0 -17
- package/dist/__internals/stores/middleware.store.d.ts +0 -7
- package/dist/__internals/stores/middleware.store.js +0 -19
- package/dist/__internals/stores/params.store.d.ts +0 -21
- package/dist/__internals/stores/params.store.js +0 -65
- package/dist/__internals/stores/routes.store.d.ts +0 -17
- package/dist/__internals/stores/routes.store.js +0 -43
- package/dist/__internals/stores/store.interface.d.ts +0 -8
- package/dist/__internals/transform/method.transform.d.ts +0 -2
- package/dist/__internals/transform/method.transform.js +0 -20
- package/dist/__internals/transform/pass.transfrom.d.ts +0 -1
- package/dist/__internals/transform/pass.transfrom.js +0 -2
- package/dist/__internals/utils/mixin.utils.d.ts +0 -11
- package/dist/__internals/utils/mixin.utils.js +0 -34
- package/dist/__internals/utils/router.d.ts +0 -1
- package/dist/__internals/utils/router.js +0 -2
- package/dist/common/openapi/collect-class-data.d.ts +0 -21
- package/dist/common/openapi/collect-class-data.js +0 -45
- package/dist/common/openapi/collect-function-data.d.ts +0 -32
- package/dist/common/openapi/collect-function-data.js +0 -70
- package/dist/common/openapi/index.d.ts +0 -2
- package/dist/decorators/File.d.ts +0 -65
- package/dist/decorators/Http.d.ts +0 -55
- package/dist/decorators/Http.js +0 -93
- package/dist/decorators/HyperApp.d.ts +0 -7
- package/dist/decorators/HyperApp.js +0 -262
- package/dist/decorators/HyperController.d.ts +0 -2
- package/dist/decorators/HyperController.js +0 -19
- package/dist/decorators/HyperModule.d.ts +0 -5
- package/dist/decorators/HyperModule.js +0 -14
- package/dist/decorators/Middleware.d.ts +0 -24
- package/dist/decorators/Middleware.js +0 -51
- package/dist/decorators/Pass.d.ts +0 -12
- package/dist/decorators/Pass.js +0 -29
- package/dist/decorators/Role.d.ts +0 -6
- package/dist/decorators/Role.js +0 -34
- package/dist/decorators/Routes.d.ts +0 -14
- package/dist/decorators/Routes.js +0 -21
- package/dist/decorators/Scope.d.ts +0 -6
- package/dist/decorators/Scope.js +0 -25
- package/dist/decorators/types.d.ts +0 -89
- /package/dist/{decorators → lib/openapi}/types.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/DuplicateControllerPathException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/DuplicateControllerPathException.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/DuplicatedException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/DuplicatedException.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/DuplicatedHandlerException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/DuplicatedHandlerException.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/HyperFileException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/MethodNotFountException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/MethodNotFountException.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/NotPropertyException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/NotPropertyException.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/NotRoleException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/NotScopeException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/WrongPlaceException.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/WrongPlaceException.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/index.d.ts +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/index.js +0 -0
- /package/dist/{exeptions → lib/server/exeptions}/types.d.ts +0 -0
- /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>;
|
|
@@ -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
|
|
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
|
-
|
|
10
|
+
var _a;
|
|
10
11
|
if (isEmtpy)
|
|
11
12
|
return next();
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
7
|
-
|
|
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
|
-
|
|
11
|
-
if (isEmtpy)
|
|
11
|
+
if (isEmpty)
|
|
12
12
|
return next();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
return next(
|
|
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 (
|
|
23
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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,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;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import { IHyperApp, IHyperApplication } from "../type";
|
|
2
|
-
|
|
3
|
-
|
|
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>>;
|
package/dist/common/bootstrap.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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
|
}
|
|
@@ -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);
|