Add support for generating code that makes accessing both headers and body data possible
This PR is a revival of https://github.com/ferdikoomen/openapi-typescript-codegen/issues/812. That issue was closed with the rationale that an OpenAPI spec response can only use one of the headers or the body, not both. As mentioned by others there, this isn't actually the case, so it'd be great if this library supported accessing both. As it is now, if you want to do that, you have to completely break out of the generated code and do it all yourself.
To centralize discussion, it'd be better if we could re-open the other issue and close this one, I just opened this issue to bring the discussion back to the fore.
Maybe just to add some real world use-case to this: our backend is sending an X-Request-ID header which contains a correlation id used to track logs associated to this ID. In case of an error happening in the backend we'd like to show this ID in the error message, such that we can get some more useful error feedback from the user.
the same +1
My use case is for an API with endpoints that are paginated, but the information about the pagination (specifically, next/prev first/last links) are only provided by the Links header.
+1 In real world you always want whole response. There should not be a problem to make one with typescript definition, which will include payload, status code, headers and other stuffs.
+1
This would be extremely helpful. We have a case where we need Blob and then we add X-Filename header for responses that return file. This would allow us to use swagger generated code and provide both, the file and filename to frontend.
I tested another library called ng-openapi-gen, which generates a request with only the body and then another request with the full response object.
I needed this in two parts of our app, one was to get the E-Tag header from the response when polling one endpoint. The other was to access some custom attributes in a file response, since the body was just the file the header contained additional info like file type.
I wrote this little wrapper for calling the generated methods to also get the response, since I use fetch here you might have to adapt it if you do not.
const requestIncludeResponseHeaders = async <T>(
method: () => Promise<T>
): Promise<{
body: T;
headers: Headers;
}> => {
let headers = new Headers();
// mock the global fetch method
const tmpFetch = globalThis.fetch;
globalThis.fetch = (async (url, options) => {
// run the request (do not wait for response)
const requestPromise = tmpFetch(url, options);
// reset mock
globalThis.fetch = tmpFetch;
const response = await requestPromise;
headers = response.headers;
return response;
}) as typeof globalThis.fetch;
const apiResponse = await method();
return {
body: apiResponse,
headers
};
};
Usage:
const response = await requestIncludeResponseHeaders(async () =>
FileService.downloadFile(file.id)
);
const type = response.headers.get("Content-Type")!;
const file = response.body;
Hope this helps anyone who needs this.