@athenna/http 1.0.3 → 1.0.6
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/package.json +2 -2
- package/src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.d.ts +1 -2
- package/src/Http.js +0 -2
- package/src/Utils/FastifyHandler.d.ts +1 -1
- package/src/Utils/FastifyHandler.js +2 -3
- package/src/Utils/defaultErrorHandler.d.ts +0 -10
- package/src/Utils/defaultErrorHandler.js +0 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@athenna/http",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "The Athenna Http server. Built on top of fastify",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "João Lenon <lenon@athenna.io>",
|
|
@@ -158,7 +158,7 @@
|
|
|
158
158
|
}
|
|
159
159
|
},
|
|
160
160
|
"dependencies": {
|
|
161
|
-
"@athenna/config": "1.0.
|
|
161
|
+
"@athenna/config": "1.0.5",
|
|
162
162
|
"@athenna/ioc": "1.0.6",
|
|
163
163
|
"@secjs/utils": "1.8.0",
|
|
164
164
|
"fastify": "3.27.4",
|
|
@@ -7,14 +7,13 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import { RequestContract } from '../../RequestContract';
|
|
10
|
-
import { ResponseContract } from '../../ResponseContract';
|
|
11
10
|
import { NextInterceptContract } from './NextInterceptContract';
|
|
12
11
|
export interface InterceptContextContract {
|
|
13
12
|
request: RequestContract;
|
|
14
|
-
response: ResponseContract;
|
|
15
13
|
params: Record<string, string>;
|
|
16
14
|
queries: Record<string, string>;
|
|
17
15
|
body: Record<string, any>;
|
|
16
|
+
status: number;
|
|
18
17
|
data: Record<string, any>;
|
|
19
18
|
next: NextInterceptContract;
|
|
20
19
|
}
|
package/src/Http.js
CHANGED
|
@@ -14,11 +14,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
14
14
|
exports.Http = void 0;
|
|
15
15
|
const fastify_1 = __importDefault(require("fastify"));
|
|
16
16
|
const FastifyHandler_1 = require("./Utils/FastifyHandler");
|
|
17
|
-
const defaultErrorHandler_1 = require("./Utils/defaultErrorHandler");
|
|
18
17
|
class Http {
|
|
19
18
|
constructor() {
|
|
20
19
|
this.server = fastify_1.default();
|
|
21
|
-
this.setErrorHandler(defaultErrorHandler_1.defaultErrorHandler);
|
|
22
20
|
}
|
|
23
21
|
setErrorHandler(handler) {
|
|
24
22
|
const fastifyErrorHandler = FastifyHandler_1.FastifyHandler.createErrorHandler(handler);
|
|
@@ -17,7 +17,7 @@ declare module 'fastify' {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
export declare class FastifyHandler {
|
|
20
|
-
static createOnSendHandler(handler: InterceptHandlerContract): (req: any,
|
|
20
|
+
static createOnSendHandler(handler: InterceptHandlerContract): (req: any, _res: any, payload: any, done: any) => any;
|
|
21
21
|
static createDoneHandler(handler: HandleHandlerContract): (req: any, res: any, done: any) => any;
|
|
22
22
|
static createErrorHandler(handler: ErrorHandlerContract): (error: any, req: FastifyRequest, res: FastifyReply) => any;
|
|
23
23
|
static createRequestHandler(handler: HandlerContract): (req: FastifyRequest, res: FastifyReply) => Promise<any>;
|
|
@@ -15,9 +15,8 @@ const Request_1 = require("../Context/Request");
|
|
|
15
15
|
const Response_1 = require("../Context/Response");
|
|
16
16
|
class FastifyHandler {
|
|
17
17
|
static createOnSendHandler(handler) {
|
|
18
|
-
return (req,
|
|
18
|
+
return (req, _res, payload, done) => {
|
|
19
19
|
const request = new Request_1.Request(req);
|
|
20
|
-
const response = new Response_1.Response(res);
|
|
21
20
|
if (!req.data)
|
|
22
21
|
req.data = {};
|
|
23
22
|
if (!req.query)
|
|
@@ -30,8 +29,8 @@ class FastifyHandler {
|
|
|
30
29
|
}
|
|
31
30
|
return handler({
|
|
32
31
|
request,
|
|
33
|
-
response,
|
|
34
32
|
body,
|
|
33
|
+
status: _res.statusCode,
|
|
35
34
|
params: req.params,
|
|
36
35
|
queries: req.query,
|
|
37
36
|
data: req.data,
|
|
@@ -1,10 +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
|
-
import { ErrorContextContract } from '../Contracts/Context/Error/ErrorContextContract';
|
|
10
|
-
export declare function defaultErrorHandler({ error, request, response, }: ErrorContextContract): void;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @athenna/http
|
|
4
|
-
*
|
|
5
|
-
* (c) João Lenon <lenon@athenna.io>
|
|
6
|
-
*
|
|
7
|
-
* For the full copyright and license information, please view the LICENSE
|
|
8
|
-
* file that was distributed with this source code.
|
|
9
|
-
*/
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.defaultErrorHandler = void 0;
|
|
12
|
-
const utils_1 = require("@secjs/utils");
|
|
13
|
-
function defaultErrorHandler({ error, request, response, }) {
|
|
14
|
-
const code = error.code || error.name;
|
|
15
|
-
const statusCode = error.statusCode || error.status || 500;
|
|
16
|
-
const body = {
|
|
17
|
-
code: utils_1.String.toSnakeCase(code).toUpperCase(),
|
|
18
|
-
path: request.baseUrl,
|
|
19
|
-
method: request.method,
|
|
20
|
-
status: statusCode <= 399 ? 'SUCCESS' : 'ERROR',
|
|
21
|
-
statusCode: statusCode,
|
|
22
|
-
error: {
|
|
23
|
-
name: error.name,
|
|
24
|
-
message: error.message,
|
|
25
|
-
stack: error.stack,
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
response.status(statusCode).send(body);
|
|
29
|
-
}
|
|
30
|
-
exports.defaultErrorHandler = defaultErrorHandler;
|