feat: make TanStack Query plugin respect responseStyle: 'fields'#3662
feat: make TanStack Query plugin respect responseStyle: 'fields'#3662JorrinKievit wants to merge 7 commits intohey-api:mainfrom
Conversation
|
|
|
Someone is attempting to deploy a commit to the Hey API Team on Vercel. A member of the Team first needs to authorize it. |
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #3662 +/- ##
==========================================
- Coverage 39.39% 39.23% -0.16%
==========================================
Files 520 520
Lines 19279 19383 +104
Branches 5714 5722 +8
==========================================
+ Hits 7595 7605 +10
- Misses 9445 9529 +84
- Partials 2239 2249 +10
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:
|
@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: |
|
Reviewed PR #3662. Found a critical runtime/type mismatch bug where the default Task list (5/5 completed)
![]() |
There was a problem hiding this comment.
Thanks for working on this — the feature is genuinely useful and the overall design (plugin-level config + per-query TStyle override) is solid. There is one critical runtime bug and a few other issues that need addressing before this can merge.
Critical: The runtime conditional options?.responseStyle === 'fields' does not match when responseStyle is undefined (the common case when the user omits it), yet the type-level default for TStyle is 'fields'. This causes a type-safety violation where TypeScript thinks the return is { data, request, response } but the actual runtime value is just the unwrapped data.
Other issues: a regression where .export(plugin.config.mutationOptions.exported) was changed to .export(), duplicate imports, and heavy code duplication across three files that could be reduced significantly.
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/mutationOptions.ts
Outdated
Show resolved
Hide resolved
packages/openapi-ts/src/plugins/@tanstack/query-core/v5/plugin.ts
Outdated
Show resolved
Hide resolved
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
8889ad1 to
9086190
Compare
|
TL;DR — Adds a Key changes
Summary | 286 files | 3 commits | base:
|
| Code path | SDK call includes responseStyle |
Return value | Generic types |
|---|---|---|---|
'data' (default) |
No | data or destructured { data } |
TData, TError |
'fields' |
responseStyle: 'fields' |
Conditional on options.responseStyle |
ResponseResult<TData, TStyle>, ResponseError<TError, TStyle> |
queryOptions.ts · mutationOptions.ts · infiniteQueryOptions.ts
Enriched error throwing in client bundles
Before: When
throwOnErrorwas true, all client bundles threwfinalErrordirectly.
After: Ifopts.responseStyle === 'fields', the client throws{ error: finalError, ...result }instead, giving the catch handler access to therequest,response, and other result fields alongside the error.
This change is applied identically to client-angular, client-fetch, client-ky, and client-ofetch bundles.
Test coverage
Before: No test scenario for TanStack Query with
responseStyle: 'fields'.
After: A new test config inplugins.test.tsgenerates@tanstack/react-queryoutput withresponseStyle: 'fields', producing snapshot suites for all three OpenAPI spec versions (2.0.x, 3.0.x, 3.1.x).

Problem
When using the TanStack Query plugin, there's no way to access the HTTP
Responseobject — status codes, headers, or request metadata. The plugin always destructures{ data }from the SDK response and discards everything else, even whenresponseStyle: 'fields'is configured.This makes it impossible to:
X-Total-Count, rate-limit headers)QueryCache.onErrorRelated issues: #3628, #3632, #2070, #1762
Solution
This PR makes the TanStack Query plugin actually respect
responseStyle. A newresponseStyleconfig option is added to the plugin. I decided to have "data" as the default, since that kinda is happening right now for the plugin.Plugin-level config
Per-query override
Each generated function accepts a generic
TStyleparameter, so you can override per-query without changing the global default:Also works the other way — if the plugin default is
'fields', you can override specific queries back to'data'.Discussion point
For me the primary reason is being able to access the response type on Errors so I can handle them globally. But also included it for succesfull responses because I saw other issues.
What I don't like is that I kinda want to enable it globally, but only for Errors and not for the data itself. Because that would mean I am gonna have nested data objects everywhere 🤔