framework-do-dede 0.0.52 → 0.0.54
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/handlers/controller.handler.js +12 -12
- package/dist/http/HttpServer.d.ts +1 -2
- package/dist/http/HttpServer.js +14 -68
- package/dist/index.d.ts +2 -2
- package/dist/protocols/HttpServerError.d.ts +5 -0
- package/dist/protocols/HttpServerError.js +1 -0
- package/dist/protocols/index.d.ts +2 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Registry } from "../di/registry";
|
|
2
|
-
import { InternalServerError } from "../http";
|
|
2
|
+
import { InternalServerError, ServerError } from "../http";
|
|
3
3
|
export default class ControllerHandler {
|
|
4
4
|
constructor(httpServer, port) {
|
|
5
5
|
for (const { instance, instanceMethod, middlewares, method, route, statusCode, params, query, validation, offLogs, metricsHandlers } of this.registryControllers()) {
|
|
@@ -8,7 +8,7 @@ export default class ControllerHandler {
|
|
|
8
8
|
route,
|
|
9
9
|
statusCode,
|
|
10
10
|
params,
|
|
11
|
-
query
|
|
11
|
+
query,
|
|
12
12
|
}, async (input) => {
|
|
13
13
|
const requestMetrics = {
|
|
14
14
|
date: new Date(),
|
|
@@ -57,10 +57,13 @@ export default class ControllerHandler {
|
|
|
57
57
|
catch (error) {
|
|
58
58
|
if (!offLogs)
|
|
59
59
|
console.log('\x1b[31m%s\x1b[0m', `❌ [LOG] Erro: "${logger.handler.instance}.${logger.handler.method}:" - error: ${error.message}`);
|
|
60
|
-
const capturedError = this.extractError(error);
|
|
60
|
+
const capturedError = this.extractError(error, httpServer);
|
|
61
61
|
requestMetrics.error = capturedError;
|
|
62
|
-
|
|
63
|
-
return
|
|
62
|
+
input.setStatus(capturedError.statusCode);
|
|
63
|
+
return {
|
|
64
|
+
message: capturedError.message,
|
|
65
|
+
statusCode: capturedError.statusCode
|
|
66
|
+
};
|
|
64
67
|
}
|
|
65
68
|
finally {
|
|
66
69
|
requestMetrics.endTime = performance.now();
|
|
@@ -73,12 +76,6 @@ export default class ControllerHandler {
|
|
|
73
76
|
}
|
|
74
77
|
httpServer.listen(port);
|
|
75
78
|
}
|
|
76
|
-
// if (error instanceof ServerError) {
|
|
77
|
-
// res.status(error.getStatusCode())
|
|
78
|
-
// throw error;
|
|
79
|
-
// }
|
|
80
|
-
// res.status(500)
|
|
81
|
-
// throw new InternalServerError(error.message, this.defaultMessageError)
|
|
82
79
|
registryControllers() {
|
|
83
80
|
const registryControllers = Registry.resolve('controllers');
|
|
84
81
|
const controllers = [];
|
|
@@ -130,7 +127,10 @@ export default class ControllerHandler {
|
|
|
130
127
|
}
|
|
131
128
|
return filter;
|
|
132
129
|
}
|
|
133
|
-
extractError(error) {
|
|
130
|
+
extractError(error, httpServer) {
|
|
131
|
+
if (error instanceof ServerError) {
|
|
132
|
+
error = new InternalServerError(error.message, httpServer.getDefaultMessageError());
|
|
133
|
+
}
|
|
134
134
|
return {
|
|
135
135
|
message: error.message,
|
|
136
136
|
statusCode: error.getStatusCode(),
|
|
@@ -20,12 +20,11 @@ export default abstract class HttpServer {
|
|
|
20
20
|
protected framework: FrameworkWeb;
|
|
21
21
|
protected frameworkName: string;
|
|
22
22
|
private defaultMessageError;
|
|
23
|
-
private set;
|
|
24
23
|
constructor(framework: FrameworkWeb, frameworkName: 'elysia' | 'express');
|
|
25
24
|
use(middleware: CallableFunction): HttpServer;
|
|
26
25
|
register(httpServerParams: HttpServerParams, handler: CallableFunction): void;
|
|
27
|
-
private test;
|
|
28
26
|
setDefaultMessageError(message: string): void;
|
|
27
|
+
getDefaultMessageError(): string;
|
|
29
28
|
listen(port: number): void;
|
|
30
29
|
private mountRoute;
|
|
31
30
|
private elysia;
|
package/dist/http/HttpServer.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { FrameworkError } from "./FrameworkError";
|
|
2
|
-
import { InternalServerError, ServerError } from "./ServerError";
|
|
3
2
|
export default class HttpServer {
|
|
4
3
|
framework;
|
|
5
4
|
frameworkName;
|
|
6
5
|
defaultMessageError = '';
|
|
7
|
-
set = null;
|
|
8
6
|
constructor(framework, frameworkName) {
|
|
9
7
|
if (frameworkName !== 'elysia' && frameworkName !== 'express')
|
|
10
8
|
throw new FrameworkError('Framework not supported');
|
|
@@ -17,49 +15,16 @@ export default class HttpServer {
|
|
|
17
15
|
}
|
|
18
16
|
register(httpServerParams, handler) {
|
|
19
17
|
const route = this.mountRoute(httpServerParams);
|
|
20
|
-
if (this.frameworkName === 'elysia')
|
|
21
|
-
this.
|
|
22
|
-
try {
|
|
23
|
-
return this.elysia(httpServerParams, route, handler);
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
if (error instanceof ServerError) {
|
|
27
|
-
this.set.status = error.getStatusCode();
|
|
28
|
-
throw error;
|
|
29
|
-
}
|
|
30
|
-
if (this.set)
|
|
31
|
-
this.set.status = 500;
|
|
32
|
-
throw new InternalServerError(error.message, this.defaultMessageError);
|
|
33
|
-
}
|
|
34
|
-
finally {
|
|
35
|
-
if (this.set)
|
|
36
|
-
this.set.status = null;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
18
|
+
if (this.frameworkName === 'elysia')
|
|
19
|
+
return this.elysia(httpServerParams, route, handler);
|
|
39
20
|
return this.express(httpServerParams, route, handler);
|
|
40
21
|
}
|
|
41
|
-
async test(httpServerParams, route, handler) {
|
|
42
|
-
return new Promise((resolve, rejected) => {
|
|
43
|
-
return resolve(this.elysia(httpServerParams, route, handler));
|
|
44
|
-
}).then((response) => {
|
|
45
|
-
if (response instanceof ServerError) {
|
|
46
|
-
console.log('the response is', response);
|
|
47
|
-
this.set.status = response.getStatusCode();
|
|
48
|
-
// throw response;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
console.log('the response is', response);
|
|
52
|
-
console.log('set', this.set);
|
|
53
|
-
if (this.set)
|
|
54
|
-
this.set.status = 500;
|
|
55
|
-
console.log('now set', this.set);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
// this.elysia(httpServerParams, route, handler)
|
|
59
|
-
}
|
|
60
22
|
setDefaultMessageError(message) {
|
|
61
23
|
this.defaultMessageError = message;
|
|
62
24
|
}
|
|
25
|
+
getDefaultMessageError() {
|
|
26
|
+
return this.defaultMessageError;
|
|
27
|
+
}
|
|
63
28
|
listen(port) {
|
|
64
29
|
this.framework.listen(port);
|
|
65
30
|
}
|
|
@@ -78,8 +43,8 @@ export default class HttpServer {
|
|
|
78
43
|
(this.framework[method])(route, async ({ headers, set, query, params, body, request }) => {
|
|
79
44
|
headers['ip'] = this.framework.server?.requestIP(request).address;
|
|
80
45
|
set.status = httpServerParams.statusCode ?? 200;
|
|
81
|
-
this.set = set;
|
|
82
46
|
const output = await handler({
|
|
47
|
+
setStatus: (statusCode) => set.status = statusCode,
|
|
83
48
|
headers,
|
|
84
49
|
query,
|
|
85
50
|
params,
|
|
@@ -92,33 +57,14 @@ export default class HttpServer {
|
|
|
92
57
|
const method = httpServerParams.method;
|
|
93
58
|
this.framework[method](route, async (request, res) => {
|
|
94
59
|
request.headers['ip'] = request.ip;
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
if (error instanceof ServerError) {
|
|
106
|
-
res.status(error.getStatusCode());
|
|
107
|
-
throw error;
|
|
108
|
-
}
|
|
109
|
-
res.status(500);
|
|
110
|
-
throw new InternalServerError(error.message, this.defaultMessageError);
|
|
111
|
-
// if (error instanceof ServerError) {
|
|
112
|
-
// return res.status(error.getStatusCode()).json({
|
|
113
|
-
// error: error.message,
|
|
114
|
-
// statusCode: error.getStatusCode()
|
|
115
|
-
// })
|
|
116
|
-
// }
|
|
117
|
-
// return res.status(500).json({
|
|
118
|
-
// error: this.defaultMessageError,
|
|
119
|
-
// statusCode: 500
|
|
120
|
-
// })
|
|
121
|
-
}
|
|
60
|
+
const output = await handler({
|
|
61
|
+
setStatus: (statusCode) => res.status(statusCode),
|
|
62
|
+
headers: request.headers,
|
|
63
|
+
query: request.query,
|
|
64
|
+
params: request.params,
|
|
65
|
+
body: request.body
|
|
66
|
+
});
|
|
67
|
+
return res.status(httpServerParams.statusCode ?? 200).json(output);
|
|
122
68
|
});
|
|
123
69
|
}
|
|
124
70
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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, Metrics, OffConsoleLog } from './decorators';
|
|
3
3
|
import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
|
|
4
|
-
import { Validation, HttpMiddleware, UseCase, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestMetricsHandler, RequestData, RequestMetrics } from './protocols';
|
|
4
|
+
import { Validation, HttpMiddleware, UseCase, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestMetricsHandler, RequestData, RequestMetrics, HttpServerError } from './protocols';
|
|
5
5
|
import { Entity } from './domain/Entity';
|
|
6
6
|
declare class UseCaseHandler {
|
|
7
7
|
static load<T extends UseCase<any, any>>(useCaseClass: new (...args: any[]) => T, request?: RequestData): T;
|
|
8
8
|
}
|
|
9
|
-
export { UseCase, HttpMiddleware, Validation, RequestMetricsHandler, RequestMetrics, 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, Metrics, OffConsoleLog };
|
|
9
|
+
export { UseCase, HttpMiddleware, Validation, RequestMetricsHandler, RequestMetrics, HttpServerError, 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, Metrics, OffConsoleLog };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -8,4 +8,5 @@ import type { RestoreRepository } from './RestoreRepository';
|
|
|
8
8
|
import type { RequestMetricsHandler } from './RequestMetricsHandler';
|
|
9
9
|
import type { RequestData } from './RequestData';
|
|
10
10
|
import type { RequestMetrics } from './RequestMetrics';
|
|
11
|
-
|
|
11
|
+
import type { HttpServerError } from './HttpServerError';
|
|
12
|
+
export type { RequestData, RequestMetrics, HttpMiddleware, UseCase, Validation, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RequestMetricsHandler, HttpServerError };
|