Skip to content
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

Hono RPC Client: add option for response parsing #3894

Open
jansepke opened this issue Feb 6, 2025 · 1 comment
Open

Hono RPC Client: add option for response parsing #3894

jansepke opened this issue Feb 6, 2025 · 1 comment
Labels
enhancement New feature or request.

Comments

@jansepke
Copy link

jansepke commented Feb 6, 2025

What is the feature you are proposing?

It would be great if the options of the hc() would allow to add a custom response parser to centralize common code that we want to be executed on every response, e.g.:

const client = hc<AppType>('', {
  handleResponse: async (res) => {
    if (!res.ok) throw Error("error")

   return await res.json()
  }
});

our current solution is to wrap every client call with a custom method that checks for errors and converts to json, e.g.
parseResponse(await client.myEndpoint.$get())

Would it be feasible to implement this as it will likely complicate typing the client methods?

@jansepke jansepke added the enhancement New feature or request. label Feb 6, 2025
@kylemclean
Copy link

If you just need the handling of !response.ok, then this is what I am doing:

const client = hc<AppType>('', {
  fetch: async (...args: Parameters<typeof fetch>) => {
    const response = await fetch(...args);
    if (!response.ok) throw new Error('Response not OK');
    return response;
  }
});

But there should be dedicated functionality for this so we can modify the response like you are doing and keep everything typesafe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

2 participants