Skip to content

Conversation

@miyaji255
Copy link
Contributor

@miyaji255 miyaji255 commented Oct 31, 2025

Summary

This pull request enhances the type safety and expressiveness of the client-side URL generation in the Hono framework. The main improvement is the introduction of a new TypedURL type, which allows the client to generate URLs with strongly-typed protocol, host, port, path, and query parameters based on the application's route schema and the provided base URL. Several generics and utility types are introduced to propagate this type information throughout the client, ensuring that URL generation is accurate and type-checked at compile time.

esbuild v0.15.18 doesn't support const type parameters, so I updated esbuild.

Motivation

I wanted to add types so that keys used with SWR would be easy to reuse in other parts of the codebase. However, $url did not preserve that type information, so I created this PR.

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

@codecov
Copy link

codecov bot commented Oct 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.47%. Comparing base (971106d) to head (372d90d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4502   +/-   ##
=======================================
  Coverage   91.47%   91.47%           
=======================================
  Files         172      172           
  Lines       11143    11143           
  Branches     3224     3224           
=======================================
  Hits        10193    10193           
  Misses        949      949           
  Partials        1        1           

☔ 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.

@yusukebe
Copy link
Member

yusukebe commented Nov 1, 2025

Hi @miyaji255

This is super interesting. I'll look at it deeply later.

@yusukebe yusukebe changed the title fix(types): make Hono client's $url return the exact URL type feat(types): make Hono client's $url return the exact URL type Nov 12, 2025
@yusukebe
Copy link
Member

Sorry for being late. Anyway, this change is not a "fix", but a "feat". It's a new feature.

.get('foo/bar', (c) => c.json({}))
.get('foo/:id/baz', (c) => c.json({}))
const client = hc<typeof app>('')
const client = hc<typeof app, 'http://localhost'>('http://localhost')
Copy link
Member

Choose a reason for hiding this comment

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

Can we go without the second argument like this?

const client = hc<typeof routes>('http://localhost/')

Or impossible caused by a TypeScript limitation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeScript requires you to specify all other generics if you specify one generic. If you want to solve this problem, you can solve it by currying generics like this.

const hcStrict = <T extends Hono<any, any, any>>() =>
  <const Prefix extends>(prefix: Prefix, options?: ClientOptions) => hc<T, Prefix>(prefix, options)

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