OpenAPI-MSW
A tiny, type-safe wrapper around MSW to add support for full
type inference from OpenAPI schema definitions that are generated with
OpenAPI-TS.
Installation
You can install OpenAPI-MSW with this shell command:
npm i -D openapi-msw
Note: This package has a peer-dependency to MSW v2. There is no plan to
provide backwards compatibility for MSW v1.
Usage Guide
This guide assumes that you already have OpenAPI-TS set up and configured to
generate paths
definitions. If you have not set it up, please refer to the
OpenAPI-TS setup guide before
continuing with this usage guide.
Getting Started
Once you have your OpenAPI schema types ready-to-go, you can use OpenAPI-MSW to
create an enhanced version of MSW's http
object. The enhanced version is
designed to be almost identical to MSW in usage. Using the http
object created
with OpenAPI-MSW enables multiple type-safety and editor suggestion benefits:
- Paths: Only accepts paths that are available for the current HTTP method
- Params: Automatically typed with path parameters in the current path
- Request Body: Automatically typed with the request-body schema of the
current path
- Response: Automatically forced to match the response-body schema of the
current path
import { HttpResponse } from "msw";
import { createOpenApiHttp } from "openapi-msw";
import type { paths } from "./your-openapi-schema";
const http = createOpenApiHttp<paths>();
const getHandler = http.get("/resource/{id}", ({ params }) => {
const id = params.id;
return HttpResponse.json({ id });
});
const getHandler = http.post("/resource", async ({ request }) => {
const data = await request.json();
return HttpResponse.json({ ...data });
});
const otherHandler = http.get("/unknown", () => {
return new HttpResponse();
});
Provide a Base URL for Paths
You can provide an optional base URL to createOpenApiHttp
, which is prepended
to all paths. This is especially useful when your application calls your API on
a subpath or another domain. The value can be any string that is resolvable by
MSW.
const http = createOpenApiHttp<paths>({ baseUrl: "/api/rest" });
export const getHandler = http.get("/resource", () => {
return HttpResponse.json();
});
Handling Unknown Paths
MSW handlers can be very flexible with the ability to define wildcards (*) in a
path. This can be very useful for catch-all handlers but clashes with your
OpenAPI spec, since it probably is not an endpoint of your API. To define
handlers that are unknown to your OpenAPI spec, you can access the original
http
object through http.untyped
.
const http = createOpenApiHttp<paths>();
const catchAll = http.untyped.all("/resource/*", ({ params }) => {
return HttpResponse.json();
});
Alternatively, you can import the original http
object from MSW and use that
one for unknown paths instead.
License
This package is published under the MIT license.