@athenna/http 1.3.3 → 1.3.4
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/LICENSE.md +3 -15
- package/README.md +8 -5
- package/package.json +95 -106
- package/src/Context/Request.js +168 -137
- package/src/Context/Response.js +102 -94
- package/src/Exceptions/UndefinedMethodException.js +30 -0
- package/src/Facades/Route.js +7 -5
- package/src/Facades/Server.js +7 -5
- package/src/Handlers/FastifyHandler.js +134 -115
- package/src/Handlers/HttpExceptionHandler.js +71 -68
- package/src/Kernels/HttpKernel.js +142 -124
- package/src/Providers/ControllerProvider.js +30 -39
- package/src/Providers/HttpRouteProvider.js +15 -16
- package/src/Providers/HttpServerProvider.js +13 -15
- package/src/Providers/MiddlewareProvider.js +30 -39
- package/src/Router/Route.js +192 -99
- package/src/Router/RouteGroup.js +66 -40
- package/src/Router/RouteResource.js +156 -74
- package/src/Router/Router.js +284 -112
- package/src/Utils/isMiddlewareContract.js +8 -6
- package/src/Utils/removeSlashes.js +21 -0
- package/src/index.d.ts +658 -0
- package/src/index.js +284 -0
- package/index.d.ts +0 -29
- package/index.js +0 -41
- package/src/Context/Request.d.ts +0 -103
- package/src/Context/Response.d.ts +0 -77
- package/src/Contracts/Context/ContextContract.d.ts +0 -17
- package/src/Contracts/Context/ContextContract.js +0 -10
- package/src/Contracts/Context/Error/ErrorContextContract.d.ts +0 -18
- package/src/Contracts/Context/Error/ErrorContextContract.js +0 -10
- package/src/Contracts/Context/Error/ErrorHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Error/ErrorHandlerContract.js +0 -10
- package/src/Contracts/Context/HandlerContract.d.ts +0 -12
- package/src/Contracts/Context/HandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.d.ts +0 -19
- package/src/Contracts/Context/Middlewares/Handle/HandleContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Handle/HandleHandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +0 -19
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Intercept/InterceptHandlerContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.d.ts +0 -23
- package/src/Contracts/Context/Middlewares/Terminate/TerminateContextContract.js +0 -10
- package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.d.ts +0 -12
- package/src/Contracts/Context/Middlewares/Terminate/TerminateHandlerContract.js +0 -10
- package/src/Contracts/Context/NextContract.d.ts +0 -11
- package/src/Contracts/Context/NextContract.js +0 -10
- package/src/Contracts/Context/RequestContract.d.ts +0 -25
- package/src/Contracts/Context/RequestContract.js +0 -10
- package/src/Contracts/Context/ResponseContract.d.ts +0 -20
- package/src/Contracts/Context/ResponseContract.js +0 -2
- package/src/Contracts/FastifyErrorHandlerContract.d.ts +0 -12
- package/src/Contracts/FastifyErrorHandlerContract.js +0 -10
- package/src/Contracts/FastifyHandlerContract.d.ts +0 -12
- package/src/Contracts/FastifyHandlerContract.js +0 -10
- package/src/Contracts/HttpMethodTypes.d.ts +0 -9
- package/src/Contracts/HttpMethodTypes.js +0 -10
- package/src/Contracts/MiddlewareContract.d.ts +0 -16
- package/src/Contracts/MiddlewareContract.js +0 -10
- package/src/Contracts/MiddlewareTypes.d.ts +0 -9
- package/src/Contracts/MiddlewareTypes.js +0 -10
- package/src/Contracts/MiddlewareTypesContract.d.ts +0 -16
- package/src/Contracts/MiddlewareTypesContract.js +0 -10
- package/src/Contracts/RouteContract.d.ts +0 -16
- package/src/Contracts/RouteContract.js +0 -10
- package/src/Exceptions/BadRequestException.d.ts +0 -12
- package/src/Exceptions/BadRequestException.js +0 -18
- package/src/Exceptions/CannotDefineGroupException.d.ts +0 -12
- package/src/Exceptions/CannotDefineGroupException.js +0 -19
- package/src/Exceptions/MiddlewareNotFoundException.d.ts +0 -12
- package/src/Exceptions/MiddlewareNotFoundException.js +0 -19
- package/src/Exceptions/UndefinedControllerMethodException.d.ts +0 -12
- package/src/Exceptions/UndefinedControllerMethodException.js +0 -19
- package/src/Facades/Route.d.ts +0 -10
- package/src/Facades/Server.d.ts +0 -10
- package/src/Handlers/FastifyHandler.d.ts +0 -26
- package/src/Handlers/HttpExceptionHandler.d.ts +0 -27
- package/src/Http.d.ts +0 -167
- package/src/Http.js +0 -223
- package/src/Kernels/HttpKernel.d.ts +0 -54
- package/src/Providers/ControllerProvider.d.ts +0 -17
- package/src/Providers/HttpRouteProvider.d.ts +0 -17
- package/src/Providers/HttpServerProvider.d.ts +0 -17
- package/src/Providers/MiddlewareProvider.d.ts +0 -17
- package/src/Router/Route.d.ts +0 -32
- package/src/Router/RouteGroup.d.ts +0 -24
- package/src/Router/RouteResource.d.ts +0 -29
- package/src/Router/Router.d.ts +0 -39
- package/src/Utils/getAppFiles.d.ts +0 -10
- package/src/Utils/getAppFiles.js +0 -22
- package/src/Utils/isMiddlewareContract.d.ts +0 -10
- package/src/Utils/removeSlash.d.ts +0 -9
- package/src/Utils/removeSlash.js +0 -23
package/src/Context/Response.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,99 +6,108 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
|
|
10
|
+
export class Response {
|
|
11
|
+
/**
|
|
12
|
+
* Fastify response object.
|
|
13
|
+
*
|
|
14
|
+
* @type {import('fastify').FastifyReply}
|
|
15
|
+
*/
|
|
16
|
+
#response
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create a new instance of Response.
|
|
20
|
+
*
|
|
21
|
+
* @param {import('fastify').FastifyReply} response
|
|
22
|
+
* @return {Response}
|
|
23
|
+
*/
|
|
24
|
+
constructor(response) {
|
|
25
|
+
this.#response = response
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Terminate the request sending the response body.
|
|
30
|
+
*
|
|
31
|
+
* @param {any} [data]
|
|
32
|
+
* @return {void}
|
|
33
|
+
*/
|
|
34
|
+
send(data) {
|
|
35
|
+
this.#response.send(data)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Set the response status code.
|
|
40
|
+
*
|
|
41
|
+
* @param {number} code
|
|
42
|
+
* @return {Response}
|
|
43
|
+
*/
|
|
44
|
+
status(code) {
|
|
45
|
+
this.#response.status(code)
|
|
46
|
+
|
|
47
|
+
return this
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Remove some header from the response.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} header
|
|
54
|
+
* @return {Response}
|
|
55
|
+
*/
|
|
56
|
+
removeHeader(header) {
|
|
57
|
+
this.#response.removeHeader(header)
|
|
58
|
+
|
|
59
|
+
return this
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Add some header to the response.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} header
|
|
66
|
+
* @param {any} value
|
|
67
|
+
* @return {Response}
|
|
68
|
+
*/
|
|
69
|
+
header(header, value) {
|
|
70
|
+
this.#response.header(header, value)
|
|
71
|
+
|
|
72
|
+
return this
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Only add some header to the response if it's not defined yet.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} header
|
|
79
|
+
* @param {any} value
|
|
80
|
+
* @return {Response}
|
|
81
|
+
*/
|
|
82
|
+
safeHeader(header, value) {
|
|
83
|
+
if (!this.#response.hasHeader(header)) {
|
|
84
|
+
this.#response.header(header, value)
|
|
20
85
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*/
|
|
36
|
-
json(data) {
|
|
37
|
-
this.response.send(this.response.serialize(data));
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Set the response status code.
|
|
41
|
-
*
|
|
42
|
-
* @param code
|
|
43
|
-
* @return Response
|
|
44
|
-
*/
|
|
45
|
-
status(code) {
|
|
46
|
-
this.response.status(code);
|
|
47
|
-
return this;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Remove some header from the response.
|
|
51
|
-
*
|
|
52
|
-
* @param header
|
|
53
|
-
* @return Response
|
|
54
|
-
*/
|
|
55
|
-
removeHeader(header) {
|
|
56
|
-
this.response.removeHeader(header);
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Add some header to the response.
|
|
61
|
-
*
|
|
62
|
-
* @param header
|
|
63
|
-
* @param value
|
|
64
|
-
* @return Response
|
|
65
|
-
*/
|
|
66
|
-
header(header, value) {
|
|
67
|
-
this.response.header(header, value);
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Only add some header to the response if it's not defined yet.
|
|
72
|
-
*
|
|
73
|
-
* @param header
|
|
74
|
-
* @param value
|
|
75
|
-
* @return Response
|
|
76
|
-
*/
|
|
77
|
-
safeHeader(header, value) {
|
|
78
|
-
if (!this.response.hasHeader(header)) {
|
|
79
|
-
this.response.header(header, value);
|
|
80
|
-
}
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Redirect the response to other url with different status code.
|
|
85
|
-
*
|
|
86
|
-
* @param url
|
|
87
|
-
* @param statusCode
|
|
88
|
-
* @return void
|
|
89
|
-
*/
|
|
90
|
-
redirectTo(url, statusCode) {
|
|
91
|
-
if (statusCode) {
|
|
92
|
-
this.response.redirect(statusCode, url);
|
|
93
|
-
}
|
|
94
|
-
this.response.redirect(url);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Get the default fastify response object.
|
|
98
|
-
*
|
|
99
|
-
* @return FastifyReply
|
|
100
|
-
*/
|
|
101
|
-
getFastifyResponse() {
|
|
102
|
-
return this.response;
|
|
86
|
+
|
|
87
|
+
return this
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Redirect the response to other url with different status code.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} url
|
|
94
|
+
* @param {number} [statusCode]
|
|
95
|
+
* @return {void}
|
|
96
|
+
*/
|
|
97
|
+
redirectTo(url, statusCode) {
|
|
98
|
+
if (statusCode) {
|
|
99
|
+
this.#response.redirect(statusCode, url)
|
|
103
100
|
}
|
|
101
|
+
|
|
102
|
+
this.#response.redirect(url)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Get the default fastify response object.
|
|
107
|
+
*
|
|
108
|
+
* @return {import('fastify').FastifyReply}
|
|
109
|
+
*/
|
|
110
|
+
getFastifyResponse() {
|
|
111
|
+
return this.#response
|
|
112
|
+
}
|
|
104
113
|
}
|
|
105
|
-
exports.Response = Response;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/logger
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Exception } from '@secjs/utils'
|
|
11
|
+
|
|
12
|
+
export class UndefinedMethodException extends Exception {
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new instance of UndefinedMethodException.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} method
|
|
17
|
+
* @param {string} className
|
|
18
|
+
* @return {UndefinedMethodException}
|
|
19
|
+
*/
|
|
20
|
+
constructor(method, className) {
|
|
21
|
+
const content = `The method ${method} is not defined inside your class ${className}.`
|
|
22
|
+
|
|
23
|
+
super(
|
|
24
|
+
content,
|
|
25
|
+
500,
|
|
26
|
+
'E_UNDEFINED_METHOD',
|
|
27
|
+
`Remember defining the method ${method} inside your class ${className}`,
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/Facades/Route.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,7 +6,10 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
|
|
10
|
+
import { Facade } from '@athenna/ioc'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @type {Facade & import('../Router/Router.js').Router}
|
|
14
|
+
*/
|
|
15
|
+
export const Route = Facade.createFor('Athenna/Core/HttpRoute')
|
package/src/Facades/Server.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,7 +6,10 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
|
|
10
|
+
import { Facade } from '@athenna/ioc'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @type {Facade & import('../index.js').Http}
|
|
14
|
+
*/
|
|
15
|
+
export const Server = Facade.createFor('Athenna/Core/HttpServer')
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @athenna/http
|
|
4
3
|
*
|
|
@@ -7,123 +6,143 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
9
|
+
|
|
10
|
+
import { Is } from '@secjs/utils'
|
|
11
|
+
import { Request } from '#src/Context/Request'
|
|
12
|
+
import { Response } from '#src/Context/Response'
|
|
13
|
+
|
|
14
|
+
export class FastifyHandler {
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new onSend fastify hook.
|
|
17
|
+
*
|
|
18
|
+
* @param {any} handler
|
|
19
|
+
* @return {any}
|
|
20
|
+
*/
|
|
21
|
+
static createOnSendHandler(handler) {
|
|
22
|
+
return async (req, res, payload) => {
|
|
23
|
+
const request = new Request(req)
|
|
24
|
+
const response = new Response(res)
|
|
25
|
+
|
|
26
|
+
if (!req.data) req.data = {}
|
|
27
|
+
|
|
28
|
+
let body = payload
|
|
29
|
+
|
|
30
|
+
if (Is.Json(payload)) {
|
|
31
|
+
body = JSON.parse(body)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
body = await handler({
|
|
35
|
+
request,
|
|
36
|
+
response,
|
|
37
|
+
body,
|
|
38
|
+
status: res.statusCode,
|
|
39
|
+
params: req.params,
|
|
40
|
+
queries: req.query,
|
|
41
|
+
data: req.data,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
if (Is.Object(body)) body = JSON.stringify(body)
|
|
45
|
+
|
|
46
|
+
return body
|
|
44
47
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new done fastify hook.
|
|
52
|
+
*
|
|
53
|
+
* @param {any} handler
|
|
54
|
+
* @return {any}
|
|
55
|
+
*/
|
|
56
|
+
static createDoneHandler(handler) {
|
|
57
|
+
return (req, res, done) => {
|
|
58
|
+
const request = new Request(req)
|
|
59
|
+
const response = new Response(res)
|
|
60
|
+
|
|
61
|
+
if (!req.data) req.data = {}
|
|
62
|
+
|
|
63
|
+
return handler({
|
|
64
|
+
request,
|
|
65
|
+
response,
|
|
66
|
+
params: req.params,
|
|
67
|
+
queries: req.query,
|
|
68
|
+
data: req.data,
|
|
69
|
+
next: done,
|
|
70
|
+
})
|
|
64
71
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new onResponse fastify hook.
|
|
76
|
+
*
|
|
77
|
+
* @param {any} handler
|
|
78
|
+
* @return {any}
|
|
79
|
+
*/
|
|
80
|
+
static createOnResponseHandler(handler) {
|
|
81
|
+
return (req, res, done) => {
|
|
82
|
+
const request = new Request(req)
|
|
83
|
+
const response = new Response(res)
|
|
84
|
+
|
|
85
|
+
if (!req.data) req.data = {}
|
|
86
|
+
|
|
87
|
+
return handler({
|
|
88
|
+
request,
|
|
89
|
+
response,
|
|
90
|
+
params: req.params,
|
|
91
|
+
queries: req.query,
|
|
92
|
+
data: req.data,
|
|
93
|
+
body: req.body,
|
|
94
|
+
headers: res.getHeaders(),
|
|
95
|
+
status: res.statusCode,
|
|
96
|
+
responseTime: res.getResponseTime(),
|
|
97
|
+
next: done,
|
|
98
|
+
})
|
|
88
99
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a new error fastify hook.
|
|
104
|
+
*
|
|
105
|
+
* @param {any} handler
|
|
106
|
+
* @return {any}
|
|
107
|
+
*/
|
|
108
|
+
static createErrorHandler(handler) {
|
|
109
|
+
return (error, req, res) => {
|
|
110
|
+
const request = new Request(req)
|
|
111
|
+
const response = new Response(res)
|
|
112
|
+
|
|
113
|
+
if (!req.data) req.data = {}
|
|
114
|
+
|
|
115
|
+
return handler({
|
|
116
|
+
request,
|
|
117
|
+
response,
|
|
118
|
+
params: req.params,
|
|
119
|
+
queries: req.query,
|
|
120
|
+
data: req.data,
|
|
121
|
+
error,
|
|
122
|
+
})
|
|
108
123
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Creates a new request fastify hook.
|
|
128
|
+
*
|
|
129
|
+
* @param {any} handler
|
|
130
|
+
* @return {any}
|
|
131
|
+
*/
|
|
132
|
+
static createRequestHandler(handler) {
|
|
133
|
+
return async (req, res) => {
|
|
134
|
+
const request = new Request(req)
|
|
135
|
+
const response = new Response(res)
|
|
136
|
+
|
|
137
|
+
if (!req.data) req.data = {}
|
|
138
|
+
|
|
139
|
+
return handler({
|
|
140
|
+
request,
|
|
141
|
+
response,
|
|
142
|
+
params: req.params,
|
|
143
|
+
queries: req.query,
|
|
144
|
+
data: req.data,
|
|
145
|
+
})
|
|
127
146
|
}
|
|
147
|
+
}
|
|
128
148
|
}
|
|
129
|
-
exports.FastifyHandler = FastifyHandler;
|
|
@@ -1,71 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
import { Config, Exception, String } from '@secjs/utils'
|
|
2
|
+
|
|
3
|
+
export class HttpExceptionHandler {
|
|
4
|
+
/**
|
|
5
|
+
* Error codes that should be ignored by Log.
|
|
6
|
+
*
|
|
7
|
+
* @type {string[]}
|
|
8
|
+
*/
|
|
9
|
+
ignoreCodes = []
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Error statuses that should be ignored by Log.
|
|
13
|
+
*
|
|
14
|
+
* @type {number[]}
|
|
15
|
+
*/
|
|
16
|
+
ignoreStatuses = []
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The global exception handler of all HTTP requests.
|
|
20
|
+
*
|
|
21
|
+
* @param {import('#src/index').ErrorContextContract} ctx
|
|
22
|
+
*/
|
|
23
|
+
async handle({ error, response }) {
|
|
24
|
+
const code = error.code || error.name
|
|
25
|
+
const statusCode = error.statusCode || error.status || 500
|
|
26
|
+
|
|
27
|
+
const body = {
|
|
28
|
+
statusCode,
|
|
29
|
+
code: String.toSnakeCase(`${code}`).toUpperCase(),
|
|
30
|
+
name: error.name,
|
|
31
|
+
message: error.message,
|
|
32
|
+
stack: error.stack,
|
|
27
33
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
* @param ctx
|
|
32
|
-
*/
|
|
33
|
-
async handle({ error, response }) {
|
|
34
|
-
const code = error.code || error.name;
|
|
35
|
-
const statusCode = error.statusCode || error.status || 500;
|
|
36
|
-
const body = {
|
|
37
|
-
statusCode,
|
|
38
|
-
code: utils_1.String.toSnakeCase(`${code}`).toUpperCase(),
|
|
39
|
-
name: error.name,
|
|
40
|
-
message: error.message,
|
|
41
|
-
stack: error.stack,
|
|
42
|
-
};
|
|
43
|
-
if (error.help) {
|
|
44
|
-
body.help = error.help;
|
|
45
|
-
}
|
|
46
|
-
const isInternalServerError = statusCode === 500;
|
|
47
|
-
const isDebugMode = config_1.Config.get('app.debug');
|
|
48
|
-
if (isInternalServerError && !isDebugMode) {
|
|
49
|
-
body.name = 'Internal server error';
|
|
50
|
-
body.message = 'An internal server exception has occurred.';
|
|
51
|
-
delete body.stack;
|
|
52
|
-
}
|
|
53
|
-
if (this.ignoreCodes.includes(code) ||
|
|
54
|
-
this.ignoreStatuses.includes(statusCode)) {
|
|
55
|
-
return response.status(statusCode).send(body);
|
|
56
|
-
}
|
|
57
|
-
const logConfig = {
|
|
58
|
-
formatterConfig: {
|
|
59
|
-
context: 'ExceptionHandler',
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
if (this.addStack) {
|
|
63
|
-
logger_1.Log.error(`(${body.statusCode}) ${body.code}: ${body.message}\nStack: ${body.stack}`, logConfig);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
logger_1.Log.error(`(${body.statusCode}) ${body.code}: ${body.message}`, logConfig);
|
|
67
|
-
}
|
|
68
|
-
return response.status(statusCode).send(body);
|
|
34
|
+
|
|
35
|
+
if (error.help) {
|
|
36
|
+
body.help = error.help
|
|
69
37
|
}
|
|
38
|
+
|
|
39
|
+
const isInternalServerError = statusCode === 500
|
|
40
|
+
const isDebugMode = Config.get('app.debug')
|
|
41
|
+
|
|
42
|
+
if (isInternalServerError && !isDebugMode) {
|
|
43
|
+
body.name = 'Internal server error'
|
|
44
|
+
body.message = 'An internal server exception has occurred.'
|
|
45
|
+
|
|
46
|
+
delete body.stack
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
this.ignoreCodes.includes(code) ||
|
|
51
|
+
this.ignoreStatuses.includes(statusCode)
|
|
52
|
+
) {
|
|
53
|
+
return response.status(statusCode).send(body)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
response.status(statusCode).send(body)
|
|
57
|
+
|
|
58
|
+
if (error.prettify) {
|
|
59
|
+
const prettyError = await error.prettify()
|
|
60
|
+
|
|
61
|
+
process.stderr.write(prettyError.concat('\n'))
|
|
62
|
+
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const prettyError = await new Exception(
|
|
67
|
+
body.message,
|
|
68
|
+
body.statusCode,
|
|
69
|
+
body.code,
|
|
70
|
+
).prettify()
|
|
71
|
+
|
|
72
|
+
process.stderr.write(prettyError.concat('\n'))
|
|
73
|
+
}
|
|
70
74
|
}
|
|
71
|
-
exports.HttpExceptionHandler = HttpExceptionHandler;
|