@athenna/http 1.8.1 → 1.8.3

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": "1.8.1",
3
+ "version": "1.8.3",
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>",
@@ -54,11 +54,11 @@
54
54
  "#tests/*": "./tests/*.js"
55
55
  },
56
56
  "dependencies": {
57
- "@athenna/artisan": "1.5.9",
58
- "@athenna/common": "1.0.1",
59
- "@athenna/config": "1.2.0",
60
- "@athenna/ioc": "1.2.9",
61
- "@athenna/logger": "1.3.7",
57
+ "@athenna/artisan": "1.6.0",
58
+ "@athenna/common": "1.0.2",
59
+ "@athenna/config": "1.2.1",
60
+ "@athenna/ioc": "1.3.0",
61
+ "@athenna/logger": "1.3.8",
62
62
  "@fastify/cors": "8.1.1",
63
63
  "@fastify/helmet": "10.0.2",
64
64
  "@fastify/rate-limit": "7.5.0",
@@ -7,8 +7,8 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
 
10
- import { Is } from '@athenna/common'
11
10
  import { Config } from '@athenna/config'
11
+ import { Is, Json } from '@athenna/common'
12
12
 
13
13
  export class Request {
14
14
  /**
@@ -149,15 +149,66 @@ export class Request {
149
149
  return this.headers[header] || defaultValue
150
150
  }
151
151
 
152
+ /**
153
+ * Get only the selected values from the request body.
154
+ *
155
+ * @param {string} keys
156
+ * @return {any}
157
+ */
158
+ only(...keys) {
159
+ const body = {}
160
+
161
+ Object.keys(this.body).forEach(key => {
162
+ if (!keys.includes(key)) {
163
+ return
164
+ }
165
+
166
+ body[key] = this.body[key]
167
+ })
168
+
169
+ return body
170
+ }
171
+
172
+ /**
173
+ * Get all the values from the request body except the selected ones.
174
+ *
175
+ * @param {string[]} keys
176
+ * @return {any}
177
+ */
178
+ except(...keys) {
179
+ const body = {}
180
+
181
+ Object.keys(this.body).forEach(key => {
182
+ if (keys.includes(key)) {
183
+ return
184
+ }
185
+
186
+ body[key] = this.body[key]
187
+ })
188
+
189
+ return body
190
+ }
191
+
192
+ /**
193
+ * Get a value from the request body or the default value.
194
+ *
195
+ * @param {string} key
196
+ * @param {string} [defaultValue]
197
+ * @return {any}
198
+ */
199
+ input(key, defaultValue) {
200
+ return this.payload(key, defaultValue)
201
+ }
202
+
152
203
  /**
153
204
  * Get a value from the request body or the default value.
154
205
  *
155
- * @param {string} payload
206
+ * @param {string} key
156
207
  * @param {string} [defaultValue]
157
208
  * @return {any}
158
209
  */
159
- payload(payload, defaultValue) {
160
- return this.body[payload] || defaultValue
210
+ payload(key, defaultValue) {
211
+ return Json.get(this.body, key, defaultValue)
161
212
  }
162
213
 
163
214
  /**
@@ -119,6 +119,8 @@ export class Response {
119
119
  redirectTo(url, statusCode) {
120
120
  if (statusCode) {
121
121
  this.#response.redirect(statusCode, url)
122
+
123
+ return
122
124
  }
123
125
 
124
126
  this.#response.redirect(url)
package/src/index.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import { Facade } from '@athenna/ioc'
11
- import { Exception } from '@athenna/common'
11
+ import { Exception, Json } from '@athenna/common'
12
12
  import { OpenAPIV2, OpenAPIV3 } from 'openapi-types'
13
13
  import { FastifyHelmetOptions } from '@fastify/helmet'
14
14
  import { FastifyReply, FastifyRequest, RouteOptions } from 'fastify'
@@ -884,14 +884,42 @@ export interface RequestContract {
884
884
  */
885
885
  header(header, defaultValue): any
886
886
 
887
+ /**
888
+ * Get only the selected values from the request body.
889
+ *
890
+ * @param {string} keys
891
+ * @return {any}
892
+ */
893
+ only(...keys: string[]): any
894
+ only(keys: string[]): any
895
+
896
+ /**
897
+ * Get all the values from the request body except the selected ones.
898
+ *
899
+ * @param {string[]} keys
900
+ * @return {any}
901
+ */
902
+ except(...keys: string[]): any
903
+ except(keys: string): any
904
+
887
905
  /**
888
906
  * Get a value from the request body or the default value.
889
907
  *
890
- * @param {string} payload
891
- * @param {string} [defaultValue]
908
+ * @param {string} key
909
+ * @param {any} [defaultValue]
910
+ * @return {any}
911
+ */
912
+ input(key: string, defaultValue?: any): any
913
+
914
+ /**
915
+ * Get a value from the request body or the default value.
916
+ *
917
+ * @param {string} key
918
+ * @param {any} [defaultValue]
892
919
  * @return {any}
893
920
  */
894
- payload(payload, defaultValue): any
921
+ payload(key: string, defaultValue?: any): any
922
+
895
923
  /**
896
924
  * Get the default fastify request object.
897
925
  *
@@ -962,19 +990,10 @@ export interface ResponseContract {
962
990
  /**
963
991
  * Redirect the response to other url with different status code.
964
992
  *
965
- * @param {string} url
966
993
  * @return {void}
967
994
  */
968
995
  redirectTo(url: string): Promise<void> | void
969
-
970
- /**
971
- * Redirect the response to other url with different status code.
972
- *
973
- * @param {string} url
974
- * @param {number} statusCode
975
- * @return {void}
976
- */
977
- redirectTo(url: string, statusCode: number): Promise<void>
996
+ redirectTo(url: string, statusCode: number): Promise<void> | void
978
997
 
979
998
  /**
980
999
  * Get the default fastify response object.