@@ -3,27 +3,21 @@ import { http, HttpResponse, passthrough } from 'msw'
3
3
4
4
it ( 'supports a single path parameter' , ( ) => {
5
5
http . get < { id : string } > ( '/user/:id' , ( { params } ) => {
6
- params . id . toUpperCase ( )
7
-
8
6
expectTypeOf ( params ) . toEqualTypeOf < { id : string } > ( )
9
7
} )
10
8
} )
11
9
12
10
it ( 'supports multiple path parameters' , ( ) => {
13
- http . get < { a : string ; b : string [ ] } > ( '/user/:a/:b/:b' , ( { params } ) => {
14
- params . a . toUpperCase ( )
15
- params . b . map ( ( x ) => x )
16
-
17
- expectTypeOf ( params ) . toEqualTypeOf < { a : string ; b : string [ ] } > ( )
11
+ type Params = { a : string ; b : string [ ] }
12
+ http . get < Params > ( '/user/:a/:b/:b' , ( { params } ) => {
13
+ expectTypeOf ( params ) . toEqualTypeOf < Params > ( )
18
14
} )
19
15
} )
20
16
21
17
it ( 'supports path parameters declared via type' , ( ) => {
22
- type UserPathParams = { id : string }
23
- http . get < UserPathParams > ( '/user/:id' , ( { params } ) => {
24
- params . id . toUpperCase ( )
25
-
26
- expectTypeOf ( params ) . toEqualTypeOf < UserPathParams > ( )
18
+ type Params = { id : string }
19
+ http . get < Params > ( '/user/:id' , ( { params } ) => {
20
+ expectTypeOf ( params ) . toEqualTypeOf < Params > ( )
27
21
} )
28
22
} )
29
23
@@ -32,13 +26,11 @@ it('supports path parameters declared via interface', () => {
32
26
id : string
33
27
}
34
28
http . get < PostPathParameters > ( '/user/:id' , ( { params } ) => {
35
- params . id . toUpperCase ( )
36
-
37
29
expectTypeOf ( params ) . toEqualTypeOf < PostPathParameters > ( )
38
30
} )
39
31
} )
40
32
41
- it ( 'supports request body generic' , ( ) => {
33
+ it ( 'supports json as a request body generic argument ' , ( ) => {
42
34
http . post < never , { id : string } > ( '/user' , async ( { request } ) => {
43
35
const data = await request . json ( )
44
36
@@ -50,7 +42,7 @@ it('supports request body generic', () => {
50
42
} )
51
43
} )
52
44
53
- it ( 'supports null as the response body generic' , ( ) => {
45
+ it ( 'supports null as the request body generic' , ( ) => {
54
46
http . get < never , null > ( '/user' , async ( { request } ) => {
55
47
const data = await request . json ( )
56
48
expectTypeOf ( data ) . toEqualTypeOf < null > ( )
@@ -63,12 +55,77 @@ it('returns plain Response withouth explicit response body generic', () => {
63
55
} )
64
56
} )
65
57
66
- it ( 'supports response body generic' , ( ) => {
58
+ it ( 'supports string as a response body generic argument' , ( ) => {
59
+ http . get < never , never , string > ( '/' , ( { request } ) => {
60
+ if ( request . headers . has ( 'x-foo' ) ) {
61
+ return HttpResponse . text ( 'conditional' )
62
+ }
63
+
64
+ return HttpResponse . text ( 'hello' )
65
+ } )
66
+ } )
67
+
68
+ it ( 'supports exact string as a response body generic argument' , ( ) => {
69
+ http . get < never , never , 'hello' > ( '/' , ( ) => {
70
+ return HttpResponse . text ( 'hello' )
71
+ } )
72
+
73
+ http . get < never , never , 'hello' > ( '/' , ( ) => {
74
+ // @ts -expect-error Non-matching response body type.
75
+ return HttpResponse . text ( 'unexpected' )
76
+ } )
77
+ } )
78
+
79
+ it ( 'supports object as a response body generic argument' , ( ) => {
67
80
http . get < never , never , { id : number } > ( '/user' , ( ) => {
68
81
return HttpResponse . json ( { id : 1 } )
69
82
} )
70
83
} )
71
84
85
+ it ( 'supports narrow object as a response body generic argument' , ( ) => {
86
+ http . get < never , never , { id : 123 } > ( '/user' , ( ) => {
87
+ return HttpResponse . json ( { id : 123 } )
88
+ } )
89
+
90
+ http . get < never , never , { id : 123 } > ( '/user' , ( ) => {
91
+ return HttpResponse . json ( {
92
+ // @ts -expect-error Non-matching response body type.
93
+ id : 456 ,
94
+ } )
95
+ } )
96
+ } )
97
+
98
+ it ( 'supports object with extra keys as a response body generic argument' , ( ) => {
99
+ type ResponseBody = {
100
+ [ key : string ] : number | string
101
+ id : 123
102
+ }
103
+
104
+ http . get < never , never , ResponseBody > ( '/user' , ( ) => {
105
+ return HttpResponse . json ( {
106
+ id : 123 ,
107
+ // Extra keys are allowed if they satisfy the index signature.
108
+ name : 'John' ,
109
+ } )
110
+ } )
111
+
112
+ http . get < never , never , ResponseBody > ( '/user' , ( ) => {
113
+ return HttpResponse . json ( {
114
+ // @ts -expect-error Must be 123.
115
+ id : 456 ,
116
+ name : 'John' ,
117
+ } )
118
+ } )
119
+
120
+ http . get < never , never , ResponseBody > ( '/user' , ( ) => {
121
+ return HttpResponse . json ( {
122
+ id : 123 ,
123
+ // @ts -expect-error Must satisfy the index signature.
124
+ name : { a : 1 } ,
125
+ } )
126
+ } )
127
+ } )
128
+
72
129
it ( 'supports response body generic declared via type' , ( ) => {
73
130
type ResponseBodyType = { id : number }
74
131
http . get < never , never , ResponseBodyType > ( '/user' , ( ) => {
@@ -127,3 +184,14 @@ it("accepts passthrough in HttpResponse's body", () => {
127
184
return HttpResponse . json ( { id : 1 } )
128
185
} )
129
186
} )
187
+
188
+ it ( 'infers a narrower json response type' , ( ) => {
189
+ type ResponseBody = {
190
+ a : number
191
+ }
192
+
193
+ http . get < never , never , ResponseBody > ( '/' , ( ) => {
194
+ // @ts -expect-error Unknown property "b".
195
+ return HttpResponse . json ( { a : 1 , b : 2 } )
196
+ } )
197
+ } )
0 commit comments