feat(plugin-axios,plugin-fetch): add unwrap() to every generated call - #820
Open
stijnvanhulle wants to merge 3 commits into
Open
feat(plugin-axios,plugin-fetch): add unwrap() to every generated call#820stijnvanhulle wants to merge 3 commits into
stijnvanhulle wants to merge 3 commits into
Conversation
kubb-labs/discussions#3952 asked for a way to get a generated call's
success body without destructuring { data } at every call site.
Rather than a plugin-wide returnType default (see PR #818), take the
Redux-Toolkit approach: every call's promise gains an unwrap() method,
resolving to the bare success body or rejecting with `error`. Plain
`await getPetById(...)` keeps returning the full result, so existing
code and the query plugins (react-query, vue-query, swr, mcp) are
unaffected.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
🦋 Changeset detectedLatest commit: a79631d The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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 |
More templates
@kubb/plugin-axios
@kubb/plugin-cypress
@kubb/plugin-faker
@kubb/plugin-fetch
@kubb/plugin-mcp
@kubb/plugin-msw
@kubb/plugin-react-query
@kubb/plugin-redoc
@kubb/plugin-swr
@kubb/plugin-ts
@kubb/plugin-vue-query
@kubb/plugin-zod
commit: |
Contributor
|
Size Change: +1.22 kB (+0.39%) Total Size: 313 kB 📦 View Changed
ℹ️ View Unchanged
|
… in withUnwrap CI caught this on this repo's own examples/axios: an operation with no documented 2xx response (deletePet, 400 only) failed to typecheck. withUnwrap(request(callConfig)) inferred its generic from the client's own CallResult type, so the outer `as Unwrappable<RequestResult<...>>` cast carried two incompatible `.then` overloads and TypeScript rejected it as too narrow a conversion. Casting the call to RequestResult first, then wrapping, keeps withUnwrap's generic inferred as RequestResult directly, matching what the code did before this feature and avoiding the second cast entirely. Reproduced the exact failure in isolation, confirmed the fix, then verified end-to-end with a real `pnpm generate` against a built plugin-axios (this sandbox has no prebuilt dist, so pnpm generate otherwise fails to resolve the plugin). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
📄 Knowledge review🆕 New pages1 new page was drafted from this PR.
|
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
stijnvanhulle
pushed a commit
that referenced
this pull request
Sep 1, 2026
…a shared runtime helper /simplify's reuse, simplification, and altitude passes all converged on the same finding: the generated return statement inlined `config.throwOnError ?? true ? result.data : result` as a literal string, restating a default the runtime already applies internally, once per operation across every spec. The sibling `text/event-stream` path already centralizes its post-processing in a runtime function (toEventStream); this does the same for returnType: 'data' with a new unwrapResult(promise, throwOnError) exported from both templates. Verified with a real `pnpm generate` against a built plugin-axios, covering the no-2xx-response edge case that broke CI on PR #820. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🎯 Changes
An alternative to the
returnTypeoption in #818, requested in kubb-labs/discussions#3952: a way to get a generated call's success body without destructuring{ data }at every call site.This takes the Redux Toolkit approach instead of a plugin-wide default. Every generated call (standalone function or SDK method) now resolves to a promise with an extra
unwrap()method.unwrap()resolves to the bare success body, or rejects witherrorfor a result that carried one (only reachable withthrowOnError: false, since the throwing path never resolves an error).await getPetById(...)still resolves to the full{ status, data, error, contentType, request, response }result, exactly as before. There's no option to turn this on or off, it's just there. That also means it composes cleanly with the query plugins:@kubb/plugin-react-query,@kubb/plugin-vue-query,@kubb/plugin-swr, and@kubb/plugin-mcpall call the generated client directly andawaitit, so their generated hooks are byte-for-byte unchanged.Built independently of #818's
returnTypeoption (this branch is offmain, not stacked on it), so the two designs can be reviewed and land separately.How it works
plugin-axios/plugin-fetchruntime templates (templates/*.ts) addUnwrapResult,Unwrappable, and awithUnwrap()helper that attaches.unwrap()to a call's result promise.internals/client:buildResultTypenow always wrapsRequestResultinUnwrappable, andbuildReturnStatementwraps every call inwithUnwrap(...). Both the standalone functions and the class-based SDK pick this up automatically.tests/3.0.xe2e snapshot and package-level generator snapshot that embeds a generated operation or.kubb/client.tswas regenerated. I checked the diff by hand: it's mechanical.✅ Checklist
pnpm run test.🚀 Release Impact
🤖 Generated with Claude Code
https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN