Description
openedon Jul 2, 2024
Environment
Node version: 20.12.2
Npm version: 10.5.0
OS and version: MacOS 14.2.1
typed-rest-client version: 2.0.0
Issue Description
Some REST API endpoints that we are working with return binary data from GET requests, but the REST client does not provide a way to access the response stream.
I'm aware that a similar issue was opened for receiving binary data through the HTTP client, however the workaround is not applicable to the REST client since the REST client methods do not return the raw http.IncomingMessage
. Doing it directly through the HTTP client as in the above issue is not convenient, since we then have to duplicate logic that is handled in the REST client.
Expected behaviour
Ideally there would be a way to retrieve the response stream in the IRestResponse
interface, something like:
export interface IRestResponse<T> {
statusCode?: number;
result: T | null;
headers: object;
responseStream?: NodeJS.ReadableStream; // Defined if the responseAsStream request option is true
}
and correspondingly there is a responseAsStream
option that we can pass in the IRequestOptions
:
export interface IRequestOptions {
// defaults to application/json
// common versioning is application/json;version=2.1
acceptHeader?: string;
// since accept is defaulted, set additional headers if needed
additionalHeaders?: ifm.IHeaders;
responseProcessor?: any;
responseAsStream?: boolean; // Return response as a ReadableStream for binary data
//Dates aren't automatically deserialized by JSON, this adds a date reviver to ensure they aren't just left as strings
deserializeDates?: boolean;
queryParameters?: ifm.IRequestQueryParams;
}
I already have a working example with these additions, so if this feature looks reasonable I can create a PR for it as well.