-
-
Notifications
You must be signed in to change notification settings - Fork 268
feat: add openapi-ts swr plugin #2956
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
base: main
Are you sure you want to change the base?
Conversation
|
|
|
|
@Pewww is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
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.
@Pewww don't delete this file please, we want to separate by major version
|
@Pewww |
| const statement = $.const(symbolUseQueryFn.placeholder) | ||
| .export(symbolUseQueryFn.exported) | ||
| .$if( | ||
| plugin.config.comments && createOperationComment({ operation }), | ||
| (c, v) => c.describe(v as Array<string>), | ||
| ) | ||
| .assign( | ||
| $.func().do( | ||
| $(symbolUseSwr.placeholder) | ||
| .call( | ||
| $.literal(operation.path), | ||
| $.func() | ||
| .async() | ||
| .do(...statements), | ||
| ) | ||
| .return(), | ||
| ), | ||
| ); |
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.
@Pewww did you see this? it's much cleaner than the tsc module. Try to use it where possible please and let me know which things can't be ported yet. I'm yet to improve some of the type interfaces for example, but you should be already able to cut 10-30% of lines per file
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’ll check it and make the necessary revisions.
I’ll also go through the rest of the comments!
|
@Pewww the only other question I have is why and how you decided to use that query key design! |
|
@mrlubos Sorry it took a bit of time to finish the revisions. Regarding the current query key design: as far as I understand, SWR allows any serializable value as a key. Also, when injecting values from Separately, it looks like some tests are failing, but they don’t seem related to my changes. |
|
@Pewww Did you rebuild the package before running those commands? |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2956 +/- ##
==========================================
- Coverage 29.98% 29.78% -0.21%
==========================================
Files 409 413 +4
Lines 35547 35894 +347
Branches 2075 2075
==========================================
+ Hits 10658 10690 +32
- Misses 24862 25177 +315
Partials 27 27
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:
|
@mrlubos Oh, I missed it. Thanks. I’ve been thinking about it, handling In contrast, libraries like React Query seems to have a structure that makes it much easier for the plugin to handle such cases. |
SWR v2 Plugin PR Summary
Overview
New SWR plugin for
@hey-api/openapi-tsthat generates type-safe SWR hooks and utilities from OpenAPI specifications. This plugin follows SWR's official best practices and recommended patterns.Key Features
1. Core Functionality
2. Architecture
Plugin Structure
Key Design Decisions
SWR Key Pattern (v2)
['/api/users']['/api/users/{id}', options]Query Options (useSWR)
keyproperty using registered SWR key functionfetcherproperty with async SDK function callthrowOnError: trueto SDK callsresponseStyleconfig (data vs object)Mutation Options (useSWRMutation)
['/api/users']or['/api/users/{id}', options]async (_key, { arg }) => ...Infinite Options (useSWRInfinite)
getKeyfunction signature matches SWR requirements[path, { ...options, query: { ...query, page: pageIndex } }]3. Configuration Options
4. Generated Code Examples
Simple Query (No Params)
Query with Required Params
Mutation
Infinite Query with Pagination
Implementation Details
Dependencies
@hey-api/sdk- Required for SDK function generation@hey-api/typescript- Required for TypeScript type generationaxios(external) - For AxiosError type importSpecial Handling
eslint-disablefor unused vars to avoid formatter issuesTesting
Test Coverage
packages/openapi-ts-tests/main/test/plugins/swr/Snapshot Tests
Usage Example
Documentation Status
docs/openapi-ts/plugins/swr.mdPR Checklist
Code Changes
Testing
Breaking Changes
None - This is a new plugin, no breaking changes expected.
Migration Notes
If users were using an unofficial or custom SWR integration, they should:
swrplugin to their configswr.gen.tsRelated Issues
Please review the PR and let me know if anything needs to be revised.
I’ve confirmed that the basic queries and mutations work well, but I don’t think the current spec is perfect yet.
Since there are naturally areas to improve, if this PR is merged, I’d like to cautiously suggest releasing it without adding it to the docs for now.
Thank you for the great library!