@athenna/http 4.23.0 → 4.25.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.23.0",
3
+ "version": "4.25.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,14 +72,14 @@
72
72
  "#tests": "./tests/index.js"
73
73
  },
74
74
  "devDependencies": {
75
- "@athenna/artisan": "^4.37.0",
76
- "@athenna/common": "^4.34.0",
77
- "@athenna/config": "^4.16.0",
78
- "@athenna/ioc": "^4.16.0",
79
- "@athenna/logger": "^4.17.0",
75
+ "@athenna/artisan": "^4.38.0",
76
+ "@athenna/common": "^4.35.0",
77
+ "@athenna/config": "^4.18.0",
78
+ "@athenna/ioc": "^4.18.0",
79
+ "@athenna/logger": "^4.18.0",
80
80
  "@athenna/test": "^4.22.0",
81
81
  "@athenna/tsconfig": "^4.12.0",
82
- "@athenna/view": "^4.18.0",
82
+ "@athenna/view": "^4.20.0",
83
83
  "@fastify/cors": "^8.4.2",
84
84
  "@fastify/helmet": "^11.1.1",
85
85
  "@fastify/rate-limit": "^8.1.1",
@@ -8,8 +8,8 @@
8
8
  */
9
9
  import { sep } from 'node:path';
10
10
  import { Config } from '@athenna/config';
11
- import { Module } from '@athenna/common';
12
11
  import { BaseCommand } from '@athenna/artisan';
12
+ import { Path, Module } from '@athenna/common';
13
13
  import { Route, HttpKernel, HttpRouteProvider, HttpServerProvider } from '#src';
14
14
  export class RouteListCommand extends BaseCommand {
15
15
  static signature() {
@@ -6,139 +6,38 @@
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
+ /// <reference types="node" resolution-mode="require"/>
10
+ /// <reference types="node" resolution-mode="require"/>
11
+ /// <reference types="node/http.js" />
12
+ /// <reference types="node_modules/got/dist/source/core/timed-out.js" />
13
+ import type { AddressInfo } from 'node:net';
9
14
  import type { FastifyRequest } from 'fastify';
10
- export declare class Request {
11
- /**
12
- * The fastify request object.
13
- */
14
- request: FastifyRequest;
15
- constructor(request: FastifyRequest);
16
- /**
17
- * Get the request id.
18
- *
19
- * @example 12345
20
- */
21
- get id(): string;
22
- /**
23
- * Get the request ip.
24
- *
25
- * @example 192.168.0.1
26
- */
27
- get ip(): string;
28
- /**
29
- * Get the request hostname.
30
- *
31
- * @example localhost
32
- */
33
- get hostname(): string;
34
- /**
35
- * Get the request protocol.
36
- *
37
- * @example http
38
- */
39
- get protocol(): 'http' | 'https';
40
- /**
41
- * Get the request method.
42
- *
43
- * @example GET
44
- */
45
- get method(): string;
46
- /**
47
- * Get the base url from request.
48
- *
49
- * @example /users/1
50
- */
51
- get baseUrl(): string;
52
- /**
53
- * Get the base url with host and port info from request.
54
- *
55
- * @example http://localhost:3030/users/1
56
- */
57
- get baseHostUrl(): string;
58
- /**
59
- * Get the route url from request.
60
- *
61
- * @example /users/:id
62
- */
63
- get routeUrl(): string;
64
- /**
65
- * Get the route url with host and port info from request.
66
- *
67
- * @example http://localhost:3030/users/:id
68
- */
69
- get routeHostUrl(): string;
70
- /**
71
- * Get the original url from request.
72
- *
73
- * @example /users/1?query=true
74
- */
75
- get originalUrl(): string;
76
- /**
77
- * Get the original url with host and port info from request.
78
- *
79
- * @example /users/1?query=true
80
- */
81
- get originalHostUrl(): string;
82
- /**
83
- * Get all body from request.
84
- */
85
- get body(): any | any[];
86
- /**
87
- * Get all params from request.
88
- */
89
- get params(): any;
90
- /**
91
- * Get all queries from request.
92
- */
93
- get queries(): any;
94
- /**
95
- * Get all headers from request.
96
- */
97
- get headers(): any;
98
- /**
99
- * Get the server port.
100
- */
101
- get port(): number;
102
- /**
103
- * Get the http version.
104
- */
105
- get version(): string;
106
- /**
107
- * Get a value from the request params or the default value.
108
- */
109
- param(param: string, defaultValue?: any): any;
110
- /**
111
- * Get a value from the request query param or the default value.
112
- */
113
- query(query: string, defaultValue?: any): any;
114
- /**
115
- * Get a value from the request header or the default value.
116
- */
117
- header(header: string, defaultValue?: any): any;
118
- /**
119
- * Get only the selected values from the request body.
120
- */
121
- only(keys: string[]): any;
122
- /**
123
- * Get all the values from the request body except the selected ones.
124
- */
125
- except(keys: string[]): any;
126
- /**
127
- * Get a value from the request body or the default value.
128
- */
129
- input(key: string, defaultValue?: any): any;
130
- /**
131
- * Get a value from the request body or the default value.
132
- */
133
- payload(key: string, defaultValue?: any): any;
134
- /**
135
- * Add the hostname and port to the url.
136
- */
137
- private getHostUrlFor;
138
- /**
139
- * Get the address info of the server. This method will return the
140
- * port used to listen the server, the family (IPv4, IPv6) and the
141
- * server address (127.0.0.1).
142
- */
143
- private getAddressInfo;
144
- }
15
+ export declare function request(req: FastifyRequest): {
16
+ request: FastifyRequest<import("fastify").RouteGenericInterface, import("http").Server, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider.js").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, import("fastify").RouteGenericInterface>>;
17
+ id: string;
18
+ ip: string;
19
+ hostname: string;
20
+ readonly port: number;
21
+ version: string;
22
+ protocol: "http" | "https";
23
+ method: string;
24
+ baseUrl: string;
25
+ readonly baseHostUrl: string;
26
+ routeUrl: string;
27
+ readonly routeHostUrl: string;
28
+ originalUrl: string;
29
+ readonly originalHostUrl: string;
30
+ body: unknown;
31
+ params: unknown;
32
+ queries: unknown;
33
+ headers: import("http").IncomingHttpHeaders;
34
+ param(param: string, defaultValue?: any): unknown;
35
+ query(query: string, defaultValue?: any): unknown;
36
+ header(header: string, defaultValue?: any): import("http").IncomingHttpHeaders;
37
+ input(key: string, defaultValue?: any): unknown;
38
+ payload(key: string, defaultValue?: any): unknown;
39
+ only(keys: string[]): {};
40
+ except(keys: string[]): {};
41
+ getHostUrlFor(url: string): string;
42
+ getAddressInfo(): AddressInfo;
43
+ };
@@ -7,209 +7,82 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { Is, Json } from '@athenna/common';
10
- export class Request {
11
- constructor(request) {
12
- this.request = request;
13
- }
14
- /**
15
- * Get the request id.
16
- *
17
- * @example 12345
18
- */
19
- get id() {
20
- return this.request.id;
21
- }
22
- /**
23
- * Get the request ip.
24
- *
25
- * @example 192.168.0.1
26
- */
27
- get ip() {
28
- return this.request.ip;
29
- }
30
- /**
31
- * Get the request hostname.
32
- *
33
- * @example localhost
34
- */
35
- get hostname() {
36
- return this.request.hostname;
37
- }
38
- /**
39
- * Get the request protocol.
40
- *
41
- * @example http
42
- */
43
- get protocol() {
44
- return this.request.protocol;
45
- }
46
- /**
47
- * Get the request method.
48
- *
49
- * @example GET
50
- */
51
- get method() {
52
- return this.request.method;
53
- }
54
- /**
55
- * Get the base url from request.
56
- *
57
- * @example /users/1
58
- */
59
- get baseUrl() {
60
- return this.request.url.split('?')[0];
61
- }
62
- /**
63
- * Get the base url with host and port info from request.
64
- *
65
- * @example http://localhost:3030/users/1
66
- */
67
- get baseHostUrl() {
68
- return this.getHostUrlFor(this.baseUrl);
69
- }
70
- /**
71
- * Get the route url from request.
72
- *
73
- * @example /users/:id
74
- */
75
- get routeUrl() {
76
- return this.request.routeOptions.url;
77
- }
78
- /**
79
- * Get the route url with host and port info from request.
80
- *
81
- * @example http://localhost:3030/users/:id
82
- */
83
- get routeHostUrl() {
84
- return this.getHostUrlFor(this.routeUrl);
85
- }
86
- /**
87
- * Get the original url from request.
88
- *
89
- * @example /users/1?query=true
90
- */
91
- get originalUrl() {
92
- return this.request.url;
93
- }
94
- /**
95
- * Get the original url with host and port info from request.
96
- *
97
- * @example /users/1?query=true
98
- */
99
- get originalHostUrl() {
100
- return this.getHostUrlFor(this.originalUrl);
101
- }
102
- /**
103
- * Get all body from request.
104
- */
105
- get body() {
106
- return this.request.body || {};
107
- }
108
- /**
109
- * Get all params from request.
110
- */
111
- get params() {
112
- return this.request.params || {};
113
- }
114
- /**
115
- * Get all queries from request.
116
- */
117
- get queries() {
118
- return this.request.query || {};
119
- }
120
- /**
121
- * Get all headers from request.
122
- */
123
- get headers() {
124
- return this.request.headers || {};
125
- }
126
- /**
127
- * Get the server port.
128
- */
129
- get port() {
130
- return this.getAddressInfo().port;
131
- }
132
- /**
133
- * Get the http version.
134
- */
135
- get version() {
136
- return this.request.raw.httpVersion;
137
- }
138
- /**
139
- * Get a value from the request params or the default value.
140
- */
141
- param(param, defaultValue) {
142
- return this.params[param] || defaultValue;
143
- }
144
- /**
145
- * Get a value from the request query param or the default value.
146
- */
147
- query(query, defaultValue) {
148
- return this.queries[query] || defaultValue;
149
- }
150
- /**
151
- * Get a value from the request header or the default value.
152
- */
153
- header(header, defaultValue) {
154
- return this.headers[header] || defaultValue;
155
- }
156
- /**
157
- * Get only the selected values from the request body.
158
- */
159
- only(keys) {
160
- const body = {};
161
- Object.keys(this.body).forEach(key => {
162
- if (!keys.includes(key)) {
163
- return;
10
+ export function request(req) {
11
+ const request = {
12
+ request: req,
13
+ id: req.id,
14
+ ip: req.ip,
15
+ hostname: req.hostname,
16
+ get port() {
17
+ return request.getAddressInfo().port;
18
+ },
19
+ version: req.raw.httpVersion,
20
+ protocol: req.protocol,
21
+ method: req.method,
22
+ baseUrl: req.url.split('?')[0],
23
+ get baseHostUrl() {
24
+ return request.getHostUrlFor(request.baseUrl);
25
+ },
26
+ routeUrl: req.routeOptions.url,
27
+ get routeHostUrl() {
28
+ return request.getHostUrlFor(request.routeUrl);
29
+ },
30
+ originalUrl: req.url,
31
+ get originalHostUrl() {
32
+ return request.getHostUrlFor(request.originalUrl);
33
+ },
34
+ body: req.body || {},
35
+ params: req.params || {},
36
+ queries: req.query || {},
37
+ headers: req.headers || {},
38
+ param(param, defaultValue) {
39
+ return Json.get(request.params, param, defaultValue);
40
+ },
41
+ query(query, defaultValue) {
42
+ return Json.get(request.queries, query, defaultValue);
43
+ },
44
+ header(header, defaultValue) {
45
+ return Json.get(request.headers, header, defaultValue);
46
+ },
47
+ input(key, defaultValue) {
48
+ return request.payload(key, defaultValue);
49
+ },
50
+ payload(key, defaultValue) {
51
+ return Json.get(request.body, key, defaultValue);
52
+ },
53
+ only(keys) {
54
+ const body = {};
55
+ Object.keys(request.body).forEach(key => {
56
+ if (!keys.includes(key)) {
57
+ return;
58
+ }
59
+ body[key] = request.body[key];
60
+ });
61
+ return body;
62
+ },
63
+ except(keys) {
64
+ const body = {};
65
+ Object.keys(request.body).forEach(key => {
66
+ if (keys.includes(key)) {
67
+ return;
68
+ }
69
+ body[key] = request.body[key];
70
+ });
71
+ return body;
72
+ },
73
+ getHostUrlFor(url) {
74
+ let { address, port } = request.getAddressInfo();
75
+ if (address === '::1') {
76
+ address = '127.0.0.1';
164
77
  }
165
- body[key] = this.body[key];
166
- });
167
- return body;
168
- }
169
- /**
170
- * Get all the values from the request body except the selected ones.
171
- */
172
- except(keys) {
173
- const body = {};
174
- Object.keys(this.body).forEach(key => {
175
- if (keys.includes(key)) {
176
- return;
78
+ if (!Is.Ip(address) && address !== 'localhost') {
79
+ return `${request.protocol}://${address}${url}`;
177
80
  }
178
- body[key] = this.body[key];
179
- });
180
- return body;
181
- }
182
- /**
183
- * Get a value from the request body or the default value.
184
- */
185
- input(key, defaultValue) {
186
- return this.payload(key, defaultValue);
187
- }
188
- /**
189
- * Get a value from the request body or the default value.
190
- */
191
- payload(key, defaultValue) {
192
- return Json.get(this.body, key, defaultValue);
193
- }
194
- /**
195
- * Add the hostname and port to the url.
196
- */
197
- getHostUrlFor(url) {
198
- let { address, port } = this.getAddressInfo();
199
- if (address === '::1') {
200
- address = '127.0.0.1';
81
+ return `${request.protocol}://${address}:${port}${url}`;
82
+ },
83
+ getAddressInfo() {
84
+ return req.server.server.address();
201
85
  }
202
- if (!Is.Ip(address) && address !== 'localhost') {
203
- return `${this.protocol}://${address}${url}`;
204
- }
205
- return `${this.protocol}://${address}:${port}${url}`;
206
- }
207
- /**
208
- * Get the address info of the server. This method will return the
209
- * port used to listen the server, the family (IPv4, IPv6) and the
210
- * server address (127.0.0.1).
211
- */
212
- getAddressInfo() {
213
- return this.request.server.server.address();
214
- }
86
+ };
87
+ return request;
215
88
  }
@@ -7,81 +7,8 @@
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';
12
- import type { FastifyHelmetOptions } from '@fastify/helmet';
13
- export declare class Response {
14
- /**
15
- * The fastify response object.
16
- */
17
- response: FastifyReply;
18
- /**
19
- * The request object from request context.
20
- */
21
- request: Request;
22
- constructor(response: FastifyReply, request?: Request);
23
- /**
24
- * Verify if the response has been already sent.
25
- */
26
- get sent(): boolean;
27
- /**
28
- * Get the response body sent in response.
29
- */
30
- get body(): any | any[];
31
- /**
32
- * Get the status code sent in response.
33
- */
34
- get statusCode(): number;
35
- /**
36
- * Get the headers sent in response.
37
- */
38
- get headers(): any;
39
- /**
40
- * Get the time in MS of how much the request has taken to response.
41
- */
42
- get responseTime(): number;
43
- /**
44
- * Terminated the request sending a view to be rendered.
45
- */
46
- view(view: string, data?: any): Promise<Response>;
47
- /**
48
- * Terminate the request sending the response body or not.
49
- */
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>;
57
- /**
58
- * Set the response status code.
59
- */
60
- status(code: number): Response;
61
- /**
62
- * Add some header to the response.
63
- */
64
- header(header: string, value: any): Response;
65
- /**
66
- * Verify if response has some header.
67
- */
68
- hasHeader(header: string): boolean;
69
- /**
70
- * Add some header safely to the response. This means that the header is not
71
- * going to be added if is already set.
72
- */
73
- safeHeader(header: string, value: any): Response;
74
- /**
75
- * Remove some header from the response.
76
- */
77
- removeHeader(header: string): Response;
78
- /**
79
- * Redirect the response to other url. You can also set a different status code
80
- * for the redirect.
81
- */
82
- redirectTo(url: string, status?: number): Promise<Response>;
83
- /**
84
- * Apply helmet in response.
85
- */
86
- helmet(options: FastifyHelmetOptions): Response;
87
- }
10
+ import type { Request, Response } from '#src/types';
11
+ /**
12
+ * Create the Athenna response object
13
+ */
14
+ export declare function response(reply: FastifyReply, request?: Request): Response;