@zenofolio/hyper-decor 1.0.4 → 1.0.7
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/README.md +64 -1
- package/debug.txt +1 -0
- package/dist/__internals/constants.d.ts +5 -1
- package/dist/__internals/constants.js +5 -1
- package/dist/__internals/creators/request.creator.d.ts +2 -3
- package/dist/__internals/creators/request.creator.js +5 -3
- package/dist/__internals/creators/routes.creator.d.ts +2 -3
- package/dist/__internals/creators/routes.creator.js +5 -3
- package/dist/__internals/decorator-base.js +47 -11
- package/dist/__internals/helpers/imports.helper.d.ts +8 -0
- package/dist/__internals/helpers/imports.helper.js +92 -0
- 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.js +16 -20
- package/dist/__internals/helpers/prepare.helper.d.ts +11 -0
- package/dist/__internals/helpers/prepare.helper.js +348 -0
- package/dist/__internals/helpers/tree.helper.d.ts +36 -0
- package/dist/__internals/helpers/tree.helper.js +81 -0
- package/dist/__internals/stores/service.store.d.ts +1 -0
- package/dist/__internals/{transform/pass.transfrom.js → stores/service.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.js +8 -5
- package/dist/__internals/transform/scope.transfrom.d.ts +5 -7
- package/dist/__internals/transform/scope.transfrom.js +52 -38
- package/dist/__internals/transform/transform.registry.d.ts +33 -0
- package/dist/__internals/transform/transform.registry.js +59 -0
- package/dist/__internals/utils/function.util.d.ts +1 -4
- package/dist/__internals/utils/function.util.js +22 -10
- package/dist/common/bootstrap.js +4 -0
- 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/transport.d.ts +9 -0
- package/dist/common/transport.js +46 -0
- package/dist/decorators/File.d.ts +1 -1
- package/dist/decorators/Http.d.ts +20 -13
- package/dist/decorators/Http.js +77 -31
- package/dist/decorators/HyperApp.d.ts +0 -4
- package/dist/decorators/HyperApp.js +16 -182
- package/dist/decorators/HyperController.js +2 -1
- package/dist/decorators/HyperService.d.ts +15 -0
- package/dist/decorators/HyperService.js +29 -0
- package/dist/decorators/Messaging.d.ts +10 -0
- package/dist/decorators/Messaging.js +22 -0
- package/dist/decorators/Middleware.d.ts +5 -4
- package/dist/decorators/Middleware.js +33 -19
- package/dist/decorators/Output.d.ts +9 -0
- package/dist/decorators/Output.js +18 -0
- package/dist/decorators/Pass.d.ts +6 -3
- package/dist/decorators/Pass.js +6 -3
- package/dist/decorators/Role.js +5 -5
- package/dist/decorators/Routes.d.ts +14 -13
- package/dist/decorators/Scope.js +3 -3
- package/dist/decorators/Transform.d.ts +14 -0
- package/dist/decorators/Transform.js +18 -0
- package/dist/decorators/index.d.ts +3 -0
- package/dist/decorators/index.js +3 -0
- package/dist/decorators/types.d.ts +45 -10
- package/dist/decorators/types.js +4 -0
- package/dist/extension.js +11 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/lib/openapi/collectors/class.collector.d.ts +9 -0
- package/dist/lib/openapi/collectors/class.collector.js +67 -0
- package/dist/lib/openapi/collectors/index.d.ts +4 -0
- package/dist/lib/openapi/collectors/index.js +20 -0
- package/dist/lib/openapi/collectors/method.collector.d.ts +2 -0
- package/dist/lib/openapi/collectors/method.collector.js +68 -0
- package/dist/lib/openapi/collectors/param.collector.d.ts +2 -0
- package/dist/lib/openapi/collectors/param.collector.js +64 -0
- package/dist/lib/openapi/collectors/schema.collector.d.ts +11 -0
- package/dist/lib/openapi/collectors/schema.collector.js +37 -0
- package/dist/lib/openapi/constants.d.ts +46 -0
- package/dist/lib/openapi/constants.js +61 -0
- package/dist/lib/openapi/decorators/api-bearer-auth.decorator.d.ts +2 -0
- package/dist/lib/openapi/decorators/api-bearer-auth.decorator.js +10 -0
- package/dist/lib/openapi/decorators/api-method.decorator.d.ts +3 -0
- package/dist/lib/openapi/decorators/api-method.decorator.js +11 -0
- package/dist/lib/openapi/decorators/api-parameter.decorator.d.ts +3 -0
- package/dist/lib/openapi/decorators/api-parameter.decorator.js +10 -0
- package/dist/lib/openapi/decorators/api-request-body.decorator.d.ts +3 -0
- package/dist/lib/openapi/decorators/api-request-body.decorator.js +10 -0
- package/dist/lib/openapi/decorators/api-response.decorator.d.ts +3 -0
- package/dist/lib/openapi/decorators/api-response.decorator.js +10 -0
- package/dist/lib/openapi/decorators/api-security.decorator.d.ts +3 -0
- package/dist/lib/openapi/decorators/api-security.decorator.js +10 -0
- package/dist/lib/openapi/decorators/api-tag.decorator.d.ts +3 -0
- package/dist/lib/openapi/decorators/api-tag.decorator.js +13 -0
- package/dist/lib/openapi/decorators/index.d.ts +7 -0
- package/dist/lib/openapi/decorators/index.js +23 -0
- package/dist/lib/openapi/helpers/index.d.ts +7 -0
- package/dist/lib/openapi/helpers/index.js +23 -0
- package/dist/lib/openapi/helpers/method.helper.d.ts +3 -0
- package/dist/lib/openapi/helpers/method.helper.js +20 -0
- package/dist/lib/openapi/helpers/openapi.helper.d.ts +7 -0
- package/dist/lib/openapi/helpers/openapi.helper.js +51 -0
- package/dist/lib/openapi/helpers/parameter.helper.d.ts +3 -0
- package/dist/lib/openapi/helpers/parameter.helper.js +16 -0
- package/dist/lib/openapi/helpers/request-body.helper.d.ts +3 -0
- package/dist/lib/openapi/helpers/request-body.helper.js +9 -0
- package/dist/lib/openapi/helpers/response.helper.d.ts +3 -0
- package/dist/lib/openapi/helpers/response.helper.js +18 -0
- package/dist/lib/openapi/helpers/security.helper.d.ts +3 -0
- package/dist/lib/openapi/helpers/security.helper.js +17 -0
- package/dist/lib/openapi/helpers/tag.helper.d.ts +3 -0
- package/dist/lib/openapi/helpers/tag.helper.js +10 -0
- package/dist/lib/openapi/index.d.ts +6 -0
- package/dist/lib/openapi/index.js +22 -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/stores/index.d.ts +1 -0
- package/dist/{common/openapi → stores}/index.js +1 -2
- package/dist/stores/scope.store.d.ts +14 -0
- package/dist/stores/scope.store.js +29 -0
- package/dist/type.d.ts +13 -2
- package/hyper-express-decorators.d.ts +1 -0
- package/package.json +73 -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/vitest.json +0 -0
- package/.mocharc.js +0 -5
- 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/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/__internals/stores/{index.d.ts → metadata.store.d.ts} +0 -0
- /package/dist/__internals/stores/{index.js → metadata.store.js} +0 -0
- /package/dist/{__internals/stores/store.interface.js → lib/openapi/types.js} +0 -0
|
@@ -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,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;
|
package/dist/common/bootstrap.js
CHANGED
|
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.default = createApplication;
|
|
13
13
|
exports.createApplication = createApplication;
|
|
14
14
|
const tsyringe_1 = require("tsyringe");
|
|
15
|
+
const message_bus_1 = require("./message-bus");
|
|
15
16
|
function createApplication(app) {
|
|
16
17
|
return __awaiter(this, void 0, void 0, function* () {
|
|
17
18
|
var _a;
|
|
@@ -20,6 +21,9 @@ function createApplication(app) {
|
|
|
20
21
|
yield instance.prepare();
|
|
21
22
|
}
|
|
22
23
|
(_a = instance === null || instance === void 0 ? void 0 : instance.onPrepare) === null || _a === void 0 ? void 0 : _a.call(instance);
|
|
24
|
+
instance.emit = (topic, data) => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
yield message_bus_1.MessageBus.emit(topic, data);
|
|
26
|
+
});
|
|
23
27
|
return instance;
|
|
24
28
|
});
|
|
25
29
|
}
|
|
@@ -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);
|
|
@@ -16,8 +16,8 @@ export declare const getScopes: (request: Request) => string[] | undefined;
|
|
|
16
16
|
/**
|
|
17
17
|
* Check if request has required scopes
|
|
18
18
|
*
|
|
19
|
-
* @param request
|
|
20
|
-
* @param scopes
|
|
21
|
-
* @returns
|
|
19
|
+
* @param request - The incoming request
|
|
20
|
+
* @param scopes - Required scopes to check
|
|
21
|
+
* @returns `true` if the request has all required scopes, otherwise `false`
|
|
22
22
|
*/
|
|
23
23
|
export declare const hasScopes: (request: Request, scopes: HyperScopeOptions) => boolean;
|
|
@@ -33,15 +33,14 @@ exports.getScopes = getScopes;
|
|
|
33
33
|
/**
|
|
34
34
|
* Check if request has required scopes
|
|
35
35
|
*
|
|
36
|
-
* @param request
|
|
37
|
-
* @param scopes
|
|
38
|
-
* @returns
|
|
36
|
+
* @param request - The incoming request
|
|
37
|
+
* @param scopes - Required scopes to check
|
|
38
|
+
* @returns `true` if the request has all required scopes, otherwise `false`
|
|
39
39
|
*/
|
|
40
40
|
const hasScopes = (request, scopes) => {
|
|
41
|
-
const requestScopes = (0, exports.getScopes)(request);
|
|
42
|
-
if (
|
|
43
|
-
return
|
|
44
|
-
|
|
45
|
-
return (0, object_util_1.$array)(scopes).every((scope) => requestScopes.includes(scope));
|
|
41
|
+
const requestScopes = new Set((0, exports.getScopes)(request) || []);
|
|
42
|
+
if (requestScopes.has("*"))
|
|
43
|
+
return true;
|
|
44
|
+
return (0, object_util_1.$array)(scopes).every(scope => requestScopes.has(scope));
|
|
46
45
|
};
|
|
47
46
|
exports.hasScopes = hasScopes;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Request } from "hyper-express";
|
|
2
|
+
/**
|
|
3
|
+
* Save a custom value against the request directly via an internal Map
|
|
4
|
+
*
|
|
5
|
+
* @param {Request} request
|
|
6
|
+
* @param {string} key
|
|
7
|
+
* @param {any} value
|
|
8
|
+
*/
|
|
9
|
+
export declare const setValue: (request: Request, key: string, value: any) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Get a custom value from the request object directly via an internal Map
|
|
12
|
+
*
|
|
13
|
+
* @param {Request} request
|
|
14
|
+
* @param {string} key
|
|
15
|
+
* @param {any} defaultValue
|
|
16
|
+
*/
|
|
17
|
+
export declare const getValue: <T>(request: Request, key: string, defaultValue?: T) => T | any;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getValue = exports.setValue = void 0;
|
|
4
|
+
const STATE_SYMBOL = Symbol("HYPER_DECOR_REQUEST_STATE");
|
|
5
|
+
/**
|
|
6
|
+
* Get or create the internal state map for the request
|
|
7
|
+
* @param request
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
function getStore(request) {
|
|
11
|
+
if (!request[STATE_SYMBOL]) {
|
|
12
|
+
request[STATE_SYMBOL] = new Map();
|
|
13
|
+
}
|
|
14
|
+
return request[STATE_SYMBOL];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Save a custom value against the request directly via an internal Map
|
|
18
|
+
*
|
|
19
|
+
* @param {Request} request
|
|
20
|
+
* @param {string} key
|
|
21
|
+
* @param {any} value
|
|
22
|
+
*/
|
|
23
|
+
const setValue = (request, key, value) => {
|
|
24
|
+
const store = getStore(request);
|
|
25
|
+
if (value === undefined) {
|
|
26
|
+
store.delete(key);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
store.set(key, value);
|
|
30
|
+
};
|
|
31
|
+
exports.setValue = setValue;
|
|
32
|
+
/**
|
|
33
|
+
* Get a custom value from the request object directly via an internal Map
|
|
34
|
+
*
|
|
35
|
+
* @param {Request} request
|
|
36
|
+
* @param {string} key
|
|
37
|
+
* @param {any} defaultValue
|
|
38
|
+
*/
|
|
39
|
+
const getValue = (request, key, defaultValue) => {
|
|
40
|
+
const store = getStore(request);
|
|
41
|
+
const value = store.get(key);
|
|
42
|
+
return value !== undefined ? value : defaultValue;
|
|
43
|
+
};
|
|
44
|
+
exports.getValue = getValue;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IMessageTransport } from "./transport";
|
|
2
|
+
export declare class MessageBus {
|
|
3
|
+
private transports;
|
|
4
|
+
registerTransport(transport: IMessageTransport): void;
|
|
5
|
+
emit(topic: string, data: any): Promise<void>;
|
|
6
|
+
listen(topic: string, handler: (data: any) => Promise<void> | void): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Static helper to emit messages without resolving the bus manually.
|
|
9
|
+
*/
|
|
10
|
+
static emit(topic: string, data: any): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
var MessageBus_1;
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.MessageBus = void 0;
|
|
20
|
+
const tsyringe_1 = require("tsyringe");
|
|
21
|
+
let MessageBus = MessageBus_1 = class MessageBus {
|
|
22
|
+
constructor() {
|
|
23
|
+
this.transports = [];
|
|
24
|
+
}
|
|
25
|
+
registerTransport(transport) {
|
|
26
|
+
this.transports.push(transport);
|
|
27
|
+
}
|
|
28
|
+
emit(topic, data) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
yield Promise.all(this.transports.map((t) => t.emit(topic, data)));
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
listen(topic, handler) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
yield Promise.all(this.transports.map((t) => t.listen(topic, handler)));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Static helper to emit messages without resolving the bus manually.
|
|
40
|
+
*/
|
|
41
|
+
static emit(topic, data) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
const bus = tsyringe_1.container.resolve(MessageBus_1);
|
|
44
|
+
yield bus.emit(topic, data);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.MessageBus = MessageBus;
|
|
49
|
+
exports.MessageBus = MessageBus = MessageBus_1 = __decorate([
|
|
50
|
+
(0, tsyringe_1.singleton)()
|
|
51
|
+
], MessageBus);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface IMessageTransport {
|
|
2
|
+
listen(topic: string, handler: (data: any) => Promise<void> | void): Promise<void>;
|
|
3
|
+
emit(topic: string, data: any): Promise<void>;
|
|
4
|
+
}
|
|
5
|
+
export declare class InternalTransport implements IMessageTransport {
|
|
6
|
+
private emitter;
|
|
7
|
+
listen(topic: string, handler: (data: any) => Promise<void> | void): Promise<void>;
|
|
8
|
+
emit(topic: string, data: any): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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.InternalTransport = void 0;
|
|
13
|
+
const eventemitter3_1 = require("eventemitter3");
|
|
14
|
+
class InternalTransport {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.emitter = new eventemitter3_1.EventEmitter();
|
|
17
|
+
}
|
|
18
|
+
listen(topic, handler) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
// Basic wildcard support: user.* -> user.created, user.updated
|
|
21
|
+
// We'll use a simple regex for this internal transport
|
|
22
|
+
if (topic.includes("*")) {
|
|
23
|
+
const pattern = new RegExp("^" + topic.replace(/\*/g, ".*") + "$");
|
|
24
|
+
// We need a catch-all for wildcards in EventEmitter3?
|
|
25
|
+
// EE3 doesn't support wildcards out of the box easily without listening to all events.
|
|
26
|
+
// For this internal implementation, we'll override emit or use a proxy.
|
|
27
|
+
// But simpler: just use a pattern-matching listener.
|
|
28
|
+
this.emitter.on("message", (t, data) => {
|
|
29
|
+
if (pattern.test(t)) {
|
|
30
|
+
handler(data);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
this.emitter.on(topic, handler);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
emit(topic, data) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
this.emitter.emit(topic, data);
|
|
42
|
+
this.emitter.emit("message", topic, data); // For wildcard listeners
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.InternalTransport = InternalTransport;
|
|
@@ -5,28 +5,35 @@ import { ParameterResolver } from "./types";
|
|
|
5
5
|
* @param key
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
export declare const Query: (key?: string, transfrom?: (data: any) => any) => ParameterDecorator;
|
|
9
8
|
/**
|
|
10
|
-
* Get the
|
|
9
|
+
* Get the value of a key from the request object or transform the entire query.
|
|
11
10
|
*
|
|
12
|
-
* @
|
|
13
|
-
*
|
|
11
|
+
* @example
|
|
12
|
+
* \@Query('id')
|
|
13
|
+
* \@Query('id', IntSchema)
|
|
14
|
+
* \@Query(UserQueryDto)
|
|
14
15
|
*/
|
|
15
|
-
export declare
|
|
16
|
+
export declare function Query(keyOrSchema?: string | any, schemaOrTransform?: any): any;
|
|
16
17
|
/**
|
|
17
|
-
* Get the
|
|
18
|
+
* Get the body of the request or transform it via a schema.
|
|
18
19
|
*
|
|
19
|
-
* @
|
|
20
|
-
*
|
|
20
|
+
* @example
|
|
21
|
+
* \@Body()
|
|
22
|
+
* \@Body(CreateUserDto)
|
|
21
23
|
*/
|
|
22
|
-
export declare
|
|
24
|
+
export declare function Body(schemaOrResolver?: any): any;
|
|
23
25
|
/**
|
|
24
|
-
*
|
|
26
|
+
* Get the params from the request or transform via schema.
|
|
25
27
|
*
|
|
26
|
-
* @
|
|
27
|
-
*
|
|
28
|
+
* @example
|
|
29
|
+
* \@Param('id')
|
|
30
|
+
* \@Param('id', IntSchema)
|
|
31
|
+
*/
|
|
32
|
+
export declare function Param(keyOrSchema: string | any, schemaOrValidator?: any): any;
|
|
33
|
+
/**
|
|
34
|
+
* Get the headers from the request.
|
|
28
35
|
*/
|
|
29
|
-
export declare
|
|
36
|
+
export declare function Headers(keyOrSchema?: string | any, schema?: any): any;
|
|
30
37
|
export declare const Req: () => ParameterDecorator;
|
|
31
38
|
export declare const Res: () => ParameterDecorator;
|
|
32
39
|
/**
|
package/dist/decorators/Http.js
CHANGED
|
@@ -12,7 +12,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.createCustomRequestDecorator = exports.Res = exports.Req =
|
|
15
|
+
exports.createCustomRequestDecorator = exports.Res = exports.Req = void 0;
|
|
16
|
+
exports.Query = Query;
|
|
17
|
+
exports.Body = Body;
|
|
18
|
+
exports.Param = Param;
|
|
19
|
+
exports.Headers = Headers;
|
|
16
20
|
const request_creator_1 = __importDefault(require("../__internals/creators/request.creator"));
|
|
17
21
|
const object_util_1 = require("../__internals/utils/object.util");
|
|
18
22
|
/**
|
|
@@ -21,45 +25,87 @@ const object_util_1 = require("../__internals/utils/object.util");
|
|
|
21
25
|
* @param key
|
|
22
26
|
* @returns
|
|
23
27
|
*/
|
|
24
|
-
const Query = (key, transfrom) => (0, request_creator_1.default)("query", "Query", (request) => {
|
|
25
|
-
const value = (0, object_util_1.$get)(request.query, key, request.query);
|
|
26
|
-
if (!transfrom)
|
|
27
|
-
return value;
|
|
28
|
-
return value;
|
|
29
|
-
});
|
|
30
|
-
exports.Query = Query;
|
|
31
28
|
/**
|
|
32
|
-
* Get the
|
|
29
|
+
* Get the value of a key from the request object or transform the entire query.
|
|
33
30
|
*
|
|
34
|
-
* @
|
|
35
|
-
*
|
|
31
|
+
* @example
|
|
32
|
+
* \@Query('id')
|
|
33
|
+
* \@Query('id', IntSchema)
|
|
34
|
+
* \@Query(UserQueryDto)
|
|
36
35
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
function Query(keyOrSchema, schemaOrTransform) {
|
|
37
|
+
// Case: @Query(UserQueryDto)
|
|
38
|
+
if (typeof keyOrSchema === 'function' && !schemaOrTransform) {
|
|
39
|
+
return (0, request_creator_1.default)("query", "Query", (request) => request.query, keyOrSchema, true);
|
|
40
|
+
}
|
|
41
|
+
// Case: @Query('id', IntSchema) or @Query('id', data => ...)
|
|
42
|
+
const key = keyOrSchema;
|
|
43
|
+
const resolver = (request) => (0, object_util_1.$get)(request.query, key, request.query);
|
|
44
|
+
if (typeof schemaOrTransform === 'function' && schemaOrTransform.prototype === undefined) {
|
|
45
|
+
// It's a legacy transform function: data => ...
|
|
46
|
+
return (0, request_creator_1.default)("query", "Query", (request) => {
|
|
47
|
+
const value = resolver(request);
|
|
48
|
+
return schemaOrTransform(value);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
// It's either a schema/DTO class or just a key
|
|
52
|
+
return (0, request_creator_1.default)("query", "Query", resolver, schemaOrTransform, false);
|
|
53
|
+
}
|
|
42
54
|
/**
|
|
43
|
-
* Get the
|
|
55
|
+
* Get the body of the request or transform it via a schema.
|
|
44
56
|
*
|
|
45
|
-
* @
|
|
46
|
-
*
|
|
57
|
+
* @example
|
|
58
|
+
* \@Body()
|
|
59
|
+
* \@Body(CreateUserDto)
|
|
47
60
|
*/
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
function Body(schemaOrResolver) {
|
|
62
|
+
const resolver = (request) => __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
return request.body !== undefined ? request.body : yield request.json();
|
|
64
|
+
});
|
|
65
|
+
if (typeof schemaOrResolver === 'function' && schemaOrResolver.prototype !== undefined) {
|
|
66
|
+
// Case: @Body(CreateUserDto)
|
|
67
|
+
return (0, request_creator_1.default)("req", "BODY", resolver, schemaOrResolver, true);
|
|
68
|
+
}
|
|
69
|
+
// Case: @Body(data => ...) (Legacy) or @Body()
|
|
70
|
+
return (0, request_creator_1.default)("req", "BODY", (request) => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const value = yield resolver(request);
|
|
72
|
+
return typeof schemaOrResolver === 'function' ? schemaOrResolver(value) : value;
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
55
75
|
/**
|
|
56
|
-
*
|
|
76
|
+
* Get the params from the request or transform via schema.
|
|
57
77
|
*
|
|
58
|
-
* @
|
|
59
|
-
*
|
|
78
|
+
* @example
|
|
79
|
+
* \@Param('id')
|
|
80
|
+
* \@Param('id', IntSchema)
|
|
60
81
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
82
|
+
function Param(keyOrSchema, schemaOrValidator) {
|
|
83
|
+
// Case: @Param(ParamsDto)
|
|
84
|
+
if (typeof keyOrSchema === 'function' && !schemaOrValidator) {
|
|
85
|
+
return (0, request_creator_1.default)("params", "Param", (req) => req.params, keyOrSchema, true);
|
|
86
|
+
}
|
|
87
|
+
const key = keyOrSchema;
|
|
88
|
+
const resolver = (req) => (0, object_util_1.$get)(req.params, key, req.params);
|
|
89
|
+
if (typeof schemaOrValidator === 'function' && schemaOrValidator.prototype === undefined) {
|
|
90
|
+
// Legacy validator function
|
|
91
|
+
return (0, request_creator_1.default)("params", "Param", (req) => {
|
|
92
|
+
const value = resolver(req);
|
|
93
|
+
return schemaOrValidator(value);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return (0, request_creator_1.default)("params", "Param", resolver, schemaOrValidator, false);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get the headers from the request.
|
|
100
|
+
*/
|
|
101
|
+
function Headers(keyOrSchema, schema) {
|
|
102
|
+
if (typeof keyOrSchema === 'function' && !schema) {
|
|
103
|
+
return (0, request_creator_1.default)("headers", "Headers", (req) => req.headers, keyOrSchema, true);
|
|
104
|
+
}
|
|
105
|
+
const key = keyOrSchema;
|
|
106
|
+
const resolver = (req) => (0, object_util_1.$get)(req.headers, key, req.headers);
|
|
107
|
+
return (0, request_creator_1.default)("headers", "Headers", resolver, schema, false);
|
|
108
|
+
}
|
|
63
109
|
const Req = () => (0, request_creator_1.default)("req", "Req", (req) => req);
|
|
64
110
|
exports.Req = Req;
|
|
65
111
|
const Res = () => (0, request_creator_1.default)("res", "Res", (req, res) => res);
|
|
@@ -1,7 +1,3 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
2
|
import { HyperAppDecorator } from "./types";
|
|
3
|
-
/**
|
|
4
|
-
* Decorator to define the main application class with assigned modules.
|
|
5
|
-
* @param modules - List of modules to be used in the application.
|
|
6
|
-
*/
|
|
7
3
|
export declare const HyperApp: HyperAppDecorator;
|