@athenna/http 3.0.10 → 3.1.0
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/build/Commands/MakeControllerCommand.d.ts +15 -0
- package/build/Commands/MakeControllerCommand.js +42 -0
- package/build/Commands/MakeInterceptorCommand.d.ts +15 -0
- package/build/Commands/MakeInterceptorCommand.js +42 -0
- package/build/Commands/MakeMiddlewareCommand.d.ts +15 -0
- package/build/Commands/MakeMiddlewareCommand.js +42 -0
- package/build/Commands/MakeTerminatorCommand.d.ts +15 -0
- package/build/Commands/MakeTerminatorCommand.js +42 -0
- package/build/Commands/RouteListCommand.d.ts +19 -0
- package/build/Commands/RouteListCommand.js +62 -0
- package/build/Context/Request.d.ts +144 -0
- package/build/Context/Request.js +219 -0
- package/build/Context/Response.d.ts +67 -0
- package/build/Context/Response.js +102 -0
- package/build/Contracts/InterceptorContract.d.ts +15 -0
- package/build/Contracts/InterceptorContract.js +9 -0
- package/build/Contracts/MiddlewareContract.d.ts +15 -0
- package/build/Contracts/MiddlewareContract.js +9 -0
- package/build/Contracts/TerminatorContract.d.ts +15 -0
- package/build/Contracts/TerminatorContract.js +9 -0
- package/build/Decorators/Controller.d.ts +14 -0
- package/build/Decorators/Controller.js +28 -0
- package/build/Decorators/Interceptor.d.ts +14 -0
- package/build/Decorators/Interceptor.js +37 -0
- package/build/Decorators/Middleware.d.ts +14 -0
- package/build/Decorators/Middleware.js +37 -0
- package/build/Decorators/Terminator.d.ts +14 -0
- package/build/Decorators/Terminator.js +37 -0
- package/build/Exceptions/BadGatewayException.d.ts +20 -0
- package/build/Exceptions/BadGatewayException.js +22 -0
- package/build/Exceptions/BadRequestException.d.ts +20 -0
- package/build/Exceptions/BadRequestException.js +22 -0
- package/build/Exceptions/ForbiddenException.d.ts +20 -0
- package/build/Exceptions/ForbiddenException.js +22 -0
- package/build/Exceptions/HttpException.d.ts +20 -0
- package/build/Exceptions/HttpException.js +27 -0
- package/build/Exceptions/InternalServerException.d.ts +20 -0
- package/build/Exceptions/InternalServerException.js +22 -0
- package/build/Exceptions/MethodNotAllowedException.d.ts +20 -0
- package/build/Exceptions/MethodNotAllowedException.js +22 -0
- package/build/Exceptions/NotAcceptableException.d.ts +20 -0
- package/build/Exceptions/NotAcceptableException.js +22 -0
- package/build/Exceptions/NotFoundException.d.ts +20 -0
- package/build/Exceptions/NotFoundException.js +22 -0
- package/build/Exceptions/NotImplementedException.d.ts +20 -0
- package/build/Exceptions/NotImplementedException.js +22 -0
- package/build/Exceptions/PayloadTooLargeException.d.ts +20 -0
- package/build/Exceptions/PayloadTooLargeException.js +22 -0
- package/build/Exceptions/RequestTimeoutException.d.ts +20 -0
- package/build/Exceptions/RequestTimeoutException.js +22 -0
- package/build/Exceptions/ServiceUnavailableException.d.ts +20 -0
- package/build/Exceptions/ServiceUnavailableException.js +22 -0
- package/build/Exceptions/UnauthorizedException.d.ts +20 -0
- package/build/Exceptions/UnauthorizedException.js +22 -0
- package/build/Exceptions/UndefinedMethodException.d.ts +12 -0
- package/build/Exceptions/UndefinedMethodException.js +19 -0
- package/build/Exceptions/UnprocessableEntityException.d.ts +20 -0
- package/build/Exceptions/UnprocessableEntityException.js +22 -0
- package/build/Facades/Route.d.ts +10 -0
- package/{src → build}/Facades/Route.js +2 -7
- package/build/Facades/Server.d.ts +10 -0
- package/{src → build}/Facades/Server.js +2 -7
- package/build/Handlers/FastifyHandler.d.ts +41 -0
- package/build/Handlers/FastifyHandler.js +116 -0
- package/build/Handlers/HttpExceptionHandler.d.ts +31 -0
- package/build/Handlers/HttpExceptionHandler.js +82 -0
- package/build/Kernels/HttpKernel.d.ts +73 -0
- package/build/Kernels/HttpKernel.js +221 -0
- package/build/Providers/HttpRouteProvider.d.ts +12 -0
- package/build/Providers/HttpRouteProvider.js +15 -0
- package/build/Providers/HttpServerProvider.d.ts +13 -0
- package/build/Providers/HttpServerProvider.js +25 -0
- package/build/Router/Route.d.ts +263 -0
- package/build/Router/Route.js +447 -0
- package/build/Router/RouteGroup.d.ts +96 -0
- package/build/Router/RouteGroup.js +123 -0
- package/build/Router/RouteResource.d.ts +108 -0
- package/build/Router/RouteResource.js +158 -0
- package/build/Router/Router.d.ts +103 -0
- package/build/Router/Router.js +188 -0
- package/build/Server/ServerImpl.d.ts +129 -0
- package/build/Server/ServerImpl.js +213 -0
- package/build/Types/Contexts/Context.d.ts +20 -0
- package/build/Types/Contexts/Context.js +9 -0
- package/build/Types/Contexts/ErrorContext.d.ts +21 -0
- package/build/Types/Contexts/ErrorContext.js +9 -0
- package/build/Types/Contexts/InterceptContext.d.ts +20 -0
- package/build/Types/Contexts/InterceptContext.js +9 -0
- package/build/Types/Contexts/TerminateContext.d.ts +21 -0
- package/build/Types/Contexts/TerminateContext.js +9 -0
- package/build/Types/Controllers/ControllerOptions.d.ts +25 -0
- package/build/Types/Controllers/ControllerOptions.js +9 -0
- package/build/Types/Middlewares/InterceptorRouteType.d.ts +11 -0
- package/build/Types/Middlewares/InterceptorRouteType.js +9 -0
- package/build/Types/Middlewares/MiddlewareHandler.d.ts +20 -0
- package/build/Types/Middlewares/MiddlewareHandler.js +9 -0
- package/build/Types/Middlewares/MiddlewareOptions.d.ts +40 -0
- package/build/Types/Middlewares/MiddlewareOptions.js +9 -0
- package/build/Types/Middlewares/MiddlewareRouteType.d.ts +11 -0
- package/build/Types/Middlewares/MiddlewareRouteType.js +9 -0
- package/build/Types/Middlewares/TerminatorRouteType.d.ts +11 -0
- package/build/Types/Middlewares/TerminatorRouteType.js +9 -0
- package/build/Types/Router/RouteHandler.d.ts +10 -0
- package/build/Types/Router/RouteHandler.js +9 -0
- package/build/Types/Router/RouteJSON.d.ts +21 -0
- package/build/Types/Router/RouteJSON.js +9 -0
- package/build/Types/Router/RouteResourceTypes.d.ts +9 -0
- package/build/Types/Router/RouteResourceTypes.js +9 -0
- package/build/index.d.ts +70 -0
- package/build/index.js +48 -0
- package/package.json +159 -76
- package/templates/controller.edge +8 -36
- package/templates/interceptor.edge +8 -0
- package/templates/middleware.edge +4 -36
- package/templates/terminator.edge +6 -0
- package/src/Commands/Make/Controller.js +0 -64
- package/src/Commands/Make/Middleware.js +0 -80
- package/src/Commands/Route/List.js +0 -107
- package/src/Context/Request.js +0 -223
- package/src/Context/Response.js +0 -137
- package/src/Exceptions/BadGatewayException.js +0 -31
- package/src/Exceptions/BadRequestException.js +0 -31
- package/src/Exceptions/ForbiddenException.js +0 -31
- package/src/Exceptions/HttpException.js +0 -33
- package/src/Exceptions/InternalServerException.js +0 -31
- package/src/Exceptions/MethodNotAllowedException.js +0 -31
- package/src/Exceptions/NotAcceptableException.js +0 -31
- package/src/Exceptions/NotFoundException.js +0 -31
- package/src/Exceptions/NotImplementedException.js +0 -31
- package/src/Exceptions/PayloadTooLargeException.js +0 -31
- package/src/Exceptions/RequestTimeoutException.js +0 -31
- package/src/Exceptions/ServiceUnavailableException.js +0 -31
- package/src/Exceptions/UnauthorizedException.js +0 -31
- package/src/Exceptions/UndefinedMethodException.js +0 -30
- package/src/Exceptions/UnprocessableEntityException.js +0 -31
- package/src/Handlers/FastifyHandler.js +0 -148
- package/src/Handlers/HttpExceptionHandler.js +0 -88
- package/src/Helpers/HttpLoader.js +0 -38
- package/src/Kernels/HttpKernel.js +0 -199
- package/src/Providers/ControllerProvider.js +0 -29
- package/src/Providers/HttpRouteProvider.js +0 -24
- package/src/Providers/HttpServerProvider.js +0 -37
- package/src/Providers/MiddlewareProvider.js +0 -29
- package/src/Router/Route.js +0 -417
- package/src/Router/RouteGroup.js +0 -119
- package/src/Router/RouteResource.js +0 -247
- package/src/Router/Router.js +0 -313
- package/src/Utils/isMiddlewareContract.js +0 -17
- package/src/Utils/removeSlashes.js +0 -30
- package/src/index.d.ts +0 -1331
- package/src/index.js +0 -377
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class NotFoundException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of NotFoundException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new NotFoundException()
|
|
18
|
-
* This exception uses the 404 status code and the "E_NOT_FOUND_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Not found error',
|
|
26
|
-
code = 'E_NOT_FOUND_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 404, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class NotImplementedException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of NotImplementedException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new NotImplementedException()
|
|
18
|
-
* This exception uses the 501 status code and the "E_NOT_IMPLEMENTED_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Not implemented error',
|
|
26
|
-
code = 'E_NOT_IMPLEMENTED_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 501, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class PayloadTooLargeException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of PayloadTooLargeException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new PayloadTooLargeException()
|
|
18
|
-
* This exception uses the 413 status code and the "E_PAYLOAD_TOO_LARGE_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Payload too large error',
|
|
26
|
-
code = 'E_PAYLOAD_TOO_LARGE_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 413, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class RequestTimeoutException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of RequestTimeoutException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new RequestTimeoutException()
|
|
18
|
-
* This exception uses the 408 status code and the "E_REQUEST_TIMEOUT_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Request timeout error',
|
|
26
|
-
code = 'E_REQUEST_TIMEOUT_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 408, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class ServiceUnavailableException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of ServiceUnavailableException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new ServiceUnavailableException()
|
|
18
|
-
* This exception uses the 503 status code and the "E_SERVICE_UNAVAILABLE_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Service unavailable error',
|
|
26
|
-
code = 'E_SERVICE_UNAVAILABLE_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 503, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class UnauthorizedException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of UnauthorizedException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new UnauthorizedException()
|
|
18
|
-
* This exception uses the 401 status code and the "E_UNAUTHORIZED_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Unauthorized error',
|
|
26
|
-
code = 'E_UNAUTHORIZED_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 401, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 '@athenna/common'
|
|
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
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { HttpException } from '#src/Exceptions/HttpException'
|
|
11
|
-
|
|
12
|
-
export class UnprocessableEntityException extends HttpException {
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new instance of UnprocessableEntityException.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* throw new UnprocessableEntityException()
|
|
18
|
-
* This exception uses the 422 status code and the "E_UNPROCESSABLE_ENTITY_ERROR" code.
|
|
19
|
-
*
|
|
20
|
-
* @param {string} [content]
|
|
21
|
-
* @param {string} [code]
|
|
22
|
-
* @param {string|null} [help]
|
|
23
|
-
*/
|
|
24
|
-
constructor(
|
|
25
|
-
content = 'Unprocessable entity error',
|
|
26
|
-
code = 'E_UNPROCESSABLE_ENTITY_ERROR',
|
|
27
|
-
help = null,
|
|
28
|
-
) {
|
|
29
|
-
super(content, 422, code, help)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { Is } from '@athenna/common'
|
|
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
|
-
const isJson = Is.Json(payload)
|
|
29
|
-
|
|
30
|
-
if (isJson) {
|
|
31
|
-
payload = JSON.parse(payload)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
payload = await handler({
|
|
35
|
-
request,
|
|
36
|
-
response,
|
|
37
|
-
body: payload,
|
|
38
|
-
status: res.statusCode,
|
|
39
|
-
params: req.params,
|
|
40
|
-
queries: req.query,
|
|
41
|
-
data: req.data,
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
if (isJson) {
|
|
45
|
-
payload = JSON.stringify(payload)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return payload
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Creates a new done fastify hook.
|
|
54
|
-
*
|
|
55
|
-
* @param {any} handler
|
|
56
|
-
* @return {any}
|
|
57
|
-
*/
|
|
58
|
-
static createDoneHandler(handler) {
|
|
59
|
-
return async (req, res) => {
|
|
60
|
-
const request = new Request(req)
|
|
61
|
-
const response = new Response(res)
|
|
62
|
-
|
|
63
|
-
if (!req.data) req.data = {}
|
|
64
|
-
|
|
65
|
-
return handler({
|
|
66
|
-
request,
|
|
67
|
-
response,
|
|
68
|
-
params: req.params,
|
|
69
|
-
queries: req.query,
|
|
70
|
-
data: req.data,
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Creates a new onResponse fastify hook.
|
|
77
|
-
*
|
|
78
|
-
* @param {any} handler
|
|
79
|
-
* @return {any}
|
|
80
|
-
*/
|
|
81
|
-
static createOnResponseHandler(handler) {
|
|
82
|
-
return async (req, res) => {
|
|
83
|
-
const request = new Request(req)
|
|
84
|
-
const response = new Response(res)
|
|
85
|
-
|
|
86
|
-
if (!req.data) req.data = {}
|
|
87
|
-
|
|
88
|
-
return handler({
|
|
89
|
-
request,
|
|
90
|
-
response,
|
|
91
|
-
params: req.params,
|
|
92
|
-
queries: req.query,
|
|
93
|
-
data: req.data,
|
|
94
|
-
body: req.body,
|
|
95
|
-
headers: res.getHeaders(),
|
|
96
|
-
status: res.statusCode,
|
|
97
|
-
responseTime: res.getResponseTime(),
|
|
98
|
-
})
|
|
99
|
-
}
|
|
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 async (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
|
-
})
|
|
123
|
-
}
|
|
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
|
-
})
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { Config } from '@athenna/config'
|
|
11
|
-
import { String } from '@athenna/common'
|
|
12
|
-
import { Log, Logger } from '@athenna/logger'
|
|
13
|
-
|
|
14
|
-
export class HttpExceptionHandler {
|
|
15
|
-
/**
|
|
16
|
-
* Error codes that should be ignored by Log.
|
|
17
|
-
*
|
|
18
|
-
* @type {string[]}
|
|
19
|
-
*/
|
|
20
|
-
get ignoreCodes() {
|
|
21
|
-
return []
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Error statuses that should be ignored by Log.
|
|
26
|
-
*
|
|
27
|
-
* @type {number[]}
|
|
28
|
-
*/
|
|
29
|
-
get ignoreStatuses() {
|
|
30
|
-
return []
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The global exception handler of all HTTP requests.
|
|
35
|
-
*
|
|
36
|
-
* @param {import('#src/index').ErrorContextContract} ctx
|
|
37
|
-
*/
|
|
38
|
-
async handle({ error, response }) {
|
|
39
|
-
const code = error.code || error.name
|
|
40
|
-
const statusCode = error.statusCode || error.status || 500
|
|
41
|
-
|
|
42
|
-
const body = {
|
|
43
|
-
statusCode,
|
|
44
|
-
code: String.toSnakeCase(`${code}`).toUpperCase(),
|
|
45
|
-
name: error.name,
|
|
46
|
-
message: error.message,
|
|
47
|
-
stack: error.stack,
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (error.help) {
|
|
51
|
-
body.help = error.help
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const isInternalServerError = statusCode === 500
|
|
55
|
-
const isDebugMode = Config.get('app.debug', false)
|
|
56
|
-
|
|
57
|
-
if (isInternalServerError && !isDebugMode) {
|
|
58
|
-
body.name = 'Internal server error'
|
|
59
|
-
body.message = 'An internal server exception has occurred.'
|
|
60
|
-
|
|
61
|
-
delete body.stack
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
response.status(statusCode).send(body)
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
this.ignoreCodes.includes(code) ||
|
|
68
|
-
this.ignoreStatuses.includes(statusCode)
|
|
69
|
-
) {
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const logger = Config.exists('logging.channels.exception')
|
|
74
|
-
? Log.channel('exception')
|
|
75
|
-
: Logger.getVanillaLogger({
|
|
76
|
-
level: 'trace',
|
|
77
|
-
driver: 'console',
|
|
78
|
-
streamType: 'stderr',
|
|
79
|
-
formatter: 'none',
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
if (!error.prettify) {
|
|
83
|
-
error = error.toAthennaException()
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
logger.error((await error.prettify()).concat('\n'))
|
|
87
|
-
}
|
|
88
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @athenna/http
|
|
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 { join } from 'node:path'
|
|
11
|
-
import { Folder, Module } from '@athenna/common'
|
|
12
|
-
|
|
13
|
-
export class HttpLoader {
|
|
14
|
-
/**
|
|
15
|
-
* Return all commands from http package.
|
|
16
|
-
*
|
|
17
|
-
* @return {any[]}
|
|
18
|
-
*/
|
|
19
|
-
static loadCommands() {
|
|
20
|
-
return [
|
|
21
|
-
import('#src/Commands/Route/List'),
|
|
22
|
-
import('#src/Commands/Make/Controller'),
|
|
23
|
-
import('#src/Commands/Make/Middleware'),
|
|
24
|
-
]
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Return all custom templates from http package.
|
|
29
|
-
*
|
|
30
|
-
* @return {any[]}
|
|
31
|
-
*/
|
|
32
|
-
static loadTemplates() {
|
|
33
|
-
const dirname = Module.createDirname(import.meta.url)
|
|
34
|
-
const templatesPath = join(dirname, '..', '..', 'templates')
|
|
35
|
-
|
|
36
|
-
return new Folder(templatesPath).loadSync().getFilesByPattern('**/*.edge')
|
|
37
|
-
}
|
|
38
|
-
}
|