|
1 | 1 | import { setupServer } from 'msw/node'
|
2 |
| -import { rest } from 'msw' |
3 |
| - |
4 |
| -// This configures a request mocking server with the given request handlers. |
| 2 | +import { headersToObject } from 'headers-polyfill' |
| 3 | +import { HttpResponse, http } from 'msw' |
5 | 4 |
|
6 | 5 | export type Post = {
|
7 |
| - id: number |
| 6 | + id: string |
8 | 7 | title: string
|
9 | 8 | body: string
|
10 | 9 | }
|
11 | 10 |
|
12 |
| -export const posts: Record<number, Post> = { |
13 |
| - 1: { id: 1, title: 'hello', body: 'extra body!' }, |
| 11 | +export const posts: Record<string, Post> = { |
| 12 | + '1': { id: '1', title: 'hello', body: 'extra body!' }, |
14 | 13 | }
|
15 | 14 |
|
16 |
| -export const server = setupServer( |
17 |
| - rest.get('https://example.com/echo', (req, res, ctx) => |
18 |
| - res(ctx.json({ ...req, headers: req.headers.all() })) |
19 |
| - ), |
20 |
| - rest.post('https://example.com/echo', (req, res, ctx) => |
21 |
| - res(ctx.json({ ...req, headers: req.headers.all() })) |
22 |
| - ), |
23 |
| - rest.get('https://example.com/success', (_, res, ctx) => |
24 |
| - res(ctx.json({ value: 'success' })) |
| 15 | +export const handlers = [ |
| 16 | + http.get( |
| 17 | + 'https://example.com/echo', |
| 18 | + async ({ request, params, cookies, requestId }) => { |
| 19 | + return HttpResponse.json( |
| 20 | + { |
| 21 | + ...request, |
| 22 | + params, |
| 23 | + cookies, |
| 24 | + requestId, |
| 25 | + url: new URL(request.url), |
| 26 | + headers: headersToObject(request.headers), |
| 27 | + }, |
| 28 | + { headers: request.headers } |
| 29 | + ) |
| 30 | + } |
25 | 31 | ),
|
26 |
| - rest.post('https://example.com/success', (_, res, ctx) => |
27 |
| - res(ctx.json({ value: 'success' })) |
| 32 | + http.post( |
| 33 | + 'https://example.com/echo', |
| 34 | + async ({ request, cookies, params, requestId }) => { |
| 35 | + const body = headersToObject(request.headers)['content-type'] === 'text/html' |
| 36 | + ? await request.text() |
| 37 | + : await request.json() |
| 38 | + |
| 39 | + return HttpResponse.json( |
| 40 | + { |
| 41 | + ...request, |
| 42 | + cookies, |
| 43 | + params, |
| 44 | + requestId, |
| 45 | + body, |
| 46 | + url: new URL(request.url), |
| 47 | + headers: request?.headers |
| 48 | + ? headersToObject(request.headers) |
| 49 | + : request?.headers, |
| 50 | + }, |
| 51 | + { headers: request.headers } |
| 52 | + ) |
| 53 | + } |
28 | 54 | ),
|
29 |
| - rest.get('https://example.com/empty', (_, res, ctx) => res(ctx.body(''))), |
30 |
| - rest.get('https://example.com/error', (_, res, ctx) => |
31 |
| - res(ctx.status(500), ctx.json({ value: 'error' })) |
| 55 | + http.get('https://example.com/success', () => { |
| 56 | + return HttpResponse.json({ value: 'success' }) |
| 57 | + }), |
| 58 | + http.post('https://example.com/success', ({ request }) => { |
| 59 | + return HttpResponse.json({ value: 'success' }) |
| 60 | + }), |
| 61 | + http.get('https://example.com/empty', () => new HttpResponse('')), |
| 62 | + http.get('https://example.com/error', () => |
| 63 | + HttpResponse.json({ value: 'error' }, { status: 500 }) |
32 | 64 | ),
|
33 |
| - rest.post('https://example.com/error', (_, res, ctx) => |
34 |
| - res(ctx.status(500), ctx.json({ value: 'error' })) |
| 65 | + http.post('https://example.com/error', () => |
| 66 | + HttpResponse.json({ value: 'error' }, { status: 500 }) |
35 | 67 | ),
|
36 |
| - rest.get('https://example.com/nonstandard-error', (_, res, ctx) => |
37 |
| - res( |
38 |
| - ctx.status(200), |
39 |
| - ctx.json({ |
| 68 | + http.get('https://example.com/nonstandard-error', () => |
| 69 | + HttpResponse.json( |
| 70 | + { |
40 | 71 | success: false,
|
41 | 72 | message: 'This returns a 200 but is really an error',
|
42 |
| - }) |
| 73 | + }, |
| 74 | + { status: 200 } |
43 | 75 | )
|
44 | 76 | ),
|
45 |
| - rest.get('https://example.com/mirror', (req, res, ctx) => |
46 |
| - res(ctx.json(req.params)) |
| 77 | + http.get('https://example.com/mirror', ({ params }) => |
| 78 | + HttpResponse.json(params) |
47 | 79 | ),
|
48 |
| - rest.post('https://example.com/mirror', (req, res, ctx) => |
49 |
| - res(ctx.json(req.params)) |
| 80 | + http.post('https://example.com/mirror', ({ params }) => |
| 81 | + HttpResponse.json(params) |
50 | 82 | ),
|
51 |
| - rest.get('https://example.com/posts/random', (req, res, ctx) => { |
| 83 | + http.get('https://example.com/posts/random', () => { |
52 | 84 | // just simulate an api that returned a random ID
|
53 |
| - const { id, ..._post } = posts[1] |
54 |
| - return res(ctx.json({ id })) |
| 85 | + const { id } = posts[1] |
| 86 | + return HttpResponse.json({ id }) |
55 | 87 | }),
|
56 |
| - rest.get<Post, any, { id: number }>( |
| 88 | + http.get<Post, any, Pick<Post, 'id'>>( |
57 | 89 | 'https://example.com/post/:id',
|
58 |
| - (req, res, ctx) => { |
59 |
| - return res(ctx.json(posts[req.params.id])) |
60 |
| - } |
61 |
| - ) |
62 |
| -) |
| 90 | + ({ params }) => HttpResponse.json(posts[params.id]) |
| 91 | + ), |
| 92 | +] |
| 93 | + |
| 94 | + |
| 95 | +// This configures a request mocking server with the given request handlers. |
| 96 | +export const server = setupServer(...handlers) |
0 commit comments