@zenofolio/hyper-decor 1.0.59 → 1.0.61

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.
@@ -1,6 +1,5 @@
1
1
  import "reflect-metadata";
2
- import { Request } from "hyper-express";
3
- import { ByPassKeys } from "../stores/params.store";
2
+ import { Request } from "hyper-express/types";
4
3
  import { ParameterResolver } from "../../decorators";
5
4
  /**
6
5
  * Creates a parameter decorator for handling request data.
@@ -9,4 +8,4 @@ import { ParameterResolver } from "../../decorators";
9
8
  * @param {IParamsResolver} resolver - Resolver function to handle the parameter.
10
9
  * @returns {ParameterDecorator} - The parameter decorator function.
11
10
  */
12
- export default function createParamDecorator(key: keyof Request | ByPassKeys, decoratorName: string, resolver: ParameterResolver): ParameterDecorator;
11
+ export default function createParamDecorator(key: keyof Request | "req" | "res", decoratorName: string, resolver: ParameterResolver): ParameterDecorator;
@@ -1,11 +1,9 @@
1
1
  import "reflect-metadata";
2
- import { Request, Response } from "hyper-express";
3
2
  /**
4
3
  * Helper function to create route decorators for HTTP methods.
5
4
  *
6
5
  * @param {string} method - The HTTP method (e.g., GET, POST).
7
- * @param {(req: Request, res: Response) => any} [resolver] - Optional resolver for the route.
8
6
  * @returns {(path?: string) => MethodDecorator} - A method decorator for defining routes.
9
7
  *
10
8
  */
11
- export default function createRouteDecorator<T extends any = undefined>(method: string, resolver?: (req: Request, res: Response) => any): (path?: string, options?: T) => MethodDecorator & ClassDecorator;
9
+ export default function createRouteDecorator<T extends any = undefined>(method: string): (path?: string, options?: T) => MethodDecorator & ClassDecorator;
@@ -8,11 +8,10 @@ const decorator_base_1 = require("../decorator-base");
8
8
  * Helper function to create route decorators for HTTP methods.
9
9
  *
10
10
  * @param {string} method - The HTTP method (e.g., GET, POST).
11
- * @param {(req: Request, res: Response) => any} [resolver] - Optional resolver for the route.
12
11
  * @returns {(path?: string) => MethodDecorator} - A method decorator for defining routes.
13
12
  *
14
13
  */
