@athenna/http 4.22.0 → 4.23.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/http",
3
- "version": "4.22.0",
3
+ "version": "4.23.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>",
@@ -72,17 +72,18 @@
72
72
  "#tests": "./tests/index.js"
73
73
  },
74
74
  "devDependencies": {
75
- "@athenna/artisan": "^4.35.0",
75
+ "@athenna/artisan": "^4.37.0",
76
76
  "@athenna/common": "^4.34.0",
77
77
  "@athenna/config": "^4.16.0",
78
78
  "@athenna/ioc": "^4.16.0",
79
79
  "@athenna/logger": "^4.17.0",
80
80
  "@athenna/test": "^4.22.0",
81
81
  "@athenna/tsconfig": "^4.12.0",
82
- "@athenna/view": "^4.14.0",
82
+ "@athenna/view": "^4.18.0",
83
83
  "@fastify/cors": "^8.4.2",
84
84
  "@fastify/helmet": "^11.1.1",
85
85
  "@fastify/rate-limit": "^8.1.1",
86
+ "@fastify/static": "^7.0.1",
86
87
  "@fastify/swagger": "^8.12.2",
87
88
  "@fastify/swagger-ui": "^3.0.0",
88
89
  "@typescript-eslint/eslint-plugin": "^6.7.4",
@@ -7,13 +7,19 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import type { FastifyReply } from 'fastify';
10
+ import type { SendOptions } from '@fastify/static';
11
+ import type { Request } from '#src/context/Request';
10
12
  import type { FastifyHelmetOptions } from '@fastify/helmet';
11
13
  export declare class Response {
12
14
  /**
13
15
  * The fastify response object.
14
16
  */
15
17
  response: FastifyReply;
16
- constructor(response: FastifyReply);
18
+ /**
19
+ * The request object from request context.
20
+ */
21
+ request: Request;
22
+ constructor(response: FastifyReply, request?: Request);
17
23
  /**
18
24
  * Verify if the response has been already sent.
19
25
  */
@@ -34,10 +40,20 @@ export declare class Response {
34
40
  * Get the time in MS of how much the request has taken to response.
35
41
  */
36
42
  get responseTime(): number;
43
+ /**
44
+ * Terminated the request sending a view to be rendered.
45
+ */
46
+ view(view: string, data?: any): Promise<Response>;
37
47
  /**
38
48
  * Terminate the request sending the response body or not.
39
49
  */
40
50
  send(data?: any): Promise<Response>;
51
+ sendFile(filename: string, filepath?: string): Promise<Response>;
52
+ sendFile(filename: string, options?: string | SendOptions): Promise<Response>;
53
+ sendFile(filename: string, filepath?: string, options?: SendOptions): Promise<Response>;
54
+ download(filepath: string, filename?: string): Promise<Response>;
55
+ download(filepath: string, options?: string | SendOptions): Promise<Response>;
56
+ download(filename: string, filepath?: string, options?: SendOptions): Promise<Response>;
41
57
  /**
42
58
  * Set the response status code.
43
59
  */
@@ -51,7 +67,7 @@ export declare class Response {
51
67
  */
52
68
  hasHeader(header: string): boolean;
53
69
  /**
54
- * Add some header safelly to the response. This means that the header is not
70
+ * Add some header safely to the response. This means that the header is not
55
71
  * going to be added if is already set.
56
72
  */
57
73
  safeHeader(header: string, value: any): Response;
@@ -6,9 +6,11 @@
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 { View } from '@athenna/view';
9
10
  export class Response {
10
- constructor(response) {
11
+ constructor(response, request) {
11
12
  this.response = response;
13
+ this.request = request;
12
14
  }
13
15
  /**
14
16
  * Verify if the response has been already sent.
@@ -38,7 +40,16 @@ export class Response {
38
40
  * Get the time in MS of how much the request has taken to response.
39
41
  */
40
42
  get responseTime() {
41
- return this.response.getResponseTime();
43
+ return this.response.elapsedTime;
44
+ }
45
+ /**
46
+ * Terminated the request sending a view to be rendered.
47
+ */
48
+ async view(view, data) {
49
+ const content = await View.render(view, { ...data, request: this.request });
50
+ await this.safeHeader('Content-Type', 'text/html; charset=utf-8').response.send(content);
51
+ this.response.body = content;
52
+ return this;
42
53
  }
43
54
  /**
44
55
  * Terminate the request sending the response body or not.
@@ -48,6 +59,17 @@ export class Response {
48
59
  this.response.body = data;
49
60
  return this;
50
61
  }
62
+ /**
63
+ * Terminated the request sending a file.
64
+ */
65
+ async sendFile(filename, filepath, options) {
66
+ await this.response.sendFile(filename, filepath, options);
67
+ return this;
68
+ }
69
+ async download(filepath, filename, options) {
70
+ await this.response.download(filename, filepath, options);
71
+ return this;
72
+ }
51
73
  /**
52
74
  * Set the response status code.
53
75
  */
@@ -69,7 +91,7 @@ export class Response {
69
91
  return this.response.hasHeader(header);
70
92
  }
71
93
  /**
72
- * Add some header safelly to the response. This means that the header is not
94
+ * Add some header safely to the response. This means that the header is not
73
95
  * going to be added if is already set.
74
96
  */
75
97
  safeHeader(header, value) {
@@ -6,9 +6,9 @@
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 type { InterceptHandler, TerminateHandler } from '#src/types';
10
9
  import type { RequestHandler } from '#src/types/contexts/Context';
11
10
  import type { ErrorHandler } from '#src/types/contexts/ErrorContext';
11
+ import type { InterceptHandler, TerminateHandler } from '#src/types';
12
12
  import type { FastifyReply, FastifyRequest, RouteHandlerMethod } from 'fastify';
13
13
  export declare class FastifyHandler {
14
14
  /**
@@ -17,10 +17,10 @@ export class FastifyHandler {
17
17
  static request(handler) {
18
18
  return async (req, res) => {
19
19
  const request = new Request(req);
20
- const response = new Response(res);
21
20
  if (!req.data) {
22
21
  req.data = {};
23
22
  }
23
+ const response = new Response(res, request);
24
24
  await handler({
25
25
  request,
26
26
  response,
@@ -44,7 +44,10 @@ export class FastifyHandler {
44
44
  static intercept(handler) {
45
45
  return async (req, res, payload) => {
46
46
  const request = new Request(req);
47
- const response = new Response(res);
47
+ if (!req.data) {
48
+ req.data = {};
49
+ }
50
+ const response = new Response(res, request);
48
51
  if (Is.Json(payload)) {
49
52
  payload = JSON.parse(payload);
50
53
  }
@@ -72,7 +75,10 @@ export class FastifyHandler {
72
75
  static terminate(handler) {
73
76
  return async (req, res) => {
74
77
  const request = new Request(req);
75
- const response = new Response(res);
78
+ if (!req.data) {
79
+ req.data = {};
80
+ }
81
+ const response = new Response(res, request);
76
82
  await handler({
77
83
  request,
78
84
  response,
@@ -82,7 +88,7 @@ export class FastifyHandler {
82
88
  body: res.body || req.body,
83
89
  headers: res.getHeaders(),
84
90
  status: res.statusCode,
85
- responseTime: res.getResponseTime()
91
+ responseTime: res.elapsedTime
86
92
  });
87
93
  };
88
94
  }
@@ -92,7 +98,10 @@ export class FastifyHandler {
92
98
  static error(handler) {
93
99
  return async (error, req, res) => {
94
100
  const request = new Request(req);
95
- const response = new Response(res);
101
+ if (!req.data) {
102
+ req.data = {};
103
+ }
104
+ const response = new Response(res, request);
96
105
  await handler({
97
106
  request,
98
107
  response,
@@ -24,6 +24,10 @@ export declare class HttpKernel {
24
24
  * Register the @fastify/rate-limit plugin in the Http server.
25
25
  */
26
26
  registerRateLimit(): Promise<void>;
27
+ /**
28
+ * Register the @fastify/static plugin in the Http server.
29
+ */
30
+ registerStatic(): Promise<void>;
27
31
  /**
28
32
  * Register the cls-rtracer plugin in the Http server.
29
33
  */
@@ -20,6 +20,7 @@ const helmetPlugin = await Module.safeImport('@fastify/helmet');
20
20
  const swaggerPlugin = await Module.safeImport('@fastify/swagger');
21
21
  const swaggerUiPlugin = await Module.safeImport('@fastify/swagger-ui');
22
22
  const rateLimitPlugin = await Module.safeImport('@fastify/rate-limit');
23
+ const staticPlugin = await Module.safeImport('@fastify/static');
23
24
  const rTracerPlugin = await Module.safeImport('cls-rtracer');
24
25
  export class HttpKernel {
25
26
  /**
@@ -90,6 +91,20 @@ export class HttpKernel {
90
91
  }
91
92
  await Server.plugin(rateLimitPlugin, this.getConfig('http.rateLimit'));
92
93
  }
94
+ /**
95
+ * Register the @fastify/static plugin in the Http server.
96
+ */
97
+ async registerStatic() {
98
+ if (Config.is('http.static.enabled', false)) {
99
+ debug('Not able to register static plugin. Set the http.static.enabled configuration as true.');
100
+ return;
101
+ }
102
+ if (!staticPlugin) {
103
+ debug('Not able to register static plugin. Install @fastify/static package.');
104
+ return;
105
+ }
106
+ await Server.plugin(staticPlugin, this.getConfig('http.static'));
107
+ }
93
108
  /**
94
109
  * Register the cls-rtracer plugin in the Http server.
95
110
  */