How do I access the response headers?
I have a mutation which returns a Content-Disposition header with the filename, etc. But my generated mutation returns only a string, any way to get access to the response headers? Thanks for the great package!
@atresnjo
The mutations generated by this library do not return headers. Instead of using the generated mutation hook, create your own hook. The following is an example using the axios client. (The problem is that the types generated by the Hey API do not have headers, though.)
import { addPet } from "../openapi/requests/services.gen";
import {
AddPetMutationResult,
UseFindPetsKeyFn,
} from "../openapi/queries";
import { AxiosError } from "axios";
import { AddPetData, AddPetError } from "../openapi/requests/types.gen";
// create your own hook
const { mutate, isError } = useMutation<
AddPetMutationResult,
AxiosError<AddPetError>,
AddPetData
>({
mutationKey: [useAddPetKey],
mutationFn: (options) => addPet(options),
});
// mutation
mutate(
{
body: { name: text },
},
{
onSuccess: (data) => {
const contentDisposition = data.headers["content-disposition"];
// do something
},
}
);
@7nohe are there any plans to expose the response headers on the queries and mutartions at some point?
What is the reason for hiding the headers from the result of the queries and mutations?
Headers can carry important information, like x-total-count for pagination.
You can get headers in the generated response by describing the headers as part of the response on the API spec.
https://swagger.io/specification/ see Response Object and the Header Object.
@seriouslag Could you please further explain that, or do you have an example? I don't get how that should work.
@7nohe Having the same issue. Need access to headers response.
@seriouslag do you have any working example to share?
thanks
same issue, need to read Content-Disposition for filename
@seriouslag please stop spreading false information. This is the second time you’ve claimed something that doesn’t exist and misled people in the wrong direction.