You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[MSW](https://mswjs.io) is an API mocking library that allows you to write client-agnostic mocks and reuse them across any frameworks, tools, and environments.
17
14
15
+
The MSW plugin for Hey API generates type-safe mock handler factories from your OpenAPI spec, removing the tedious work of defining mock endpoints and ensuring your mocks stay in sync with your API.
16
+
17
+
## Features
18
+
19
+
- type-safe mock handlers generated from your OpenAPI spec
20
+
- seamless integration with `@hey-api/openapi-ts` ecosystem
21
+
- support for static response values or custom MSW resolver functions
22
+
- typed path parameters and request bodies
23
+
- automatic base URL inference from OpenAPI `servers` field
24
+
- minimal learning curve thanks to extending the underlying technology
25
+
26
+
## Installation
27
+
28
+
::: warning
29
+
MSW plugin requires `msw@^2` as a peer dependency. Make sure to install it in your project.
30
+
:::
31
+
32
+
In your [configuration](/openapi-ts/get-started), add `msw` to your plugins and you'll be ready to generate MSW artifacts. :tada:
33
+
34
+
```js
35
+
exportdefault {
36
+
input:'hey-api/backend', // sign up at app.heyapi.dev
37
+
output:'src/client',
38
+
plugins: [
39
+
// ...other plugins
40
+
'msw', // [!code ++]
41
+
],
42
+
};
43
+
```
44
+
45
+
## Output
46
+
47
+
The MSW plugin will generate a `msw.gen.ts` file containing the following artifacts.
48
+
49
+
### Handler Factory
50
+
51
+
The `createMswHandlerFactory` function is the main export. It returns an object with an `of` property containing a handler creator for each operation in your spec.
baseUrl: 'http://localhost:8000', // optional, inferred from spec servers
58
+
});
59
+
```
60
+
61
+
If your OpenAPI spec defines a `servers` field, the base URL will be inferred automatically. You can override it by passing `baseUrl` in the configuration.
62
+
63
+
### Handler Creators
64
+
65
+
Each operation becomes a handler creator on the `of` object. Handler creators accept either a static response value or a custom MSW `HttpResponseResolver` function.
66
+
67
+
## Usage
68
+
69
+
### Static Response
70
+
71
+
The simplest way to mock an endpoint is to provide a static response value. The value is type-checked against the operation's response type.
Path parameters are typed as `string | ReadonlyArray<string>` because MSW normalizes all path parameters to strings. Use `Number()` or similar conversions if you need numeric values.
117
+
:::
118
+
119
+
### Operations Without Responses
120
+
121
+
For operations that don't define a response type, the handler creator accepts `undefined` or a custom resolver function.
- Query parameters are not typed in the resolver. MSW doesn't provide typed query params natively — use `new URL(request.url).searchParams` instead.
134
+
- The response type generic is omitted from `HttpResponseResolver` to avoid MSW's `DefaultBodyType` constraint issues with union and void response types.
135
+
136
+
## API
137
+
138
+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/msw/types.ts) interface.
0 commit comments