@athenna/http 4.22.0 → 4.24.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.24.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",
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.14.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",
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",
@@ -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() {
@@ -16,121 +16,225 @@ export declare class Request {
16
16
  /**
17
17
  * Get the request id.
18
18
  *
19
- * @example 12345
19
+ * @example
20
+ * ```ts
21
+ * console.log(request.id) // '12345'
22
+ * ```
20
23
  */
21
24
  get id(): string;
22
25
  /**
23
26
  * Get the request ip.
24
27
  *
25
- * @example 192.168.0.1
28
+ * @example
29
+ * ```ts
30
+ * console.log(request.ip) // '192.168.0.1'
31
+ * ```
26
32
  */
27
33
  get ip(): string;
28
34
  /**
29
35
  * Get the request hostname.
30
36
  *
31
- * @example localhost
37
+ * @example
38
+ * ```ts
39
+ * console.log(request.hostname) // 'localhost'
40
+ * ```
32
41
  */
33
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;
34
61
  /**
35
62
  * Get the request protocol.
36
63
  *
37
- * @example http
64
+ * @example
65
+ * ```ts
66
+ * console.log(request.protocol) // 'http'
67
+ * ```
38
68
  */
39
69
  get protocol(): 'http' | 'https';
40
70
  /**
41
71
  * Get the request method.
42
72
  *
43
- * @example GET
73
+ * @example
74
+ * ```ts
75
+ * console.log(request.method) // 'GET'
76
+ * ```
44
77
  */
45
78
  get method(): string;
46
79
  /**
47
80
  * Get the base url from request.
48
81
  *
49
- * @example /users/1
82
+ * @example
83
+ * ```ts
84
+ * console.log(request.baseUrl) // '/users/1'
85
+ * ```
50
86
  */
51
87
  get baseUrl(): string;
52
88
  /**
53
89
  * Get the base url with host and port info from request.
54
90
  *
55
- * @example http://localhost:3030/users/1
91
+ * @example
92
+ * ```ts
93
+ * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1'
94
+ * ```
56
95
  */
57
96
  get baseHostUrl(): string;
58
97
  /**
59
98
  * Get the route url from request.
60
99
  *
61
- * @example /users/:id
100
+ * @example
101
+ * ```ts
102
+ * console.log(request.routeUrl) // '/users/:id'
103
+ * ```
62
104
  */
63
105
  get routeUrl(): string;
64
106
  /**
65
107
  * Get the route url with host and port info from request.
66
108
  *
67
- * @example http://localhost:3030/users/:id
109
+ * @example
110
+ * ```ts
111
+ * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id'
112
+ * ```
68
113
  */
69
114
  get routeHostUrl(): string;
70
115
  /**
71
116
  * Get the original url from request.
72
117
  *
73
- * @example /users/1?query=true
118
+ * @example
119
+ * ```ts
120
+ * console.log(request.originalUrl) // '/users/1?query=true'
121
+ * ```
74
122
  */
75
123
  get originalUrl(): string;
76
124
  /**
77
125
  * Get the original url with host and port info from request.
78
126
  *
79
- * @example /users/1?query=true
127
+ * @example
128
+ * ```ts
129
+ * console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true'
130
+ * ```
80
131
  */
81
132
  get originalHostUrl(): string;
82
133
  /**
83
134
  * Get all body from request.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * const { name, email } = request.body
139
+ * ```
84
140
  */
85
141
  get body(): any | any[];
86
142
  /**
87
143
  * Get all params from request.
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * const { id } = request.params
148
+ * ```
88
149
  */
89
150
  get params(): any;
90
151
  /**
91
152
  * Get all queries from request.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * const { page, limit } = request.queries
157
+ * ```
92
158
  */
93
159
  get queries(): any;
94
160
  /**
95
161
  * Get all headers from request.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * const { accept } = request.headers
166
+ * ```
96
167
  */
97
168
  get headers(): any;
98
169
  /**
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.
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
+ * ```
108
177
  */
109
178
  param(param: string, defaultValue?: any): any;
110
179
  /**
111
- * Get a value from the request query param or the default value.
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
+ * ```
112
187
  */
113
188
  query(query: string, defaultValue?: any): any;
114
189
  /**
115
- * Get a value from the request header or the default value.
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
+ * ```
116
197
  */
117
198
  header(header: string, defaultValue?: any): any;
118
199
  /**
119
- * Get only the selected values from the request body.
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
+ * ```
120
207
  */
121
- only(keys: string[]): any;
208
+ input(key: string, defaultValue?: any): any;
122
209
  /**
123
- * Get all the values from the request body except the selected ones.
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
+ * ```
124
217
  */
125
- except(keys: string[]): any;
218
+ payload(key: string, defaultValue?: any): any;
126
219
  /**
127
- * Get a value from the request body or the default value.
220
+ * Get only the selected values from the request body.
221
+ *
222
+ * @example
223
+ * ```ts
224
+ * const body = request.only(['name', 'email'])
225
+ * ```
128
226
  */
129
- input(key: string, defaultValue?: any): any;
227
+ only(keys: string[]): any;
130
228
  /**
131
- * Get a value from the request body or the default value.
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
+ * ```
132
236
  */
133
- payload(key: string, defaultValue?: any): any;
237
+ except(keys: string[]): any;
134
238
  /**
135
239
  * Add the hostname and port to the url.
136
240
  */
@@ -14,7 +14,10 @@ export class Request {
14
14
  /**
15
15
  * Get the request id.
16
16
  *
17
- * @example 12345
17
+ * @example
18
+ * ```ts
19
+ * console.log(request.id) // '12345'
20
+ * ```
18
21
  */
19
22
  get id() {
20
23
  return this.request.id;
@@ -22,7 +25,10 @@ export class Request {
22
25
  /**
23
26
  * Get the request ip.
24
27
  *
25
- * @example 192.168.0.1
28
+ * @example
29
+ * ```ts
30
+ * console.log(request.ip) // '192.168.0.1'
31
+ * ```
26
32
  */
27
33
  get ip() {
28
34
  return this.request.ip;
@@ -30,15 +36,43 @@ export class Request {
30
36
  /**
31
37
  * Get the request hostname.
32
38
  *
33
- * @example localhost
39
+ * @example
40
+ * ```ts
41
+ * console.log(request.hostname) // 'localhost'
42
+ * ```
34
43
  */
35
44
  get hostname() {
36
45
  return this.request.hostname;
37
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
+ }
38
69
  /**
39
70
  * Get the request protocol.
40
71
  *
41
- * @example http
72
+ * @example
73
+ * ```ts
74
+ * console.log(request.protocol) // 'http'
75
+ * ```
42
76
  */
43
77
  get protocol() {
44
78
  return this.request.protocol;
@@ -46,7 +80,10 @@ export class Request {
46
80
  /**
47
81
  * Get the request method.
48
82
  *
49
- * @example GET
83
+ * @example
84
+ * ```ts
85
+ * console.log(request.method) // 'GET'
86
+ * ```
50
87
  */
51
88
  get method() {
52
89
  return this.request.method;
@@ -54,7 +91,10 @@ export class Request {
54
91
  /**
55
92
  * Get the base url from request.
56
93
  *
57
- * @example /users/1
94
+ * @example
95
+ * ```ts
96
+ * console.log(request.baseUrl) // '/users/1'
97
+ * ```
58
98
  */
59
99
  get baseUrl() {
60
100
  return this.request.url.split('?')[0];
@@ -62,7 +102,10 @@ export class Request {
62
102
  /**
63
103
  * Get the base url with host and port info from request.
64
104
  *
65
- * @example http://localhost:3030/users/1
105
+ * @example
106
+ * ```ts
107
+ * console.log(request.baseHostUrl) // 'http://localhost:3030/users/1'
108
+ * ```
66
109
  */
67
110
  get baseHostUrl() {
68
111
  return this.getHostUrlFor(this.baseUrl);
@@ -70,7 +113,10 @@ export class Request {
70
113
  /**
71
114
  * Get the route url from request.
72
115
  *
73
- * @example /users/:id
116
+ * @example
117
+ * ```ts
118
+ * console.log(request.routeUrl) // '/users/:id'
119
+ * ```
74
120
  */
75
121
  get routeUrl() {
76
122
  return this.request.routeOptions.url;
@@ -78,7 +124,10 @@ export class Request {
78
124
  /**
79
125
  * Get the route url with host and port info from request.
80
126
  *
81
- * @example http://localhost:3030/users/:id
127
+ * @example
128
+ * ```ts
129
+ * console.log(request.routeHostUrl) // 'http://localhost:3030/users/:id'
130
+ * ```
82
131
  */
83
132
  get routeHostUrl() {
84
133
  return this.getHostUrlFor(this.routeUrl);
@@ -86,7 +135,10 @@ export class Request {
86
135
  /**
87
136
  * Get the original url from request.
88
137
  *
89
- * @example /users/1?query=true
138
+ * @example
139
+ * ```ts
140
+ * console.log(request.originalUrl) // '/users/1?query=true'
141
+ * ```
90
142
  */
91
143
  get originalUrl() {
92
144
  return this.request.url;
@@ -94,67 +146,125 @@ export class Request {
94
146
  /**
95
147
  * Get the original url with host and port info from request.
96
148
  *
97
- * @example /users/1?query=true
149
+ * @example
150
+ * ```ts
151
+ * console.log(request.originalHostUrl) // 'http://localhost:3000/users/1?query=true'
152
+ * ```
98
153
  */
99
154
  get originalHostUrl() {
100
155
  return this.getHostUrlFor(this.originalUrl);
101
156
  }
102
157
  /**
103
158
  * Get all body from request.
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * const { name, email } = request.body
163
+ * ```
104
164
  */
105
165
  get body() {
106
166
  return this.request.body || {};
107
167
  }
108
168
  /**
109
169
  * Get all params from request.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * const { id } = request.params
174
+ * ```
110
175
  */
111
176
  get params() {
112
177
  return this.request.params || {};
113
178
  }
114
179
  /**
115
180
  * Get all queries from request.
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * const { page, limit } = request.queries
185
+ * ```
116
186
  */
117
187
  get queries() {
118
188
  return this.request.query || {};
119
189
  }
120
190
  /**
121
191
  * Get all headers from request.
192
+ *
193
+ * @example
194
+ * ```ts
195
+ * const { accept } = request.headers
196
+ * ```
122
197
  */
123
198
  get headers() {
124
199
  return this.request.headers || {};
125
200
  }
126
201
  /**
127
- * Get the server port.
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
+ * ```
128
209
  */
129
- get port() {
130
- return this.getAddressInfo().port;
210
+ param(param, defaultValue) {
211
+ return Json.get(this.params, param, defaultValue);
131
212
  }
132
213
  /**
133
- * Get the http version.
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
+ * ```
134
221
  */
135
- get version() {
136
- return this.request.raw.httpVersion;
222
+ query(query, defaultValue) {
223
+ return Json.get(this.queries, query, defaultValue);
137
224
  }
138
225
  /**
139
- * Get a value from the request params or the default value.
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
+ * ```
140
233
  */
141
- param(param, defaultValue) {
142
- return this.params[param] || defaultValue;
234
+ header(header, defaultValue) {
235
+ return Json.get(this.headers, header, defaultValue);
143
236
  }
144
237
  /**
145
- * Get a value from the request query param or the default value.
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
+ * ```
146
245
  */
147
- query(query, defaultValue) {
148
- return this.queries[query] || defaultValue;
246
+ input(key, defaultValue) {
247
+ return this.payload(key, defaultValue);
149
248
  }
150
249
  /**
151
- * Get a value from the request header or the default value.
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
+ * ```
152
257
  */
153
- header(header, defaultValue) {
154
- return this.headers[header] || defaultValue;
258
+ payload(key, defaultValue) {
259
+ return Json.get(this.body, key, defaultValue);
155
260
  }
156
261
  /**
157
262
  * Get only the selected values from the request body.
263
+ *
264
+ * @example
265
+ * ```ts
266
+ * const body = request.only(['name', 'email'])
267
+ * ```
158
268
  */
159
269
  only(keys) {
160
270
  const body = {};
@@ -167,7 +277,13 @@ export class Request {
167
277
  return body;
168
278
  }
169
279
  /**
170
- * Get all the values from the request body except the selected ones.
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
+ * ```
171
287
  */
172
288
  except(keys) {
173
289
  const body = {};
@@ -179,18 +295,6 @@ export class Request {
179
295
  });
180
296
  return body;
181
297
  }
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
298
  /**
195
299
  * Add the hostname and port to the url.
196
300
  */