httpie icon indicating copy to clipboard operation
httpie copied to clipboard

Support AbortController

Open endel opened this issue 6 months ago • 1 comments

Hi there 👋 I hope all is well @lukeed!

This PR adds the possibility to provide a signal from AbortController and handles abort errors by assigning .aborted = true to the Error instance.

I noticed the test-suite is currently using the reqres.in external service, which currently requires a "free tier API key", and limits those requests to 100/day. I've added the free API Key in the tests, but ideally it would be best to have an http server on the side, just like you have here, that responds with the expected results instead.

Usage

import httpie from "httpie"

const controller = new AbortController();

httpie.get("...url", { signal: controller.signal }).
  then(() => {/* ... */}).
  catch((err) => {
    if (err.aborted) {
      // request has been aborted!
    }
  });

controller.abort();

Please let me know if you have feedback - I've been using httpie on colyseus.js for years and it's been serving me very well!

All the best!

endel avatar May 30 '25 20:05 endel

Just pushed to include the "types" key on exports list, so editors (e.g. VSCode) can pick the types up for auto-completion.

endel avatar May 30 '25 23:05 endel