feat: implement msw plugin#3570
Conversation
|
|
|
Error agent completed without reporting progress |
|
@malcolm-kee is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
🦋 Changeset detectedLatest commit: 25fdbb9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3570 +/- ##
==========================================
- Coverage 40.08% 39.42% -0.67%
==========================================
Files 522 545 +23
Lines 19323 20459 +1136
Branches 5761 6175 +414
==========================================
+ Hits 7746 8065 +319
- Misses 9374 9989 +615
- Partials 2203 2405 +202
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
7a46406 to
bc978a9
Compare
|
@malcolm-kee Before I go into it, two questions:
|
bc978a9 to
dedf542
Compare
|
@mrlubos I come up with the API design and AI was doing most of the implementations while I watch. Not final. I'm happy to iterate on this, just want some progress on this plugin. |
|
The diff is big is mostly because of the tests and snapshots. |
dedf542 to
c7133e3
Compare
@hey-api/codegen-core
@hey-api/json-schema-ref-parser
@hey-api/nuxt
@hey-api/openapi-ts
@hey-api/shared
@hey-api/spec-types
@hey-api/types
@hey-api/vite-plugin
commit: |
c7133e3 to
b119b40
Compare
|
I did some refactoring/enhancements:
|
|
@mrlubos I manually validated the code and made some refactoring. It would be great if you can provide some feedbacks, especially on the API. |
ac3c300 to
b34b5bd
Compare
|
More revision:
|
b34b5bd to
9b8e509
Compare
|
Added |
9b8e509 to
1963185
Compare
|
Change plugin options from |
e3e18f0 to
7bb2ae4
Compare
|
Ideas on how to continue enhancing this PR, in case anyone want to take over this, since I might not be free to iterate on this: Implement
|
Add automatic bot filtering to update-contributors.sh: - Skip logins ending with [bot] suffix - Skip accounts with GitHub API type "Bot" Remove duplicate pullfrog[bot] entries from contributors-list.md. Closes hey-api#3764
feat: add z.input and z.output
…-options-headers-type fix(client-fetch): narrow headers to Headers in ResolvedRequestOptions
…ve-preview-7.x chore(deps): update dependency @typescript/native-preview to v7.0.0-dev.20260411.1
chore(deps): update dependency msw to v2.13.2
…plugins feat: warn when duplicate plugins are specified
|
Hi @mrlubos is there anything that I can do to help to make this ready for merge? |
|
@malcolm-kee the next step would be to finish the examples plugin #3772 The way I imagine it working is each component (schema, parameter, etc) generates a getter function which would return its example(s). If there are none, no function is generated. Operations would also generate a getter function, with the support for discrimination by status code. The MSW plugin would then need to call these functions and get the result. If the result is defined we'd return it, otherwise fall through. That's the last big change I planned to do so we can remove the hard-coded values and support replacing them with Faker and more in the future. I don't currently have time to polish it. If you want, open a pull request merging into the Examples plugin, I'd review it |

Closes #1486
Summary
Implement
mswplugin that generates amsw.gen.tsfile with type-safe mock handler factories from OpenAPI specs. Each operation is exported as a named handler creator (<operationId>Mock) with a wildcard base URL, plus agetAllMockshelper to generate handlers for all operations at once. AcreateMswHandlerFactoryfunction is also exported for custom base URL binding.Important
Even though many expect fake data generation is part of this plugin, that probably overlaps with faker plugin. The only mock data handled by this plugin at the moment is the
exampledefined in the OpenAPI spec.API Design
Configuration
Usage
Individual handler exports (wildcard base URL)
Handler options
MSW handler options can be passed as a second argument:
Custom base URL (
createMswHandlerFactory)All handlers (
getAllMocks)Design decisions
Why
<operationId>Mocknaming? — AppendingMockavoids naming collisions with other generated artifacts (types, SDK functions) while keeping the handler clearly associated with its operation.Why both individual exports and
createMswHandlerFactory? — Individual exports use a wildcard (*) base URL for zero-config convenience. The factory function allows binding to a specific base URL when needed (e.g. integration tests against a specific server).Why
valueSourcesinstead ofexample: boolean? — Extensible for future sources (e.g.['example', 'faker']when faker plugin is ready).onMissingMock— Operations that require a response argument (no default example) are either skipped ('skip') or return a 501 ('error'). Overrides always take precedence.Handler creator signatures
{ result, status? } | ToResponseUnion<Responses> | HttpResponseResolver<PathParams, Body>{ result, status? } | ToResponseUnion<Responses> | HttpResponseResolver<PathParams, Body>HttpResponseResolver<PathParams, Body>* Optional if the spec defines an
examplefor the dominant response.Response method selection
application/jsonHttpResponse.json()text/*HttpResponse.text()binary/octet-streamnew HttpResponse()new HttpResponse(null)When multiple 2xx responses exist, the dominant one is chosen by priority: json > text > binary > void.
Known limitations
HttpResponseResolverto avoid MSW'sDefaultBodyTypeconstraint issues with union/void response types