Skip to content

Commit

Permalink
fix: use msw 2.0 in the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 26, 2023
1 parent 9e8ceb4 commit e980211
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Spawn an [Express](https://expressjs.com) server from your [Mock Service Worker]

## When to use this?

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.
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.

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

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

```js
// src/mocks/handlers.js
import { rest, graphql } from 'msw'
import { http, graphql, HttpResponse } from 'msw'

export const handlers = [
rest.post('/user', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }))
http.post('/user', () => {
return HttpResponse.json({ firstName: 'John' })
}),
graphql.query('GetUser', (req, res, ctx) => {
return res(
ctx.data({
graphql.query('GetUser', () => {
return HttpResponse.json({
data: {
user: {
firstName: 'John',
},
}),
)
},
})
}),
]
```

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

#### A. Spawn a standalone server
#### Option 1: Standalone server

```js
import { createServer } from '@mswjs/http-middleware'
Expand All @@ -57,7 +57,7 @@ const httpServer = createServer(...handlers)
httpServer.listen(9090)
```

#### B. Apply as a middleware
#### Option 2: Middleware

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

```ts
import { rest } from 'msw'
import { http, HttpResponse } from 'msw'
import { createServer } from '@mswjs/http-middleware'

const httpServer = createServer(
rest.get('/user', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }))
http.get('/user', () => {
return HttpResponse.json({ firstName: 'John' })
}),
)

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

```ts
import { rest } from 'msw'
import { http, HttpResponse } from 'msw'
import { createMiddleware } from '@mswjs/http-middleware'

const app = express()

app.use(
createMiddleware(
rest.get('/user', (req, res, ctx) => {
return res(ctx.json({ firstName: 'John' }))
http.get('/user', () => {
return HttpResponse.json({ firstName: 'John' })
}),
),
)
Expand Down

0 comments on commit e980211

Please sign in to comment.