Open steinathan opened 1 week ago
Yeah I think this is more TanStack Query integration question as it will need a slightly different implementation to make this work 👀
Ok, i'll stick to using fetch then until @hey-api/client-axios
is available
the returning of an object of { data: DataType, error: ErrorType, request: Request, response: Response }
makes things quite hard to work with Tanstack Query, but it's totally doable. With regards to not throwing an error, I encountered this yesterday and just put this into a response interceptor, hope it helps!
client.interceptors.response.use(async (response, request) => {
const statusCategory = response?.status.toString()[0]
switch (statusCategory) {
case '4': {
throw new MyApiError()
}
case '5':
throw new MyApiError()
}
return response
})
@frdwhite24 Thanks a lot - your solution works for me
const setOpenAPIClient = () => {
const client = createClient({
baseUrl: import.meta.env["VITE_APP_API_URL"],
headers: {
Authorization: `Bearer ${idToken}`,
},
});
client.interceptors.response.use(async (response, _) => {
const statusCategory = response?.status?.toString()[0];
if (!statusCategory) {
throw new Error("Invalid response status");
}
const responseData = await response.clone().json();
const message = responseData?.detail;
if (["3", "4", "5"].includes(statusCategory)) {
const serverError = new ServerApiError(message);
console.error(message, statusCategory);
throw serverError;
}
return response;
});
};
Description
Using
@hey-api/client-fetch
does not actually show errors where they should be, see the comparison examples belowMy client (would love an axios client :) )
@hey-api/client-fetch ❌
Axios client ✅
OpenAPI specification (optional)
Configuration
System information (optional)
No response