@athenna/http 4.24.0 → 4.26.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/package.json +4 -1
- package/src/context/Request.d.ts +5 -1
- package/src/context/Request.js +6 -0
- package/src/context/Response.d.ts +6 -3
- package/src/context/Response.js +7 -1
- package/src/handlers/FastifyHandler.js +25 -29
- package/src/router/Router.d.ts +10 -0
- package/src/router/Router.js +18 -0
- package/src/server/ServerImpl.js +1 -1
- package/src/types/contexts/Context.d.ts +15 -2
- package/src/types/contexts/Context.js +1 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@athenna/http",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.26.0",
|
|
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>",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "node node_modules/@athenna/tsconfig/src/build.js",
|
|
31
31
|
"lint:fix": "eslint \"{bin,src,tests}/**/*.ts\" --fix",
|
|
32
|
+
"benchmark": "node --no-warnings --enable-source-maps --import=@athenna/tsconfig tests/benchmarks/ServerTest.ts",
|
|
32
33
|
"test": "npm run --silent lint:fix && node --enable-source-maps --import=@athenna/tsconfig bin/test.ts",
|
|
33
34
|
"test:debug": "cross-env NODE_DEBUG=athenna:* node --inspect --enable-source-maps --import=@athenna/tsconfig bin/test.ts",
|
|
34
35
|
"test:coverage": "c8 npm run --silent test"
|
|
@@ -88,6 +89,7 @@
|
|
|
88
89
|
"@fastify/swagger-ui": "^3.0.0",
|
|
89
90
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
90
91
|
"@typescript-eslint/parser": "^6.7.4",
|
|
92
|
+
"autocannon": "^7.15.0",
|
|
91
93
|
"commitizen": "^4.2.6",
|
|
92
94
|
"cz-conventional-changelog": "^3.3.0",
|
|
93
95
|
"eslint": "^8.36.0",
|
|
@@ -100,6 +102,7 @@
|
|
|
100
102
|
"foreground-child": "^2.0.0",
|
|
101
103
|
"husky": "^3.1.0",
|
|
102
104
|
"lint-staged": "^12.5.0",
|
|
105
|
+
"ora": "^8.0.1",
|
|
103
106
|
"prettier": "^2.8.7"
|
|
104
107
|
},
|
|
105
108
|
"c8": {
|
package/src/context/Request.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare class Request {
|
|
|
11
11
|
/**
|
|
12
12
|
* The fastify request object.
|
|
13
13
|
*/
|
|
14
|
-
request
|
|
14
|
+
private request;
|
|
15
15
|
constructor(request: FastifyRequest);
|
|
16
16
|
/**
|
|
17
17
|
* Get the request id.
|
|
@@ -235,6 +235,10 @@ export declare class Request {
|
|
|
235
235
|
* ```
|
|
236
236
|
*/
|
|
237
237
|
except(keys: string[]): any;
|
|
238
|
+
/**
|
|
239
|
+
* Get the original fastify request.
|
|
240
|
+
*/
|
|
241
|
+
getFastifyRequest(): FastifyRequest;
|
|
238
242
|
/**
|
|
239
243
|
* Add the hostname and port to the url.
|
|
240
244
|
*/
|
package/src/context/Request.js
CHANGED
|
@@ -8,17 +8,16 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import type { FastifyReply } from 'fastify';
|
|
10
10
|
import type { SendOptions } from '@fastify/static';
|
|
11
|
-
import type { Request } from '#src/context/Request';
|
|
12
11
|
import type { FastifyHelmetOptions } from '@fastify/helmet';
|
|
13
12
|
export declare class Response {
|
|
14
13
|
/**
|
|
15
14
|
* The fastify response object.
|
|
16
15
|
*/
|
|
17
|
-
response
|
|
16
|
+
private response;
|
|
18
17
|
/**
|
|
19
18
|
* The request object from request context.
|
|
20
19
|
*/
|
|
21
|
-
request
|
|
20
|
+
private request;
|
|
22
21
|
constructor(response: FastifyReply, request?: Request);
|
|
23
22
|
/**
|
|
24
23
|
* Verify if the response has been already sent. Keep
|
|
@@ -213,4 +212,8 @@ export declare class Response {
|
|
|
213
212
|
* ```
|
|
214
213
|
*/
|
|
215
214
|
helmet(options: FastifyHelmetOptions): Response;
|
|
215
|
+
/**
|
|
216
|
+
* Get the original fastify response.
|
|
217
|
+
*/
|
|
218
|
+
getFastifyResponse(): FastifyReply;
|
|
216
219
|
}
|
package/src/context/Response.js
CHANGED
|
@@ -97,7 +97,7 @@ export class Response {
|
|
|
97
97
|
*/
|
|
98
98
|
async view(view, data) {
|
|
99
99
|
const content = await View.render(view, { ...data, request: this.request });
|
|
100
|
-
await this.safeHeader('Content-Type', 'text/html; charset=utf-8').
|
|
100
|
+
await this.safeHeader('Content-Type', 'text/html; charset=utf-8').send(content);
|
|
101
101
|
this.response.body = content;
|
|
102
102
|
return this;
|
|
103
103
|
}
|
|
@@ -233,4 +233,10 @@ export class Response {
|
|
|
233
233
|
this.response.helmet(options);
|
|
234
234
|
return this;
|
|
235
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* Get the original fastify response.
|
|
238
|
+
*/
|
|
239
|
+
getFastifyResponse() {
|
|
240
|
+
return this.response;
|
|
241
|
+
}
|
|
236
242
|
}
|
|
@@ -19,9 +19,11 @@ export class FastifyHandler {
|
|
|
19
19
|
if (!req.data) {
|
|
20
20
|
req.data = {};
|
|
21
21
|
}
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const ctx = {};
|
|
23
|
+
ctx.data = req.data;
|
|
24
|
+
ctx.request = new Request(req);
|
|
25
|
+
ctx.response = new Response(res, ctx.request);
|
|
26
|
+
await handler(ctx);
|
|
25
27
|
};
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
@@ -38,19 +40,17 @@ export class FastifyHandler {
|
|
|
38
40
|
if (!req.data) {
|
|
39
41
|
req.data = {};
|
|
40
42
|
}
|
|
41
|
-
const request = new Request(req);
|
|
42
|
-
const response = new Response(res, request);
|
|
43
43
|
if (Is.Json(payload)) {
|
|
44
44
|
payload = JSON.parse(payload);
|
|
45
45
|
}
|
|
46
46
|
res.body = payload;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
const ctx = {};
|
|
48
|
+
ctx.data = req.data;
|
|
49
|
+
ctx.request = new Request(req);
|
|
50
|
+
ctx.response = new Response(res, ctx.request);
|
|
51
|
+
ctx.status = ctx.response.statusCode;
|
|
52
|
+
payload = await handler(ctx);
|
|
53
|
+
req.body = payload;
|
|
54
54
|
if (Is.Object(payload)) {
|
|
55
55
|
payload = JSON.stringify(payload);
|
|
56
56
|
}
|
|
@@ -65,15 +65,13 @@ export class FastifyHandler {
|
|
|
65
65
|
if (!req.data) {
|
|
66
66
|
req.data = {};
|
|
67
67
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
responseTime: res.elapsedTime
|
|
76
|
-
});
|
|
68
|
+
const ctx = {};
|
|
69
|
+
ctx.data = req.data;
|
|
70
|
+
ctx.request = new Request(req);
|
|
71
|
+
ctx.response = new Response(res, ctx.request);
|
|
72
|
+
ctx.status = ctx.response.statusCode;
|
|
73
|
+
ctx.responseTime = ctx.response.elapsedTime;
|
|
74
|
+
await handler(ctx);
|
|
77
75
|
};
|
|
78
76
|
}
|
|
79
77
|
/**
|
|
@@ -84,14 +82,12 @@ export class FastifyHandler {
|
|
|
84
82
|
if (!req.data) {
|
|
85
83
|
req.data = {};
|
|
86
84
|
}
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
error
|
|
94
|
-
});
|
|
85
|
+
const ctx = {};
|
|
86
|
+
ctx.data = req.data;
|
|
87
|
+
ctx.request = new Request(req);
|
|
88
|
+
ctx.response = new Response(res, ctx.request);
|
|
89
|
+
ctx.error = error;
|
|
90
|
+
await handler(ctx);
|
|
95
91
|
};
|
|
96
92
|
}
|
|
97
93
|
}
|
package/src/router/Router.d.ts
CHANGED
|
@@ -44,6 +44,16 @@ export declare class Router {
|
|
|
44
44
|
* Creates a new route resource.
|
|
45
45
|
*/
|
|
46
46
|
resource(resource: string, controller?: any): RouteResource;
|
|
47
|
+
/**
|
|
48
|
+
* This method is a convenient shortcut to render a view without
|
|
49
|
+
* defining an explicit handler.
|
|
50
|
+
*/
|
|
51
|
+
view(pattern: string, view: string, data?: any): Route;
|
|
52
|
+
/**
|
|
53
|
+
* This method is a convenient shortcut to redirect a route without
|
|
54
|
+
* defining an explicit handler.
|
|
55
|
+
*/
|
|
56
|
+
redirect(pattern: string, url: string, status?: number): Route;
|
|
47
57
|
/**
|
|
48
58
|
* Define a route that handles all common HTTP methods.
|
|
49
59
|
*/
|
package/src/router/Router.js
CHANGED
|
@@ -79,6 +79,24 @@ export class Router {
|
|
|
79
79
|
}
|
|
80
80
|
return resourceInstance;
|
|
81
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* This method is a convenient shortcut to render a view without
|
|
84
|
+
* defining an explicit handler.
|
|
85
|
+
*/
|
|
86
|
+
view(pattern, view, data) {
|
|
87
|
+
return this.route(pattern, ['GET', 'HEAD'], ctx => {
|
|
88
|
+
return ctx.response.view(view, data);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* This method is a convenient shortcut to redirect a route without
|
|
93
|
+
* defining an explicit handler.
|
|
94
|
+
*/
|
|
95
|
+
redirect(pattern, url, status) {
|
|
96
|
+
return this.route(pattern, ['GET', 'HEAD'], ctx => {
|
|
97
|
+
return ctx.response.redirectTo(url, status);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
82
100
|
/**
|
|
83
101
|
* Define a route that handles all common HTTP methods.
|
|
84
102
|
*/
|
package/src/server/ServerImpl.js
CHANGED
|
@@ -125,7 +125,7 @@ export class ServerImpl {
|
|
|
125
125
|
if (!this.isListening) {
|
|
126
126
|
return;
|
|
127
127
|
}
|
|
128
|
-
await this.fastify.close().then(() => (this.isListening =
|
|
128
|
+
await this.fastify.close().then(() => (this.isListening = false));
|
|
129
129
|
}
|
|
130
130
|
/**
|
|
131
131
|
* Add a new route to the http server.
|
|
@@ -6,11 +6,24 @@
|
|
|
6
6
|
* For the full copyright and license information, please view the LICENSE
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
|
-
import { Request } from '#src/context/Request';
|
|
10
|
-
import { Response } from '#src/context/Response';
|
|
9
|
+
import type { Request } from '#src/context/Request';
|
|
10
|
+
import type { Response } from '#src/context/Response';
|
|
11
11
|
export type Context = {
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve any kind of data from your request by using the
|
|
14
|
+
* request object.
|
|
15
|
+
*/
|
|
12
16
|
request: Request;
|
|
17
|
+
/**
|
|
18
|
+
* Return a response from the request using the response
|
|
19
|
+
* object.
|
|
20
|
+
*/
|
|
13
21
|
response: Response;
|
|
22
|
+
/**
|
|
23
|
+
* Save any kind of data that will be shared in all of your
|
|
24
|
+
* request flow. The data defined here will be available in
|
|
25
|
+
* middlewares, route handlers, interceptors and terminators.
|
|
26
|
+
*/
|
|
14
27
|
data: any;
|
|
15
28
|
};
|
|
16
29
|
export type RequestHandler = (ctx: Context) => any | Promise<any>;
|