Skip to content

fix(client-fetch): type response as Response | undefined in error interceptors#3750

Open
inas-sirhan wants to merge 3 commits intohey-api:mainfrom
inas-sirhan:fix/error-interceptor-response-type
Open

fix(client-fetch): type response as Response | undefined in error interceptors#3750
inas-sirhan wants to merge 3 commits intohey-api:mainfrom
inas-sirhan:fix/error-interceptor-response-type

Conversation

@inas-sirhan
Copy link
Copy Markdown
Contributor

Fixes #3519

The ErrInterceptor type and RequestResult type incorrectly typed response as always-defined Response. When fetch() throws (network failure, AbortError, etc.), the error interceptor receives undefined for response, and the error result also returns undefined for response.

Changes:

  • ErrInterceptor: response parameter is now Res | undefined
  • RequestResult (non-throw error branch): response is now Response | undefined
  • Removes undefined as any casts in the client implementation

…erceptors and error results

When fetch() throws (network failure, AbortError, etc.), the error
interceptor is called with undefined for the response parameter, and the
error result also has an undefined response. Previously, both the
ErrInterceptor type and RequestResult type incorrectly typed response as
always-defined Response, requiring unsafe `as any` casts.

This change makes the types honest:
- ErrInterceptor: response parameter is now Res | undefined
- RequestResult: response in the error branch is now Response | undefined
- Removes `undefined as any` casts in the client implementation

Closes hey-api#3519
@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 13, 2026

🦋 Changeset detected

Latest commit: 9d97fb3

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

This PR includes changesets to release 1 package
Name Type
@hey-api/openapi-ts Patch

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

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 13, 2026
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 13, 2026

@inas-sirhan is attempting to deploy a commit to the Hey API Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the bug 🔥 Broken or incorrect behavior. label Apr 13, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 40.04%. Comparing base (5e1eaea) to head (9d97fb3).
⚠️ Report is 117 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3750      +/-   ##
==========================================
+ Coverage   38.38%   40.04%   +1.65%     
==========================================
  Files         520      520              
  Lines       19228    19243      +15     
  Branches     5689     5720      +31     
==========================================
+ Hits         7380     7705     +325     
+ Misses       9587     9342     -245     
+ Partials     2261     2196      -65     
Flag Coverage Δ
unittests 40.04% <ø> (+1.65%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pullfrog
Copy link
Copy Markdown
Contributor

pullfrog bot commented Apr 13, 2026

TL;DR — When a fetch() call throws (network failure, AbortError, etc.), the error interceptors and error results receive undefined for response. This PR updates the @hey-api/client-fetch plugin types to reflect that reality instead of hiding it behind as any casts, and updates example tests to use optional chaining accordingly.

Key changes

  • Type response as Response | undefined in error interceptors and error results — The ErrInterceptor type, the RequestResult error branch, and the runtime error-path code all now correctly model that response can be undefined when the error originates from a thrown exception rather than an HTTP response.
  • Remove as any casts from error path — The undefined as any workarounds in createClient are replaced with plain undefined, now that the types accept it.
  • Update example tests to use optional chaining on response — Tests in the Fastify and NestJS examples now access result.response?.status instead of result.response.status to align with the new Response | undefined type.
  • Regenerate examples and update snapshots — All test snapshots and example generated files are regenerated to reflect the new type signatures and removed casts.

Summary | 510 files | 3 commits | base: mainfix/error-interceptor-response-type

Before: response was typed as Response in error results and Res in error interceptors — always defined — and the runtime used undefined as any to work around the mismatch when no Response existed.
After: response is typed as Response | undefined in error results and Res | undefined in error interceptors, and the runtime passes plain undefined without casts.

The core change is 4 single-line edits across 3 source files in the client-fetch bundle. When fetch() throws before a Response is available, the error path now honestly types response as potentially undefined, giving consumers proper type narrowing instead of a lie hidden behind as any. The third commit updates example test assertions to use optional chaining (response?.status) so they compile cleanly against the stricter type.

.changeset/fix-error-interceptor-response-type.md · bundle/client.ts · bundle/types.ts · bundle/utils.ts · pets.test.ts (fastify) · pets.test.ts (nestjs)

Pullfrog  | View workflow run | Triggered by Pullfrog𝕏

Copy link
Copy Markdown
Contributor

@pullfrog pullfrog bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — no issues found.

The fix correctly types response as Response | undefined in error interceptors and error results, matching the runtime behavior when fetch() throws (network failure, AbortError, etc.). The undefined as any casts are properly removed since the types now accommodate undefined. The ThrowOnError extends true branch is correctly left untouched — when throwOnError is enabled, fetch exceptions are re-thrown and the promise never resolves, so response: Response remains accurate there.

Note: client-angular has a similar pattern where result.response starts as null and uses as any in its error path — that could be addressed in a follow-up.

Task list (4/4 completed)
  • Read and analyze the PR diff
  • Investigate source changes to client-fetch types and implementation
  • Verify snapshot changes are consistent with source changes
  • Self-critique drafted comments and submit review

Pullfrog  | View workflow run𝕏

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Apr 13, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 13, 2026

Open in StackBlitz

@hey-api/codegen-core

npm i https://pkg.pr.new/@hey-api/codegen-core@3750

@hey-api/json-schema-ref-parser

npm i https://pkg.pr.new/@hey-api/json-schema-ref-parser@3750

@hey-api/nuxt

npm i https://pkg.pr.new/@hey-api/nuxt@3750

@hey-api/openapi-ts

npm i https://pkg.pr.new/@hey-api/openapi-ts@3750

@hey-api/shared

npm i https://pkg.pr.new/@hey-api/shared@3750

@hey-api/spec-types

npm i https://pkg.pr.new/@hey-api/spec-types@3750

@hey-api/types

npm i https://pkg.pr.new/@hey-api/types@3750

@hey-api/vite-plugin

npm i https://pkg.pr.new/@hey-api/vite-plugin@3750

commit: 9d97fb3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🔥 Broken or incorrect behavior. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorectly typed error interceptor of fetch-client

1 participant