Skip to content

Add support to REST client for getting binary data #374

Open

Description

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.

Actual behaviour

Steps to reproduce

Logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions