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
A way to pass RequestInit on hc instance #2542
Comments
Ahhhh, indeed, Next.js overrides So, will the type FetchOptions<ExtraTypes = Record<string, unknown>> = Omit<RequestInit, 'method' | 'body'> &
ExtraTypes |
I think it should work yes! For reference, here is how NextJs overrides |
Sorry for the late response. I've thought it. A method like client.index.$get(
{},
{
fetch(input, requestInit) {
return fetch(input, {
...requestInit,
next: {
revalidate: 3600,
},
})
},
}
) |
It works, but I think having to redeclare the entire fetch function every time is hurting the DX. Also one more thing since we are talking about fetch, as I said above I'm used to use I would like to propose a That type Requester<C = Record<string, never>> = <TResponse = unknown>(
input: RequestInfo,
init: RequestInit,
options?: C
) => Promise<TResponse> Here I would create a fetchRequester that would use ofetch instead of fetch: type ExcludedFetchOptions<ExtraTypes = Record<string, unknown>> = Omit<
FetchOptions,
'method' | 'body' | 'responseType'
> &
ExtraTypes
const fetchRequester: Requester = async <TResponse = unknown>(
input: RequestInfo,
init: RequestInit,
options?: ExcludedFetchOptions
) => {
return await ofetch<TResponse>(input, {
...init,
...options
})
}
const client = hc<AppType>('https://api.example.com', {
fetchRequester: fetchRequester
}) Here since we passed a const res = await hc.my.request.$get({}, options) If no fetchRequest is passed, it would use the default fetch: const client = hc<AppType>('https://api.example.com')
const res = await hc.my.request.$get({}, options) and Here I used The only thing missing now, would the ability to pass headers to the hono client initialization. type ExcludedRequestInit<ExtraTypes = Record<string, unknown>> = Omit<
RequestInit,
'method' | 'body' | 'responseType'
> &
ExtraTypes
type HonoClientParams = {
fetchRequester?: Requester
requestInit?: ExcludedRequestInit
}
const hc = <T>(basePath: string, params?: HonoClientParams) => {
// ...
}
// And then we could pass headers like this:
const client = hc<AppType>('https://api.example.com', {
requestInit: {
headers: {
Authorization: `Bearer ${token}`
}
}
}) In that way the requestInit from the hc init would be the default passed to each request, but they could be overriden in each request if needed, like with the In that way |
Thank you for suggesting and explaining!
The const client = hc<typeof app>('http://localhoste', {
fetch: (input, requestInit) => {
return fetch(input, {
...requestInit,
next: {
revalidate: 3600,
},
})
},
})
Does this mean that if you use const res = await hc.my.request.$get({}, options) // If fetchRequester uses ofetch, res is not Response?? We designed the client by |
@yusukebe Found this Issue with the same needs as @AlexisWalravens. I find that your suggestions satisfy the need, but I wonder if there's some sort of higher order abstraction that could make this easier? Like a separate hono client that accepts Next's custom fetch options and passes them downstream? export const client = hcNext<ApiType>('[baseUrl]');
//...
await client.your.route.here.$get({ query: {} }, { next: { tags: [...] } }) |
Hi @devjmetivier and @AlexisWalravens ! #2592 introduced const res = await client.about.me.$get(
{},
{
init: {
next: {
revalidate: 3600,
},
},
}
) Does it not satisfy your request? |
Works perfectly! Thanks 😁 |
Goood! I think we can close this issue. If you have any problem with this, feel free to re-open it. |
What is the feature you are proposing?
Hi,
I'm using NextJS and I would like to be able to pass the
RequestInit
param of the fetch instance on individual calls.Why ?
Because NextJS overrides
fetch
'sRequestInit
type to also include thenext
key to handle cache at the fetch level:For now we only can override the whole fetch function, but I would like to be able to only override it's params with maybe something like this:
It makes sense to me that since
hc
is just a fetch wrapper aroundfetch
we should be able to pass it the same params.For reference
I'm used to use
ofetch
(which is not NextJs related) which is a wrapper of fetch with some built in features like retries, errors on non 200/300 statusCode, json parsing, etc...And as
ofetch
just extendsRequestInit
params fromfetch
I can use thenext
key directly in it just like I would with the basicfetch
:Maybe we could extends
RequestInit
and exclude themethod
&body
keys since they are already defined by the hc method.Thanks for reading me,
And Keep up with the good stuff 🔥 !
The text was updated successfully, but these errors were encountered: