-
-
Notifications
You must be signed in to change notification settings - Fork 850
feat(types): make Hono client's $url return the exact URL type #4502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Hi @miyaji255 This is super interesting. I'll look at it deeply later. |
|
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') |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
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
TypedURLtype, 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.
bun run format:fix && bun run lint:fixto format the code