Skip to content

Commit

Permalink
feat(client): allow binary returns (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Nov 3, 2023
1 parent ea97913 commit 5983d5e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core.ts
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 5983d5e

Please sign in to comment.