-
import { type ApiRoutes } from '@server/index';
import { hc } from 'hono/client';
const token = localStorage.getItem("token");
const client = hc<ApiRoutes>("/", {
headers: {
"Authorization": "Bearer " + token,
}
});
// need interceptor for request and response
export const api = client.api; |
Beta Was this translation helpful? Give feedback.
Answered by
GGTMiku
Aug 2, 2024
Replies: 1 comment 1 reply
-
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 | URL, requestInit?: RequestInit) => {
return kyapi(`${baseUrl}${input}`, {
method: requestInit?.method,
headers: {
Authorization: "Bearer " + token,
"content-type": "application/json",
...requestInit?.headers,
},
body: requestInit?.body,
});
},
});
export const api = client.api; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
GGTMiku
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got it