Skip to content

How to use Hono RPC Client with interceptor? #3222

Answered by GGTMiku
GGTMiku asked this question in Q&A
Discussion options

You must be logged in to vote

I got it

import { type ApiRoutes } from "@server/index";
import { hc } from "hono/client";
import ky from "ky";

const token = localStorage.getItem("token");

const baseUrl = "http://localhost:8998";

const kyapi = ky.extend({
  hooks: {
    beforeRequest: [(request) => {}],
    afterResponse: [
      (_, __, response: Response) => {
        console.log(response);
        if (response.ok) {
          return response;
        } else if (response.status === 401) {
          // TODO 
          throw new Error(response.statusText);
        } else {
          throw new Error(response.statusText);
        }
      },
    ],
  },
});

const client = hc<ApiRoutes>(`/`, {
  fetch: (input: RequestInfo 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rmingon-unicell
Comment options

Answer selected by GGTMiku
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants