Skip to content

Commit e980211

Browse files
committed
fix: use msw 2.0 in the readme
1 parent 9e8ceb4 commit e980211

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Spawn an [Express](https://expressjs.com) server from your [Mock Service Worker]
44

55
## When to use this?
66

7-
You should always prefer Mock Service Worker for API mocking because it can meet most of your requirements without having to spawn and maintain an actual HTTP server. Please refer to the [Getting started](https://mswjs.io/docs/getting-started/install) tutorial to integrate next-generation API mocking into your application.
7+
You should always prefer Mock Service Worker for API mocking because it can meet most of your requirements without having to spawn and maintain an actual HTTP server. Please refer to the [Getting started](https://mswjs.io/docs/getting-started) tutorial to integrate next-generation API mocking into your application.
88

99
There are, however, use cases when this extension can be applicable:
1010

@@ -24,29 +24,29 @@ $ npm install @mswjs/http-middleware
2424

2525
```js
2626
// src/mocks/handlers.js
27-
import { rest, graphql } from 'msw'
27+
import { http, graphql, HttpResponse } from 'msw'
2828

2929
export const handlers = [
30-
rest.post('/user', (req, res, ctx) => {
31-
return res(ctx.json({ firstName: 'John' }))
30+
http.post('/user', () => {
31+
return HttpResponse.json({ firstName: 'John' })
3232
}),
33-
graphql.query('GetUser', (req, res, ctx) => {
34-
return res(
35-
ctx.data({
33+
graphql.query('GetUser', () => {
34+
return HttpResponse.json({
35+
data: {
3636
user: {
3737
firstName: 'John',
3838
},
39-
}),
40-
)
39+
},
40+
})
4141
}),
4242
]
4343
```
4444

45-
> Learn more about writing [request handlers](https://mswjs.io/docs/getting-started/mocks).
45+
> Learn more about writing [request handlers](https://mswjs.io/docs/concepts/request-handler).
4646
47-
### Integrate
47+
### Integration
4848

49-
#### A. Spawn a standalone server
49+
#### Option 1: Standalone server
5050

5151
```js
5252
import { createServer } from '@mswjs/http-middleware'
@@ -57,7 +57,7 @@ const httpServer = createServer(...handlers)
5757
httpServer.listen(9090)
5858
```
5959

60-
#### B. Apply as a middleware
60+
#### Option 2: Middleware
6161

6262
```js
6363
import { createMiddleware } from '@mswjs/http-middleware'
@@ -74,12 +74,12 @@ app.use(createMiddleware(...handlers))
7474
Establishes a standalone Express server that uses the given request handlers to process all incoming requests.
7575

7676
```ts
77-
import { rest } from 'msw'
77+
import { http, HttpResponse } from 'msw'
7878
import { createServer } from '@mswjs/http-middleware'
7979

8080
const httpServer = createServer(
81-
rest.get('/user', (req, res, ctx) => {
82-
return res(ctx.json({ firstName: 'John' }))
81+
http.get('/user', () => {
82+
return HttpResponse.json({ firstName: 'John' })
8383
}),
8484
)
8585

@@ -102,15 +102,15 @@ Content-Type: application/json
102102
Creates an Express middleware function that uses the given request handlers to process all incoming requests.
103103

104104
```ts
105-
import { rest } from 'msw'
105+
import { http, HttpResponse } from 'msw'
106106
import { createMiddleware } from '@mswjs/http-middleware'
107107

108108
const app = express()
109109

110110
app.use(
111111
createMiddleware(
112-
rest.get('/user', (req, res, ctx) => {
113-
return res(ctx.json({ firstName: 'John' }))
112+
http.get('/user', () => {
113+
return HttpResponse.json({ firstName: 'John' })
114114
}),
115115
),
116116
)

0 commit comments

Comments
 (0)