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

Add support for AbortSignal to all API methods #339

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

Conversation

aron
Copy link
Contributor

@aron aron commented Feb 5, 2025

This PR adds support for passing AbortSignal to all API methods that make HTTP requests, these are passed directly into the native fetch() implementation, so it's up to the user to handle the AbortError raised, if any.

const controller = new AbortController();
try {
  const prediction = await replicate.predictions.create({
    version: 'xyz',
    ...,
    signal: controller.signal,
  });
} catch (err) {
  if (err instanceof DOMException && err.name === "AbortError") {
    ...
  }
}

The paginate function also checks to see whether the signal was aborted before proceeding to the next iteration. If so it returns immediately to avoid making a redundant fetch call.

This allows the client to take advantage of various frameworks that provide an AbortSignal instance to tear down any in flight requests.

Copy link
Member

@erbridge erbridge left a comment

Choose a reason for hiding this comment

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

One minor comment about the docs, but otherwise, I think this looks good.

Comment on lines +1211 to +1216
| `options.route` | `string` | Required. REST API endpoint path. |
| `options.params` | `object` | URL query parameters for the given route. |
| `options.method` | `string` | HTTP method for the given route. |
| `options.headers` | `object` | Additional HTTP headers for the given route. |
| `options.data` | `object | FormData` | Request body. |
| `options.signal` | `AbortSignal` | Optional `AbortSignal`. |
Copy link
Member

Choose a reason for hiding this comment

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

Can you fix the formatting here so the table's dividers align vertically, please?

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

Successfully merging this pull request may close these issues.

4 participants