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(typescript): support bytes responses #5402

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

dsinghvi
Copy link
Member

@dsinghvi dsinghvi commented Dec 12, 2024

Description

This PR supports bytes responses in the TypeScript SDK. A bytes response is returned as an ArrayBuffer.

Changes Made

  • Upgrade to IR v54
  • Handle HttpResponseBody.Bytes
  • Introduce a new endpoint to the bytes Fern Definition for bytes download (affects seed)
  • Release a new CLI version that will run appropriate IR migrations for v55 -> v54

Testing

  • Unit tests added/updated
  • Manual testing completed

@dsinghvi dsinghvi marked this pull request as ready for review December 12, 2024 20:44
@dsinghvi dsinghvi requested a review from amckinney December 12, 2024 20:45
/**
* @param {Service.RequestOptions} requestOptions - Request-specific configuration.
*/
public async download(requestOptions?: Service.RequestOptions): Promise<ArrayBuffer> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated SDKs should be flexible enough to retrieve the bytes as an array buffer (fully in-memory) or using a stream where the whole file isn't in memory.

Instead of returning ArrayBuffer, maybe we can return a type where the user can decide to get the buffer or stream.

export interface FooBarResponse {
  buffer: () => Promise<ArrayBuffer>;
  steam: () => Promise<ReadableStream>;
}

At AssemblyAI we did this:

const response = await fetch(redacted_audio_url);
if (!response.ok) {
  throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
}
return {
  arrayBuffer: response.arrayBuffer.bind(response),
  blob: response.blob.bind(response),
  body: response.body,
  bodyUsed: response.bodyUsed,
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case Response is the best option, it can be streamed, turned into a buffer, blob, etc

Copy link

github-actions bot commented Dec 12, 2024

🌿 Preview your docs: https://fern-preview-538aa1ff-0b97-4a61-ab46-9c8b19560a43.docs.buildwithfern.com/learn

amckinney
amckinney previously approved these changes Dec 12, 2024
Copy link
Member

@amckinney amckinney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👏

@@ -1,3 +1,9 @@
- changelogEntry:
- summary: Upgraces to v53 of the IR.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Upgraces" typo

a `Promise<ArrayBuffer>`. This is particularly useful for usecases like text to speech
where you would like to play the audio in the browser.

Additional this feature ugprade the dependency of the generator to Intermediate Representation v54.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ugprade" typo

/**
* @param {Service.RequestOptions} requestOptions - Request-specific configuration.
*/
public async download(requestOptions?: Service.RequestOptions): Promise<ArrayBuffer> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated SDKs should be flexible enough to retrieve the bytes as an array buffer (fully in-memory) or using a stream where the whole file isn't in memory.

Instead of returning ArrayBuffer, maybe we can return a type where the user can decide to get the buffer or stream.

export interface FooBarResponse {
  buffer: () => Promise<ArrayBuffer>;
  steam: () => Promise<ReadableStream>;
}

At AssemblyAI we did this:

const response = await fetch(redacted_audio_url);
if (!response.ok) {
  throw new Error(`Failed to fetch redacted audio: ${response.statusText}`);
}
return {
  arrayBuffer: response.arrayBuffer.bind(response),
  blob: response.blob.bind(response),
  body: response.body,
  bodyUsed: response.bodyUsed,
};

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

Successfully merging this pull request may close these issues.

4 participants