feat(plugin-axios,plugin-fetch): add returnType option to unwrap the resolved data - #818
feat(plugin-axios,plugin-fetch): add returnType option to unwrap the resolved data#818stijnvanhulle wants to merge 8 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: 0b79872 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 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: |
|
Size Change: +3.82 kB (+1.23%) Total Size: 315 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
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
…'s returnType
Query and mutation bodies always destructured `{ data }` off the client call, which broke once
the registered client plugin was configured with returnType: 'data' (the call already resolves
to the bare body). The three plugins now read returnType off resolveClientOperation and build
the matching body through a new shared buildCallResultBody helper.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Team Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Auto fix applied for linting and formating |
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
🎯 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