-
-
Notifications
You must be signed in to change notification settings - Fork 639
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
How to use hono/client
with a different fetch client
#3699
Comments
hono/client
with a diffent fetch client
hono/client
with a diffent fetch clienthono/client
with a different fetch client
A temporary workaround is a helper function that can be passed into the import type { ClientResponse } from "hono/client"
import { hc } from "hono/client"
import { ofetch } from "ofetch"
import type { AppType } from "@api"
const client = hc<AppType>("http://localhost:3000", {
fetch: (input: RequestInfo | URL, requestInit?: RequestInit) => {
return ofetch(input instanceof URL ? input.toString() : input, requestInit)
},
})
function infere<T extends ClientResponse<any>>(data: T) {
type JsonData = Awaited<ReturnType<(typeof data)["json"]>>
return data as any as JsonData
}
// res is now typed correctly
const res = await client.api.auth.me.$get().then(infere) I hope to find some time soon to explore a propper solution in the sourcecode. |
This would be awesome to have |
Having something like https://elysiajs.com/eden/fetch.html would be amazing. Type inference requires less work from TS server and the syntax is much better IMO, no need to learn anything, just use fetch syntax. See comparison: // client
const roleRes = await client.orgs[':orgId'].members.me.role.$get({
param: { orgId: organizationId },
})
// fetch
const { data, error } = await client('/api/orgs/:orgId/me/role', {
params: { orgId: organizationId },
}) |
What is the feature you are proposing?
I am trying to use
hc
withunjs/ofetch
. Ofetch makes it quite easy to implement a jwt refresh flow, retries, interceptor, etc.. Technically, it does work fine like so:The only issue is that the return of ofetch does not match hc's
ClientResponse
. This is due to the fact that ofetch unwraps the data by default. Soawait res.json()
is not neccessary.I do not see any way to update the hono's typing. I understand that hono is about working with web standards, but it would be super useful to work with a different client. Basically, what I would like to do is something like this:
This was already mentioned in #2542.
Are there any plans to make this possible or do you see a solution right away?
Thank you for the amazing project!
The text was updated successfully, but these errors were encountered: