H
Hono13mo ago
ForkMeDaddy

Client Response

I have a problem where what my API returns is not what the TS types say it is: For example what I actually receive when logging res is simply an object that looks like this {email, message}. But there is no ok, json() that my IDE says there should be. This is the route:
const ResponeSchema = z.object({
message: z.string(),
email: z.string().email(),
});
export const secret = new OpenAPIHono<{
Variables: ContextVariables;
}>().openapi(
createRoute({
method: "get",
path: "/api/secret",
tags: ["Secret"],
summary: "Shhh...",
responses: {
200: {
description: "Success",
content: {
"application/json": {
schema: ResponeSchema,
},
},
},
},
}),
(c) => {
const user = c.get("user")!;
return c.json(
{
message: "Secret Message",
email: user.email,
},
200
);
}
);
const ResponeSchema = z.object({
message: z.string(),
email: z.string().email(),
});
export const secret = new OpenAPIHono<{
Variables: ContextVariables;
}>().openapi(
createRoute({
method: "get",
path: "/api/secret",
tags: ["Secret"],
summary: "Shhh...",
responses: {
200: {
description: "Success",
content: {
"application/json": {
schema: ResponeSchema,
},
},
},
},
}),
(c) => {
const user = c.get("user")!;
return c.json(
{
message: "Secret Message",
email: user.email,
},
200
);
}
);
No description
6 Replies
Aditya Mathur
Aditya Mathur13mo ago
Seems to be working fine on my end
No description
ForkMeDaddy
ForkMeDaddyOP13mo ago
I am using ofetch as my fetch fn. could this be related?
export const client = hc<AppType>(getBaseUrl(), {
async fetch(input, requestInit, _, __) {
return $fetch(input instanceof URL ? input.toString() : input, {
...requestInit,
...(typeof window === "undefined"
? (await import("next/headers")).headers()
: {}),
});
},
});
export const client = hc<AppType>(getBaseUrl(), {
async fetch(input, requestInit, _, __) {
return $fetch(input instanceof URL ? input.toString() : input, {
...requestInit,
...(typeof window === "undefined"
? (await import("next/headers")).headers()
: {}),
});
},
});
ForkMeDaddy
ForkMeDaddyOP13mo ago
ok when I change it to this it works: (use normal fetch)
No description
ForkMeDaddy
ForkMeDaddyOP13mo ago
neat
Aditya Mathur
Aditya Mathur13mo ago
So is this issue resolved for you?
Angus
Angus3mo ago
maybe because ofetch will auto parse reponse to json,so res.json() not found ,ts don't now about this .... ref: https://github.com/honojs/hono/issues/3699 https://github.com/honojs/hono/issues/2542#issuecomment-2151608887 use ofetch and without res.json() https://github.com/RSSNext/Folo/blob/dev/patches/hono.patch
GitHub
How to use hono/client with a different fetch client · Issue #36...
What is the feature you are proposing? I am trying to use hc with unjs/ofetch. Ofetch makes it quite easy to implement a jwt refresh flow, retries, interceptor, etc.. Technically, it does work fine...
GitHub
Folo/patches/hono.patch at dev · RSSNext/Folo
🧡 Follow everything in one place. Contribute to RSSNext/Folo development by creating an account on GitHub.
GitHub
A way to pass RequestInit on hc instance · Issue #2542 · honojs/hono
What is the feature you are proposing? Hi, I&#39;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 f...

Did you find this page helpful?