15
- function createRouteDecorator(method, resolver) {
14
+ function createRouteDecorator(method) {
16
15
  return (path = "/", options) => (0, decorator_base_1.DecoratorHelper)({
17
16
  type: constants_1.KEY_TYPE_CONTROLLER,
18
17
  key: constants_1.KEY_PARAMS_ROUTE,
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.prepareApplication = prepareApplication;
16
16
  const hyper_express_1 = require("hyper-express");
17
- const collectors_1 = require("../../collectors");
17
+ const stores_1 = require("../../stores");
18
18
  const role_transform_1 = __importDefault(require("../transform/role.transform"));
19
19
  const scope_transfrom_1 = __importDefault(require("../transform/scope.transfrom"));
20
20
  const object_util_1 = require("../utils/object.util");
@@ -84,7 +84,7 @@ function prepareApplication(options, Target, app, log) {
84
84
  .join(", ")}`);
85
85
  }
86
86
  (0, scope_transfrom_1.default)(data.scopes, (middleware, scopes) => {
87
- collectors_1.ScopeStore.addAll(scopes);
87
+ stores_1.ScopeStore.addAll(scopes);
88
88
  app.use(middleware);
89
89
  log("middleware", `${Target.name} with scopes: ${data.scopes.join(", ")}`);
90
90
  });
@@ -139,7 +139,7 @@ function prepareTarget(_a) {
139
139
  ////////////////////////////////
140
140
  _router.use(...middlewares);
141
141
  (0, scope_transfrom_1.default)(scopes, (middleware, scopes) => {
142
- collectors_1.ScopeStore.addAll(scopes);
142
+ stores_1.ScopeStore.addAll(scopes);
143
143
  _router.use(middleware);
144
144
  });
145
145
  (0, role_transform_1.default)(roles, (middleware) => _router.use(middleware));
@@ -220,7 +220,7 @@ function prepareRoutes(_a) {
220
220
  (0, role_transform_1.default)(metadata.roles, (middleware) => middlewares.push(middleware));
221
221
  (0, scope_transfrom_1.default)(metadata.scopes, (middleware, scopes) => {
222
222
  middlewares.push(middleware);
223
- collectors_1.ScopeStore.addAll(scopes);
223
+ stores_1.ScopeStore.addAll(scopes);
224
224
  });
225
225
  log("routes", `${namespace}/${propertyKey} ${method.toUpperCase()} { ${path} }`);
226
226
  if (!hasParams) {
@@ -0,0 +1,15 @@
1
+ interface MetadataStoreOptions {
2
+ target: any;
3
+ propertyKey?: string | symbol;
4
+ descriptorOrIndex?: any;
5
+ }
6
+ declare class MetadatStore {
7
+ static define(key: string | symbol, value: any, { target, propertyKey }: MetadataStoreOptions): void;
8
+ static get<T>(key: string | symbol, { target, propertyKey }: MetadataStoreOptions, def: T): T;
9
+ static list<T extends any = any>(key: string | symbol, { target, propertyKey }: MetadataStoreOptions): {
10
+ has: () => boolean;
11
+ get: () => never[];
12
+ set: (...value: T[]) => void;
13
+ };
14
+ }
15
+ export default MetadatStore;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const object_util_1 = require("../utils/object.util");
4
+ class MetadatStore {
5
+ static define(key, value, { target, propertyKey }) {
6
+ if (target && typeof propertyKey === "string") {
7
+ Reflect.defineMetadata(key, value, Reflect.get(target, propertyKey));
8
+ }
9
+ else {
10
+ Reflect.defineMetadata(key, value, (0, object_util_1.$constructor)(target));
11
+ }
12
+ }
13
+ static get(key, { target, propertyKey }, def) {
14
+ if (target && typeof propertyKey === "string") {
15
+ return Reflect.getMetadata(key, Reflect.get(target, propertyKey)) || def;
16
+ }
17
+ else {
18
+ return Reflect.getMetadata(key, (0, object_util_1.$constructor)(target)) || def;
19
+ }
20
+ }
21
+ static list(key, { target, propertyKey }) {
22
+ return {
23
+ has: () => {
24
+ return this.get(key, { target, propertyKey }, []).length > 0;
25
+ },
26
+ get: () => {
27
+ return this.get(key, { target, propertyKey }, []);
28
+ },
29
+ set: (...value) => {
30
+ const list = this.get(key, { target, propertyKey }, []);
31
+ list.push(...value);
32
+ this.define(key, list, { target, propertyKey });
33
+ },
34
+ };
35
+ }
36
+ }
37
+ exports.default = MetadatStore;
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = middlewareTransformer;
4
4
  const tsyringe_1 = require("tsyringe");
5
5
  function middlewareTransformer(list) {
6
+ if (!(list === null || list === void 0 ? void 0 : list.length))
7
+ return [];
6
8
  return list
7
9
  .map((middleware) => {
8
10
  if (isClass(middleware)) {
@@ -38,6 +38,9 @@ const hasRole = (request, role) => {
38
38
  if (!roles) {
39
39
  return false;
40
40
  }
41
- return (0, object_util_1.$array)(role).every((r) => roles.includes(r));
41
+ if (roles.includes("*")) {
42
+ return true;
43
+ }
44
+ return (0, object_util_1.$array)(role).some((r) => roles.includes(r));
42
45
  };
43
46
  exports.hasRole = hasRole;
@@ -1,4 +1,4 @@
1
- import { Request } from "hyper-express";
1
+ import { Request } from "hyper-express/types";
2
2
  /**
3
3
  * File upload restrictions
4
4
  *
@@ -0,0 +1,14 @@
1
+ import { Constructor } from "./types";
2
+ interface ServiceDecoratorOptions {
3
+ singleton?: boolean;
4
+ }
5
+ /**
6
+ * HyperService decorator is used to register a class as a service in the tsyringe container.
7
+ *
8
+ * No matter where this decorator is applied, the class will be registered in the tsyringe container
9
+ * and included in the service store to be launched when the application starts.
10
+ *
11
+ * @param options.singleton - Whether to register as a singleton (default: true)
12
+ */
13
+ export declare const HyperService: ({ singleton, }?: ServiceDecoratorOptions) => (target: Constructor) => void;
14
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HyperService = void 0;
4
+ const tsyringe_1 = require("tsyringe");
5
+ const service_store_1 = require("../__internals/stores/service.store");
6
+ /**
7
+ * HyperService decorator is used to register a class as a service in the tsyringe container.
8
+ *
9
+ * No matter where this decorator is applied, the class will be registered in the tsyringe container
10
+ * and included in the service store to be launched when the application starts.
11
+ *
12
+ * @param options.singleton - Whether to register as a singleton (default: true)
13
+ */
14
+ const HyperService = ({ singleton = true, } = {}) => {
15
+ return (target) => {
16
+ if (!tsyringe_1.container.isRegistered(target)) {
17
+ if (singleton) {
18
+ tsyringe_1.container.registerSingleton(target);
19
+ }
20
+ else {
21
+ tsyringe_1.container.register(target, { useClass: target });
22
+ }
23
+ }
24
+ service_store_1.serviceStore.add(target);
25
+ };
26
+ };
27
+ exports.HyperService = HyperService;
@@ -9,11 +9,11 @@ export declare const Middleware: {
9
9
  /**
10
10
  * Exclude middleware from matching paths.
11
11
  *
12
- * @param expresiosn
12
+ * @param expressions
13
13
  * @param middleware
14
14
  * @returns
15
15
  */
16
- exclude(expresiosn: RegExp | RegExp[], middleware: MiddlewareHandler): ClassDecorator & MethodDecorator;
16
+ exclude(expressions: RegExp | RegExp[], middleware: MiddlewareHandler): ClassDecorator & MethodDecorator;
17
17
  /**
18
18
  * Only run middleware on matching paths.
19
19
  *
@@ -21,5 +21,5 @@ export declare const Middleware: {
21
21
  * @param middleware
22
22
  * @returns
23
23
  */
24
- only(expresiosn: RegExp | RegExp[], middleware: MiddlewareHandler): ClassDecorator & MethodDecorator;
24
+ only(expressions: RegExp | RegExp[], middleware: MiddlewareHandler): ClassDecorator & MethodDecorator;
25
25
  };
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Middleware = void 0;
7
7
  require("reflect-metadata");
8
8
  const constants_1 = require("../__internals/constants");
9
- const stores_1 = __importDefault(require("../__internals/stores"));
9
+ const metadata_store_1 = __importDefault(require("../__internals/stores/metadata.store"));
10
10
  /**
11
11
  * Middleware decorator to attach middleware to controllers, modules, and routes.
12
12
  */
13
- const Middleware = (...middleware) => (Target, propertyKey, descriptor) => {
14
- stores_1.default.define(constants_1.KEY_PARAMS_MIDDLEWARES, middleware, {
13
+ const Middleware = (...middleware) => (Target, propertyKey) => {
14
+ metadata_store_1.default.define(constants_1.KEY_PARAMS_MIDDLEWARES, middleware, {
15
15
  target: Target,
16
16
  propertyKey,
17
17
  });
@@ -20,19 +20,11 @@ exports.Middleware = Middleware;
20
20
  /**
21
21
  * Exclude middleware from matching paths.
22
22
  *
23
- * @param expresiosn
23
+ * @param expressions
24
24
  * @param middleware
25
25
  * @returns
26
26
  */
27
- exports.Middleware.exclude = (expresiosn, middleware) => {
28
- return (0, exports.Middleware)((req, res, next) => {
29
- expresiosn = Array.isArray(expresiosn) ? expresiosn : [expresiosn];
30
- const excluude = expresiosn.some((exp) => exp.test(req.path));
31
- if (excluude)
32
- return next();
33
- return middleware(req, res, next);
34
- });
35
- };
27
+ exports.Middleware.exclude = (expressions, middleware) => (0, exports.Middleware)(buildHandler("exclude", expressions, middleware));
36
28
  /**
37
29
  * Only run middleware on matching paths.
38
30
  *
@@ -40,12 +32,22 @@ exports.Middleware.exclude = (expresiosn, middleware) => {
40
32
  * @param middleware
41
33
  * @returns
42
34
  */
43
- exports.Middleware.only = (expresiosn, middleware) => {
44
- return (0, exports.Middleware)((req, res, next) => {
45
- expresiosn = Array.isArray(expresiosn) ? expresiosn : [expresiosn];
46
- const only = expresiosn.some((exp) => exp.test(req.path));
47
- if (!only)
35
+ exports.Middleware.only = (expressions, middleware) => (0, exports.Middleware)(buildHandler("only", expressions, middleware));
36
+ /**
37
+ *
38
+ * Helper function to build middleware handlers for "only" and "exclude" modes.
39
+ *
40
+ * @param mode
41
+ * @param expresions
42
+ * @returns
43
+ */
44
+ const buildHandler = (mode, expressions, middleware) => {
45
+ const matchers = Array.isArray(expressions) ? expressions : [expressions];
46
+ return (req, res, next) => {
47
+ const matches = matchers.some((rx) => rx.test(req.path));
48
+ if ((mode === "only" && !matches) || (mode === "exclude" && matches)) {
48
49
  return next();
50
+ }
49
51
  return middleware(req, res, next);
50
- });
52
+ };
51
53
  };
@@ -3,10 +3,13 @@ interface PassOptions {
3
3
  (req: Request, res: Response): boolean | Promise<boolean>;
4
4
  }
5
5
  /**
6
- * This method will pass all checks if the function returns true.
6
+ * Pass decorator
7
7
  *
8
- * @param options
9
- * @returns
8
+ * This decorator allows bypassing checks when the provided function returns `true`.
9
+ * It can be applied to classes or methods, but not to properties.
10
+ *
11
+ * @param options - A function that returns true to allow bypassing
12
+ * @returns A class or method decorator
10
13
  */
11
14
  export declare const Pass: (options: PassOptions) => ClassDecorator & MethodDecorator;
12
15
  export {};
@@ -8,10 +8,13 @@ const who_helper_1 = __importDefault(require("../__internals/helpers/who.helper"
8
8
  const NotPropertyException_1 = __importDefault(require("../exeptions/NotPropertyException"));
9
9
  const constants_1 = require("../__internals/constants");
10
10
  /**
11
- * This method will pass all checks if the function returns true.
11
+ * Pass decorator
12
12
  *
13
- * @param options
14
- * @returns
13
+ * This decorator allows bypassing checks when the provided function returns `true`.
14
+ * It can be applied to classes or methods, but not to properties.
15
+ *
16
+ * @param options - A function that returns true to allow bypassing
17
+ * @returns A class or method decorator
15
18
  */
16
19
  const Pass = (options) => (target, propertyKey, descriptorOrIndex) => {
17
20
  const { isProperty, isMethod } = (0, who_helper_1.default)(target, propertyKey, descriptorOrIndex);
@@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Role = void 0;
7
7
  require("reflect-metadata");
8
- const constants_1 = require("../__internals/constants");
9
- const who_helper_1 = __importDefault(require("../__internals/helpers/who.helper"));
10
- const stores_1 = __importDefault(require("../__internals/stores"));
11
8
  const exeptions_1 = require("../exeptions");
9
+ const who_helper_1 = __importDefault(require("../__internals/helpers/who.helper"));
10
+ const constants_1 = require("../__internals/constants");
11
+ const metadata_store_1 = __importDefault(require("../__internals/stores/metadata.store"));
12
12
  /**
13
13
  * Role decorator for setting role-based access control.
14
14
  */
@@ -21,11 +21,11 @@ const Role = (roles) => (target, propertyKey, descriptorOrIndex) => {
21
21
  descriptorOrIndex,
22
22
  });
23
23
  }
24
- const { isProperty, isMethod } = (0, who_helper_1.default)(target, propertyKey, descriptorOrIndex);
24
+ const { isProperty } = (0, who_helper_1.default)(target, propertyKey, descriptorOrIndex);
25
25
  if (isProperty) {
26
26
  throw new Error(`Scope decorator cannot be used as parameter decorator in ${target.constructor.name}.${propertyKey}`);
27
27
  }
28
- const list = stores_1.default.list(constants_1.KEY_PARAMS_ROLE, {
28
+ const list = metadata_store_1.default.list(constants_1.KEY_PARAMS_ROLE, {
29
29
  target,
30
30
  propertyKey,
31
31
  });
@@ -7,7 +7,7 @@ exports.Scope = void 0;
7
7
  require("reflect-metadata");
8
8
  const constants_1 = require("../__internals/constants");
9
9
  const who_helper_1 = __importDefault(require("../__internals/helpers/who.helper"));
10
- const stores_1 = __importDefault(require("../__internals/stores"));
10
+ const metadata_store_1 = __importDefault(require("../__internals/stores/metadata.store"));
11
11
  /**
12
12
  * Scope decorator for defining access scopes.
13
13
  */
@@ -16,7 +16,7 @@ const Scope = (scopes) => (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
  }
19
- const list = stores_1.default.list(constants_1.KEY_PARAMS_SCOPE, {
19
+ const list = metadata_store_1.default.list(constants_1.KEY_PARAMS_SCOPE, {
20
20
  target,
21
21
  propertyKey,
22
22
  });
@@ -1,6 +1,7 @@
1
1
  export * from "./HyperApp";
2
2
  export * from "./HyperModule";
3
3
  export * from "./HyperController";
4
+ export * from "./HyperService";
4
5
  export * from "./Middleware";
5
6
  export * from "./Scope";
6
7
  export * from "./Role";
@@ -9,4 +10,3 @@ export * from "./types";
9
10
  export * from "./Http";
10
11
  export * from "./Pass";
11
12
  export * from "./File";
12
- export * from "./Service";
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./HyperApp"), exports);
18
18
  __exportStar(require("./HyperModule"), exports);
19
19
  __exportStar(require("./HyperController"), exports);
20
+ __exportStar(require("./HyperService"), exports);
20
21
  __exportStar(require("./Middleware"), exports);
21
22
  __exportStar(require("./Scope"), exports);
22
23
  __exportStar(require("./Role"), exports);
@@ -25,4 +26,3 @@ __exportStar(require("./types"), exports);
25
26
  __exportStar(require("./Http"), exports);
26
27
  __exportStar(require("./Pass"), exports);
27
28
  __exportStar(require("./File"), exports);
28
- __exportStar(require("./Service"), exports);
package/dist/index.d.ts CHANGED
@@ -6,5 +6,5 @@ export * from "./common/helpers";
6
6
  export * from "./common/bootstrap";
7
7
  export * from "hyper-express";
8
8
  export * from "tsyringe";
9
- export * from "./collectors";
9
+ export * from "./stores";
10
10
  export * from "./type";
package/dist/index.js CHANGED
@@ -22,5 +22,5 @@ __exportStar(require("./common/helpers"), exports);
22
22
  __exportStar(require("./common/bootstrap"), exports);
23
23
  __exportStar(require("hyper-express"), exports);
24
24
  __exportStar(require("tsyringe"), exports);
25
- __exportStar(require("./collectors"), exports);
25
+ __exportStar(require("./stores"), exports);
26
26
  __exportStar(require("./type"), exports);
@@ -0,0 +1 @@
1
+ export * from "./scope.store";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./scope.store"), exports);
@@ -0,0 +1,14 @@
1
+ import { ScopeMap } from "../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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenofolio/hyper-decor",
3
- "version": "1.0.59",
3
+ "version": "1.0.61",
4
4
  "description": "Project core with utilities and features",
5
5
  "main": "dist/index.js",
6
6
  "author": "zenozaga",