Open History to see the last commit
1import { delay, HttpResponse, http, passthrough } from 'msw'
2import { setupWorker } from 'msw/browser'
3import ky from 'ky'
4β
5const movies = [
6 {
7 id: '1',
8 title: 'Guardians of the Galaxy',
9 comment: '',
10 },
11 {
12 id: '2',
13 title: 'Wall-E',
14 comment: '',
15 },
16]
17β
18export const fetchMovie = (id) => ky.get(`/movies/${id}`).json()
19export const fetchMovies = () => ky.get('/movies').json()
20export const updateMovie = (id, comment) =>
21 ky.post(`/movies/${id}`, { json: { comment } }).json()
22β
23export const worker = setupWorker(
24 ...[
25 http.get('/movies', async () => {
26 console.log('movies')
27 await delay(1000)
28 return HttpResponse.json({
29 ts: Date.now(),
30 movies: movies.map(({ id, title }) => ({ id, title })),
31 })
32 }),
33 http.get('/movies/:id', async ({ params }) => {
34 const { id } = params
35β
36 const movie = movies.find((movie) => movie.id === id)
37 if (!movie) {
38 return new HttpResponse(`Movie with id ${id} not found`, {
39 status: 404,
40 })
41 }
42β
43 await delay(1000)
44 return HttpResponse.json({ ts: Date.now(), movie })
45 }),
46 http.post('/movies/:id', async ({ request, params }) => {
47 const { id } = params
48 const body = await request.json()
49 const { comment } = body
50β
51 movies.forEach((movie) => {
52 if (movie.id === id) {
53 movie.comment = comment.toUpperCase()
54 }
55 })
56β
57 await delay(1000)
58 return HttpResponse.json({ message: `Successfully updated movie ${id}` })
59 }),
60 http.get('*.js', () => passthrough()),
61 http.get('*.svg', () => passthrough()),
62 ],
63)