feat(plugin-axios,plugin-fetch): add returnType option to unwrap the resolved data - #818
Open
stijnvanhulle wants to merge 5 commits into
Open
feat(plugin-axios,plugin-fetch): add returnType option to unwrap the resolved data#818stijnvanhulle wants to merge 5 commits into
stijnvanhulle wants to merge 5 commits into
Conversation
…resolved data
Discussion kubb-labs/discussions#3952 asked for a way to get the bare
success body back from a generated call instead of the full
{ status, data, error, contentType, request, response } result. Add
returnType: 'full' | 'data' (default 'full') to plugin-axios and
plugin-fetch, applying to both the standalone functions and the
class-based SDK.
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: f2bf4a4 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 |
3 tasks
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.37 kB (+0.44%) Total Size: 313 kB 📦 View Changed
ℹ️ View Unchanged
|
…type The plugin-axios and plugin-fetch runtime templates gained an UnwrappedResult type for returnType: 'data' (PR #818). The tests/3.0.x suite snapshots the generated .kubb/client.ts verbatim, so every consumer's snapshot (axios, fetch, mcp, react-query, swr, vue-query) needed the same addition. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
4 tasks
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
…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
unwrapResult is a pure function over a promise, so it doesn't need the axios/fetch client mocking machinery. Call it directly with a resolved promise instead. 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
Adds
returnType: 'full' | 'data'(default'full') to@kubb/plugin-axiosand@kubb/plugin-fetch, requested in kubb-labs/discussions#3952.'data'unwraps a generated call down to the bare success body oncethrowOnError(on by default) rules out the error branch. It falls back to the full{ status, data, error, contentType, request, response }result for a call that setsthrowOnError: false, since that path still needserrorto tell success from failure.Applies to both the standalone functions and the class-based SDK. It doesn't change
@kubb/plugin-react-query,@kubb/plugin-vue-query, or@kubb/plugin-swr, which call the client directly and expect the full result, and the docs note that limitation explicitly.How it works
internals/client: newReturnTypeOptiononOptions/ResolvedOptions, threaded through the sharedOperationandSdkClientbuilders.buildResultTypepicksRequestResultor the runtime's newUnwrappedResulttype, andbuildReturnStatementadds a.then((result) => (config.throwOnError ?? true ? result.data : result))step for'data'.plugin-axios/plugin-fetch: defaultreturnType = 'full', plus the matchingUnwrappedResultruntime type in eachtemplates/*.ts.✅ Checklist
pnpm run test.🚀 Release Impact
🤖 Generated with Claude Code
https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN