@athenna/http 1.8.1 → 1.8.2
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 +1 -1
- package/src/Context/Request.js +55 -4
- package/src/Context/Response.js +2 -0
- package/src/index.d.ts +33 -14
package/package.json
CHANGED
package/src/Context/Request.js
CHANGED
|
@@ -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}
|
|
206
|
+
* @param {string} key
|
|
156
207
|
* @param {string} [defaultValue]
|
|
157
208
|
* @return {any}
|
|
158
209
|
*/
|
|
159
|
-
payload(
|
|
160
|
-
return this.body
|
|
210
|
+
payload(key, defaultValue) {
|
|
211
|
+
return Json.get(this.body, key, defaultValue)
|
|
161
212
|
}
|
|
162
213
|
|
|
163
214
|
/**
|
package/src/Context/Response.js
CHANGED
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}
|
|
891
|
-
* @param {
|
|
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(
|
|
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.
|