@zenofolio/hyper-decor 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/.mocharc.js +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +191 -0
  4. package/dist/__internals/constants.d.ts +61 -0
  5. package/dist/__internals/constants.js +72 -0
  6. package/dist/__internals/creators/request.creator.d.ts +12 -0
  7. package/dist/__internals/creators/request.creator.js +53 -0
  8. package/dist/__internals/creators/routes.creator.d.ts +10 -0
  9. package/dist/__internals/creators/routes.creator.js +37 -0
  10. package/dist/__internals/decorator-base.d.ts +30 -0
  11. package/dist/__internals/decorator-base.js +86 -0
  12. package/dist/__internals/helpers/merge-metadata.d.ts +14 -0
  13. package/dist/__internals/helpers/merge-metadata.js +44 -0
  14. package/dist/__internals/helpers/who.helper.d.ts +9 -0
  15. package/dist/__internals/helpers/who.helper.js +19 -0
  16. package/dist/__internals/store.d.ts +10 -0
  17. package/dist/__internals/store.js +17 -0
  18. package/dist/__internals/stores/index.d.ts +15 -0
  19. package/dist/__internals/stores/index.js +37 -0
  20. package/dist/__internals/stores/middleware.store.d.ts +7 -0
  21. package/dist/__internals/stores/middleware.store.js +19 -0
  22. package/dist/__internals/stores/params.store.d.ts +21 -0
  23. package/dist/__internals/stores/params.store.js +65 -0
  24. package/dist/__internals/stores/routes.store.d.ts +17 -0
  25. package/dist/__internals/stores/routes.store.js +43 -0
  26. package/dist/__internals/stores/store.interface.d.ts +8 -0
  27. package/dist/__internals/stores/store.interface.js +2 -0
  28. package/dist/__internals/transform/method.transform.d.ts +2 -0
  29. package/dist/__internals/transform/method.transform.js +20 -0
  30. package/dist/__internals/transform/pass.transfrom.d.ts +1 -0
  31. package/dist/__internals/transform/pass.transfrom.js +2 -0
  32. package/dist/__internals/transform/role.transform.d.ts +7 -0
  33. package/dist/__internals/transform/role.transform.js +86 -0
  34. package/dist/__internals/transform/scope.transfrom.d.ts +7 -0
  35. package/dist/__internals/transform/scope.transfrom.js +72 -0
  36. package/dist/__internals/types.d.ts +8 -0
  37. package/dist/__internals/types.js +2 -0
  38. package/dist/__internals/utils/function.util.d.ts +4 -0
  39. package/dist/__internals/utils/function.util.js +20 -0
  40. package/dist/__internals/utils/mixin.utils.d.ts +11 -0
  41. package/dist/__internals/utils/mixin.utils.js +34 -0
  42. package/dist/__internals/utils/object.util.d.ts +27 -0
  43. package/dist/__internals/utils/object.util.js +72 -0
  44. package/dist/__internals/utils/path.util.d.ts +4 -0
  45. package/dist/__internals/utils/path.util.js +8 -0
  46. package/dist/__internals/utils/router.d.ts +1 -0
  47. package/dist/__internals/utils/router.js +2 -0
  48. package/dist/common/bootstrap.d.ts +3 -0
  49. package/dist/common/bootstrap.js +25 -0
  50. package/dist/common/helpers/index.d.ts +2 -0
  51. package/dist/common/helpers/index.js +18 -0
  52. package/dist/common/helpers/role.d.ts +22 -0
  53. package/dist/common/helpers/role.js +43 -0
  54. package/dist/common/helpers/scopes.d.ts +23 -0
  55. package/dist/common/helpers/scopes.js +47 -0
  56. package/dist/common/openapi/collect-class-data.d.ts +21 -0
  57. package/dist/common/openapi/collect-class-data.js +45 -0
  58. package/dist/common/openapi/collect-function-data.d.ts +32 -0
  59. package/dist/common/openapi/collect-function-data.js +70 -0
  60. package/dist/common/openapi/index.d.ts +2 -0
  61. package/dist/common/openapi/index.js +18 -0
  62. package/dist/constants.d.ts +3 -0
  63. package/dist/constants.js +6 -0
  64. package/dist/decorators/File.d.ts +65 -0
  65. package/dist/decorators/File.js +180 -0
  66. package/dist/decorators/Http.d.ts +55 -0
  67. package/dist/decorators/Http.js +93 -0
  68. package/dist/decorators/HyperApp.d.ts +7 -0
  69. package/dist/decorators/HyperApp.js +262 -0
  70. package/dist/decorators/HyperController.d.ts +2 -0
  71. package/dist/decorators/HyperController.js +19 -0
  72. package/dist/decorators/HyperModule.d.ts +5 -0
  73. package/dist/decorators/HyperModule.js +14 -0
  74. package/dist/decorators/Middleware.d.ts +24 -0
  75. package/dist/decorators/Middleware.js +51 -0
  76. package/dist/decorators/Pass.d.ts +12 -0
  77. package/dist/decorators/Pass.js +29 -0
  78. package/dist/decorators/Role.d.ts +6 -0
  79. package/dist/decorators/Role.js +34 -0
  80. package/dist/decorators/Routes.d.ts +14 -0
  81. package/dist/decorators/Routes.js +21 -0
  82. package/dist/decorators/Scope.d.ts +6 -0
  83. package/dist/decorators/Scope.js +25 -0
  84. package/dist/decorators/index.d.ts +11 -0
  85. package/dist/decorators/index.js +27 -0
  86. package/dist/decorators/types.d.ts +89 -0
  87. package/dist/decorators/types.js +2 -0
  88. package/dist/exeptions/DuplicateControllerPathException.d.ts +14 -0
  89. package/dist/exeptions/DuplicateControllerPathException.js +12 -0
  90. package/dist/exeptions/DuplicatedException.d.ts +8 -0
  91. package/dist/exeptions/DuplicatedException.js +12 -0
  92. package/dist/exeptions/DuplicatedHandlerException.d.ts +4 -0
  93. package/dist/exeptions/DuplicatedHandlerException.js +12 -0
  94. package/dist/exeptions/HyperException.d.ts +7 -0
  95. package/dist/exeptions/HyperException.js +13 -0
  96. package/dist/exeptions/HyperFileException.d.ts +4 -0
  97. package/dist/exeptions/HyperFileException.js +12 -0
  98. package/dist/exeptions/MethodNotFountException.d.ts +4 -0
  99. package/dist/exeptions/MethodNotFountException.js +12 -0
  100. package/dist/exeptions/NotPropertyException.d.ts +6 -0
  101. package/dist/exeptions/NotPropertyException.js +16 -0
  102. package/dist/exeptions/NotRoleException.d.ts +6 -0
  103. package/dist/exeptions/NotRoleException.js +17 -0
  104. package/dist/exeptions/NotScopeException.d.ts +7 -0
  105. package/dist/exeptions/NotScopeException.js +18 -0
  106. package/dist/exeptions/WrongPlaceException.d.ts +8 -0
  107. package/dist/exeptions/WrongPlaceException.js +21 -0
  108. package/dist/exeptions/index.d.ts +8 -0
  109. package/dist/exeptions/index.js +18 -0
  110. package/dist/exeptions/types.d.ts +1 -0
  111. package/dist/exeptions/types.js +2 -0
  112. package/dist/extension.d.ts +1 -0
  113. package/dist/extension.js +41 -0
  114. package/dist/index.d.ts +9 -0
  115. package/dist/index.js +25 -0
  116. package/dist/type.d.ts +10 -0
  117. package/dist/type.js +2 -0
  118. package/hyper-express-decorators.d.ts +16 -0
  119. package/package.json +61 -0
  120. package/tsconfig.json +17 -0
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = who;
4
+ const object_util_1 = require("../utils/object.util");
5
+ function who(target, propertyKey, descriptorOrIndex) {
6
+ const isMethod = typeof propertyKey === "string";
7
+ const isProperty = typeof descriptorOrIndex === "number";
8
+ const Target = (0, object_util_1.$constructor)(target);
9
+ const Method = isMethod ? Reflect.get(target, propertyKey) : null;
10
+ return {
11
+ isMethod,
12
+ isProperty,
13
+ isClass: !!target && !isMethod && !isProperty,
14
+ Target,
15
+ Method,
16
+ key: propertyKey,
17
+ descriptorOrIndex,
18
+ };
19
+ }
@@ -0,0 +1,10 @@
1
+ export default class HyperAppStore {
2
+ private roles;
3
+ private scopes;
4
+ private modules;
5
+ private controllers;
6
+ private routes;
7
+ private middlewares;
8
+ addRole(role: string): void;
9
+ addModuleRole(module: string, role: string): void;
10
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class HyperAppStore {
4
+ constructor() {
5
+ this.roles = new Set();
6
+ this.scopes = new Set();
7
+ this.modules = new Map();
8
+ this.controllers = new Map();
9
+ this.routes = new Map();
10
+ this.middlewares = new Map();
11
+ }
12
+ addRole(role) {
13
+ this.roles.add(role);
14
+ }
15
+ addModuleRole(module, role) { }
16
+ }
17
+ exports.default = HyperAppStore;
@@ -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;
@@ -0,0 +1,7 @@
1
+ import "reflect-metadata";
2
+ import { MiddlewareHandler } from "hyper-express";
3
+ export declare const middlewareStore: Readonly<{
4
+ get(target: any): any;
5
+ has(target: any): boolean;
6
+ set(target: any, ...middlewares: MiddlewareHandler[]): void;
7
+ }>;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.middlewareStore = void 0;
4
+ require("reflect-metadata");
5
+ const constants_1 = require("../constants");
6
+ const { MIDDLEWARES } = constants_1.METADATA_KEYS;
7
+ exports.middlewareStore = Object.freeze({
8
+ get(target) {
9
+ return Reflect.getMetadata(MIDDLEWARES, target);
10
+ },
11
+ has(target) {
12
+ return !!this.get(target);
13
+ },
14
+ set(target, ...middlewares) {
15
+ const list = this.get(target) || [];
16
+ list.push(...middlewares);
17
+ Reflect.defineMetadata(MIDDLEWARES, list, target);
18
+ },
19
+ });
@@ -0,0 +1,21 @@
1
+ import { Request, Response } from "hyper-express";
2
+ export type ByPassKeys = "req" | "res";
3
+ export type IParamsResolver = (req: Request, res: Response) => any | Promise<any>;
4
+ export type IStoreParams = {
5
+ name: string;
6
+ type: any;
7
+ index: number;
8
+ key: string;
9
+ propertyKey: string;
10
+ resolver: IParamsResolver;
11
+ };
12
+ export type IStoreParamsMap = Map<string, IStoreParams>;
13
+ export declare const paramsStore: Readonly<{
14
+ has(target: any, propety: any, key: string): boolean;
15
+ get(target: any, propety?: any): IStoreParamsMap | undefined;
16
+ set(target: any, propety: any, value: IStoreParams): void;
17
+ delete(target: any, propety: any): void;
18
+ support(target: any, propety: any): boolean;
19
+ prepareArgs(target: any, propety: any, req: Request, res: Response): Promise<any[]>;
20
+ intercept(target: any, propety: any, req: Request, res: Response): Promise<void>;
21
+ }>;
@@ -0,0 +1,65 @@
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.paramsStore = void 0;
13
+ const no_conflict_1 = require("reflect-metadata/no-conflict");
14
+ const constants_1 = require("../constants");
15
+ exports.paramsStore = Object.freeze({
16
+ has(target, propety, key) {
17
+ const data = this.get(target, propety);
18
+ return data ? data.has(key) : false;
19
+ },
20
+ get(target, propety) {
21
+ if (!propety) {
22
+ return (0, no_conflict_1.getMetadata)(constants_1.KEY_PARAMS_PARAM, target);
23
+ }
24
+ return (0, no_conflict_1.getMetadata)(constants_1.KEY_PARAMS_PARAM, target, propety);
25
+ },
26
+ set(target, propety, value) {
27
+ const data = this.get(target, propety) || new Map();
28
+ data.set(value.key, value);
29
+ (0, no_conflict_1.defineMetadata)(constants_1.KEY_PARAMS_PARAM, data, target, propety);
30
+ },
31
+ delete(target, propety) {
32
+ (0, no_conflict_1.deleteMetadata)(constants_1.KEY_PARAMS_PARAM, target, propety);
33
+ },
34
+ support(target, propety) {
35
+ const params = (0, no_conflict_1.getMetadata)(constants_1.DESIGN_PARAMTYPES, target, propety) || [];
36
+ return params.length > 0;
37
+ },
38
+ prepareArgs(target, propety, req, res) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const data = this.get(target, propety);
41
+ if (!data)
42
+ return [];
43
+ const args = [];
44
+ yield Promise.all(Array.from(data.values()).map((value) => __awaiter(this, void 0, void 0, function* () {
45
+ const { key, resolver } = value;
46
+ switch (key) {
47
+ case "req":
48
+ args[value.index] = req;
49
+ break;
50
+ case "res":
51
+ args[value.index] = res;
52
+ break;
53
+ default: {
54
+ const result = yield resolver(req, res);
55
+ args[value.index] = result;
56
+ }
57
+ }
58
+ })));
59
+ return args;
60
+ });
61
+ },
62
+ intercept(target, propety, req, res) {
63
+ return __awaiter(this, void 0, void 0, function* () { });
64
+ },
65
+ });
@@ -0,0 +1,17 @@
1
+ import { IStore } from "./store.interface";
2
+ interface IRoute {
3
+ className: string;
4
+ method: string;
5
+ path: string;
6
+ handler: (...args: any[]) => any;
7
+ }
8
+ export declare class RouteStore implements IStore<any> {
9
+ has(target: any, property?: any): boolean;
10
+ get(target: any, property?: any): Set<IRoute> | undefined;
11
+ set(target: any, property: any, value: IRoute): void;
12
+ create(target: any, property?: any, key?: any): Set<IRoute> | null;
13
+ delete(target: any, value?: IRoute): void;
14
+ support(target: any, property: any): boolean;
15
+ }
16
+ declare const routeStore: RouteStore;
17
+ export { routeStore };
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.routeStore = exports.RouteStore = void 0;
4
+ const no_conflict_1 = require("reflect-metadata/no-conflict");
5
+ const constants_1 = require("../constants");
6
+ class RouteStore {
7
+ has(target, property) {
8
+ return (0, no_conflict_1.hasMetadata)(constants_1.METADATA_KEYS.ROUTES, target);
9
+ }
10
+ get(target, property) {
11
+ return (0, no_conflict_1.getMetadata)(constants_1.METADATA_KEYS.ROUTES, target);
12
+ }
13
+ set(target, property, value) {
14
+ const routes = this.get(target) || new Set();
15
+ routes.add(value);
16
+ (0, no_conflict_1.defineMetadata)(constants_1.METADATA_KEYS.ROUTES, routes, target);
17
+ }
18
+ create(target, property, key) {
19
+ const data = this.has(target, property)
20
+ ? this.get(target, property)
21
+ : new Set();
22
+ (0, no_conflict_1.defineMetadata)(constants_1.METADATA_KEYS.ROUTES, data, target);
23
+ return data;
24
+ }
25
+ delete(target, value) {
26
+ const routes = this.create(target, value === null || value === void 0 ? void 0 : value.className);
27
+ if (!routes)
28
+ return;
29
+ if (value) {
30
+ routes.delete(value);
31
+ (0, no_conflict_1.defineMetadata)(constants_1.METADATA_KEYS.ROUTES, routes, target);
32
+ }
33
+ else {
34
+ (0, no_conflict_1.deleteMetadata)(constants_1.METADATA_KEYS.ROUTES, target);
35
+ }
36
+ }
37
+ support(target, property) {
38
+ return this.has(target, property);
39
+ }
40
+ }
41
+ exports.RouteStore = RouteStore;
42
+ const routeStore = new RouteStore();
43
+ exports.routeStore = routeStore;
@@ -0,0 +1,8 @@
1
+ export interface IStore<T> {
2
+ has(target: any, property: any): boolean;
3
+ get(target: any, property: any): T | undefined;
4
+ set(target: any, property: any, value: T): void;
5
+ create(target: any, property: any, extra?: Partial<T>): T | null;
6
+ delete(target: any, property?: any): void;
7
+ support(target: any, property: any): boolean;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import "reflect-metadata";
2
+ export default function methodTransformer(target: any, key: string | symbol, transfrom?: (...args: any[]) => any): void;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = methodTransformer;
4
+ require("reflect-metadata");
5
+ function methodTransformer(target, key, transfrom) {
6
+ const prototype = target.constructor.prototype;
7
+ if (!prototype || prototype.__transformed)
8
+ return;
9
+ prototype.__transformed = true;
10
+ const original = Reflect.getOwnMetadata(key, target);
11
+ if (!(original === null || original === void 0 ? void 0 : original.value))
12
+ return;
13
+ console.log(original, "original");
14
+ original.value = function (...args) {
15
+ console.log("Before method call", args);
16
+ };
17
+ // const descriptor = Object.getOwnPropertyDescriptor(prototype, key);
18
+ // if(!descriptor) return;
19
+ // console.log(descriptor, "descriptor");
20
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { MiddlewareHandler } from "hyper-express";
2
+ import { RoleType } from "../../decorators";
3
+ export default function roleTransform(list: RoleType[], callback?: (middleware: MiddlewareHandler, roles: {
4
+ role: string;
5
+ description: string;
6
+ message: string | null;
7
+ }[], names: Set<string>) => void): MiddlewareHandler;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = roleTransform;
4
+ const exeptions_1 = require("../../exeptions");
5
+ const helpers_1 = require("../../common/helpers");
6
+ function roleTransform(list, callback) {
7
+ const { roles, names, isEmtpy } = resolveRoles(list);
8
+ const middleware = (req, res, next) => {
9
+ // if scopes is empty, then we don't need to check for scopes
10
+ if (isEmtpy)
11
+ return next();
12
+ // get the user scopes
13
+ const requestRoles = (0, helpers_1.getRoles)(req);
14
+ // find the first scope that is not in the userScopes
15
+ const role = roles.find((scope) => requestRoles === null || requestRoles === void 0 ? void 0 : requestRoles.includes(scope.role));
16
+ if (role) {
17
+ return next();
18
+ }
19
+ return next(new exeptions_1.NotRoleException(`Only ${Array.from(names).join(", ")} can access this resource`, requestRoles, Array.from(names)));
20
+ };
21
+ if (names.size > 0 && callback) {
22
+ callback(middleware, roles, names);
23
+ }
24
+ return middleware;
25
+ }
26
+ /**
27
+ *
28
+ * Convert list of roles to a standard format
29
+ *
30
+ * @param list
31
+ * @returns
32
+ */
33
+ const resolveRoles = (list) => {
34
+ var _a, _b, _c, _d;
35
+ const $roles = [];
36
+ for (const role of list) {
37
+ if (typeof role === "string") {
38
+ $roles.push({
39
+ role: role,
40
+ description: "",
41
+ message: null,
42
+ });
43
+ }
44
+ else if (Array.isArray(role)) {
45
+ for (const s of role) {
46
+ switch (typeof s) {
47
+ case "string":
48
+ $roles.push({
49
+ role: s,
50
+ description: "",
51
+ message: null,
52
+ });
53
+ break;
54
+ case "object":
55
+ $roles.push({
56
+ role: s.role,
57
+ description: (_a = s.description) !== null && _a !== void 0 ? _a : "",
58
+ message: (_b = s.message) !== null && _b !== void 0 ? _b : null,
59
+ });
60
+ break;
61
+ }
62
+ }
63
+ }
64
+ else {
65
+ if (typeof role === "object") {
66
+ $roles.push({
67
+ role: role.role,
68
+ description: (_c = role.description) !== null && _c !== void 0 ? _c : "",
69
+ message: (_d = role.message) !== null && _d !== void 0 ? _d : null,
70
+ });
71
+ }
72
+ else {
73
+ $roles.push({
74
+ role: role,
75
+ description: "",
76
+ message: null,
77
+ });
78
+ }
79
+ }
80
+ }
81
+ return {
82
+ roles: $roles,
83
+ names: new Set($roles.map((s) => s.role)),
84
+ isEmtpy: $roles.length === 0,
85
+ };
86
+ };
@@ -0,0 +1,7 @@
1
+ import { MiddlewareHandler } from "hyper-express";
2
+ import { ScopeType } from "../../decorators";
3
+ export default function scopeTransfrom(listScopes: ScopeType[], callback?: (middleware: MiddlewareHandler, scopes: {
4
+ scope: string;
5
+ description: string;
6
+ message: string | null;
7
+ }[], names: Set<string>) => void): MiddlewareHandler;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = scopeTransfrom;
4
+ const exeptions_1 = require("../../exeptions");
5
+ const helpers_1 = require("../../common/helpers");
6
+ function scopeTransfrom(listScopes, callback) {
7
+ const { scopes, scopeNames, isEmtpy } = resolveScopes(listScopes);
8
+ const middleware = (req, res, next) => {
9
+ var _a;
10
+ // if scopes is empty, then we don't need to check for scopes
11
+ if (isEmtpy)
12
+ return next();
13
+ // get the user scopes
14
+ const userScopes = (0, helpers_1.getScopes)(req);
15
+ // find the first scope that is not in the userScopes
16
+ const error = scopes.find((scope) => !(userScopes === null || userScopes === void 0 ? void 0 : userScopes.includes(scope.scope)));
17
+ if (error) {
18
+ return next(new exeptions_1.NotScopeException((_a = error.message) !== null && _a !== void 0 ? _a : `You don't have the required scopes to access this resource`, userScopes, Array.from(scopeNames)));
19
+ }
20
+ return next();
21
+ };
22
+ if (scopeNames.size > 0 && callback) {
23
+ callback(middleware, scopes, scopeNames);
24
+ }
25
+ return middleware;
26
+ }
27
+ /**
28
+ *
29
+ * Convert list of scopes to a standard format
30
+ *
31
+ * @param scopes
32
+ * @returns
33
+ */
34
+ const resolveScopes = (scopes) => {
35
+ var _a, _b;
36
+ const $scopes = {};
37
+ for (const scope of scopes) {
38
+ if (typeof scope === "string") {
39
+ $scopes[scope] = {
40
+ scope,
41
+ description: "",
42
+ message: null,
43
+ };
44
+ }
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
+ }
66
+ const values = Object.values($scopes);
67
+ return {
68
+ scopes: values,
69
+ scopeNames: new Set(Object.keys($scopes)),
70
+ isEmtpy: values.length === 0,
71
+ };
72
+ };
@@ -0,0 +1,8 @@
1
+ import { RouteMetadata } from "../decorators";
2
+ export interface RouterList {
3
+ type: string;
4
+ routes: Set<RouteMetadata>;
5
+ }
6
+ export type HyperApplicationPrivate<T> = T & {
7
+ prepare(): Promise<void>;
8
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Extracts argument names from a function.
3
+ */
4
+ export declare const extreactArgsNames: (fn: (...args: any[]) => any) => string[] | null;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extreactArgsNames = void 0;
4
+ /**
5
+ * Extracts argument names from a function.
6
+ */
7
+ const extreactArgsNames = (fn) => {
8
+ try {
9
+ const str = fn.toString();
10
+ const args = str
11
+ .slice(str.indexOf("(") + 1, str.indexOf(")"))
12
+ .split(",")
13
+ .map((arg) => arg.trim());
14
+ return args;
15
+ }
16
+ catch (error) {
17
+ return null;
18
+ }
19
+ };
20
+ exports.extreactArgsNames = extreactArgsNames;
@@ -0,0 +1,11 @@
1
+ import "reflect-metadata";
2
+ type Constructor<T = any> = new (...args: any[]) => T;
3
+ export declare class MixinBuilder<TClass extends Constructor<any>, Type = InstanceType<TClass>> {
4
+ private __class;
5
+ constructor(__class: TClass);
6
+ mix<NClass extends Constructor<any>>(clazz: NClass): MixinBuilder<NClass & TClass>;
7
+ build(): TClass;
8
+ construct(...args: any[]): Type;
9
+ static mix<NClass extends Constructor<any>>(clazz: NClass): MixinBuilder<NClass, InstanceType<NClass>>;
10
+ }
11
+ export {};
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MixinBuilder = void 0;
4
+ require("reflect-metadata");
5
+ class MixinBuilder {
6
+ constructor(__class) {
7
+ this.__class = __class;
8
+ }
9
+ mix(clazz) {
10
+ return new MixinBuilder(Mix(this.__class, clazz));
11
+ }
12
+ build() {
13
+ return this.__class;
14
+ }
15
+ construct(...args) {
16
+ return new this.__class(...args);
17
+ }
18
+ ////////////////////////////
19
+ /// Static methods
20
+ ////////////////////////////
21
+ static mix(clazz) {
22
+ return new MixinBuilder(clazz);
23
+ }
24
+ }
25
+ exports.MixinBuilder = MixinBuilder;
26
+ const Mix = (a, b) => {
27
+ return class extends a {
28
+ constructor(...args) {
29
+ super(...args);
30
+ Object.assign(this, new b(...args));
31
+ console.log(a);
32
+ }
33
+ };
34
+ };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Tipo recursivo para obtener claves anidadas dentro de un objeto, permitiendo solo strings.
3
+ */
4
+ export type NestedKeyOf<T> = T extends (infer U)[] ? `${number}` | `${number}.${NestedKeyOf<U>}` : T extends object ? {
5
+ [K in keyof T]: K extends string ? K | `${K}.${NestedKeyOf<T[K]>}` : never;
6
+ }[keyof T] : never;
7
+ export type ObjectValue<T, K extends string> = T extends object ? K extends keyof T ? T[K] : K extends `${infer MainKey}.${infer Rest}` ? MainKey extends keyof T ? ObjectValue<T[MainKey], Rest> : never : never : T;
8
+ /**
9
+ * Retrieves a value from an object using a path.
10
+ */
11
+ declare const $get: <T, K extends NestedKeyOf<T>, TDefault extends ObjectValue<T, K>>(data: T, name?: K, def?: TDefault) => TDefault extends undefined ? TDefault | undefined : TDefault;
12
+ /**
13
+ * Ensures the given value is returned as an array.
14
+ *
15
+ * @param {T | T[]} value - The value to ensure as an array.
16
+ * @returns {T[]} - The value as an array.
17
+ */
18
+ declare const $array: <T>(value: T | T[]) => T[];
19
+ /**
20
+ * Extract constructor from value.
21
+ *
22
+ * @param value
23
+ */
24
+ declare const $constructor: (value: any) => any;
25
+ declare const $each: <T>(data: T[] | undefined, callback: (value: T, index: number) => Promise<any>) => Promise<any[]>;
26
+ declare const $except_slash: (message: string, length?: number, middle?: boolean) => string;
27
+ export { $array, $get, $constructor, $each, $except_slash };