hono icon indicating copy to clipboard operation
hono copied to clipboard

feat(client): Add `ApplyGlobalResponse` type helper for RPC Client

Open mohankumarelec opened this issue 1 month ago • 3 comments

Problem

Hono RPC client doesn't infer response types from global error handlers like app.onError() or global middlewares like app.use("*"). This means developers lose type safety for common error responses like 500 and so on when using the RPC client.

Solution

Added ApplyGlobalResponse<App, ResponseType, StatusCode, Format> helper that merges global error response types into all routes.

Usage:

const app = new Hono()
  .get('/users', (c) => c.json({ users: [...] }))
  .onError((err, c) => c.json({ error: err.message }, 500))

// Apply global error type
type AppWithErrors = ApplyGlobalResponse<typeof app, { error: string }, 500, 'json'>

const client = hc<AppWithErrors>('http://api.example.com')
// Now client knows about both success and error responses

The author should do the following, if applicable

  • [x] Add tests
  • [x] Run tests
  • [x] bun run format:fix && bun run lint:fix to format the code
  • [x] Add TSDoc/JSDoc to document the code

mohankumarelec avatar Nov 30 '25 14:11 mohankumarelec

Please let me know if the changes looks good, so that i can create a PR for the honojs/website as well so that the docs are updated. Thanks

mohankumarelec avatar Nov 30 '25 14:11 mohankumarelec

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 91.49%. Comparing base (b06005a) to head (580db4b). :warning: Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4556   +/-   ##
=======================================
  Coverage   91.49%   91.49%           
=======================================
  Files         172      172           
  Lines       11230    11230           
  Branches     3257     3257           
=======================================
  Hits        10275    10275           
  Misses        954      954           
  Partials        1        1           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Dec 01 '25 00:12 codecov[bot]

@mohankumarelec Thank you for the PR!

Hey @NamesMT What do you think of this feature? I think this is an interesting idea. But I don't know if the API is best or not.

yusukebe avatar Dec 11 '25 09:12 yusukebe