@@ -2,13 +2,13 @@ import { headersToObject } from 'headers-polyfill'
2
2
import { HttpResponse , http } from 'msw'
3
3
4
4
export type Post = {
5
- id : string
5
+ id : number
6
6
title : string
7
7
body : string
8
8
}
9
9
10
10
export const posts : Record < string , Post > = {
11
- '1' : { id : '1' , title : 'hello' , body : 'extra body!' } ,
11
+ 1 : { id : 1 , title : 'hello' , body : 'extra body!' } ,
12
12
}
13
13
14
14
export const handlers = [
@@ -25,6 +25,7 @@ export const handlers = [
25
25
} )
26
26
}
27
27
) ,
28
+
28
29
http . post (
29
30
'https://example.com/echo' ,
30
31
async ( { request, cookies, params, requestId } ) => {
@@ -50,19 +51,25 @@ export const handlers = [
50
51
} )
51
52
}
52
53
) ,
54
+
53
55
http . get ( 'https://example.com/success' , ( ) =>
54
56
HttpResponse . json ( { value : 'success' } )
55
57
) ,
58
+
56
59
http . post ( 'https://example.com/success' , ( ) =>
57
60
HttpResponse . json ( { value : 'success' } )
58
61
) ,
62
+
59
63
http . get ( 'https://example.com/empty' , ( ) => new HttpResponse ( '' ) ) ,
64
+
60
65
http . get ( 'https://example.com/error' , ( ) =>
61
66
HttpResponse . json ( { value : 'error' } , { status : 500 } )
62
67
) ,
68
+
63
69
http . post ( 'https://example.com/error' , ( ) =>
64
70
HttpResponse . json ( { value : 'error' } , { status : 500 } )
65
71
) ,
72
+
66
73
http . get ( 'https://example.com/nonstandard-error' , ( ) =>
67
74
HttpResponse . json (
68
75
{
@@ -72,18 +79,22 @@ export const handlers = [
72
79
{ status : 200 }
73
80
)
74
81
) ,
82
+
75
83
http . get ( 'https://example.com/mirror' , ( { params } ) =>
76
84
HttpResponse . json ( params )
77
85
) ,
86
+
78
87
http . post ( 'https://example.com/mirror' , ( { params } ) =>
79
88
HttpResponse . json ( params )
80
89
) ,
90
+
81
91
http . get ( 'https://example.com/posts/random' , ( ) => {
82
92
// just simulate an api that returned a random ID
83
93
const { id } = posts [ 1 ]
84
94
return HttpResponse . json ( { id } )
85
95
} ) ,
86
- http . get < Post , any , Pick < Post , 'id' > > (
96
+
97
+ http . get < { id : string } , any , Pick < Post , 'id' > > (
87
98
'https://example.com/post/:id' ,
88
99
( { params } ) => HttpResponse . json ( posts [ params . id ] )
89
100
) ,
0 commit comments