Skip to content

Commit

Permalink
feat(client): allow binary returns (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Nov 3, 2023
1 parent fb2996f commit 02f7ad7
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -61,10 +65,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 @@ -729,6 +734,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 @@ -747,6 +754,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

0 comments on commit 02f7ad7

Please sign in to comment.