@athenna/http 4.24.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.24.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>",
@@ -6,243 +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
20
- * ```ts
21
- * console.log(request.id) // '12345'
22
- * ```
23
- */
24
- get id(): string;
25
- /**
26
- * Get the request ip.
27
- *
28
- * @example
29
- * ```ts
30
- * console.log(request.ip) // '192.168.0.1'
31
- * ```
32
- */
33
- get ip(): string;
34
- /**
35
- * Get the request hostname.
36
- *
37
- * @example
38
- * ```ts
39
- * console.log(request.hostname) // 'localhost'
40
- * ```
41
- */
42
- get hostname(): string;
43
- /**
44
- * Get the server port.
45
- *
46
- * @example
47
- * ```ts
48
- * console.log(request.port) // 3000
49
- * ```
50
- */
51
- get port(): number;
52
- /**
53
- * Get the http version.
54
- *
55
- * @example
56
- * ```ts
57
- * console.log(request.version) // 1
58
- * ```
59
- */
60
- get version(): string;
61
- /**
62
- * Get the request protocol.
63
- *
64
- * @example
65
- * ```ts
66
- * console.log(request.protocol) // 'http'
67
- * ```
68
- */
69
- get protocol(): 'http' | 'https';
70
- /**
71
- * Get the request method.
72
- *
73
- * @example
74
- * ```ts
75
- * console.log(request.method) // 'GET'
76
- * ```
77
- */
78
- get method(): string;
79
- /**
80
- * Get the base url from request.
81
- *
82
- * @example
83
- * ```ts
84
- * console.log(request.baseUrl) // '/users/1'
85
- * ```
86
- */
87
- get baseUrl(): string;
88
- /**
89
- * Get the base url with host and port info from request.
90
- *
91
- * @example
92
- * ```ts
93
- * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1'
94
- * ```
95
- */
96
- get baseHostUrl(): string;
97
- /**
98
- * Get the route url from request.
99
- *
100
- * @example
101
- * ```ts
102
- * console.log(request.routeUrl) // '/users/:id'
103
- * ```
104
- */
105
- get routeUrl(): string;
106
- /**
107
- * Get the route url with host and port info from request.
108
- *
109
- * @example
110
- * ```ts
111
- * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id'
112
- * ```
113
- */
114
- get routeHostUrl(): string;
115
- /**
116
- * Get the original url from request.
117
- *
118
- * @example
119
- * ```ts
120
- * console.log(request.originalUrl) // '/users/1?query=true'
121
- * ```
122
- */
123
- get originalUrl(): string;
124
- /**
125
- * Get the original url with host and port info from request.
126
- *
127
- * @example
128
- * ```ts
129
- * console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true'
130
- * ```
131
- */
132
- get originalHostUrl(): string;
133
- /**
134
- * Get all body from request.
135
- *
136
- * @example
137
- * ```ts
138
- * const { name, email } = request.body
139
- * ```
140
- */
141
- get body(): any | any[];
142
- /**
143
- * Get all params from request.
144
- *
145
- * @example
146
- * ```ts
147
- * const { id } = request.params
148
- * ```
149
- */
150
- get params(): any;
151
- /**
152
- * Get all queries from request.
153
- *
154
- * @example
155
- * ```ts
156
- * const { page, limit } = request.queries
157
- * ```
158
- */
159
- get queries(): any;
160
- /**
161
- * Get all headers from request.
162
- *
163
- * @example
164
- * ```ts
165
- * const { accept } = request.headers
166
- * ```
167
- */
168
- get headers(): any;
169
- /**
170
- * Get a value from the request params or return
171
- * the default value.
172
- *
173
- * @example
174
- * ```ts
175
- * const id = request.param('id', '1')
176
- * ```
177
- */
178
- param(param: string, defaultValue?: any): any;
179
- /**
180
- * Get a value from the request query param or return
181
- * the default value.
182
- *
183
- * @example
184
- * ```ts
185
- * const page = request.query('page', '1')
186
- * ```
187
- */
188
- query(query: string, defaultValue?: any): any;
189
- /**
190
- * Get a value from the request header or return
191
- * the default value.
192
- *
193
- * @example
194
- * ```ts
195
- * const accept = request.header('accept', 'application/json')
196
- * ```
197
- */
198
- header(header: string, defaultValue?: any): any;
199
- /**
200
- * Get a value from the request body or return
201
- * the default value.
202
- *
203
- * @example
204
- * ```ts
205
- * const name = request.input('name', 'lenon')
206
- * ```
207
- */
208
- input(key: string, defaultValue?: any): any;
209
- /**
210
- * Get a value from the request body or return
211
- * the default value.
212
- *
213
- * @example
214
- * ```ts
215
- * const name = request.payload('name', 'lenon')
216
- * ```
217
- */
218
- payload(key: string, defaultValue?: any): any;
219
- /**
220
- * Get only the selected values from the request body.
221
- *
222
- * @example
223
- * ```ts
224
- * const body = request.only(['name', 'email'])
225
- * ```
226
- */
227
- only(keys: string[]): any;
228
- /**
229
- * Get all the values from the request body except the
230
- * selected ones.
231
- *
232
- * @example
233
- * ```ts
234
- * const body = request.except(['name'])
235
- * ```
236
- */
237
- except(keys: string[]): any;
238
- /**
239
- * Add the hostname and port to the url.
240
- */
241
- private getHostUrlFor;
242
- /**
243
- * Get the address info of the server. This method will return the
244
- * port used to listen the server, the family (IPv4, IPv6) and the
245
- * server address (127.0.0.1).
246
- */
247
- private getAddressInfo;
248
- }
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,313 +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
18
- * ```ts
19
- * console.log(request.id) // '12345'
20
- * ```
21
- */
22
- get id() {
23
- return this.request.id;
24
- }
25
- /**
26
- * Get the request ip.
27
- *
28
- * @example
29
- * ```ts
30
- * console.log(request.ip) // '192.168.0.1'
31
- * ```
32
- */
33
- get ip() {
34
- return this.request.ip;
35
- }
36
- /**
37
- * Get the request hostname.
38
- *
39
- * @example
40
- * ```ts
41
- * console.log(request.hostname) // 'localhost'
42
- * ```
43
- */
44
- get hostname() {
45
- return this.request.hostname;
46
- }
47
- /**
48
- * Get the server port.
49
- *
50
- * @example
51
- * ```ts
52
- * console.log(request.port) // 3000
53
- * ```
54
- */
55
- get port() {
56
- return this.getAddressInfo().port;
57
- }
58
- /**
59
- * Get the http version.
60
- *
61
- * @example
62
- * ```ts
63
- * console.log(request.version) // 1
64
- * ```
65
- */
66
- get version() {
67
- return this.request.raw.httpVersion;
68
- }
69
- /**
70
- * Get the request protocol.
71
- *
72
- * @example
73
- * ```ts
74
- * console.log(request.protocol) // 'http'
75
- * ```
76
- */
77
- get protocol() {
78
- return this.request.protocol;
79
- }
80
- /**
81
- * Get the request method.
82
- *
83
- * @example
84
- * ```ts
85
- * console.log(request.method) // 'GET'
86
- * ```
87
- */
88
- get method() {
89
- return this.request.method;
90
- }
91
- /**
92
- * Get the base url from request.
93
- *
94
- * @example
95
- * ```ts
96
- * console.log(request.baseUrl) // '/users/1'
97
- * ```
98
- */
99
- get baseUrl() {
100
- return this.request.url.split('?')[0];
101
- }
102
- /**
103
- * Get the base url with host and port info from request.
104
- *
105
- * @example
106
- * ```ts
107
- * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1'
108
- * ```
109
- */
110
- get baseHostUrl() {
111
- return this.getHostUrlFor(this.baseUrl);
112
- }
113
- /**
114
- * Get the route url from request.
115
- *
116
- * @example
117
- * ```ts
118
- * console.log(request.routeUrl) // '/users/:id'
119
- * ```
120
- */
121
- get routeUrl() {
122
- return this.request.routeOptions.url;
123
- }
124
- /**
125
- * Get the route url with host and port info from request.
126
- *
127
- * @example
128
- * ```ts
129
- * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id'
130
- * ```
131
- */
132
- get routeHostUrl() {
133
- return this.getHostUrlFor(this.routeUrl);
134
- }
135
- /**
136
- * Get the original url from request.
137
- *
138
- * @example
139
- * ```ts
140
- * console.log(request.originalUrl) // '/users/1?query=true'
141
- * ```
142
- */
143
- get originalUrl() {
144
- return this.request.url;
145
- }
146
- /**
147
- * Get the original url with host and port info from request.
148
- *
149
- * @example
150
- * ```ts
151
- * console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true'
152
- * ```
153
- */
154
- get originalHostUrl() {
155
- return this.getHostUrlFor(this.originalUrl);
156
- }
157
- /**
158
- * Get all body from request.
159
- *
160
- * @example
161
- * ```ts
162
- * const { name, email } = request.body
163
- * ```
164
- */
165
- get body() {
166
- return this.request.body || {};
167
- }
168
- /**
169
- * Get all params from request.
170
- *
171
- * @example
172
- * ```ts
173
- * const { id } = request.params
174
- * ```
175
- */
176
- get params() {
177
- return this.request.params || {};
178
- }
179
- /**
180
- * Get all queries from request.
181
- *
182
- * @example
183
- * ```ts
184
- * const { page, limit } = request.queries
185
- * ```
186
- */
187
- get queries() {
188
- return this.request.query || {};
189
- }
190
- /**
191
- * Get all headers from request.
192
- *
193
- * @example
194
- * ```ts
195
- * const { accept } = request.headers
196
- * ```
197
- */
198
- get headers() {
199
- return this.request.headers || {};
200
- }
201
- /**
202
- * Get a value from the request params or return
203
- * the default value.
204
- *
205
- * @example
206
- * ```ts
207
- * const id = request.param('id', '1')
208
- * ```
209
- */
210
- param(param, defaultValue) {
211
- return Json.get(this.params, param, defaultValue);
212
- }
213
- /**
214
- * Get a value from the request query param or return
215
- * the default value.
216
- *
217
- * @example
218
- * ```ts
219
- * const page = request.query('page', '1')
220
- * ```
221
- */
222
- query(query, defaultValue) {
223
- return Json.get(this.queries, query, defaultValue);
224
- }
225
- /**
226
- * Get a value from the request header or return
227
- * the default value.
228
- *
229
- * @example
230
- * ```ts
231
- * const accept = request.header('accept', 'application/json')
232
- * ```
233
- */
234
- header(header, defaultValue) {
235
- return Json.get(this.headers, header, defaultValue);
236
- }
237
- /**
238
- * Get a value from the request body or return
239
- * the default value.
240
- *
241
- * @example
242
- * ```ts
243
- * const name = request.input('name', 'lenon')
244
- * ```
245
- */
246
- input(key, defaultValue) {
247
- return this.payload(key, defaultValue);
248
- }
249
- /**
250
- * Get a value from the request body or return
251
- * the default value.
252
- *
253
- * @example
254
- * ```ts
255
- * const name = request.payload('name', 'lenon')
256
- * ```
257
- */
258
- payload(key, defaultValue) {
259
- return Json.get(this.body, key, defaultValue);
260
- }
261
- /**
262
- * Get only the selected values from the request body.
263
- *
264
- * @example
265
- * ```ts
266
- * const body = request.only(['name', 'email'])
267
- * ```
268
- */
269
- only(keys) {
270
- const body = {};
271
- Object.keys(this.body).forEach(key => {
272
- if (!keys.includes(key)) {
273
- 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';
274
77
  }
275
- body[key] = this.body[key];
276
- });
277
- return body;
278
- }
279
- /**
280
- * Get all the values from the request body except the
281
- * selected ones.
282
- *
283
- * @example
284
- * ```ts
285
- * const body = request.except(['name'])
286
- * ```
287
- */
288
- except(keys) {
289
- const body = {};
290
- Object.keys(this.body).forEach(key => {
291
- if (keys.includes(key)) {
292
- return;
78
+ if (!Is.Ip(address) && address !== 'localhost') {
79
+ return `${request.protocol}://${address}${url}`;
293
80
  }
294
- body[key] = this.body[key];
295
- });
296
- return body;
297
- }
298
- /**
299
- * Add the hostname and port to the url.
300
- */
301
- getHostUrlFor(url) {
302
- let { address, port } = this.getAddressInfo();
303
- if (address === '::1') {
304
- address = '127.0.0.1';
81
+ return `${request.protocol}://${address}:${port}${url}`;
82
+ },
83
+ getAddressInfo() {
84
+ return req.server.server.address();
305
85
  }
306
- if (!Is.Ip(address) && address !== 'localhost') {
307
- return `${this.protocol}://${address}${url}`;
308
- }
309
- return `${this.protocol}://${address}:${port}${url}`;
310
- }
311
- /**
312
- * Get the address info of the server. This method will return the
313
- * port used to listen the server, the family (IPv4, IPv6) and the
314
- * server address (127.0.0.1).
315
- */
316
- getAddressInfo() {
317
- return this.request.server.server.address();
318
- }
86
+ };
87
+ return request;
319
88
  }