-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add installation and complete usage guide #9
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78e8e3b
docs: add installation and usage guide
christoph-fricke 1b6bc26
chore: add changeset
christoph-fricke 0021efc
docs: apply most review feedback
christoph-fricke 0bf3d04
docs: rename OpenApi to OpenAPI
christoph-fricke fa52160
chore: rename OpenApi to OpenAPI in source code
christoph-fricke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-msw": minor | ||
--- | ||
|
||
Added installation and complete usage guide to the documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,104 @@ | ||
# openapi-msw | ||
<h1 align="center">OpenAPI-MSW</h1> | ||
|
||
A tiny, type-safe wrapper around [MSW](https://mswjs.io) to add support for full | ||
type inference from OpenAPI schema definitions that are generated with | ||
[OpenAPI-TS](https://openapi-ts.pages.dev/introduction). | ||
|
||
## Installation | ||
|
||
You can install OpenAPI-MSW with this shell command: | ||
|
||
```bash | ||
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](https://openapi-ts.pages.dev/introduction) 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 | ||
|
||
```typescript | ||
import { HttpResponse } from "msw"; | ||
import { createOpenApiHttp } from "openapi-msw"; | ||
// 1. Import the paths from your OpenAPI schema definitions | ||
import type { paths } from "./your-openapi-schema"; | ||
|
||
// 2. Provide your paths definition to enable the above benefits during usage | ||
const http = createOpenApiHttp<paths>(); | ||
|
||
// TS only suggests available GET paths | ||
const getHandler = http.get("/resource/{id}", ({ params }) => { | ||
const id = params.id; | ||
return HttpResponse.json({ id /* ... more response data */ }); | ||
}); | ||
|
||
// TS only suggests available POST paths | ||
const getHandler = http.post("/resource", async ({ request }) => { | ||
const data = await request.json(); | ||
return HttpResponse.json({ ...data /* ... more response data */ }); | ||
}); | ||
|
||
// TS shows an error when "/unknown" is not defined in the OpenAPI schema paths | ||
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. | ||
|
||
```typescript | ||
const http = createOpenApiHttp<paths>({ baseUrl: "/api/rest" }); | ||
|
||
// Requests will be matched by MSW against "/api/rest/resource" | ||
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`. | ||
|
||
```typescript | ||
const http = createOpenApiHttp<paths>(); | ||
|
||
// Fallback to MSW's original implementation and typings | ||
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](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opinionated structuring idea: Add a "Prerequisites" section before installation, containing this note and the first paragraph of the usage guide. That way, its clear what needs to be done before installing this package makes sense. Also, the "Usage Guide > Getting Started" subsection title might obsolete then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am considering other changes to the docs as well, such as a list of key features/achievements in in the beginning and and outlining the motivation behind openapi-msw, i.e. errors for spec conflicts across the stack (app + test code). I will keep this idea in mind and come back to it when I make other improvements to the docs as well.