File tree Expand file tree Collapse file tree 4 files changed +74
-1
lines changed Expand file tree Collapse file tree 4 files changed +74
-1
lines changed Original file line number Diff line number Diff line change @@ -2,3 +2,4 @@ export const HTTP_INTERFACE_METADATA = Symbol("HTTP_INTERFACE_METADATA");
2
2
export const HTTP_EXCHANGE_METADATA = Symbol ( "HTTP_EXCHANGE_METADATA" ) ;
3
3
export const PATH_VARIABLE_METADATA = Symbol ( "PATH_VARIABLE_METADATA" ) ;
4
4
export const REQUEST_PARAM_METADATA = Symbol ( "REQUEST_PARAM_METADATA" ) ;
5
+ export const REQUEST_BODY_METADATA = Symbol ( "REQUEST_BODY_METADATA" ) ;
Original file line number Diff line number Diff line change
1
+ import { describe , test , expect } from "vitest" ;
2
+ import { REQUEST_BODY_METADATA } from "./constants" ;
3
+ import {
4
+ RequestBody ,
5
+ type RequestBodyMetadata ,
6
+ } from "./request-body.decorator" ;
7
+
8
+ describe ( "RequestBody" , ( ) => {
9
+ test ( "should set request body metadata with empty key" , ( ) => {
10
+ // given
11
+ class TestService {
12
+ request ( @RequestBody ( ) query : { foo : string } ) : string {
13
+ return query . foo ;
14
+ }
15
+ }
16
+
17
+ // when
18
+ const result : RequestBodyMetadata = Reflect . getMetadata (
19
+ REQUEST_BODY_METADATA ,
20
+ TestService . prototype ,
21
+ "request"
22
+ ) ;
23
+
24
+ // then
25
+ expect ( result ) . toHaveLength ( 1 ) ;
26
+ expect ( result . get ( 0 ) ) . toBeUndefined ( ) ;
27
+ } ) ;
28
+
29
+ test ( "should set request body metadata with key" , ( ) => {
30
+ // given
31
+ class TestService {
32
+ request ( @RequestBody ( "foo" ) bar : string ) : string {
33
+ return bar ;
34
+ }
35
+ }
36
+
37
+ // when
38
+ const result : RequestBodyMetadata = Reflect . getMetadata (
39
+ REQUEST_BODY_METADATA ,
40
+ TestService . prototype ,
41
+ "request"
42
+ ) ;
43
+
44
+ // then
45
+ expect ( result ) . toHaveLength ( 1 ) ;
46
+ expect ( result . get ( 0 ) ) . toBe ( "foo" ) ;
47
+ } ) ;
48
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { REQUEST_BODY_METADATA } from "./constants" ;
2
+
3
+ export type RequestBodyMetadata = Map < number , string | undefined > ;
4
+
5
+ export function RequestBody ( key ?: string ) : ParameterDecorator {
6
+ return ( target , propertyKey , parameterIndex ) => {
7
+ if ( typeof propertyKey === "undefined" ) {
8
+ return ;
9
+ }
10
+
11
+ const metadata =
12
+ Reflect . getMetadata ( REQUEST_BODY_METADATA , target , propertyKey ) ??
13
+ new Map ( ) ;
14
+
15
+ metadata . set ( parameterIndex , key ) ;
16
+
17
+ Reflect . defineMetadata (
18
+ REQUEST_BODY_METADATA ,
19
+ metadata ,
20
+ target ,
21
+ propertyKey
22
+ ) ;
23
+ } ;
24
+ }
Original file line number Diff line number Diff line change 21
21
"homepage" : " https://github.com/r2don/nest-http-interface#readme" ,
22
22
"main" : " index.js" ,
23
23
"scripts" : {
24
- "test" : " echo \" Error: no test specified \" && exit 1 " ,
24
+ "test" : " vitest run " ,
25
25
"prepare" : " husky install"
26
26
},
27
27
"devDependencies" : {
You can’t perform that action at this time.
0 commit comments