Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): allow binary returns #274

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
return null as T;
}

if (props.options.__binaryResponse) {
return response as unknown as T;
}

const contentType = response.headers.get('content-type');
if (contentType?.includes('application/json')) {
const json = await response.json();
Expand All @@ -52,10 +56,11 @@ async function defaultParseResponse<T>(props: APIResponseProps): Promise<T> {
return json as T;
}

// TODO handle blob, arraybuffer, other content types, etc.
const text = await response.text();
debug('response', response.status, response.url, response.headers, text);
return text as any as T;

// TODO handle blob, arraybuffer, other content types, etc.
return text as unknown as T;
}

/**
Expand Down Expand Up @@ -720,6 +725,8 @@ export type RequestOptions<Req extends {} = Record<string, unknown> | Readable>
httpAgent?: Agent;
signal?: AbortSignal | undefined | null;
idempotencyKey?: string;

__binaryResponse?: boolean | undefined;
};

// This is required so that we can determine if a given object matches the RequestOptions
Expand All @@ -738,6 +745,8 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
httpAgent: true,
signal: true,
idempotencyKey: true,

__binaryResponse: true,
};

export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<string, unknown> | Readable> => {
Expand Down