Skip to content

feat(plugin-axios,plugin-fetch): add returnType option to unwrap the resolved data - #818

Open
stijnvanhulle wants to merge 8 commits into
mainfrom
claude/unwrap-alternative-research-ga21xw
Open

feat(plugin-axios,plugin-fetch): add returnType option to unwrap the resolved data#818
stijnvanhulle wants to merge 8 commits into
mainfrom
claude/unwrap-alternative-research-ga21xw

Conversation

@stijnvanhulle

@stijnvanhulle stijnvanhulle commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

Adds returnType: 'full' | 'data' (default 'full') to @kubb/plugin-axios and @kubb/plugin-fetch, requested in kubb-labs/discussions#3952.

'data' unwraps a generated call down to the bare success body once throwOnError (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 sets throwOnError: false, since that path still needs error to tell success from failure.

pluginAxios({ returnType: 'data' })
// const pet = await getPetById({ path: { petId: 1 } }) // Pet, not { status, data, ... }

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: new ReturnTypeOption on Options/ResolvedOptions, threaded through the shared Operation and SdkClient builders. buildResultType picks RequestResult or the runtime's new UnwrappedResult type, and buildReturnStatement adds a .then((result) => (config.throwOnError ?? true ? result.data : result)) step for 'data'.
  • plugin-axios/plugin-fetch: default returnType = 'full', plus the matching UnwrappedResult runtime type in each templates/*.ts.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is for the docs (no release).

🤖 Generated with Claude Code

https://claude.ai/code/session_012DSVW79h2FdFDkoWWr6PwN

…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
@codesandbox

codesandbox Bot commented Sep 1, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@changeset-bot

changeset-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0b79872

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@kubb/plugin-axios Minor
@kubb/plugin-fetch Minor
@kubb/plugin-react-query Minor
@kubb/plugin-vue-query Minor
@kubb/plugin-swr Minor

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 1, 2026

Copy link
Copy Markdown
More templates

@kubb/plugin-axios

npm i https://pkg.pr.new/@kubb/plugin-axios@818

@kubb/plugin-cypress

npm i https://pkg.pr.new/@kubb/plugin-cypress@818

@kubb/plugin-faker

npm i https://pkg.pr.new/@kubb/plugin-faker@818

@kubb/plugin-fetch

npm i https://pkg.pr.new/@kubb/plugin-fetch@818

@kubb/plugin-mcp

npm i https://pkg.pr.new/@kubb/plugin-mcp@818

@kubb/plugin-msw

npm i https://pkg.pr.new/@kubb/plugin-msw@818

@kubb/plugin-react-query

npm i https://pkg.pr.new/@kubb/plugin-react-query@818

@kubb/plugin-redoc

npm i https://pkg.pr.new/@kubb/plugin-redoc@818

@kubb/plugin-swr

npm i https://pkg.pr.new/@kubb/plugin-swr@818

@kubb/plugin-ts

npm i https://pkg.pr.new/@kubb/plugin-ts@818

@kubb/plugin-vue-query

npm i https://pkg.pr.new/@kubb/plugin-vue-query@818

@kubb/plugin-zod

npm i https://pkg.pr.new/@kubb/plugin-zod@818

commit: 0b79872

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Size Change: +3.82 kB (+1.23%)

Total Size: 315 kB

📦 View Changed
Filename Size Change
packages/plugin-axios/dist/index.cjs 15.6 kB +335 B (+2.19%)
packages/plugin-axios/dist/index.js 15.2 kB +348 B (+2.35%)
packages/plugin-fetch/dist/index.cjs 15.6 kB +335 B (+2.19%)
packages/plugin-fetch/dist/index.js 15.2 kB +350 B (+2.36%)
packages/plugin-mcp/dist/index.cjs 10.1 kB +81 B (+0.81%)
packages/plugin-mcp/dist/index.js 9.62 kB +82 B (+0.86%)
packages/plugin-react-query/dist/index.cjs 18.2 kB +370 B (+2.08%)
packages/plugin-react-query/dist/index.js 17.6 kB +347 B (+2.01%)
packages/plugin-swr/dist/index.cjs 12.3 kB +370 B (+3.09%)
packages/plugin-swr/dist/index.js 11.8 kB +377 B (+3.29%)
packages/plugin-vue-query/dist/index.cjs 15.3 kB +439 B (+2.96%)
packages/plugin-vue-query/dist/index.js 14.7 kB +385 B (+2.69%)
ℹ️ View Unchanged
Filename Size
packages/plugin-axios/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-cypress/dist/index.cjs 5.71 kB
packages/plugin-cypress/dist/index.js 5.62 kB
packages/plugin-cypress/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-faker/dist/index.cjs 14.5 kB
packages/plugin-faker/dist/index.js 14.4 kB
packages/plugin-faker/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-fetch/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-mcp/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-msw/dist/index.cjs 5.81 kB
packages/plugin-msw/dist/index.js 5.72 kB
packages/plugin-msw/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-react-query/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-redoc/dist/index.cjs 2.21 kB
packages/plugin-redoc/dist/index.js 1.74 kB
packages/plugin-redoc/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-swr/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-ts/dist/index.cjs 23.8 kB
packages/plugin-ts/dist/index.js 23.1 kB
packages/plugin-ts/dist/rolldown-runtime-CNktS9qV.js 291 B
packages/plugin-vue-query/dist/rolldown-runtime-C0LytTxp.js 168 B
packages/plugin-zod/dist/index.cjs 19.7 kB
packages/plugin-zod/dist/index.js 19.6 kB
packages/plugin-zod/dist/rolldown-runtime-C0LytTxp.js 168 B

compressed-size-action

…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
…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
@coderabbitai

coderabbitai Bot commented Sep 2, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: 21208222-42fb-4a54-b97a-d43a6171f0cf

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@autofix-ci

autofix-ci Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Auto fix applied for linting and formating

stijnvanhulle pushed a commit that referenced this pull request Sep 2, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants