framework-do-dede 0.0.29 → 0.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/decorators/controller.d.ts +2 -1
- package/dist/decorators/controller.js +5 -14
- package/dist/handlers/controller.handler.d.ts +21 -0
- package/dist/handlers/controller.handler.js +11 -6
- package/dist/index.d.ts +2 -2
- package/dist/protocols/Controller.d.ts +3 -0
- package/dist/protocols/RequestMetricsHandler.d.ts +4 -0
- package/dist/protocols/RequestMetricsHandler.js +1 -0
- package/dist/protocols/index.d.ts +2 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Validation } from "../protocols/Validation";
|
|
2
|
-
import { HttpMiddleware } from "../protocols";
|
|
2
|
+
import { HttpMiddleware, RequestMetricsHandler } from "../protocols";
|
|
3
3
|
export declare function Controller(basePath: string): (target: any) => void;
|
|
4
4
|
export declare function Middleware(middlewareClass: new (...args: any[]) => HttpMiddleware): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
5
5
|
export declare function Post(config?: {
|
|
@@ -33,3 +33,4 @@ export declare function Delete(config?: {
|
|
|
33
33
|
query?: string[];
|
|
34
34
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
35
35
|
export declare function Validator(validationClass: new () => Validation): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
36
|
+
export declare function Metrics(...handlers: (new (...args: any[]) => RequestMetricsHandler)[]): MethodDecorator;
|
|
@@ -8,20 +8,6 @@ export function Controller(basePath) {
|
|
|
8
8
|
Registry.addDependency('controllers', target);
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
|
-
// export function Middleware(middlewareClass: new () => HttpMiddleware) {
|
|
12
|
-
// return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
13
|
-
// if (typeof middlewareClass.prototype.execute !== 'function') {
|
|
14
|
-
// throw new FrameworkError('The concrete class does not implement the Middleware interface.');
|
|
15
|
-
// }
|
|
16
|
-
// // Retrieve existing middlewares for this method or initialize an empty array
|
|
17
|
-
// const middlewares: HttpMiddleware[] = Reflect.getMetadata('middlewares', target, propertyKey) || [];
|
|
18
|
-
// // Create a new instance of the middleware and add it to the array
|
|
19
|
-
// const middlewareInstance = new middlewareClass();
|
|
20
|
-
// middlewares.push(middlewareInstance);
|
|
21
|
-
// // Update the metadata with the new array of middleware instances
|
|
22
|
-
// Reflect.defineMetadata('middlewares', middlewares, target, propertyKey);
|
|
23
|
-
// };
|
|
24
|
-
// }
|
|
25
11
|
export function Middleware(middlewareClass) {
|
|
26
12
|
return function (target, propertyKey, descriptor) {
|
|
27
13
|
if (!middlewareClass.prototype.execute) {
|
|
@@ -95,3 +81,8 @@ export function Validator(validationClass) {
|
|
|
95
81
|
Reflect.defineMetadata('validation', new validationClass(), target, propertyKey);
|
|
96
82
|
};
|
|
97
83
|
}
|
|
84
|
+
export function Metrics(...handlers) {
|
|
85
|
+
return (target, propertyKey, descriptor) => {
|
|
86
|
+
Reflect.defineMetadata('metricsHandlers', handlers, target, propertyKey);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
import HttpServer from "../http/HttpServer";
|
|
2
|
+
export type RequestMetrics = {
|
|
3
|
+
startTime: number;
|
|
4
|
+
elapsedTime: number;
|
|
5
|
+
endTime: number;
|
|
6
|
+
date: Date;
|
|
7
|
+
method: string;
|
|
8
|
+
route: string;
|
|
9
|
+
agent: string;
|
|
10
|
+
ip: string;
|
|
11
|
+
contentLength: string;
|
|
12
|
+
error?: {
|
|
13
|
+
stack: string;
|
|
14
|
+
originalMessage: string;
|
|
15
|
+
message: string;
|
|
16
|
+
statusCode: number;
|
|
17
|
+
};
|
|
18
|
+
handler: {
|
|
19
|
+
instance: string;
|
|
20
|
+
method: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
2
23
|
export default class ControllerHandler {
|
|
3
24
|
constructor(httpServer: HttpServer, port: number);
|
|
4
25
|
private registryControllers;
|
|
@@ -2,7 +2,7 @@ import { Registry } from "../di/registry";
|
|
|
2
2
|
import { ServerError } from "../http";
|
|
3
3
|
export default class ControllerHandler {
|
|
4
4
|
constructor(httpServer, port) {
|
|
5
|
-
for (const { instance, instanceMethod, middlewares, method, route, statusCode, params, query, validation } of this.registryControllers()) {
|
|
5
|
+
for (const { instance, instanceMethod, middlewares, method, route, statusCode, params, query, validation, offLogs, metricsHandlers } of this.registryControllers()) {
|
|
6
6
|
httpServer.register({
|
|
7
7
|
method,
|
|
8
8
|
route,
|
|
@@ -10,7 +10,6 @@ export default class ControllerHandler {
|
|
|
10
10
|
params,
|
|
11
11
|
query
|
|
12
12
|
}, async (input) => {
|
|
13
|
-
console.log('headers = ', input.headers);
|
|
14
13
|
const requestMetrics = {
|
|
15
14
|
date: new Date(),
|
|
16
15
|
startTime: 0,
|
|
@@ -49,17 +48,21 @@ export default class ControllerHandler {
|
|
|
49
48
|
const request = { headers: input.headers, data: mergedParams, middlewareData };
|
|
50
49
|
try {
|
|
51
50
|
const response = await instance[instanceMethod](mergedParams, request);
|
|
52
|
-
|
|
51
|
+
if (!offLogs)
|
|
52
|
+
console.log('\x1b[32m%s\x1b[0m', `✅ [LOG] Finish: "${logger.handler.instance}.${logger.handler.method}:" - ${requestMetrics.elapsedTime}ms`);
|
|
53
53
|
return response;
|
|
54
54
|
}
|
|
55
55
|
catch (error) {
|
|
56
|
-
|
|
56
|
+
if (!offLogs)
|
|
57
|
+
console.log('\x1b[31m%s\x1b[0m', `❌ [LOG] Erro: "${logger.handler.instance}.${logger.handler.method}:" - error: ${error.message}`);
|
|
57
58
|
requestMetrics.error = this.extractError(error);
|
|
58
59
|
}
|
|
59
60
|
finally {
|
|
60
61
|
requestMetrics.endTime = performance.now();
|
|
61
62
|
requestMetrics.elapsedTime = requestMetrics.endTime - requestMetrics.startTime;
|
|
62
|
-
|
|
63
|
+
if (metricsHandlers?.length) {
|
|
64
|
+
await Promise.all(metricsHandlers.map((handler) => handler.handle(requestMetrics)));
|
|
65
|
+
}
|
|
63
66
|
}
|
|
64
67
|
});
|
|
65
68
|
}
|
|
@@ -78,6 +81,7 @@ export default class ControllerHandler {
|
|
|
78
81
|
const validation = Reflect.getMetadata('validation', controller.prototype, methodName);
|
|
79
82
|
const routeConfig = Reflect.getMetadata('route', controller.prototype, methodName);
|
|
80
83
|
const middlewares = Reflect.getMetadata('middlewares', controller.prototype, methodName);
|
|
84
|
+
const metricsHandlers = Reflect.getMetadata('metricsHandlers', controller.prototype, methodName);
|
|
81
85
|
controllers.push({
|
|
82
86
|
method: routeConfig.method,
|
|
83
87
|
route: basePath + routeConfig.path,
|
|
@@ -87,7 +91,8 @@ export default class ControllerHandler {
|
|
|
87
91
|
instance,
|
|
88
92
|
instanceMethod: methodName,
|
|
89
93
|
middlewares: middlewares ? middlewares.map(middleware => Registry.classLoader(middleware)) : [],
|
|
90
|
-
validation
|
|
94
|
+
validation,
|
|
95
|
+
metricsHandlers: metricsHandlers ? metricsHandlers.map(metricsHandler => Registry.classLoader(metricsHandler)) : [],
|
|
91
96
|
});
|
|
92
97
|
}
|
|
93
98
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Dede, Register as DedeRegister, Options as DedeOptions } from './dede';
|
|
2
2
|
import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Auth, Inject, Restrict } from './decorators';
|
|
3
3
|
import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
|
|
4
|
-
import { Validation, HttpMiddleware, UseCase, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository } from './protocols';
|
|
4
|
+
import { Validation, HttpMiddleware, UseCase, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestMetricsHandler } from './protocols';
|
|
5
5
|
import { Entity } from './domain/Entity';
|
|
6
6
|
declare class RequestData {
|
|
7
7
|
headers: any;
|
|
@@ -12,4 +12,4 @@ declare class RequestData {
|
|
|
12
12
|
declare class UseCaseHandler {
|
|
13
13
|
static load<T extends UseCase<any, any>>(useCaseClass: new (...args: any[]) => T, request?: RequestData): T;
|
|
14
14
|
}
|
|
15
|
-
export { UseCase, HttpMiddleware, Validation, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestData, Dede, DedeRegister, DedeOptions, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Auth, Inject, Entity, Restrict };
|
|
15
|
+
export { UseCase, HttpMiddleware, Validation, RequestMetricsHandler, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestData, Dede, DedeRegister, DedeOptions, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Auth, Inject, Entity, Restrict };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AllowedMethods, HttpStatusCode } from "../http/HttpServer";
|
|
2
2
|
import type { Validation } from "./Validation";
|
|
3
3
|
import type { HttpMiddleware } from "./HttpMiddleware";
|
|
4
|
+
import { RequestMetricsHandler } from "./RequestMetricsHandler";
|
|
4
5
|
export type Controller = {
|
|
5
6
|
instance: any;
|
|
6
7
|
instanceMethod: string;
|
|
@@ -11,4 +12,6 @@ export type Controller = {
|
|
|
11
12
|
validation?: Validation;
|
|
12
13
|
params?: any;
|
|
13
14
|
query?: any;
|
|
15
|
+
metricsHandlers?: RequestMetricsHandler[];
|
|
16
|
+
offLogs?: boolean;
|
|
14
17
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -5,4 +5,5 @@ import type { CreateRepository } from './CreateRepository';
|
|
|
5
5
|
import type { DeleteRepository } from './DeleteRepository';
|
|
6
6
|
import type { UpdateRepository } from './UpdateRepository';
|
|
7
7
|
import type { RestoreRepository } from './RestoreRepository';
|
|
8
|
-
|
|
8
|
+
import type { RequestMetricsHandler } from './RequestMetricsHandler';
|
|
9
|
+
export type { HttpMiddleware, UseCase, Validation, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestMetricsHandler };
|